ok
This commit is contained in:
116
EMERGENCY_SYSTEMS_RECOVERY/PathfindingSystem.js
Normal file
116
EMERGENCY_SYSTEMS_RECOVERY/PathfindingSystem.js
Normal file
@@ -0,0 +1,116 @@
|
||||
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 (v2.1 - Tiled Support Ready).');
|
||||
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;
|
||||
|
||||
// 🗺️ 1. TILED MAP SUPPORT
|
||||
if (ts.tiledMap) {
|
||||
// Check various obstacle layers
|
||||
const obstacleLayers = ['Obstacles', 'Buildings', 'Fences', 'Objects', '02_Obstacles', '03_Fences', '04_Buildings'];
|
||||
let isBlocked = false;
|
||||
|
||||
for (const layerName of obstacleLayers) {
|
||||
// Safety check: ensure layer exists
|
||||
const layerExists = ts.tiledMap.getLayer(layerName);
|
||||
if (!layerExists) continue;
|
||||
|
||||
const tile = ts.tiledMap.getTileAt(x, y, true, layerName);
|
||||
if (tile && tile.index !== -1) {
|
||||
isBlocked = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isBlocked) {
|
||||
blocked = 1;
|
||||
}
|
||||
}
|
||||
// 📊 2. PROCEDURAL / FLAT TILES SUPPORT
|
||||
else {
|
||||
const tile = ts.tiles && ts.tiles[y] ? ts.tiles[y][x] : null;
|
||||
|
||||
// 1. Voda in void
|
||||
if (!tile || tile.type === 'water' || tile.type === 'void') {
|
||||
blocked = 1;
|
||||
} else {
|
||||
// 2. Dekoracije (Ovire)
|
||||
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'
|
||||
];
|
||||
const isSolid = solidTypes.some(t => decor.type && 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user