2021-07-23 19:32:15 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2021 James Lyne
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import {Coordinate, LiveAtlasWorldDefinition} from "@/index";
|
|
|
|
import {LatLng} from "leaflet";
|
|
|
|
import {LiveAtlasProjection} from "@/model/LiveAtlasProjection";
|
|
|
|
|
|
|
|
export interface LiveAtlasMapDefinitionOptions {
|
|
|
|
world: LiveAtlasWorldDefinition;
|
|
|
|
name: string;
|
2021-07-24 19:12:18 +00:00
|
|
|
displayName?: string;
|
2021-07-23 19:32:15 +00:00
|
|
|
icon?: string;
|
|
|
|
background?: string;
|
|
|
|
nightAndDay?: boolean;
|
|
|
|
backgroundDay?: string;
|
|
|
|
backgroundNight?: string;
|
|
|
|
imageFormat: string;
|
|
|
|
prefix?: string;
|
|
|
|
protected?: boolean;
|
|
|
|
mapToWorld?: [number, number, number, number, number, number, number, number, number];
|
|
|
|
worldToMap?: [number, number, number, number, number, number, number, number, number];
|
|
|
|
nativeZoomLevels: number;
|
|
|
|
extraZoomLevels: number;
|
2021-08-13 22:51:03 +00:00
|
|
|
tileUpdateInterval?: number;
|
2021-07-23 19:32:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class LiveAtlasMapDefinition {
|
|
|
|
readonly world: LiveAtlasWorldDefinition;
|
|
|
|
readonly name: string;
|
|
|
|
readonly icon?: string;
|
2021-07-24 19:12:18 +00:00
|
|
|
readonly displayName: string;
|
2021-07-23 19:32:15 +00:00
|
|
|
readonly background: string;
|
|
|
|
readonly nightAndDay: boolean;
|
|
|
|
readonly backgroundDay?: string;
|
|
|
|
readonly backgroundNight?: string;
|
|
|
|
readonly imageFormat: string;
|
|
|
|
readonly prefix: string;
|
|
|
|
readonly protected: boolean;
|
|
|
|
private readonly projection?: Readonly<LiveAtlasProjection>;
|
|
|
|
readonly nativeZoomLevels: number;
|
|
|
|
readonly extraZoomLevels: number;
|
|
|
|
readonly scale: number;
|
2021-08-13 22:51:03 +00:00
|
|
|
readonly tileUpdateInterval?: number;
|
2021-07-23 19:32:15 +00:00
|
|
|
|
|
|
|
constructor(options: LiveAtlasMapDefinitionOptions) {
|
|
|
|
this.world = options.world; //Ignore append_to_world here otherwise things break
|
|
|
|
this.name = options.name;
|
|
|
|
this.icon = options.icon || undefined;
|
2021-07-24 19:12:18 +00:00
|
|
|
this.displayName = options.displayName || '';
|
2021-07-23 19:32:15 +00:00
|
|
|
|
|
|
|
this.background = options.background || '#000000';
|
|
|
|
this.nightAndDay = options.nightAndDay || false;
|
|
|
|
this.backgroundDay = options.backgroundDay || '#000000';
|
|
|
|
this.backgroundNight = options.backgroundNight || '#000000';
|
|
|
|
|
|
|
|
this.imageFormat = options.imageFormat;
|
|
|
|
this.prefix = options.prefix || '';
|
|
|
|
this.protected = options.protected || false;
|
|
|
|
|
|
|
|
this.nativeZoomLevels = options.nativeZoomLevels || 1;
|
|
|
|
this.extraZoomLevels = options.extraZoomLevels || 0;
|
|
|
|
this.scale = (1 / Math.pow(2, this.nativeZoomLevels));
|
2021-08-13 22:51:03 +00:00
|
|
|
this.tileUpdateInterval = options.tileUpdateInterval || undefined;
|
2021-07-23 19:32:15 +00:00
|
|
|
|
|
|
|
if(options.mapToWorld || options.worldToMap) {
|
2021-07-27 03:31:43 +00:00
|
|
|
this.projection = new LiveAtlasProjection({
|
2021-07-23 19:32:15 +00:00
|
|
|
mapToWorld: options.mapToWorld || [0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
worldToMap: options.worldToMap || [0, 0, 0, 0, 0, 0, 0, 0, 0],
|
|
|
|
nativeZoomLevels: this.nativeZoomLevels,
|
2021-07-27 03:31:43 +00:00
|
|
|
});
|
2021-07-23 19:32:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
locationToLatLng(location: Coordinate): LatLng {
|
|
|
|
return this.projection ? this.projection.locationToLatLng(location)
|
|
|
|
: new LatLng(-location.z * this.scale, location.x * this.scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
latLngToLocation(latLng: LatLng, y: number): Coordinate {
|
|
|
|
return this.projection ? this.projection.latLngToLocation(latLng, y)
|
|
|
|
: {x: latLng.lng / this.scale, y: y, z: -latLng.lat / this.scale};
|
|
|
|
}
|
|
|
|
|
|
|
|
getIcon(): string {
|
|
|
|
let worldType: string,
|
|
|
|
mapType: string;
|
|
|
|
|
|
|
|
switch(this.world.dimension) {
|
|
|
|
case 'nether':
|
|
|
|
worldType = 'nether';
|
|
|
|
mapType = ['surface', 'nether'].includes(this.name) ? 'surface' : 'flat';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'end':
|
|
|
|
worldType = 'the_end';
|
|
|
|
mapType = ['surface', 'the_end'].includes(this.name) ? 'surface' : 'flat';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'overworld':
|
|
|
|
default:
|
|
|
|
worldType = 'world';
|
|
|
|
mapType = ['surface', 'flat', 'biome', 'cave'].includes(this.name) ? this.name : 'flat';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `block_${worldType}_${mapType}`;
|
|
|
|
}
|
|
|
|
}
|