import { getTileCacheKey } from './tileCache' ; const width = 224 ; const height = 224 ; const tileWidth = 256 ; const tileHeight = 256 ; const gameMapWidth = 14336 ; const gameMapHeight = 14336 ; export function getTileCoordinatesForWorldCoordinate ( worldPos , tileScale ) { const totalWidth = width * tileWidth / tileScale ; const totalHeight = height * tileHeight / tileScale ; const imageX = worldPos . x / gameMapWidth * totalWidth ; const imageY = ( gameMapHeight - worldPos . y ) / gameMapHeight * totalHeight ; const tileX = Math . floor ( imageX / tileWidth ) ; const tileY = Math . floor ( imageY / tileHeight ) ; return { x : tileX , y : tileY } ; } export function getTileCacheKeyFromWorldCoordinate ( worldPos ) { const tilePos = getTileCoordinatesForWorldCoordinate ( worldPos , 1 ) ; return getTileCacheKey ( 8 , tilePos ) ; } export function getDimensions ( screenWidth , screenHeight , angle ? ) { if ( ! angle ) { angle = 0 ; } const x = Math . ceil ( screenWidth / tileWidth / 2 ) * 2 + 3 ; const y = Math . ceil ( screenHeight / tileHeight / 2 ) * 2 + 3 ; if ( angle === 0 ) { return { x , y } ; } return { x , y } ; } export function getMinimapTilePosition ( playerWorldPos , worldPos , screenWidth , screenHeight , zoomLevel , tileScale ) { const dimensions = getDimensions ( screenWidth * zoomLevel / tileScale , screenHeight * zoomLevel / tileScale ) ; const totalWidth = tileWidth * width / tileScale ; const totalHeight = tileHeight * height / tileScale ; const tileCoordinates = getTileCoordinatesForWorldCoordinate ( playerWorldPos , tileScale ) ; const { x : tileX , y : tileY } = tileCoordinates ; const pixelX = worldPos . x / gameMapWidth * totalWidth ; const pixelY = ( gameMapHeight - worldPos . y ) / gameMapHeight * totalHeight ; const imageX = pixelX - ( ( tileX - Math . floor ( dimensions . x / 2 ) ) * tileWidth ) ; const imageY = pixelY - ( ( tileY - Math . floor ( dimensions . y / 2 ) ) * tileHeight ) ; return { x : imageX * tileScale , y : imageY * tileScale } ; } param worldPos param centerPos param zoomLevel param canvasWidth param canvasHeight returns export function worldCoordinateToCanvas ( worldPos , centerPos , zoomLevel , canvasWidth , canvasHeight ) { const centerDistanceX = centerPos . x - worldPos . x ; const centerDistanceY = centerPos . y - worldPos . y ; const scaleWorldToCanvasX = ( width * tileWidth ) / gameMapWidth ; const scaleWorldToCanvasY = ( height * tileHeight ) / gameMapHeight ; const scaleX = scaleWorldToCanvasX / zoomLevel ; const scaleY = scaleWorldToCanvasY / zoomLevel ; const centerCanvasX = canvasWidth / 2 ; const centerCanvasY = canvasHeight / 2 ; const x = centerCanvasX - ( centerDistanceX * scaleX ) ; const y = centerCanvasY + ( centerDistanceY * scaleY ) ; return { x , y } ; } param worldPos param centerPos param zoomLevel param canvasWidth param canvasHeight returns export function canvasCoordinateToWorld ( canvasPos , centerPos , zoomLevel , canvasWidth , canvasHeight ) { const centerCanvasX = canvasWidth / 2 ; const centerCanvasY = canvasHeight / 2 ; const centerDistanceX = centerCanvasX - canvasPos . x ; const centerDistanceY = centerCanvasY - canvasPos . y ; const scaleCanvasToWorldX = gameMapWidth / ( width * tileWidth ) ; const scaleCanvasToWorldY = gameMapHeight / ( height * tileHeight ) ; const scaleX = scaleCanvasToWorldX * zoomLevel ; const scaleY = scaleCanvasToWorldY * zoomLevel ; const x = centerPos . x - ( centerDistanceX * scaleX ) ; const y = centerPos . y + ( centerDistanceY * scaleY ) ; return { x , y } ; } export function getTileLevel ( zoomLevel ) { if ( zoomLevel < 2 ) { return 8 ; } if ( zoomLevel < 4 ) { return 7 ; } if ( zoomLevel < 8 ) { return 6 ; } if ( zoomLevel < 16 ) { return 5 ; } if ( zoomLevel < 32 ) { return 4 ; } if ( zoomLevel < 64 ) { return 3 ; } return 2 ; } export function getTileScale ( tileLevel ) { return 2 ** ( 8 - tileLevel ) as TileScale ; }