Make grass block vision

This commit is contained in:
Peter Stockings
2026-01-05 21:32:18 +11:00
parent 39528d297e
commit a01d4abdf7
4 changed files with 18 additions and 8 deletions

View File

@@ -16,13 +16,14 @@ export interface TileBehavior {
isBlocking: boolean; isBlocking: boolean;
isDestructible: boolean; isDestructible: boolean;
isDestructibleByWalk?: boolean; isDestructibleByWalk?: boolean;
blocksVision?: boolean;
destructsTo?: TileType; destructsTo?: TileType;
} }
export const TILE_DEFINITIONS: Record<number, TileBehavior> = { export const TILE_DEFINITIONS: Record<number, TileBehavior> = {
[TileType.EMPTY]: { id: TileType.EMPTY, isBlocking: false, isDestructible: false }, [TileType.EMPTY]: { id: TileType.EMPTY, isBlocking: false, isDestructible: false },
[TileType.WALL]: { id: TileType.WALL, isBlocking: true, isDestructible: false }, [TileType.WALL]: { id: TileType.WALL, isBlocking: true, isDestructible: false },
[TileType.GRASS]: { id: TileType.GRASS, isBlocking: false, isDestructible: true, isDestructibleByWalk: true, destructsTo: TileType.GRASS_SAPLINGS }, [TileType.GRASS]: { id: TileType.GRASS, isBlocking: false, isDestructible: true, isDestructibleByWalk: true, blocksVision: true, destructsTo: TileType.GRASS_SAPLINGS },
[TileType.GRASS_SAPLINGS]: { id: TileType.GRASS_SAPLINGS, isBlocking: false, isDestructible: false }, [TileType.GRASS_SAPLINGS]: { id: TileType.GRASS_SAPLINGS, isBlocking: false, isDestructible: false },
[TileType.EMPTY_DECO]: { id: TileType.EMPTY_DECO, isBlocking: false, isDestructible: false }, [TileType.EMPTY_DECO]: { id: TileType.EMPTY_DECO, isBlocking: false, isDestructible: false },
[TileType.WALL_DECO]: { id: TileType.WALL_DECO, isBlocking: true, isDestructible: false }, [TileType.WALL_DECO]: { id: TileType.WALL_DECO, isBlocking: true, isDestructible: false },
@@ -45,6 +46,11 @@ export function isDestructibleByWalk(tile: number): boolean {
return def ? !!def.isDestructibleByWalk : false; return def ? !!def.isDestructibleByWalk : false;
} }
export function blocksSight(tile: number): boolean {
const def = TILE_DEFINITIONS[tile];
return def ? (def.isBlocking || !!def.blocksVision) : false;
}
export function getDestructionResult(tile: number): number | undefined { export function getDestructionResult(tile: number): number | undefined {
const def = TILE_DEFINITIONS[tile]; const def = TILE_DEFINITIONS[tile];
return def ? def.destructsTo : undefined; return def ? def.destructsTo : undefined;

View File

@@ -82,8 +82,8 @@ describe('Combat Simulation', () => {
const player = world.actors.get(1); const player = world.actors.get(1);
expect(player!.pos).toEqual({ x: 4, y: 3 }); expect(player!.pos).toEqual({ x: 4, y: 3 });
// Tile should effectively be destroyed (turned to empty/1) // Tile should effectively be destroyed (turned to saplings/2)
expect(world.tiles[grassIdx]).toBe(1); // TileType.EMPTY expect(world.tiles[grassIdx]).toBe(2); // TileType.GRASS_SAPLINGS
}); });
}); });

View File

@@ -1,7 +1,7 @@
import type { World, EntityId, Action, SimEvent, Actor, CombatantActor, CollectibleActor, ActorType } from "../../core/types"; import type { World, EntityId, Action, SimEvent, Actor, CombatantActor, CollectibleActor, ActorType } from "../../core/types";
import { isBlocked, inBounds, isWall, tryDestructTile } from "../world/world-logic"; import { isBlocked, inBounds, tryDestructTile } from "../world/world-logic";
import { isDestructibleByWalk } from "../../core/terrain"; import { isDestructibleByWalk, blocksSight } from "../../core/terrain";
import { findPathAStar } from "../world/pathfinding"; import { findPathAStar } from "../world/pathfinding";
import { GAME_CONFIG } from "../../core/config/GameConfig"; import { GAME_CONFIG } from "../../core/config/GameConfig";
import { type EntityManager } from "../EntityManager"; import { type EntityManager } from "../EntityManager";
@@ -240,9 +240,11 @@ function canEnemySeePlayer(w: World, enemy: CombatantActor, player: CombatantAct
const viewRadius = 8; // Enemy vision range const viewRadius = 8; // Enemy vision range
let canSee = false; let canSee = false;
const fov = new FOV.PreciseShadowcasting((x: number, y: number) => { const fov = new FOV.PreciseShadowcasting((x: number, y: number) => {
if (!inBounds(w, x, y)) return false; if (!inBounds(w, x, y)) return false;
return !isWall(w, x, y); const idx = y * w.width + x;
return !blocksSight(w.tiles[idx]);
}); });
fov.compute(enemy.pos.x, enemy.pos.y, viewRadius, (x: number, y: number) => { fov.compute(enemy.pos.x, enemy.pos.y, viewRadius, (x: number, y: number) => {

View File

@@ -1,6 +1,7 @@
import { FOV } from "rot-js"; import { FOV } from "rot-js";
import { type World, type EntityId } from "../core/types"; import { type World, type EntityId } from "../core/types";
import { idx, inBounds, isWall } from "../engine/world/world-logic"; import { idx, inBounds } from "../engine/world/world-logic";
import { blocksSight } from "../core/terrain";
import { GAME_CONFIG } from "../core/config/GameConfig"; import { GAME_CONFIG } from "../core/config/GameConfig";
import Phaser from "phaser"; import Phaser from "phaser";
@@ -21,7 +22,8 @@ export class FovManager {
this.fov = new FOV.PreciseShadowcasting((x: number, y: number) => { this.fov = new FOV.PreciseShadowcasting((x: number, y: number) => {
if (!inBounds(world, x, y)) return false; if (!inBounds(world, x, y)) return false;
return !isWall(world, x, y); const idx = y * world.width + x;
return !blocksSight(world.tiles[idx]);
}); });
} }