Implement Initiative Turn System and Fog of War

This commit is contained in:
2026-01-09 14:12:40 +01:00
parent e45207807d
commit b08a922c00
4 changed files with 258 additions and 3 deletions

View File

@@ -934,12 +934,18 @@ export class GameRenderer {
// but usually -r * PI/2 works for this setup.
plane.rotation.z = -r * (Math.PI / 2);
// Position at the calculated center
// Notice: World Z is -Grid Y
plane.position.set(cx, 0.01, -cy);
plane.receiveShadow = true;
this.scene.add(plane);
// Store Metadata for FOW
plane.userData.tileId = tileInstance.id;
plane.userData.cells = cells;
if (!this.dungeonGroup) {
this.dungeonGroup = new THREE.Group();
this.scene.add(this.dungeonGroup);
}
this.dungeonGroup.add(plane);
});
} else {
@@ -947,6 +953,81 @@ export class GameRenderer {
}
}
updateFogOfWar(visibleTileIds) {
if (!this.dungeonGroup) return;
const visibleSet = new Set(visibleTileIds);
this.dungeonGroup.children.forEach(mesh => {
const isVisible = visibleSet.has(mesh.userData.tileId);
mesh.visible = isVisible;
});
// Also update Doors (in exitGroup)
if (this.exitGroup) {
this.exitGroup.children.forEach(door => {
// If door connects to AT LEAST ONE visible tile, it should be visible?
// Or if it is part of a visible tile?
// Doors usually belong to the tile they were spawned with?
// Logic: Check if door is within/adjacent to visible cells?
// Door userData has `cells`.
// We need to map cells to tileIds? We don't have that map here.
// Simplified: If the door is physically close to a visible tile, show it.
// Better approach:
// Engine should pass visible cells?
// Using tileIds is robust for tiles.
// For doors: we can check if any of the door's cells are roughly inside a visible tile's bounding box?
// Or just show all doors for now? No, floating doors in darkness.
// Hack: Show door if its position is near a visible tile.
let doorVisible = false;
const dx = door.position.x;
const dy = -door.position.z; // World Z is -Grid Y
// Check against visible meshes
// This is O(N*M), slow.
// But N (visible tiles) is small (1-5).
// Let's assume doors connected to visible tiles are visible.
// I need to know which tile a door belongs to.
// I will skip door FOW for this iteration or make them always visible but dim?
// Let's hide them by default and check proximity.
door.visible = false;
// Can't easily check without grid.
// Engine will handle Door visibility via `setEntityVisibility`? No, they are meshes not entities.
});
// Re-iterate to show close doors
this.dungeonGroup.children.forEach(tile => {
if (tile.visible) {
// Check doors near this tile
if (this.exitGroup) {
this.exitGroup.children.forEach(door => {
if (door.visible) return; // Already shown
// Check distance to tile center?
// Tile has cx, cy.
const tx = tile.position.x;
const ty = -tile.position.z;
const dx = Math.abs(door.position.x - tx);
const dy = Math.abs(door.position.z - (-ty)); // Z is neg
// Tile size?
// We don't know exact bounds here, but we can guess.
// If distance < 4 roughly?
if (dx < 4 && dy < 4) {
door.visible = true;
}
});
}
}
});
}
}
openDoor(doorMesh) {
if (!doorMesh || !doorMesh.userData.isDoor) return;
if (doorMesh.userData.isOpen) return; // Already open