changed flyTo to use mercator functions

This commit is contained in:
AloeSapling 2025-08-28 14:52:52 +02:00
parent 4ce132a652
commit 622a32bf9d
2 changed files with 8 additions and 29 deletions

View file

@ -2,7 +2,7 @@ import { TBlueMarbleJSON, TBlueMarbleTemplate, TCharityTemplate } from "../types
import { Schemas } from "../types/types";
import { dataManager, uiManager } from "./main";
import { drawAllTemplates } from "./templates";
import { coordinatesToLatLng, createElementWithAttributes, download } from "./utils";
import { createElementWithAttributes, download } from "./utils";
// Typescript / Javascript for the "manageTemplates" window
@ -50,9 +50,13 @@ function exportToggle(idx: number){
*/
function flyToTemplate(idx: number){
const coordsArr = (dataManager.get() as TBlueMarbleJSON).templates[idx].coords;
const lngLat = coordinatesToLatLng(coordsArr);
if(!lngLat){ return };
charity.game.map.flyTo({center: lngLat, zoom: 9}) // Fly to the template's position
const zoom = charity.game.map.getZoom();
// Convert to Px and Py used in mercator's functions
const mercatorPixelsX = coordsArr[0]*1000+coordsArr[2]; // Tx * 1000 + Px
const mercatorPixelsY = coordsArr[1]*1000+coordsArr[3]; // Ty * 1000 + Px
const lngLatBounds = charity.game.mercator.pixelsToLatLon(mercatorPixelsX, mercatorPixelsY, zoom);
if(!lngLatBounds){ return };
charity.game.map.flyTo({center: lngLatBounds, zoom: 9}) // Fly to the template's position
}
/**Creates a one-click shareable link for the given template and copies it to the clipboard

View file

@ -58,29 +58,4 @@ export function generateUUID(): string{
}
return uuid;
}
/**Converts a 4 element array of coordinates into map longitude and latitude
* @param {number[]} coordinates A 4 element array of coordinates (Tile X, Tile Y, Pixel X, Pixel Y)
* @returns A lngLat object or undefined if an error occured e.g. malformed coordinates data
* @since 0.3.0-overhaul
*/
export function coordinatesToLatLng(coordinates: number[]): {lng: number, lat: number} | undefined{
// Function provided by courtesy of CloudBurst
if(coordinates.length !== 4) { return };
let actualX = (coordinates[0] * 1000) + coordinates[2];
let actualY = (coordinates[1] * 1000) + coordinates[3];
const mapSize = 2048000;
let x = actualX / mapSize;
let y = actualY / mapSize;
function inverseO_(n: number) { return 360 * x - 180 }
function inverseN_(n: number) { return (Math.atan(Math.exp(Math.PI - 2 * Math.PI * n)) - Math.PI / 4) * 360 / Math.PI; }
return {
lng: inverseO_(x),
lat: inverseN_(y)
}
}