class PathfindingSystem { constructor(scene) { this.scene = scene; this.worker = null; this.callbacks = new Map(); this.requestId = 0; this.initialized = false; try { // Ustvarimo workerja this.worker = new Worker('src/workers/pathfinding.worker.js'); this.worker.onmessage = this.handleMessage.bind(this); console.log('✅ PathfindingWorker initialized.'); this.initialized = true; } catch (err) { console.error('❌ Failed to init PathfindingWorker:', err); } } updateGrid() { if (!this.initialized || !this.scene.terrainSystem) return; const ts = this.scene.terrainSystem; const width = ts.width; const height = ts.height; // Ustvarimo flat array (0 = prehodno, 1 = ovira) // Uporabimo Uint8Array za učinkovitost prenosa const grid = new Uint8Array(width * height); for (let y = 0; y < height; y++) { for (let x = 0; x < width; x++) { let blocked = 0; const tile = ts.tiles[y][x]; // 1. Voda in void if (!tile || tile.type === 'water' || tile.type === 'void') { blocked = 1; } else { // 2. Dekoracije (Ovire) // Uporabimo že obstoječo logiko v TerrainSystemu (če obstaja) ali preverimo dekoracije const key = `${x},${y}`; const decor = ts.decorationsMap.get(key); if (decor) { const solidTypes = [ 'tree', 'tree_green', 'tree_blue', 'tree_dead', 'tree_green_new', 'tree_blue_new', 'tree_dead_new', 'rock', 'rock_asset', 'rock_new', 'rock_small', 'rock_1', 'rock_2', 'wall', 'fence', 'house', 'gravestone' ]; // Preverimo substring za tipe (npr. 'tree' ujame 'tree_blue') const isSolid = solidTypes.some(t => decor.type.includes(t)); if (isSolid) blocked = 1; } } grid[y * width + x] = blocked; } } this.worker.postMessage({ type: 'UPDATE_GRID', payload: { grid, width, height } }); // console.log('🗺️ Pathfinding Grid updated sent to worker.'); } findPath(startX, startY, endX, endY, callback) { if (!this.initialized) return; const id = this.requestId++; this.callbacks.set(id, callback); this.worker.postMessage({ type: 'FIND_PATH', id: id, payload: { startX, startY, endX, endY } }); } handleMessage(e) { const { type, id, path } = e.data; if (type === 'PATH_FOUND') { const callback = this.callbacks.get(id); if (callback) { callback(path); this.callbacks.delete(id); } } } }