Files
ws4kp/server/scripts/modules/radar/tiles.mjs
T

113 lines
4.6 KiB
JavaScript

import {
RADAR_FINAL_SIZE, TILE_SIZE, TILE_COUNT, PX, PY,
} from './constants.mjs';
import elemForEach from '../utils/elem-for-each.mjs';
import { shiftPixelForUserGenerator } from './positions.mjs';
// convert a pixel location to a file/tile combination
const pixelToFile = (xPixel, yPixel) => {
const xTile = Math.floor(xPixel / TILE_SIZE.x);
const yTile = Math.floor(yPixel / TILE_SIZE.y);
if (xTile < 0 || xTile >= TILE_COUNT.x || yTile < 0 || yTile >= TILE_COUNT.y) return false;
return `${xTile.toString().padStart(2, '0')}-${yTile.toString().padStart(2, '0')}`;
};
// convert a pixel location in the overall map to a pixel location on the tile set
const modTile = (xPixel, yPixel) => {
// adjust for additional 1 tile when odd
const x = (Math.floor(xPixel) % (TILE_SIZE.x));
const y = (Math.floor(yPixel) % (TILE_SIZE.y));
return { x, y };
};
// creates the radar background map image and overlay transparency
// which remain fixed on the page as the radar image changes in layered divs
// it returns 4 ImageBitmaps that represent the base map, and 4 ImageBitmaps that are the overlay
// the main thread pushes these ImageBitmaps into the image placeholders on the page
const setTiles = (data) => {
const {
user,
elemId,
} = data;
const elemIdFull = `${elemId}-html`;
// shift the working location to the top-left corner to center the resulting map on the user
const shiftPixelForUser = shiftPixelForUserGenerator(user);
const topLeft = shiftPixelForUser([0, 0]);
// determine the basemap images needed
const baseMapTiles = [
pixelToFile(topLeft[PX] + TILE_SIZE.x * 0, topLeft[PY]),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 1, topLeft[PY]),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 2, topLeft[PY]),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 0, topLeft[PY] + TILE_SIZE.y),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 1, topLeft[PY] + TILE_SIZE.y),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 2, topLeft[PY] + TILE_SIZE.y),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 0, topLeft[PY] + TILE_SIZE.y * 2),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 1, topLeft[PY] + TILE_SIZE.y * 2),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 2, topLeft[PY] + TILE_SIZE.y * 2),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 0, topLeft[PY] + TILE_SIZE.y * 3),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 1, topLeft[PY] + TILE_SIZE.y * 3),
pixelToFile(topLeft[PX] + TILE_SIZE.x * 2, topLeft[PY] + TILE_SIZE.y * 3),
];
// do some calculations
// the tiles are arranged as follows, with the horizontal axis as x, and correlating with the second set of digits in the image file number
// T[0] T[1] T[2]
// T[3] T[4] T[5]
// calculate the shift of tile 0 (upper left)
const tileShift = modTile(topLeft[PX], topLeft[PY]);
// determine which tiles are used
const secondRow = TILE_SIZE.y - tileShift.y < RADAR_FINAL_SIZE().height;
const thirdRow = (TILE_SIZE.y * 2) - tileShift.y < RADAR_FINAL_SIZE().height;
const fourthRow = (TILE_SIZE.y * 3) - tileShift.y < RADAR_FINAL_SIZE().height;
const usedTiles = [
true,
TILE_SIZE.x - tileShift.x < RADAR_FINAL_SIZE().width,
(TILE_SIZE.x * 2) - tileShift.x < RADAR_FINAL_SIZE().width,
];
// rows 2, 3 and 4 are a copy of the first row when in use
// calculate T[4] and T[5]
usedTiles.push(secondRow && usedTiles[0], secondRow && usedTiles[1], secondRow && usedTiles[2]);
usedTiles.push(thirdRow && usedTiles[0], thirdRow && usedTiles[1], thirdRow && usedTiles[2]);
usedTiles.push(fourthRow && usedTiles[0], fourthRow && usedTiles[1], fourthRow && usedTiles[2]);
// helper function for populating tiles
const populateTile = (tileName) => (elem, index) => {
// always set the size to flow the images correctly
elem.width = TILE_SIZE.x;
elem.height = TILE_SIZE.y;
// check if the tile is used
if (!usedTiles[index]) {
elem.src = '';
return;
}
// set the image source and size
const newSource = `/images/maps/radar-conus/${tileName}/${baseMapTiles[index]}.webp`;
if (elem.src === newSource) return;
elem.src = newSource;
};
// populate the map and overlay tiles
// fill the tiles with the map
elemForEach(`#${elemIdFull} .map-tiles img`, populateTile('base'));
elemForEach(`#${elemIdFull} .overlay-tiles img`, populateTile('overlay'));
// fill the tiles with the overlay
// shift the map tile containers
const mapTileContainer = document.querySelector(`#${elemIdFull} .map-tiles`);
mapTileContainer.style.top = `${-tileShift.y}px`;
mapTileContainer.style.left = `${-tileShift.x}px`;
// // and the same for the overlay
const overlayTileContainer = document.querySelector(`#${elemIdFull} .overlay-tiles`);
overlayTileContainer.style.top = `${-tileShift.y}px`;
overlayTileContainer.style.left = `${-tileShift.x}px`;
};
export default setTiles;