Files
novafarma/old_logic/src_backup_1768938138/utils/IsometricUtils.js
2026-01-21 01:08:21 +01:00

71 lines
2.4 KiB
JavaScript

// Isometric Utilities
// Konverzija med kartezičnimi in izometričnimi koordinatami
class IsometricUtils {
constructor(tileWidth = 48, tileHeight = 24) {
this.tileWidth = tileWidth;
this.tileHeight = tileHeight;
// Layer Constants
this.LAYER_FLOOR = 0;
this.LAYER_GRID_DECO = 100000; // Decorations on floor (shadows)
this.LAYER_OBJECTS = 200000; // Dynamic entities (Trees, Player, NPCs)
this.LAYER_UI = 300000;
}
// Kartezične (grid) koordinate -> Isometrične (screen) koordinate
toScreen(gridX, gridY) {
const screenX = (gridX - gridY) * (this.tileWidth / 2);
const screenY = (gridX + gridY) * (this.tileHeight / 2);
return { x: screenX, y: screenY };
}
// Isometrične (screen) koordinate -> Kartezične (grid) koordinate
toGrid(screenX, screenY) {
const gridX = (screenX / (this.tileWidth / 2) + screenY / (this.tileHeight / 2)) / 2;
const gridY = (screenY / (this.tileHeight / 2) - screenX / (this.tileWidth / 2)) / 2;
return { x: Math.floor(gridX), y: Math.floor(gridY) };
}
// Izračun depth (z-index) za pravilno sortiranje
getDepth(gridX, gridY, layerBase = 0) {
// Depth = LayerBase + ScreenY
// To zagotovi, da so objekti v layerju sortirani po Y
return layerBase + ((gridX + gridY) * (this.tileHeight / 2));
}
// Izračun centerja tile-a
getTileCenter(gridX, gridY) {
const screen = this.toScreen(gridX, gridY);
return {
x: screen.x,
y: screen.y + this.tileHeight / 2
};
}
// Preveri ali je grid koordinata znotraj meja
isInBounds(gridX, gridY, mapWidth, mapHeight) {
return gridX >= 0 && gridX < mapWidth && gridY >= 0 && gridY < mapHeight;
}
// Dobi sosednje tile-e (NSEW)
getNeighbors(gridX, gridY, mapWidth, mapHeight) {
const neighbors = [];
const directions = [
{ x: 0, y: -1 }, // North
{ x: 1, y: 0 }, // East
{ x: 0, y: 1 }, // South
{ x: -1, y: 0 } // West
];
for (const dir of directions) {
const nx = gridX + dir.x;
const ny = gridY + dir.y;
if (this.isInBounds(nx, ny, mapWidth, mapHeight)) {
neighbors.push({ x: nx, y: ny });
}
}
return neighbors;
}
}