Begin refactoring GameScene

This commit is contained in:
Peter Stockings
2026-01-26 15:30:14 +11:00
parent 1d7be54fd9
commit ef7d85750f
46 changed files with 2459 additions and 1291 deletions

View File

@@ -0,0 +1,348 @@
import type {
World,
EntityId,
Actor,
CombatantActor,
CollectibleActor,
ItemDropActor,
Vec2,
EnemyAIState
} from "../core/types";
import type { ECSWorld } from "./ecs/World";
/**
* Centralized accessor for game entities.
* Provides a unified interface for querying actors from the World.
*
* This facade:
* - Centralizes entity access patterns
* - Makes it easy to migrate to ECS later
* - Reduces scattered world.actors calls
*/
export class EntityAccessor {
private _playerId: EntityId;
private ecsWorld: ECSWorld;
private actorCache: Map<EntityId, Actor> = new Map();
constructor(
_world: World,
playerId: EntityId,
ecsWorld: ECSWorld
) {
this._playerId = playerId;
this.ecsWorld = ecsWorld;
}
/**
* Updates the world reference (called when loading new floors).
*/
updateWorld(_world: World, playerId: EntityId, ecsWorld: ECSWorld): void {
this._playerId = playerId;
this.ecsWorld = ecsWorld;
this.actorCache.clear();
}
private entityToActor(id: EntityId): Actor | null {
if (!this.ecsWorld) return null;
// Check cache first
const cached = this.actorCache.get(id);
if (cached) {
// Double check it still exists in ECS
if (!this.ecsWorld.hasEntity(id)) {
this.actorCache.delete(id);
return null;
}
return cached;
}
const pos = this.ecsWorld.getComponent(id, "position");
if (!pos) return null;
// Check for combatant
const stats = this.ecsWorld.getComponent(id, "stats");
const actorType = this.ecsWorld.getComponent(id, "actorType");
if (stats && actorType) {
const energyComp = this.ecsWorld.getComponent(id, "energy");
const playerComp = this.ecsWorld.getComponent(id, "player");
const ai = this.ecsWorld.getComponent(id, "ai");
const inventory = this.ecsWorld.getComponent(id, "inventory");
const equipment = this.ecsWorld.getComponent(id, "equipment");
// Create a proxy-like object to ensure writes persist to ECS components
let localEnergy = 0;
const actor = {
id,
// Pass Reference to PositionComponent so moves persist
pos: pos,
category: "combatant",
isPlayer: !!playerComp,
type: actorType.type,
// Pass Reference to StatsComponent
stats: stats,
// Speed defaults
speed: energyComp?.speed ?? 100,
// Pass Reference (or fallback)
inventory: inventory ?? { gold: 0, items: [] },
equipment: equipment
} as CombatantActor;
// Manually define 'energy' property to proxy to component
Object.defineProperty(actor, 'energy', {
get: () => energyComp ? energyComp.current : localEnergy,
set: (v: number) => {
if (energyComp) {
energyComp.current = v;
} else {
localEnergy = v;
}
},
enumerable: true,
configurable: true
});
// Proxy AI state properties
Object.defineProperty(actor, 'aiState', {
get: () => ai?.state,
set: (v: EnemyAIState) => { if (ai) ai.state = v; },
enumerable: true,
configurable: true
});
Object.defineProperty(actor, 'alertedAt', {
get: () => ai?.alertedAt,
set: (v: number) => { if (ai) ai.alertedAt = v; },
enumerable: true,
configurable: true
});
Object.defineProperty(actor, 'lastKnownPlayerPos', {
get: () => ai?.lastKnownPlayerPos,
set: (v: Vec2) => { if (ai) ai.lastKnownPlayerPos = v; },
enumerable: true,
configurable: true
});
this.actorCache.set(id, actor);
return actor;
}
// Check for collectible
const collectible = this.ecsWorld.getComponent(id, "collectible");
if (collectible) {
const actor = {
id,
pos: pos, // Reference
category: "collectible",
type: "exp_orb",
expAmount: collectible.amount
} as CollectibleActor;
this.actorCache.set(id, actor);
return actor;
}
// Check for Item Drop
const groundItem = this.ecsWorld.getComponent(id, "groundItem");
if (groundItem) {
const actor = {
id,
pos: pos,
category: "item_drop",
item: groundItem.item
} as ItemDropActor;
this.actorCache.set(id, actor);
return actor;
}
return null;
}
// ==========================================
// Player Access
// ==========================================
/**
* Gets the player's entity ID.
*/
get playerId(): EntityId {
return this._playerId;
}
/**
* Gets the player entity.
*/
getPlayer(): CombatantActor | null {
const actor = this.entityToActor(this._playerId);
if (actor?.category === "combatant") return actor as CombatantActor;
return null;
}
/**
* Gets the player's current position.
*/
getPlayerPos(): Vec2 | null {
const player = this.getPlayer();
return player ? { ...player.pos } : null;
}
/**
* Checks if the player exists (is alive).
*/
isPlayerAlive(): boolean {
return this.ecsWorld.hasEntity(this._playerId) && (this.ecsWorld.getComponent(this._playerId, "position") !== undefined);
}
// ==========================================
// Generic Actor Access
// ==========================================
/**
* Gets any actor by ID.
*/
getActor(id: EntityId): Actor | null {
return this.entityToActor(id);
}
/**
* Gets a combatant actor by ID.
*/
getCombatant(id: EntityId): CombatantActor | null {
const actor = this.entityToActor(id);
if (actor?.category === "combatant") return actor as CombatantActor;
return null;
}
/**
* Checks if an actor exists.
*/
hasActor(id: EntityId): boolean {
return this.ecsWorld.hasEntity(id) && (this.ecsWorld.getComponent(id, "position") !== undefined);
}
// ==========================================
// Spatial Queries
// ==========================================
/**
* Gets all actors at a specific position.
*/
getActorsAt(x: number, y: number): Actor[] {
// Query ECS
return [...this.getAllActors()].filter(a => a.pos.x === x && a.pos.y === y);
}
/**
* Finds an enemy combatant at a specific position.
*/
findEnemyAt(x: number, y: number): CombatantActor | null {
const actors = this.getActorsAt(x, y);
for (const actor of actors) {
if (actor.category === "combatant" && !actor.isPlayer) {
return actor;
}
}
return null;
}
/**
* Checks if there's any enemy at the given position.
*/
hasEnemyAt(x: number, y: number): boolean {
return this.findEnemyAt(x, y) !== null;
}
/**
* Finds a collectible at a specific position.
*/
findCollectibleAt(x: number, y: number): CollectibleActor | null {
const actors = this.getActorsAt(x, y);
for (const actor of actors) {
if (actor.category === "collectible") {
return actor;
}
}
return null;
}
/**
* Finds an item drop at a specific position.
*/
findItemDropAt(x: number, y: number): ItemDropActor | null {
const actors = this.getActorsAt(x, y);
for (const actor of actors) {
if (actor.category === "item_drop") {
return actor;
}
}
return null;
}
// ==========================================
// Collection Queries
// ==========================================
/**
* Gets all enemy combatants in the world.
*/
getEnemies(): CombatantActor[] {
return [...this.getAllActors()].filter(
(a): a is CombatantActor => a.category === "combatant" && !a.isPlayer
);
}
/**
* Gets all combatants (player + enemies).
*/
getCombatants(): CombatantActor[] {
return [...this.getAllActors()].filter(
(a): a is CombatantActor => a.category === "combatant"
);
}
/**
* Gets all collectibles (exp orbs, etc.).
*/
getCollectibles(): CollectibleActor[] {
return [...this.getAllActors()].filter(
(a): a is CollectibleActor => a.category === "collectible"
);
}
/**
* Gets all item drops.
*/
getItemDrops(): ItemDropActor[] {
return [...this.getAllActors()].filter(
(a): a is ItemDropActor => a.category === "item_drop"
);
}
/**
* Iterates over all actors (for rendering, etc.).
*/
getAllActors(): IterableIterator<Actor> {
const actors: Actor[] = [];
// Get all entities with position (candidates)
const entities = this.ecsWorld.getEntitiesWith("position");
for (const id of entities) {
const actor = this.entityToActor(id);
if (actor) actors.push(actor);
}
return actors.values();
}
/**
* Removes an actor from the world.
*/
removeActor(id: EntityId): void {
this.ecsWorld.destroyEntity(id);
}
/**
* Access to the raw ECS world if needed for specialized systems.
*/
get context(): ECSWorld | undefined {
return this.ecsWorld;
}
}

View File

@@ -1,160 +0,0 @@
import { type World, type EntityId, type Actor, type Vec2, type CombatantActor } from "../core/types";
import { idx } from "./world/world-logic";
import { ECSWorld } from "./ecs/World";
import { MovementSystem } from "./ecs/MovementSystem";
import { AISystem } from "./ecs/AISystem";
export class EntityManager {
private grid: Map<number, EntityId[]> = new Map();
private actors: Map<EntityId, Actor>;
private world: World;
private lastId: number = 0;
private ecs: ECSWorld;
private movementSystem: MovementSystem;
private aiSystem: AISystem;
constructor(world: World) {
this.world = world;
this.actors = world.actors;
this.ecs = new ECSWorld();
this.movementSystem = new MovementSystem(this.ecs, this.world, this);
this.aiSystem = new AISystem(this.ecs, this.world, this);
this.lastId = Math.max(0, ...this.actors.keys());
this.ecs.setNextId(this.lastId + 1);
this.rebuildGrid();
}
get ecsWorld(): ECSWorld {
return this.ecs;
}
get movement(): MovementSystem {
return this.movementSystem;
}
get ai(): AISystem {
return this.aiSystem;
}
rebuildGrid() {
this.grid.clear();
// Also re-sync ECS if needed, though typically we do this once at start
for (const actor of this.actors.values()) {
this.syncActorToECS(actor);
this.addToGrid(actor);
}
}
private syncActorToECS(actor: Actor) {
const id = actor.id;
this.ecs.addComponent(id, "position", actor.pos);
this.ecs.addComponent(id, "name", { name: actor.id.toString() });
if (actor.category === "combatant") {
const c = actor as CombatantActor;
this.ecs.addComponent(id, "stats", c.stats);
this.ecs.addComponent(id, "energy", { current: c.energy, speed: c.speed });
this.ecs.addComponent(id, "actorType", { type: c.type });
if (c.isPlayer) {
this.ecs.addComponent(id, "player", {});
} else {
this.ecs.addComponent(id, "ai", {
state: c.aiState || "wandering",
alertedAt: c.alertedAt,
lastKnownPlayerPos: c.lastKnownPlayerPos
});
}
} else if (actor.category === "collectible") {
this.ecs.addComponent(id, "collectible", { type: "exp_orb", amount: actor.expAmount });
}
}
private addToGrid(actor: Actor) {
const i = idx(this.world, actor.pos.x, actor.pos.y);
if (!this.grid.has(i)) {
this.grid.set(i, []);
}
this.grid.get(i)!.push(actor.id);
}
private removeFromGrid(actor: Actor) {
const i = idx(this.world, actor.pos.x, actor.pos.y);
const ids = this.grid.get(i);
if (ids) {
const index = ids.indexOf(actor.id);
if (index !== -1) {
ids.splice(index, 1);
}
if (ids.length === 0) {
this.grid.delete(i);
}
}
}
moveActor(actorId: EntityId, from: Vec2, to: Vec2) {
const actor = this.actors.get(actorId);
if (!actor) return;
// Remove from old position
const oldIdx = idx(this.world, from.x, from.y);
const ids = this.grid.get(oldIdx);
if (ids) {
const index = ids.indexOf(actorId);
if (index !== -1) ids.splice(index, 1);
if (ids.length === 0) this.grid.delete(oldIdx);
}
// Update position
actor.pos.x = to.x;
actor.pos.y = to.y;
// Update ECS
const posComp = this.ecs.getComponent(actorId, "position");
if (posComp) {
posComp.x = to.x;
posComp.y = to.y;
}
// Add to new position
const newIdx = idx(this.world, to.x, to.y);
if (!this.grid.has(newIdx)) this.grid.set(newIdx, []);
this.grid.get(newIdx)!.push(actorId);
}
addActor(actor: Actor) {
this.actors.set(actor.id, actor);
this.syncActorToECS(actor);
this.addToGrid(actor);
}
removeActor(actorId: EntityId) {
const actor = this.actors.get(actorId);
if (actor) {
this.removeFromGrid(actor);
this.ecs.destroyEntity(actorId);
this.actors.delete(actorId);
}
}
getActorsAt(x: number, y: number): Actor[] {
const i = idx(this.world, x, y);
const ids = this.grid.get(i);
if (!ids) return [];
return ids.map(id => this.actors.get(id)!).filter(Boolean);
}
isOccupied(x: number, y: number, ignoreType?: string): boolean {
const actors = this.getActorsAt(x, y);
if (ignoreType) {
return actors.some(a => a.type !== ignoreType);
}
return actors.length > 0;
}
getNextId(): EntityId {
this.lastId++;
return this.lastId;
}
}

View File

@@ -0,0 +1,275 @@
import { describe, it, expect, beforeEach } from "vitest";
import { EntityAccessor } from "../EntityAccessor";
import { ECSWorld } from "../ecs/World";
import type { World, CombatantActor, CollectibleActor, ItemDropActor, Actor, EntityId } from "../../core/types";
function createMockWorld(): World {
return {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
exit: { x: 9, y: 9 },
};
}
function createPlayer(id: number, x: number, y: number): CombatantActor {
return {
id: id as EntityId,
pos: { x, y },
category: "combatant",
isPlayer: true,
type: "player",
speed: 100,
energy: 0,
stats: {
maxHp: 20, hp: 20, maxMana: 10, mana: 10,
attack: 5, defense: 2, level: 1, exp: 0, expToNextLevel: 10,
critChance: 5, critMultiplier: 150, accuracy: 90, lifesteal: 0,
evasion: 5, blockChance: 0, luck: 0,
statPoints: 0, skillPoints: 0, strength: 10, dexterity: 10, intelligence: 10,
passiveNodes: [],
},
};
}
function createEnemy(id: number, x: number, y: number, type: "rat" | "bat" = "rat"): CombatantActor {
return {
id: id as EntityId,
pos: { x, y },
category: "combatant",
isPlayer: false,
type,
speed: 80,
energy: 0,
stats: {
maxHp: 10, hp: 10, maxMana: 0, mana: 0,
attack: 3, defense: 1, level: 1, exp: 0, expToNextLevel: 10,
critChance: 0, critMultiplier: 100, accuracy: 80, lifesteal: 0,
evasion: 0, blockChance: 0, luck: 0,
statPoints: 0, skillPoints: 0, strength: 5, dexterity: 5, intelligence: 5,
passiveNodes: [],
},
};
}
function createExpOrb(id: number, x: number, y: number): CollectibleActor {
return {
id: id as EntityId,
pos: { x, y },
category: "collectible",
type: "exp_orb",
expAmount: 5,
};
}
function createItemDrop(id: number, x: number, y: number): ItemDropActor {
return {
id: id as EntityId,
pos: { x, y },
category: "item_drop",
item: {
id: "health_potion",
name: "Health Potion",
type: "Consumable",
textureKey: "items",
spriteIndex: 0,
},
};
}
describe("EntityAccessor", () => {
let world: World;
let ecsWorld: ECSWorld;
let accessor: EntityAccessor;
const PLAYER_ID = 1;
beforeEach(() => {
world = createMockWorld();
ecsWorld = new ECSWorld();
accessor = new EntityAccessor(world, PLAYER_ID as EntityId, ecsWorld);
});
function syncActor(actor: Actor) {
ecsWorld.addComponent(actor.id, "position", actor.pos);
ecsWorld.addComponent(actor.id, "name", { name: actor.id.toString() });
if (actor.category === "combatant") {
const c = actor as CombatantActor;
ecsWorld.addComponent(actor.id, "stats", c.stats);
ecsWorld.addComponent(actor.id, "energy", { current: c.energy, speed: c.speed });
ecsWorld.addComponent(actor.id, "actorType", { type: c.type });
if (c.isPlayer) {
ecsWorld.addComponent(actor.id, "player", {});
} else {
ecsWorld.addComponent(actor.id, "ai", { state: "wandering" });
}
} else if (actor.category === "collectible") {
ecsWorld.addComponent(actor.id, "collectible", { type: "exp_orb", amount: (actor as CollectibleActor).expAmount });
} else if (actor.category === "item_drop") {
ecsWorld.addComponent(actor.id, "groundItem", { item: (actor as ItemDropActor).item });
}
}
describe("Player Access", () => {
it("getPlayer returns player when exists", () => {
const player = createPlayer(PLAYER_ID, 5, 5);
syncActor(player);
expect(accessor.getPlayer()?.id).toBe(player.id);
});
it("getPlayer returns null when player doesn't exist", () => {
expect(accessor.getPlayer()).toBeNull();
});
it("getPlayerPos returns position copy", () => {
const player = createPlayer(PLAYER_ID, 3, 4);
syncActor(player);
const pos = accessor.getPlayerPos();
expect(pos).toEqual({ x: 3, y: 4 });
// Verify it's a copy
if (pos) {
pos.x = 99;
const freshPlayer = accessor.getPlayer();
expect(freshPlayer?.pos.x).toBe(3);
}
});
it("isPlayerAlive returns true when player exists", () => {
syncActor(createPlayer(PLAYER_ID, 5, 5));
expect(accessor.isPlayerAlive()).toBe(true);
});
it("isPlayerAlive returns false when player is dead", () => {
expect(accessor.isPlayerAlive()).toBe(false);
});
});
describe("Generic Actor Access", () => {
it("getActor returns actor by ID", () => {
const enemy = createEnemy(2, 3, 3);
syncActor(enemy);
expect(accessor.getActor(2 as EntityId)?.id).toBe(enemy.id);
});
it("getActor returns null for non-existent ID", () => {
expect(accessor.getActor(999 as EntityId)).toBeNull();
});
it("getCombatant returns combatant by ID", () => {
const enemy = createEnemy(2, 3, 3);
syncActor(enemy);
expect(accessor.getCombatant(2 as EntityId)?.id).toBe(enemy.id);
});
it("getCombatant returns null for non-combatant", () => {
const orb = createExpOrb(3, 5, 5);
syncActor(orb);
expect(accessor.getCombatant(3 as EntityId)).toBeNull();
});
it("hasActor returns true for existing actor", () => {
syncActor(createEnemy(2, 3, 3));
expect(accessor.hasActor(2 as EntityId)).toBe(true);
});
it("hasActor returns false for non-existent ID", () => {
expect(accessor.hasActor(999 as EntityId)).toBe(false);
});
});
describe("Spatial Queries", () => {
it("findEnemyAt returns enemy at position", () => {
const enemy = createEnemy(2, 4, 4);
syncActor(enemy);
expect(accessor.findEnemyAt(4, 4)?.id).toBe(enemy.id);
});
it("findEnemyAt returns null when no enemy at position", () => {
syncActor(createPlayer(PLAYER_ID, 4, 4));
expect(accessor.findEnemyAt(4, 4)).toBeNull();
});
it("hasEnemyAt returns true when enemy exists at position", () => {
syncActor(createEnemy(2, 4, 4));
expect(accessor.hasEnemyAt(4, 4)).toBe(true);
});
it("findCollectibleAt returns collectible at position", () => {
const orb = createExpOrb(3, 6, 6);
syncActor(orb);
expect(accessor.findCollectibleAt(6, 6)?.id).toBe(orb.id);
});
it("findItemDropAt returns item drop at position", () => {
const drop = createItemDrop(4, 7, 7);
syncActor(drop);
expect(accessor.findItemDropAt(7, 7)?.id).toBe(drop.id);
});
});
describe("Collection Queries", () => {
beforeEach(() => {
syncActor(createPlayer(PLAYER_ID, 5, 5));
syncActor(createEnemy(2, 3, 3));
syncActor(createEnemy(3, 4, 4, "bat"));
syncActor(createExpOrb(4, 6, 6));
syncActor(createItemDrop(5, 7, 7));
});
it("getEnemies returns only non-player combatants", () => {
const enemies = accessor.getEnemies();
expect(enemies.length).toBe(2);
expect(enemies.every(e => !e.isPlayer)).toBe(true);
});
it("getCombatants returns player and enemies", () => {
const combatants = accessor.getCombatants();
expect(combatants.length).toBe(3);
});
it("getCollectibles returns only collectibles", () => {
const collectibles = accessor.getCollectibles();
expect(collectibles.length).toBe(1);
expect(collectibles[0].id).toBe(4);
});
it("getItemDrops returns only item drops", () => {
const drops = accessor.getItemDrops();
expect(drops.length).toBe(1);
expect(drops[0].id).toBe(5);
});
});
describe("updateWorld", () => {
it("updates references correctly", () => {
syncActor(createPlayer(PLAYER_ID, 1, 1));
const newWorld = createMockWorld();
const newEcsWorld = new ECSWorld();
const newPlayerId = 10;
const newPlayer = createPlayer(newPlayerId, 8, 8);
// Manually add to newEcsWorld
newEcsWorld.addComponent(newPlayer.id, "position", newPlayer.pos);
newEcsWorld.addComponent(newPlayer.id, "actorType", { type: "player" });
newEcsWorld.addComponent(newPlayer.id, "stats", newPlayer.stats);
newEcsWorld.addComponent(newPlayer.id, "player", {});
accessor.updateWorld(newWorld, newPlayerId as EntityId, newEcsWorld);
const player = accessor.getPlayer();
expect(player?.id).toBe(newPlayerId);
expect(player?.pos).toEqual({ x: 8, y: 8 });
});
});
});

View File

@@ -1,132 +0,0 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { EntityManager } from '../EntityManager';
import { type World, type Actor } from '../../core/types';
describe('EntityManager', () => {
let mockWorld: World;
let entityManager: EntityManager;
beforeEach(() => {
mockWorld = {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
actors: new Map<number, Actor>(),
exit: { x: 9, y: 9 }
};
entityManager = new EntityManager(mockWorld);
});
it('should add an actor and update the grid', () => {
const actor: Actor = { id: 1, category: 'combatant', type: 'player', pos: { x: 2, y: 3 }, isPlayer: true } as any;
entityManager.addActor(actor);
expect(mockWorld.actors.has(1)).toBe(true);
expect(entityManager.getActorsAt(2, 3).map(a => a.id)).toContain(1);
expect(entityManager.isOccupied(2, 3)).toBe(true);
});
it('should remove an actor and update the grid', () => {
const actor: Actor = { id: 1, category: 'combatant', type: 'player', pos: { x: 2, y: 3 }, isPlayer: true } as any;
entityManager.addActor(actor);
entityManager.removeActor(1);
expect(mockWorld.actors.has(1)).toBe(false);
expect(entityManager.getActorsAt(2, 3).map(a => a.id)).not.toContain(1);
expect(entityManager.isOccupied(2, 3)).toBe(false);
});
it('should update the grid when an actor moves', () => {
const actor: Actor = { id: 1, category: 'combatant', type: 'player', pos: { x: 2, y: 3 }, isPlayer: true } as any;
entityManager.addActor(actor);
entityManager.moveActor(1, { x: 2, y: 3 }, { x: 4, y: 5 });
expect(actor.pos.x).toBe(4);
expect(actor.pos.y).toBe(5);
expect(entityManager.isOccupied(2, 3)).toBe(false);
expect(entityManager.isOccupied(4, 5)).toBe(true);
expect(entityManager.getActorsAt(4, 5).map(a => a.id)).toContain(1);
});
it('should correctly identify occupied tiles while ignoring specific types', () => {
const orb: Actor = { id: 1, category: 'collectible', type: 'exp_orb', pos: { x: 2, y: 2 } } as any;
const enemy: Actor = { id: 2, category: 'combatant', type: 'rat', pos: { x: 5, y: 5 } } as any;
entityManager.addActor(orb);
entityManager.addActor(enemy);
expect(entityManager.isOccupied(2, 2)).toBe(true);
expect(entityManager.isOccupied(2, 2, 'exp_orb')).toBe(false);
expect(entityManager.isOccupied(5, 5)).toBe(true);
expect(entityManager.isOccupied(5, 5, 'exp_orb')).toBe(true);
});
it('should generate the next available ID by scanning current actors', () => {
mockWorld.actors.set(10, { id: 10, pos: { x: 0, y: 0 } } as any);
mockWorld.actors.set(15, { id: 15, pos: { x: 1, y: 1 } } as any);
// Create new manager to trigger scan since current one has stale lastId
const manager = new EntityManager(mockWorld);
expect(manager.getNextId()).toBe(16);
});
it('should handle multiple actors at the same position', () => {
const actor1: Actor = { id: 1, pos: { x: 1, y: 1 } } as any;
const actor2: Actor = { id: 2, pos: { x: 1, y: 1 } } as any;
entityManager.addActor(actor1);
entityManager.addActor(actor2);
const atPos = entityManager.getActorsAt(1, 1);
expect(atPos.length).toBe(2);
expect(atPos.map(a => a.id)).toContain(1);
expect(atPos.map(a => a.id)).toContain(2);
entityManager.removeActor(1);
expect(entityManager.getActorsAt(1, 1).map(a => a.id)).toEqual([2]);
});
it('should handle removing non-existent actor gracefully', () => {
// Should not throw
entityManager.removeActor(999);
});
it('should handle moving non-existent actor gracefully', () => {
// Should not throw
entityManager.moveActor(999, { x: 0, y: 0 }, { x: 1, y: 1 });
});
it('should handle moving an actor that is not in the grid at expected position (inconsistent state)', () => {
const actor: Actor = { id: 1, pos: { x: 0, y: 0 } } as any;
// Add to actors map but NOT to grid (simulating desync)
mockWorld.actors.set(1, actor);
// Attempt move
entityManager.moveActor(1, { x: 0, y: 0 }, { x: 1, y: 1 });
expect(actor.pos.x).toBe(1);
expect(actor.pos.y).toBe(1);
// Should be added to new position in grid
expect(entityManager.getActorsAt(1, 1).map(a => a.id)).toContain(1);
});
it('should handle moving an actor that is in grid but ID not found in list (very rare edge case)', () => {
// Manually pollute grid with empty array for old pos
// This forces `ids` to exist but `indexOf` to return -1
const idx = 0; // 0,0
// @ts-ignore
entityManager.grid.set(idx, [999]); // occupied by someone else
const actor: Actor = { id: 1, pos: { x: 0, y:0 } } as any;
mockWorld.actors.set(1, actor);
entityManager.moveActor(1, { x: 0, y: 0 }, { x: 1, y: 1 });
expect(actor.pos).toEqual({ x: 1, y: 1 });
});
});

View File

@@ -0,0 +1,64 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { TriggerSystem } from '../ecs/systems/TriggerSystem';
import { ECSWorld } from '../ecs/World';
import { EventBus } from '../ecs/EventBus';
import { Prefabs } from '../ecs/Prefabs';
import type { EntityId } from '../../core/types';
describe('Prefab Trap Integration', () => {
let world: ECSWorld;
let eventBus: EventBus;
let system: TriggerSystem;
beforeEach(() => {
world = new ECSWorld();
eventBus = new EventBus();
system = new TriggerSystem();
system.setEventBus(eventBus);
});
it('should trigger poison trap when player moves onto it', () => {
// Setup Player (ID 1)
const playerId = 1 as EntityId;
world.addComponent(playerId, 'position', { x: 1, y: 1 });
world.addComponent(playerId, 'stats', { hp: 10, maxHp: 10 } as any);
world.addComponent(playerId, 'player', {});
// Setup Prefab Trap (ID 100) at (2, 1)
// Use a high ID to avoid collision (simulating generator fix)
world.setNextId(100);
const trapId = Prefabs.poisonTrap(world, 2, 1, 5, 2);
// Register system (initializes entity positions)
system.onRegister(world);
const spy = vi.spyOn(eventBus, 'emit');
// === MOVE PLAYER ===
// Update Player Position to (2, 1)
const pos = world.getComponent(playerId, 'position');
if (pos) pos.x = 2; // Move reference
// Update System
system.update([trapId], world);
// Expect trigger activated
expect(spy).toHaveBeenCalledWith(expect.objectContaining({
type: 'trigger_activated',
triggerId: trapId,
activatorId: playerId
}));
// Expect damage (magnitude 2)
expect(spy).toHaveBeenCalledWith(expect.objectContaining({
type: 'damage',
amount: 2
}));
// Expect status applied
expect(spy).toHaveBeenCalledWith(expect.objectContaining({
type: 'status_applied',
status: 'poison'
}));
});
});

View File

@@ -0,0 +1,55 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { TriggerSystem } from '../ecs/systems/TriggerSystem';
import { ECSWorld } from '../ecs/World';
import { EventBus } from '../ecs/EventBus';
import type { EntityId } from '../../core/types';
describe('TriggerSystem Integration', () => {
let world: ECSWorld;
let eventBus: EventBus;
let system: TriggerSystem;
beforeEach(() => {
world = new ECSWorld();
eventBus = new EventBus();
system = new TriggerSystem();
system.setEventBus(eventBus);
});
it('should trigger onEnter when player moves onto trap', () => {
// Setup Player (ID 1)
const playerId = 1 as EntityId;
const playerPos = { x: 1, y: 1 };
world.addComponent(playerId, 'position', playerPos);
world.addComponent(playerId, 'player', {});
// Setup Trap (ID 100) at (2, 1)
const trapId = 100 as EntityId;
world.addComponent(trapId, 'position', { x: 2, y: 1 });
world.addComponent(trapId, 'trigger', {
onEnter: true,
damage: 10
});
// Register system (initializes entity positions)
system.onRegister(world);
// Verify initial state: Player at (1,1), Trap at (2,1)
// System tracking: Player at (1,1)
const spy = vi.spyOn(eventBus, 'emit');
// === MOVE PLAYER ===
// Simulate MovementSystem update
playerPos.x = 2; // Move to (2,1) directly (reference update)
// System Update
system.update([trapId], world);
// Expect trigger activation
expect(spy).toHaveBeenCalledWith(expect.objectContaining({
type: 'trigger_activated',
triggerId: trapId,
activatorId: playerId
}));
});
});

View File

@@ -1,16 +1,15 @@
import { describe, it, expect } from 'vitest';
import { describe, it, expect, beforeEach } from 'vitest';
import { decideEnemyAction, applyAction, stepUntilPlayerTurn } from '../simulation/simulation';
import { type World, type Actor, type EntityId, type CombatantActor } from '../../core/types';
import { EntityManager } from '../EntityManager';
import { EntityAccessor } from '../EntityAccessor';
import { TileType } from '../../core/terrain';
import { ECSWorld } from '../ecs/World';
const createTestWorld = (actors: Map<EntityId, Actor>): World => {
const createTestWorld = (): World => {
return {
width: 10,
height: 10,
tiles: new Array(100).fill(TileType.EMPTY),
actors,
exit: { x: 9, y: 9 }
};
};
@@ -23,7 +22,37 @@ const createTestStats = (overrides: Partial<any> = {}) => ({
});
describe('AI Behavior & Scheduling', () => {
let entityManager: EntityManager;
let accessor: EntityAccessor;
let ecsWorld: ECSWorld;
beforeEach(() => {
ecsWorld = new ECSWorld();
});
const syncToECS = (actors: Map<EntityId, Actor>) => {
let maxId = 0;
for (const actor of actors.values()) {
if (actor.id > maxId) maxId = actor.id;
ecsWorld.addComponent(actor.id, "position", actor.pos);
ecsWorld.addComponent(actor.id, "name", { name: actor.id.toString() });
if (actor.category === "combatant") {
const c = actor as CombatantActor;
ecsWorld.addComponent(actor.id, "stats", c.stats || createTestStats());
ecsWorld.addComponent(actor.id, "energy", { current: c.energy, speed: c.speed || 100 });
ecsWorld.addComponent(actor.id, "actorType", { type: c.type || "player" });
if (c.isPlayer) {
ecsWorld.addComponent(actor.id, "player", {});
} else {
ecsWorld.addComponent(actor.id, "ai", {
state: c.aiState || "wandering",
alertedAt: c.alertedAt,
lastKnownPlayerPos: c.lastKnownPlayerPos
});
}
}
}
ecsWorld.setNextId(maxId + 1);
};
// -------------------------------------------------------------------------
// Scheduling Fairness
@@ -33,37 +62,34 @@ describe('AI Behavior & Scheduling', () => {
const actors = new Map<EntityId, Actor>();
// Player Speed 100
const player = {
id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 },
id: 1 as EntityId, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 },
speed: 100, stats: createTestStats(), energy: 0
} as any;
// Rat Speed 80 (Slow)
const rat = {
id: 2, category: "combatant", isPlayer: false, pos: { x: 9, y: 9 },
id: 2 as EntityId, category: "combatant", isPlayer: false, pos: { x: 9, y: 9 },
speed: 80, stats: createTestStats(), aiState: "wandering", energy: 0
} as any;
actors.set(1, player);
actors.set(2, rat);
const world = createTestWorld(actors);
entityManager = new EntityManager(world);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, rat);
const world = createTestWorld();
syncToECS(actors);
accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
let ratMoves = 0;
// Simulate 20 player turns
// With fair scheduling, Rat (80 speed) should move approx 80% as often as Player (100 speed).
// So in 20 turns, approx 16 moves. Definitley > 0.
for (let i = 0; i < 20; i++) {
const result = stepUntilPlayerTurn(world, 1, entityManager);
const result = stepUntilPlayerTurn(world, 1 as EntityId, accessor);
const enemyActs = result.events.filter(e =>
(e.type === "moved" || e.type === "waited" || e.type === "enemy-alerted") &&
((e as any).actorId === 2 || (e as any).enemyId === 2)
);
// console.log(`Turn ${i}: Events`, result.events);
if (enemyActs.length > 0) ratMoves++;
}
// console.log(`Total Rat Moves: ${ratMoves}`);
expect(ratMoves).toBeGreaterThan(0);
});
});
@@ -81,19 +107,22 @@ describe('AI Behavior & Scheduling', () => {
terrainTypes.forEach(({ type, name }) => {
it(`should see player when standing on ${name}`, () => {
const actors = new Map<EntityId, Actor>();
actors.set(1, { id: 1, category: "combatant", isPlayer: true, pos: { x: 5, y: 0 }, stats: createTestStats(), energy: 0 } as any);
actors.set(2, {
id: 2, category: "combatant", isPlayer: false, pos: { x: 0, y: 0 },
actors.set(1 as EntityId, { id: 1 as EntityId, category: "combatant", isPlayer: true, pos: { x: 5, y: 0 }, stats: createTestStats(), energy: 0 } as any);
actors.set(2 as EntityId, {
id: 2 as EntityId, category: "combatant", isPlayer: false, pos: { x: 0, y: 0 },
stats: createTestStats(), aiState: "wandering", energy: 0
} as any);
const world = createTestWorld(actors);
const world = createTestWorld();
world.tiles[0] = type;
syncToECS(actors);
const testAccessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
// Rat at 0,0. Player at 5,0.
decideEnemyAction(world, actors.get(2) as any, actors.get(1) as any, new EntityManager(world));
decideEnemyAction(world, testAccessor.getCombatant(2 as EntityId) as any, testAccessor.getCombatant(1 as EntityId) as any, testAccessor);
expect((actors.get(2) as CombatantActor).aiState).toBe("alerted");
const updatedRat = testAccessor.getCombatant(2 as EntityId);
expect(updatedRat?.aiState).toBe("alerted");
});
});
});
@@ -105,29 +134,30 @@ describe('AI Behavior & Scheduling', () => {
it('should become pursuing when damaged by player, even if not sighting player', () => {
const actors = new Map<EntityId, Actor>();
// Player far away/invisible (simulated logic)
const player = { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 }, stats: createTestStats({ attack: 1, accuracy: 100 }), energy: 0 } as any;
const player = { id: 1 as EntityId, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 }, stats: createTestStats({ attack: 1, accuracy: 100 }), energy: 0 } as any;
const enemy = {
id: 2, category: "combatant", isPlayer: false, pos: { x: 0, y: 5 },
id: 2 as EntityId, category: "combatant", isPlayer: false, pos: { x: 0, y: 5 },
stats: createTestStats({ hp: 10, defense: 0, evasion: 0 }), aiState: "wandering", energy: 0
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
const em = new EntityManager(world);
applyAction(world, 1, { type: "attack", targetId: 2 }, em);
const testAccessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, testAccessor);
const updatedEnemy = actors.get(2) as CombatantActor;
expect(updatedEnemy.aiState).toBe("pursuing");
expect(updatedEnemy.lastKnownPlayerPos).toEqual(player.pos);
const updatedEnemy = testAccessor.getCombatant(2 as EntityId);
expect(updatedEnemy?.aiState).toBe("pursuing");
expect(updatedEnemy?.lastKnownPlayerPos).toEqual(player.pos);
});
it("should transition from alerted to pursuing after delay even if sight is blocked", () => {
const actors = new Map<EntityId, Actor>();
const player = { id: 1, category: "combatant", isPlayer: true, pos: { x: 9, y: 9 }, stats: createTestStats(), energy: 0 } as any;
const player = { id: 1 as EntityId, category: "combatant", isPlayer: true, pos: { x: 9, y: 9 }, stats: createTestStats(), energy: 0 } as any;
const enemy = {
id: 2,
id: 2 as EntityId,
category: "combatant",
isPlayer: false,
pos: { x: 0, y: 0 },
@@ -138,17 +168,20 @@ describe('AI Behavior & Scheduling', () => {
energy: 0
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
// Player is far away and potentially blocked
world.tiles[1] = TileType.WALL; // x=1, y=0 blocked
syncToECS(actors);
decideEnemyAction(world, enemy, player, new EntityManager(world));
const testAccessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
const rat = testAccessor.getCombatant(2 as EntityId)!;
decideEnemyAction(world, rat, testAccessor.getPlayer()!, testAccessor);
// alerted -> pursuing (due to time) -> searching (due to no sight)
expect(enemy.aiState).toBe("searching");
expect(rat.aiState).toBe("searching");
});
});
});

View File

@@ -1,9 +1,16 @@
import { describe, it, expect } from "vitest";
import { describe, it, expect, beforeEach } from "vitest";
import { getClosestVisibleEnemy } from "../gameplay/CombatLogic";
import type { World, CombatantActor } from "../../core/types";
import type { World, CombatantActor, Actor, EntityId } from "../../core/types";
import { EntityAccessor } from "../EntityAccessor";
import { ECSWorld } from "../ecs/World";
describe("CombatLogic - getClosestVisibleEnemy", () => {
let ecsWorld: ECSWorld;
beforeEach(() => {
ecsWorld = new ECSWorld();
});
// Helper to create valid default stats for testing
const createMockStats = () => ({
@@ -21,29 +28,40 @@ describe("CombatLogic - getClosestVisibleEnemy", () => {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
actors: new Map(),
exit: { x: 9, y: 9 }
};
const actors = new Map<EntityId, Actor>();
const player: CombatantActor = {
id: 0, category: "combatant", type: "player", pos: { x: 5, y: 5 }, isPlayer: true,
stats: createMockStats(),
inventory: { gold: 0, items: [] }, equipment: {},
speed: 1, energy: 0
};
world.actors.set(0, player);
actors.set(0 as EntityId, player);
const enemy: CombatantActor = {
id: 1, category: "combatant", type: "rat", pos: { x: 6, y: 6 }, isPlayer: false,
stats: createMockStats(),
speed: 1, energy: 0
};
world.actors.set(1, enemy);
actors.set(1 as EntityId, enemy);
for (const a of actors.values()) {
ecsWorld.addComponent(a.id, "position", a.pos);
ecsWorld.addComponent(a.id, "actorType", { type: a.type as any });
if (a.category === "combatant") {
ecsWorld.addComponent(a.id, "stats", a.stats);
if (a.isPlayer) ecsWorld.addComponent(a.id, "player", {});
}
}
const accessor = new EntityAccessor(world, 0 as EntityId, ecsWorld);
// Mock seenArray where nothing is seen
const seenArray = new Uint8Array(100).fill(0);
const result = getClosestVisibleEnemy(world, player.pos, seenArray, 10);
const result = getClosestVisibleEnemy(player.pos, seenArray, 10, accessor);
expect(result).toBeNull();
});
@@ -52,17 +70,17 @@ describe("CombatLogic - getClosestVisibleEnemy", () => {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
actors: new Map(),
exit: { x: 9, y: 9 }
};
const actors = new Map<EntityId, Actor>();
const player: CombatantActor = {
id: 0, category: "combatant", type: "player", pos: { x: 5, y: 5 }, isPlayer: true,
stats: createMockStats(),
inventory: { gold: 0, items: [] }, equipment: {},
speed: 1, energy: 0
};
world.actors.set(0, player);
actors.set(0 as EntityId, player);
// Enemy 1: Close (distance sqrt(2) ~= 1.41)
const enemy1: CombatantActor = {
@@ -70,7 +88,7 @@ describe("CombatLogic - getClosestVisibleEnemy", () => {
stats: createMockStats(),
speed: 1, energy: 0
};
world.actors.set(1, enemy1);
actors.set(1 as EntityId, enemy1);
// Enemy 2: Farther (distance sqrt(8) ~= 2.82)
const enemy2: CombatantActor = {
@@ -78,14 +96,25 @@ describe("CombatLogic - getClosestVisibleEnemy", () => {
stats: createMockStats(),
speed: 1, energy: 0
};
world.actors.set(2, enemy2);
actors.set(2 as EntityId, enemy2);
for (const a of actors.values()) {
ecsWorld.addComponent(a.id, "position", a.pos);
ecsWorld.addComponent(a.id, "actorType", { type: a.type as any });
if (a.category === "combatant") {
ecsWorld.addComponent(a.id, "stats", a.stats);
if (a.isPlayer) ecsWorld.addComponent(a.id, "player", {});
}
}
const accessor = new EntityAccessor(world, 0 as EntityId, ecsWorld);
// Mock seenArray where both are seen
const seenArray = new Uint8Array(100).fill(0);
seenArray[6 * 10 + 6] = 1; // Enemy 1 visible
seenArray[7 * 10 + 7] = 1; // Enemy 2 visible
const result = getClosestVisibleEnemy(world, player.pos, seenArray, 10);
const result = getClosestVisibleEnemy(player.pos, seenArray, 10, accessor);
expect(result).toEqual({ x: 6, y: 6 });
});
@@ -94,17 +123,17 @@ describe("CombatLogic - getClosestVisibleEnemy", () => {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
actors: new Map(),
exit: { x: 9, y: 9 }
};
const actors = new Map<EntityId, Actor>();
const player: CombatantActor = {
id: 0, category: "combatant", type: "player", pos: { x: 5, y: 5 }, isPlayer: true,
stats: createMockStats(),
inventory: { gold: 0, items: [] }, equipment: {},
speed: 1, energy: 0
};
world.actors.set(0, player);
actors.set(0 as EntityId, player);
// Enemy 1: Close but invisible
const enemy1: CombatantActor = {
@@ -112,7 +141,7 @@ describe("CombatLogic - getClosestVisibleEnemy", () => {
stats: createMockStats(),
speed: 1, energy: 0
};
world.actors.set(1, enemy1);
actors.set(1 as EntityId, enemy1);
// Enemy 2: Farther but visible
const enemy2: CombatantActor = {
@@ -120,13 +149,24 @@ describe("CombatLogic - getClosestVisibleEnemy", () => {
stats: createMockStats(),
speed: 1, energy: 0
};
world.actors.set(2, enemy2);
actors.set(2 as EntityId, enemy2);
for (const a of actors.values()) {
ecsWorld.addComponent(a.id, "position", a.pos);
ecsWorld.addComponent(a.id, "actorType", { type: a.type as any });
if (a.category === "combatant") {
ecsWorld.addComponent(a.id, "stats", a.stats);
if (a.isPlayer) ecsWorld.addComponent(a.id, "player", {});
}
}
const accessor = new EntityAccessor(world, 0 as EntityId, ecsWorld);
// Mock seenArray where only Enemy 2 is seen
const seenArray = new Uint8Array(100).fill(0);
seenArray[5 * 10 + 8] = 1; // Enemy 2 visible at (8,5)
const result = getClosestVisibleEnemy(world, player.pos, seenArray, 10);
const result = getClosestVisibleEnemy(player.pos, seenArray, 10, accessor);
expect(result).toEqual({ x: 8, y: 5 });
});
});

View File

@@ -1,8 +1,10 @@
import { describe, it, expect } from 'vitest';
import { generateWorld } from '../world/generator';
import { isWall, inBounds } from '../world/world-logic';
import { type CombatantActor } from '../../core/types';
import { TileType } from '../../core/terrain';
import { EntityAccessor } from '../EntityAccessor';
import * as ROT from 'rot-js';
describe('World Generator', () => {
@@ -36,14 +38,17 @@ describe('World Generator', () => {
inventory: { gold: 0, items: [] }
};
const { world, playerId } = generateWorld(1, runState);
const { world, playerId, ecsWorld } = generateWorld(1, runState);
const accessor = new EntityAccessor(world, playerId, ecsWorld);
expect(playerId).toBe(1);
const player = world.actors.get(playerId) as CombatantActor;
expect(playerId).toBeGreaterThan(0);
const player = accessor.getPlayer();
expect(player).toBeDefined();
expect(player.category).toBe("combatant");
expect(player.isPlayer).toBe(true);
expect(player.stats).toEqual(runState.stats);
expect(player?.category).toBe("combatant");
expect(player?.isPlayer).toBe(true);
// We expect the stats to be the same, but they are proxies now
expect(player?.stats.hp).toEqual(runState.stats.hp);
expect(player?.stats.attack).toEqual(runState.stats.attack);
});
it('should create walkable rooms', () => {
@@ -57,8 +62,9 @@ describe('World Generator', () => {
inventory: { gold: 0, items: [] }
};
const { world, playerId } = generateWorld(1, runState);
const player = world.actors.get(playerId)!;
const { world, playerId, ecsWorld } = generateWorld(1, runState);
const accessor = new EntityAccessor(world, playerId, ecsWorld);
const player = accessor.getPlayer()!;
// Player should spawn in a walkable area
expect(isWall(world, player.pos.x, player.pos.y)).toBe(false);
@@ -93,13 +99,10 @@ describe('World Generator', () => {
inventory: { gold: 0, items: [] }
};
const { world } = generateWorld(1, runState);
const { world, playerId, ecsWorld } = generateWorld(1, runState);
const accessor = new EntityAccessor(world, playerId, ecsWorld);
// Should have player + enemies
expect(world.actors.size).toBeGreaterThan(1);
// All non-player actors should be enemies
const enemies = Array.from(world.actors.values()).filter(a => a.category === "combatant" && !a.isPlayer) as CombatantActor[];
const enemies = accessor.getEnemies();
expect(enemies.length).toBeGreaterThan(0);
// Enemies should have stats
@@ -121,15 +124,18 @@ describe('World Generator', () => {
inventory: { gold: 0, items: [] }
};
const { world: world1, playerId: player1 } = generateWorld(1, runState);
const { world: world2, playerId: player2 } = generateWorld(1, runState);
const { world: world1, playerId: player1, ecsWorld: ecs1 } = generateWorld(1, runState);
const { world: world2, playerId: player2, ecsWorld: ecs2 } = generateWorld(1, runState);
// Same level should generate identical layouts
expect(world1.tiles).toEqual(world2.tiles);
expect(world1.exit).toEqual(world2.exit);
const player1Pos = world1.actors.get(player1)!.pos;
const player2Pos = world2.actors.get(player2)!.pos;
const accessor1 = new EntityAccessor(world1, player1, ecs1);
const accessor2 = new EntityAccessor(world2, player2, ecs2);
const player1Pos = accessor1.getPlayer()!.pos;
const player2Pos = accessor2.getPlayer()!.pos;
expect(player1Pos).toEqual(player2Pos);
});
@@ -162,11 +168,14 @@ describe('World Generator', () => {
inventory: { gold: 0, items: [] }
};
const { world: world1 } = generateWorld(1, runState);
const { world: world5 } = generateWorld(5, runState);
const { world: world1, playerId: p1, ecsWorld: ecs1 } = generateWorld(1, runState);
const { world: world5, playerId: p5, ecsWorld: ecs5 } = generateWorld(5, runState);
const enemies1 = Array.from(world1.actors.values()).filter(a => a.category === "combatant" && !a.isPlayer) as CombatantActor[];
const enemies5 = Array.from(world5.actors.values()).filter(a => a.category === "combatant" && !a.isPlayer) as CombatantActor[];
const accessor1 = new EntityAccessor(world1, p1, ecs1);
const accessor5 = new EntityAccessor(world5, p5, ecs5);
const enemies1 = accessor1.getEnemies();
const enemies5 = accessor5.getEnemies();
// Higher level should have more enemies
expect(enemies5.length).toBeGreaterThan(enemies1.length);
@@ -213,8 +222,9 @@ describe('World Generator', () => {
// Generate multiple worlds to stress test spawn placement
for (let i = 0; i < 10; i++) {
const { world, playerId } = generateWorld(1, runState);
const player = world.actors.get(playerId)!;
const { world, playerId, ecsWorld } = generateWorld(1, runState);
const accessor = new EntityAccessor(world, playerId, ecsWorld);
const player = accessor.getPlayer()!;
// Check tile under player
const tileIdx = player.pos.y * world.width + player.pos.x;
@@ -259,8 +269,9 @@ describe('World Generator', () => {
inventory: { gold: 0, items: [] }
};
const { world } = generateWorld(11, runState);
const enemies = Array.from(world.actors.values()).filter(a => a.category === 'combatant' && !a.isPlayer);
const { world, playerId, ecsWorld } = generateWorld(11, runState);
const accessor = new EntityAccessor(world, playerId, ecsWorld);
const enemies = accessor.getEnemies();
expect(enemies.length).toBeGreaterThan(0);
});
@@ -276,8 +287,9 @@ describe('World Generator', () => {
};
for (let i = 0; i < 5; i++) {
const { world, playerId } = generateWorld(10 + i, runState);
const player = world.actors.get(playerId)!;
const { world, playerId, ecsWorld } = generateWorld(10 + i, runState);
const accessor = new EntityAccessor(world, playerId, ecsWorld);
const player = accessor.getPlayer()!;
const exit = world.exit;
const pathfinder = new ROT.Path.AStar(exit.x, exit.y, (x, y) => {
@@ -304,8 +316,9 @@ describe('World Generator', () => {
},
inventory: { gold: 0, items: [] }
};
const { world, playerId } = generateWorld(12, runState);
const player = world.actors.get(playerId)!;
const { world, playerId, ecsWorld } = generateWorld(12, runState);
const accessor = new EntityAccessor(world, playerId, ecsWorld);
const player = accessor.getPlayer()!;
expect(world.tiles[player.pos.y * world.width + player.pos.x]).toBe(TileType.EMPTY);
});

View File

@@ -1,39 +1,49 @@
import { describe, it, expect, beforeEach } from "vitest";
import { ItemManager } from "../../scenes/systems/ItemManager";
import type { World, CombatantActor, Item } from "../../core/types";
import { EntityManager } from "../../engine/EntityManager";
import type { World, CombatantActor, Item, EntityId } from "../../core/types";
import { EntityAccessor } from "../../engine/EntityAccessor";
import { ECSWorld } from "../../engine/ecs/World";
describe("ItemManager - Stacking Logic", () => {
let itemManager: ItemManager;
let entityManager: EntityManager;
let accessor: EntityAccessor;
let world: World;
let player: CombatantActor;
let ecsWorld: ECSWorld;
beforeEach(() => {
world = {
width: 10,
height: 10,
tiles: [],
actors: new Map(),
tiles: new Array(100).fill(0),
exit: { x: 9, y: 9 }
} as any;
entityManager = new EntityManager(world);
itemManager = new ItemManager(world, entityManager);
ecsWorld = new ECSWorld();
accessor = new EntityAccessor(world, 0 as EntityId, ecsWorld);
itemManager = new ItemManager(world, accessor, ecsWorld);
player = {
id: 0,
id: 0 as EntityId,
pos: { x: 1, y: 1 },
category: "combatant",
isPlayer: true,
type: "player",
inventory: { gold: 0, items: [] },
stats: {} as any,
stats: { hp: 10, maxHp: 10 } as any,
equipment: {} as any,
speed: 1,
speed: 100,
energy: 0
};
world.actors.set(0, player);
// Sync player to ECS
ecsWorld.addComponent(player.id, "position", player.pos);
ecsWorld.addComponent(player.id, "player", {});
ecsWorld.addComponent(player.id, "stats", player.stats);
ecsWorld.addComponent(player.id, "actorType", { type: "player" });
ecsWorld.addComponent(player.id, "inventory", player.inventory!);
ecsWorld.addComponent(player.id, "energy", { current: 0, speed: 100 });
});
it("should stack stackable items when picked up", () => {
@@ -47,25 +57,27 @@ describe("ItemManager - Stacking Logic", () => {
quantity: 1
};
const playerActor = accessor.getPlayer()!;
// First potion
itemManager.spawnItem(potion, { x: 1, y: 1 });
itemManager.tryPickup(player);
itemManager.tryPickup(playerActor);
expect(player.inventory!.items.length).toBe(1);
expect(player.inventory!.items[0].quantity).toBe(1);
expect(playerActor.inventory!.items.length).toBe(1);
expect(playerActor.inventory!.items[0].quantity).toBe(1);
// Second potion
itemManager.spawnItem(potion, { x: 1, y: 1 });
itemManager.tryPickup(player);
itemManager.tryPickup(playerActor);
expect(player.inventory!.items.length).toBe(1);
expect(player.inventory!.items[0].quantity).toBe(2);
expect(playerActor.inventory!.items.length).toBe(1);
expect(playerActor.inventory!.items[0].quantity).toBe(2);
});
it("should NOT stack non-stackable items", () => {
const sword: Item = {
id: "sword",
name: "Sword",
id: "iron_sword",
name: "Iron Sword",
type: "Weapon",
weaponType: "melee",
textureKey: "items",
@@ -74,40 +86,44 @@ describe("ItemManager - Stacking Logic", () => {
stats: { attack: 1 }
} as any;
const playerActor = accessor.getPlayer()!;
// First sword
itemManager.spawnItem(sword, { x: 1, y: 1 });
itemManager.tryPickup(player);
itemManager.tryPickup(playerActor);
expect(player.inventory!.items.length).toBe(1);
expect(playerActor.inventory!.items.length).toBe(1);
// Second sword
itemManager.spawnItem(sword, { x: 1, y: 1 });
itemManager.tryPickup(player);
itemManager.tryPickup(playerActor);
expect(player.inventory!.items.length).toBe(2);
expect(playerActor.inventory!.items.length).toBe(2);
});
it("should sum quantities of stackable items correctly", () => {
const ammo: Item = {
id: "ammo",
name: "Ammo",
id: "9mm_ammo",
name: "9mm Ammo",
type: "Ammo",
textureKey: "items",
spriteIndex: 2,
stackable: true,
quantity: 10,
ammoType: "9mm"
};
} as any;
const playerActor = accessor.getPlayer()!;
itemManager.spawnItem(ammo, { x: 1, y: 1 });
itemManager.tryPickup(player);
itemManager.tryPickup(playerActor);
expect(player.inventory!.items[0].quantity).toBe(10);
expect(playerActor.inventory!.items[0].quantity).toBe(10);
const moreAmmo = { ...ammo, quantity: 5 };
itemManager.spawnItem(moreAmmo, { x: 1, y: 1 });
itemManager.tryPickup(player);
itemManager.tryPickup(playerActor);
expect(player.inventory!.items[0].quantity).toBe(15);
expect(playerActor.inventory!.items[0].quantity).toBe(15);
});
});

View File

@@ -1,14 +1,16 @@
import { describe, it, expect } from 'vitest';
import { findPathAStar } from '../world/pathfinding';
import { type World } from '../../core/types';
import type { World, EntityId } from '../../core/types';
import { TileType } from '../../core/terrain';
import { ECSWorld } from '../ecs/World';
import { EntityAccessor } from '../EntityAccessor';
describe('Pathfinding', () => {
const createTestWorld = (width: number, height: number, tileType: number = TileType.EMPTY): World => ({
width,
height,
tiles: new Array(width * height).fill(tileType),
actors: new Map(),
exit: { x: 0, y: 0 }
});
@@ -47,23 +49,22 @@ describe('Pathfinding', () => {
it('should respect ignoreBlockedTarget option', () => {
const world = createTestWorld(10, 10);
const ecsWorld = new ECSWorld();
// Place an actor at target
world.actors.set(1, { id: 1, pos: { x: 0, y: 3 }, type: 'rat', category: 'combatant' } as any);
ecsWorld.addComponent(1 as EntityId, "position", { x: 0, y: 3 });
ecsWorld.addComponent(1 as EntityId, "actorType", { type: "rat" });
ecsWorld.addComponent(1 as EntityId, "stats", { hp: 10 } as any);
const seen = new Uint8Array(100).fill(1);
const accessor = new EntityAccessor(world, 0 as EntityId, ecsWorld);
// Without option, it should be blocked (because actor is there)
// Wait, default pathfinding might treat actors as blocking unless specified.
// Let's check `isBlocked` usage in `pathfinding.ts`.
// It calls `isBlocked` which checks actors.
// However, findPathAStar has logic:
// if (!options.ignoreBlockedTarget && isBlocked(w, end.x, end.y, options.em)) return [];
const pathBlocked = findPathAStar(world, seen, { x: 0, y: 0 }, { x: 0, y: 3 });
// With accessor, it should be blocked
const pathBlocked = findPathAStar(world, seen, { x: 0, y: 0 }, { x: 0, y: 3 }, { accessor });
expect(pathBlocked).toEqual([]);
const pathIgnored = findPathAStar(world, seen, { x: 0, y: 0 }, { x: 0, y: 3 }, { ignoreBlockedTarget: true });
// With ignoreBlockedTarget, it should succeed
const pathIgnored = findPathAStar(world, seen, { x: 0, y: 0 }, { x: 0, y: 3 }, { ignoreBlockedTarget: true, accessor });
expect(pathIgnored.length).toBeGreaterThan(0);
expect(pathIgnored[pathIgnored.length - 1]).toEqual({ x: 0, y: 3 });
});

View File

@@ -1,14 +1,15 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { applyAction, decideEnemyAction, stepUntilPlayerTurn } from '../simulation/simulation';
import { type World, type Actor, type EntityId, type CombatantActor } from '../../core/types';
import { EntityManager } from '../EntityManager';
import { EntityAccessor } from '../EntityAccessor';
import { ECSWorld } from '../ecs/World';
const createTestWorld = (actors: Map<EntityId, Actor>): World => {
const createTestWorld = (): World => {
return {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
actors,
exit: { x: 9, y: 9 }
};
};
@@ -21,14 +22,45 @@ const createTestStats = (overrides: Partial<any> = {}) => ({
});
describe('Combat Simulation', () => {
let entityManager: EntityManager;
let ecsWorld: ECSWorld;
beforeEach(() => {
ecsWorld = new ECSWorld();
});
const syncToECS = (actors: Map<EntityId, Actor>) => {
let maxId = 0;
for (const actor of actors.values()) {
if (actor.id > maxId) maxId = actor.id;
ecsWorld.addComponent(actor.id, "position", actor.pos);
ecsWorld.addComponent(actor.id, "name", { name: actor.id.toString() });
if (actor.category === "combatant") {
const c = actor as CombatantActor;
ecsWorld.addComponent(actor.id, "stats", c.stats || createTestStats());
ecsWorld.addComponent(actor.id, "energy", { current: c.energy, speed: c.speed || 100 });
ecsWorld.addComponent(actor.id, "actorType", { type: c.type || "player" });
if (c.isPlayer) {
ecsWorld.addComponent(actor.id, "player", {});
} else {
ecsWorld.addComponent(actor.id, "ai", {
state: c.aiState || "wandering",
alertedAt: c.alertedAt,
lastKnownPlayerPos: c.lastKnownPlayerPos
});
}
} else if (actor.category === "collectible") {
ecsWorld.addComponent(actor.id, "collectible", { type: "exp_orb", amount: actor.expAmount });
}
}
ecsWorld.setNextId(maxId + 1);
};
describe('applyAction', () => {
it('should return empty events if actor does not exist', () => {
const world = createTestWorld(new Map());
const events = applyAction(world, 999, { type: "wait" });
const world = createTestWorld();
const events = applyAction(world, 999 as EntityId, { type: "wait" }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(events).toEqual([]);
});
});
@@ -36,60 +68,63 @@ describe('Combat Simulation', () => {
describe('applyAction - success paths', () => {
it('should deal damage when player attacks enemy', () => {
const actors = new Map<EntityId, Actor>();
actors.set(1, {
actors.set(1 as EntityId, {
id: 1, category: "combatant", isPlayer: true, type: "player", pos: { x: 3, y: 3 }, speed: 100, stats: createTestStats(), energy: 0
} as any);
actors.set(2, {
actors.set(2 as EntityId, {
id: 2, category: "combatant", isPlayer: false, type: "rat", pos: { x: 4, y: 3 }, speed: 100, stats: createTestStats({ maxHp: 10, hp: 10, attack: 3, defense: 1 }), energy: 0
} as any);
const world = createTestWorld(actors);
entityManager = new EntityManager(world);
const events = applyAction(world, 1, { type: "attack", targetId: 2 }, entityManager);
const world = createTestWorld();
syncToECS(actors);
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
const events = applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, accessor);
const enemy = world.actors.get(2) as CombatantActor;
expect(enemy.stats.hp).toBeLessThan(10);
const enemy = accessor.getCombatant(2 as EntityId);
expect(enemy?.stats.hp).toBeLessThan(10);
expect(events.some(e => e.type === "attacked")).toBe(true);
});
it("should kill enemy and spawn EXP orb without ID reuse collision", () => {
const actors = new Map<EntityId, Actor>();
actors.set(1, {
actors.set(1 as EntityId, {
id: 1, category: "combatant", isPlayer: true, type: "player", pos: { x: 3, y: 3 }, speed: 100, stats: createTestStats({ attack: 50 }), energy: 0
} as any);
actors.set(2, {
actors.set(2 as EntityId, {
id: 2, category: "combatant", isPlayer: false, type: "rat", pos: { x: 4, y: 3 }, speed: 100, stats: createTestStats({ maxHp: 10, hp: 10, attack: 3, defense: 1 }), energy: 0
} as any);
const world = createTestWorld(actors);
entityManager = new EntityManager(world);
applyAction(world, 1, { type: "attack", targetId: 2 }, entityManager);
const world = createTestWorld();
syncToECS(actors);
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, accessor);
// Enemy (id 2) should be gone
expect(world.actors.has(2)).toBe(false);
expect(accessor.hasActor(2 as EntityId)).toBe(false);
// A new ID should be generated for the orb (should be 3)
const orb = [...world.actors.values()].find(a => a.type === "exp_orb");
const orb = accessor.getCollectibles().find(a => a.type === "exp_orb");
expect(orb).toBeDefined();
expect(orb!.id).toBe(3);
});
it("should destruction tile when walking on destructible-by-walk tile", () => {
const actors = new Map<EntityId, Actor>();
actors.set(1, {
actors.set(1 as EntityId, {
id: 1, category: "combatant", isPlayer: true, type: "player", pos: { x: 3, y: 3 }, speed: 100, stats: createTestStats(), energy: 0
} as any);
const world = createTestWorld(actors);
const world = createTestWorld();
// tile at 4,3 is grass (15) which is destructible by walk
const grassIdx = 3 * 10 + 4;
world.tiles[grassIdx] = 15; // TileType.GRASS
entityManager = new EntityManager(world);
applyAction(world, 1, { type: "move", dx: 1, dy: 0 }, entityManager);
syncToECS(actors);
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
applyAction(world, 1 as EntityId, { type: "move", dx: 1, dy: 0 }, accessor);
// Player moved to 4,3
const player = world.actors.get(1);
const player = accessor.getActor(1 as EntityId);
expect(player!.pos).toEqual({ x: 4, y: 3 });
// Tile should effectively be destroyed (turned to saplings/2)
@@ -98,28 +133,30 @@ describe('Combat Simulation', () => {
it("should handle wait action", () => {
const actors = new Map<EntityId, Actor>();
actors.set(1, { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 } } as any);
const world = createTestWorld(actors);
const events = applyAction(world, 1, { type: "wait" }, new EntityManager(world));
actors.set(1 as EntityId, { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 }, stats: createTestStats(), type: "player" } as any);
const world = createTestWorld();
syncToECS(actors);
const events = applyAction(world, 1 as EntityId, { type: "wait" }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(events).toEqual([{ type: "waited", actorId: 1 }]);
});
it("should default to wait for unknown action type", () => {
const actors = new Map<EntityId, Actor>();
actors.set(1, { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 } } as any);
const world = createTestWorld(actors);
actors.set(1 as EntityId, { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 }, stats: createTestStats(), type: "player" } as any);
const world = createTestWorld();
syncToECS(actors);
const events = applyAction(world, 1, { type: "unknown_hack" } as any, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, { type: "unknown_hack" } as any, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(events).toEqual([{ type: "waited", actorId: 1 }]);
});
it("should NOT emit wait event for throw action", () => {
const actors = new Map<EntityId, Actor>();
actors.set(1, { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 } } as any);
const world = createTestWorld(actors);
actors.set(1 as EntityId, { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 }, stats: createTestStats(), type: "player" } as any);
const world = createTestWorld();
syncToECS(actors);
const events = applyAction(world, 1, { type: "throw" }, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, { type: "throw" }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(events).toEqual([]);
});
});
@@ -129,14 +166,15 @@ describe('Combat Simulation', () => {
const actors = new Map<EntityId, Actor>();
const player = { id: 1, category: "combatant", isPlayer: true, pos: { x: 5, y: 3 }, stats: createTestStats(), energy: 0 } as any;
const enemy = { id: 2, category: "combatant", isPlayer: false, pos: { x: 3, y: 3 }, stats: createTestStats(), energy: 0 } as any;
actors.set(1, player);
actors.set(2, enemy);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld(actors);
const world = createTestWorld();
world.tiles[3 * 10 + 4] = 4; // Wall
syncToECS(actors);
entityManager = new EntityManager(world);
const decision = decideEnemyAction(world, enemy, player, entityManager);
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
const decision = decideEnemyAction(world, enemy, player, accessor);
expect(decision.action.type).toBe("move");
});
@@ -154,13 +192,14 @@ describe('Combat Simulation', () => {
aiState: "pursuing",
lastKnownPlayerPos: { x: 4, y: 3 }
} as any;
actors.set(1, player);
actors.set(2, enemy);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld(actors);
entityManager = new EntityManager(world);
const world = createTestWorld();
syncToECS(actors);
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
const decision = decideEnemyAction(world, enemy, player, entityManager);
const decision = decideEnemyAction(world, enemy, player, accessor);
expect(decision.action).toEqual({ type: "attack", targetId: 1 });
});
@@ -176,13 +215,15 @@ describe('Combat Simulation', () => {
aiState: "wandering",
energy: 0
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
const decision = decideEnemyAction(world, enemy, player, new EntityManager(world));
const decision = decideEnemyAction(world, enemy, player, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(enemy.aiState).toBe("alerted");
const updatedEnemy = ecsWorld.getComponent(2 as EntityId, "ai");
expect(updatedEnemy?.state).toBe("alerted");
expect(decision.justAlerted).toBe(true);
});
@@ -199,14 +240,16 @@ describe('Combat Simulation', () => {
aiState: "pursuing", // Currently pursuing
lastKnownPlayerPos: { x: 5, y: 5 }
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
// Should switch to searching because can't see player
decideEnemyAction(world, enemy, player, new EntityManager(world));
decideEnemyAction(world, enemy, player, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(enemy.aiState).toBe("searching");
const updatedEnemy = ecsWorld.getComponent(2 as EntityId, "ai");
expect(updatedEnemy?.state).toBe("searching");
});
it("should transition from searching to alerted when sight regained", () => {
@@ -222,13 +265,15 @@ describe('Combat Simulation', () => {
aiState: "searching",
lastKnownPlayerPos: { x: 5, y: 5 }
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
const decision = decideEnemyAction(world, enemy, player, new EntityManager(world));
const decision = decideEnemyAction(world, enemy, player, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(enemy.aiState).toBe("alerted");
const updatedEnemy = ecsWorld.getComponent(2 as EntityId, "ai");
expect(updatedEnemy?.state).toBe("alerted");
expect(decision.justAlerted).toBe(true);
});
@@ -246,13 +291,15 @@ describe('Combat Simulation', () => {
aiState: "searching",
lastKnownPlayerPos: { x: 9, y: 9 }
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
decideEnemyAction(world, enemy, player, new EntityManager(world));
decideEnemyAction(world, enemy, player, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(enemy.aiState).toBe("wandering");
const updatedEnemy = ecsWorld.getComponent(2 as EntityId, "ai");
expect(updatedEnemy?.state).toBe("wandering");
});
});
@@ -263,12 +310,13 @@ describe('Combat Simulation', () => {
const player = { id: 1, category: "combatant", isPlayer: true, pos: { x: 0, y: 0 }, speed: 10, stats: createTestStats(), energy: 0 } as any;
const enemy = { id: 2, category: "combatant", isPlayer: false, pos: { x: 5, y: 5 }, speed: 100, stats: createTestStats(), energy: 0 } as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
const em = new EntityManager(world);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
const result = stepUntilPlayerTurn(world, 1, em);
const result = stepUntilPlayerTurn(world, 1 as EntityId, accessor);
// Enemy should have taken at least one action
expect(result.events.length).toBeGreaterThan(0);
@@ -290,15 +338,16 @@ describe('Combat Simulation', () => {
energy: 100
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
const em = new EntityManager(world);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
const result = stepUntilPlayerTurn(world, 1, em);
const result = stepUntilPlayerTurn(world, 1 as EntityId, accessor);
expect(world.actors.has(1)).toBe(false); // Player dead
expect(result.events.some(e => e.type === "killed" && e.targetId === 1)).toBe(true);
expect(accessor.hasActor(1 as EntityId)).toBe(false); // Player dead
expect(result.events.some((e: any) => e.type === "killed" && e.targetId === 1)).toBe(true);
});
});
@@ -319,17 +368,22 @@ describe('Combat Simulation', () => {
const player = { id: 1, category: "combatant", isPlayer: true, pos: { x: 3, y: 3 }, stats: createTestStats({ accuracy: 100 }), energy: 0 } as any;
const enemy = { id: 2, category: "combatant", isPlayer: false, pos: { x: 4, y: 3 }, stats: createTestStats({ evasion: 50, hp: 10 }), energy: 0 } as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
// Mock random to be 51 (scale 0-100 logic uses * 100) -> 0.51
mockRandom.mockReturnValue(0.1); // Hit roll
// Wait, hitChance is Acc (100) - Eva (50) = 50.
// Roll 0.51 * 100 = 51. 51 > 50 -> Dodge.
mockRandom.mockReturnValue(0.51);
const events = applyAction(world, 1, { type: "attack", targetId: 2 }, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(events.some(e => e.type === "dodged")).toBe(true);
expect(enemy.stats.hp).toBe(10); // No damage
const updatedEnemy = ecsWorld.getComponent(2 as EntityId, "stats");
expect(updatedEnemy?.hp).toBe(10); // No damage
});
it("should crit when roll < crit chance", () => {
@@ -343,9 +397,10 @@ describe('Combat Simulation', () => {
} as any;
const enemy = { id: 2, category: "combatant", isPlayer: false, pos: { x: 4, y: 3 }, stats: createTestStats({ evasion: 0, defense: 0, hp: 50 }), energy: 0 } as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
// Mock random:
// 1. Hit roll: 0.1 (Hit)
@@ -353,7 +408,7 @@ describe('Combat Simulation', () => {
// 3. Block roll: 0.9 (No block)
mockRandom.mockReturnValueOnce(0.1).mockReturnValueOnce(0.4).mockReturnValueOnce(0.9);
const events = applyAction(world, 1, { type: "attack", targetId: 2 }, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
// Damage = 10 * 2 = 20
const dmgEvent = events.find(e => e.type === "damaged") as any;
@@ -373,9 +428,10 @@ describe('Combat Simulation', () => {
stats: createTestStats({ evasion: 0, defense: 0, hp: 50, blockChance: 50 }), energy: 0
} as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
// Mock random:
// 1. Hit roll: 0.1
@@ -383,7 +439,7 @@ describe('Combat Simulation', () => {
// 3. Block roll: 0.4 (Block, since < 0.5)
mockRandom.mockReturnValueOnce(0.1).mockReturnValueOnce(0.9).mockReturnValueOnce(0.4);
const events = applyAction(world, 1, { type: "attack", targetId: 2 }, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
// Damage = 10 * 0.5 = 5
const dmgEvent = events.find(e => e.type === "damaged") as any;
@@ -399,17 +455,19 @@ describe('Combat Simulation', () => {
} as any;
const enemy = { id: 2, category: "combatant", isPlayer: false, pos: { x: 4, y: 3 }, stats: createTestStats({ hp: 20, defense: 0 }) } as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
// Standard hit
mockRandom.mockReturnValue(0.1);
const events = applyAction(world, 1, { type: "attack", targetId: 2 }, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
// Damage 10. Heal 50% = 5. HP -> 15.
expect(player.stats.hp).toBe(15);
const updatedPlayer = ecsWorld.getComponent(1 as EntityId, "stats");
expect(updatedPlayer?.hp).toBe(15);
expect(events.some(e => e.type === "healed")).toBe(true);
});
@@ -421,16 +479,18 @@ describe('Combat Simulation', () => {
} as any;
const enemy = { id: 2, category: "combatant", isPlayer: false, pos: { x: 4, y: 3 }, stats: createTestStats({ hp: 20, defense: 0 }) } as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
mockRandom.mockReturnValue(0.1);
applyAction(world, 1, { type: "attack", targetId: 2 }, new EntityManager(world));
applyAction(world, 1 as EntityId, { type: "attack", targetId: 2 as EntityId }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
// Damage 10. Heal 10. HP 19+10 = 29 > 20. Should be 20.
expect(player.stats.hp).toBe(20);
const updatedPlayer = ecsWorld.getComponent(1 as EntityId, "stats");
expect(updatedPlayer?.hp).toBe(20);
});
});
@@ -446,15 +506,17 @@ describe('Combat Simulation', () => {
id: 2, category: "collectible", type: "exp_orb", pos: { x: 4, y: 3 }, expAmount: 150
} as any;
actors.set(1, player);
actors.set(2, orb);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, orb);
const world = createTestWorld();
syncToECS(actors);
// Move player onto orb
const events = applyAction(world, 1, { type: "move", dx: 1, dy: 0 }, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, { type: "move", dx: 1, dy: 0 }, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(player.stats.level).toBe(2);
expect(player.stats.exp).toBe(50); // 150 - 100 = 50
const updatedPlayer = ecsWorld.getComponent(1 as EntityId, "stats");
expect(updatedPlayer?.level).toBe(2);
expect(updatedPlayer?.exp).toBe(50); // 150 - 100 = 50
expect(events.some(e => e.type === "leveled-up")).toBe(true);
});
});
@@ -480,12 +542,13 @@ describe('Combat Simulation', () => {
energy: 0
} as any;
actors.set(1, enemy);
actors.set(2, player);
const world = createTestWorld(actors);
actors.set(1 as EntityId, enemy);
actors.set(2 as EntityId, player);
const world = createTestWorld();
syncToECS(actors);
// Enemy should decide to attack
const decision = decideEnemyAction(world, enemy, player, new EntityManager(world));
const decision = decideEnemyAction(world, enemy, player, new EntityAccessor(world, 1 as EntityId, ecsWorld));
expect(decision.action.type).toBe("attack");
if (decision.action.type === "attack") {
@@ -498,12 +561,13 @@ describe('Combat Simulation', () => {
const player = { id: 1, category: "combatant", isPlayer: true, pos: { x: 4, y: 4 }, stats: createTestStats(), energy: 0 } as any;
const enemy = { id: 2, category: "combatant", isPlayer: false, pos: { x: 5, y: 5 }, stats: createTestStats(), energy: 0 } as any;
actors.set(1, player);
actors.set(2, enemy);
const world = createTestWorld(actors);
actors.set(1 as EntityId, player);
actors.set(2 as EntityId, enemy);
const world = createTestWorld();
syncToECS(actors);
const action: any = { type: "attack", targetId: 2 };
const events = applyAction(world, 1, action, new EntityManager(world));
const events = applyAction(world, 1 as EntityId, action, new EntityAccessor(world, 1 as EntityId, ecsWorld));
const attackEvent = events.find(e => e.type === "attacked");
expect(attackEvent).toBeDefined();
@@ -524,11 +588,12 @@ describe('Combat Simulation', () => {
} as any;
const player = { id: 2, category: "combatant", isPlayer: true, pos: { x: 4, y: 6 }, stats: createTestStats(), energy: 0 } as any;
actors.set(1, enemy);
actors.set(2, player);
const world = createTestWorld(actors);
actors.set(1 as EntityId, enemy);
actors.set(2 as EntityId, player);
const world = createTestWorld();
syncToECS(actors);
const decision = decideEnemyAction(world, enemy, player, new EntityManager(world));
const decision = decideEnemyAction(world, enemy, player, new EntityAccessor(world, 1 as EntityId, ecsWorld));
if (decision.action.type === "move") {
const { dx, dy } = decision.action;
// Should be (0, 1) or cardinal, sum of abs should be 1

View File

@@ -1,23 +1,24 @@
import { describe, it, expect } from 'vitest';
import { traceProjectile } from '../gameplay/CombatLogic';
import { EntityManager } from '../EntityManager';
import { type World, type Actor, type EntityId } from '../../core/types';
import { EntityAccessor } from '../EntityAccessor';
import { ECSWorld } from '../ecs/World';
import type { World, EntityId } from '../../core/types';
const createTestWorld = (actors: Map<EntityId, Actor>): World => {
const createTestWorld = (): World => {
return {
width: 10,
height: 10,
tiles: new Array(100).fill(0), // 0 = Floor
actors,
exit: { x: 9, y: 9 }
};
};
describe('Throwing Mechanics', () => {
it('should land ON the wall currently (demonstrating the bug)', () => {
const actors = new Map<EntityId, Actor>();
const world = createTestWorld(actors);
const entityManager = new EntityManager(world);
const world = createTestWorld();
const ecsWorld = new ECSWorld();
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
// Wall at (5, 0)
world.tiles[5] = 4; // Wall
@@ -25,16 +26,16 @@ describe('Throwing Mechanics', () => {
const start = { x: 0, y: 0 };
const target = { x: 5, y: 0 }; // Target the wall directly
const result = traceProjectile(world, start, target, entityManager);
const result = traceProjectile(world, start, target, accessor);
// NEW BEHAVIOR: blockedPos is the tile BEFORE the wall (4, 0)
expect(result.blockedPos).toEqual({ x: 4, y: 0 });
});
it('should land ON the wall when throwing PAST a wall (demonstrating the bug)', () => {
const actors = new Map<EntityId, Actor>();
const world = createTestWorld(actors);
const entityManager = new EntityManager(world);
const world = createTestWorld();
const ecsWorld = new ECSWorld();
const accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
// Wall at (3, 0)
world.tiles[3] = 4; // Wall
@@ -42,7 +43,7 @@ describe('Throwing Mechanics', () => {
const start = { x: 0, y: 0 };
const target = { x: 5, y: 0 }; // Target past the wall
const result = traceProjectile(world, start, target, entityManager);
const result = traceProjectile(world, start, target, accessor);
// NEW BEHAVIOR: Hits the wall at 3,0, stops at 2,0
expect(result.blockedPos).toEqual({ x: 2, y: 0 });

View File

@@ -9,7 +9,6 @@ describe('World Utilities', () => {
width,
height,
tiles,
actors: new Map(),
exit: { x: 0, y: 0 }
});
@@ -81,38 +80,37 @@ describe('World Utilities', () => {
const world = createTestWorld(10, 10, tiles);
expect(isBlocked(world, 5, 5)).toBe(true);
const mockAccessor = { getActorsAt: () => [] } as any;
expect(isBlocked(world, 5, 5, mockAccessor)).toBe(true);
});
it('should return true for actor positions', () => {
const world = createTestWorld(10, 10, new Array(100).fill(0));
world.actors.set(1, {
id: 1,
category: "combatant",
isPlayer: true,
type: "player",
pos: { x: 3, y: 3 },
speed: 100,
stats: { hp: 10, maxHp: 10, attack: 1, defense: 0 } as any,
energy: 100
});
const mockAccessor = {
getActorsAt: (x: number, y: number) => {
if (x === 3 && y === 3) return [{ category: "combatant" }];
return [];
}
} as any;
expect(isBlocked(world, 3, 3)).toBe(true);
expect(isBlocked(world, 3, 3, mockAccessor)).toBe(true);
});
it('should return false for empty floor tiles', () => {
const world = createTestWorld(10, 10, new Array(100).fill(0));
const mockAccessor = { getActorsAt: () => [] } as any;
expect(isBlocked(world, 3, 3)).toBe(false);
expect(isBlocked(world, 7, 7)).toBe(false);
expect(isBlocked(world, 3, 3, mockAccessor)).toBe(false);
expect(isBlocked(world, 7, 7, mockAccessor)).toBe(false);
});
it('should return true for out of bounds', () => {
const world = createTestWorld(10, 10, new Array(100).fill(0));
const mockAccessor = { getActorsAt: () => [] } as any;
expect(isBlocked(world, -1, 0)).toBe(true);
expect(isBlocked(world, 10, 10)).toBe(true);
expect(isBlocked(world, -1, 0, mockAccessor)).toBe(true);
expect(isBlocked(world, 10, 10, mockAccessor)).toBe(true);
});
});
describe('tryDestructTile', () => {
@@ -148,32 +146,34 @@ describe('World Utilities', () => {
it('should return true when player is on exit', () => {
const world = createTestWorld(10, 10, new Array(100).fill(TileType.EMPTY));
world.exit = { x: 5, y: 5 };
world.actors.set(1, {
id: 1,
pos: { x: 5, y: 5 },
isPlayer: true
} as any);
const mockAccessor = {
getPlayer: () => ({ pos: { x: 5, y: 5 } })
} as any;
expect(isPlayerOnExit(world, 1)).toBe(true);
expect(isPlayerOnExit(world, mockAccessor)).toBe(true);
});
it('should return false when player is not on exit', () => {
const world = createTestWorld(10, 10, new Array(100).fill(TileType.EMPTY));
world.exit = { x: 5, y: 5 };
world.actors.set(1, {
id: 1,
pos: { x: 4, y: 4 },
isPlayer: true
} as any);
const mockAccessor = {
getPlayer: () => ({ pos: { x: 4, y: 4 } })
} as any;
expect(isPlayerOnExit(world, 1)).toBe(false);
expect(isPlayerOnExit(world, mockAccessor)).toBe(false);
});
it('should return false when player does not exist', () => {
const world = createTestWorld(10, 10, new Array(100).fill(TileType.EMPTY));
world.exit = { x: 5, y: 5 };
expect(isPlayerOnExit(world, 999)).toBe(false);
const mockAccessor = {
getPlayer: () => null
} as any;
expect(isPlayerOnExit(world, mockAccessor)).toBe(false);
});
});
});

View File

@@ -1,6 +1,6 @@
import { type ECSWorld } from "./World";
import { type World as GameWorld, type EntityId, type Vec2, type Action } from "../../core/types";
import { type EntityManager } from "../EntityManager";
import { type EntityAccessor } from "../EntityAccessor";
import { findPathAStar } from "../world/pathfinding";
import { isBlocked, inBounds } from "../world/world-logic";
import { blocksSight } from "../../core/terrain";
@@ -9,12 +9,12 @@ import { FOV } from "rot-js";
export class AISystem {
private ecsWorld: ECSWorld;
private gameWorld: GameWorld;
private em?: EntityManager;
private accessor: EntityAccessor;
constructor(ecsWorld: ECSWorld, gameWorld: GameWorld, em?: EntityManager) {
constructor(ecsWorld: ECSWorld, gameWorld: GameWorld, accessor: EntityAccessor) {
this.ecsWorld = ecsWorld;
this.gameWorld = gameWorld;
this.em = em;
this.accessor = accessor;
}
update(enemyId: EntityId, playerId: EntityId): { action: Action; justAlerted: boolean } {
@@ -82,7 +82,11 @@ export class AISystem {
// A* Pathfinding
const dummySeen = new Uint8Array(this.gameWorld.width * this.gameWorld.height).fill(1);
const path = findPathAStar(this.gameWorld, dummySeen, pos, targetPos, { ignoreBlockedTarget: true, ignoreSeen: true, em: this.em });
const path = findPathAStar(this.gameWorld, dummySeen, pos, targetPos, {
ignoreBlockedTarget: true,
ignoreSeen: true,
accessor: this.accessor
});
if (path.length >= 2) {
const next = path[1];
@@ -111,7 +115,7 @@ export class AISystem {
const directions = [{ dx: 0, dy: -1 }, { dx: 0, dy: 1 }, { dx: -1, dy: 0 }, { dx: 1, dy: 0 }];
// Simple shuffle and try
for (const dir of directions.sort(() => Math.random() - 0.5)) {
if (!isBlocked(this.gameWorld, pos.x + dir.dx, pos.y + dir.dy, this.em)) {
if (!isBlocked(this.gameWorld, pos.x + dir.dx, pos.y + dir.dy, this.accessor)) {
return { type: "move", ...dir };
}
}

View File

@@ -1,6 +1,6 @@
import { type ECSWorld } from "./World";
import { type ComponentMap } from "./components";
import { type EntityId, type Stats, type EnemyAIState, type ActorType } from "../../core/types";
import { type EntityId, type Stats, type EnemyAIState, type ActorType, type Inventory, type Equipment, type Item } from "../../core/types";
import { GAME_CONFIG } from "../../core/config/GameConfig";
/**
@@ -89,6 +89,22 @@ export class EntityBuilder {
return this;
}
/**
* Add inventory component.
*/
withInventory(inventory: Inventory): this {
this.components.inventory = inventory;
return this;
}
/**
* Add equipment component.
*/
withEquipment(equipment: Equipment): this {
this.components.equipment = equipment;
return this;
}
/**
* Add AI component for enemy behavior.
*/
@@ -192,8 +208,8 @@ export class EntityBuilder {
/**
* Configure as an item on the ground.
*/
asGroundItem(itemId: string, quantity: number = 1): this {
this.components.groundItem = { itemId, quantity };
asGroundItem(item: Item): this {
this.components.groundItem = { item };
return this;
}

View File

@@ -1,17 +1,17 @@
import { type ECSWorld } from "./World";
import { type World as GameWorld, type EntityId } from "../../core/types";
import { isBlocked } from "../world/world-logic";
import { type EntityManager } from "../EntityManager";
import { type EntityAccessor } from "../EntityAccessor";
export class MovementSystem {
private ecsWorld: ECSWorld;
private gameWorld: GameWorld;
private em?: EntityManager;
private accessor: EntityAccessor;
constructor(ecsWorld: ECSWorld, gameWorld: GameWorld, em?: EntityManager) {
constructor(ecsWorld: ECSWorld, gameWorld: GameWorld, accessor: EntityAccessor) {
this.ecsWorld = ecsWorld;
this.gameWorld = gameWorld;
this.em = em;
this.accessor = accessor;
}
move(entityId: EntityId, dx: number, dy: number): boolean {
@@ -21,18 +21,11 @@ export class MovementSystem {
const nx = pos.x + dx;
const ny = pos.y + dy;
if (!isBlocked(this.gameWorld, nx, ny, this.em)) {
const oldPos = { ...pos };
if (!isBlocked(this.gameWorld, nx, ny, this.accessor)) {
// Update ECS Position
pos.x = nx;
pos.y = ny;
// Update grid-based EntityManager if present
if (this.em) {
this.em.moveActor(entityId, oldPos, { x: nx, y: ny });
}
return true;
}

View File

@@ -1,6 +1,6 @@
import { type ECSWorld } from "./World";
import { EntityBuilder } from "./EntityBuilder";
import { type EntityId } from "../../core/types";
import { type EntityId, type Item } from "../../core/types";
import { GAME_CONFIG } from "../../core/config/GameConfig";
/**
@@ -176,12 +176,12 @@ export const Prefabs = {
/**
* Create an item drop on the ground.
*/
itemDrop(world: ECSWorld, x: number, y: number, itemId: string, quantity: number = 1, spriteIndex: number = 0): EntityId {
itemDrop(world: ECSWorld, x: number, y: number, item: Item, spriteIndex: number = 0): EntityId {
return EntityBuilder.create(world)
.withPosition(x, y)
.withName("Item")
.withName(item.name)
.withSprite("items", spriteIndex)
.asGroundItem(itemId, quantity)
.asGroundItem(item)
.build();
},

View File

@@ -12,6 +12,10 @@ export class ECSWorld {
return id;
}
hasEntity(id: EntityId): boolean {
return this.entities.has(id);
}
destroyEntity(id: EntityId) {
this.entities.delete(id);
for (const type in this.components) {
@@ -20,6 +24,7 @@ export class ECSWorld {
}
addComponent<K extends ComponentType>(id: EntityId, type: K, data: ComponentMap[K]) {
this.entities.add(id); // Ensure entity is registered
if (!this.components[type]) {
this.components[type] = new Map();
}
@@ -71,4 +76,8 @@ export class ECSWorld {
setNextId(id: number) {
this.nextId = id;
}
get currentNextId(): number {
return this.nextId;
}
}

View File

@@ -0,0 +1,38 @@
import { describe, it, expect } from 'vitest';
import { ECSWorld } from '../World';
import { EntityAccessor } from '../../EntityAccessor';
import { EntityBuilder } from '../EntityBuilder';
import type { World as GameWorld, EntityId } from '../../../core/types';
describe('ECS Removal and Accessor', () => {
it('should not report destroyed entities in getAllActors', () => {
const ecsWorld = new ECSWorld();
const gameWorld: GameWorld = {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
exit: { x: 0, y: 0 }
};
const accessor = new EntityAccessor(gameWorld, 999 as EntityId, ecsWorld);
// Create Entity
const id = EntityBuilder.create(ecsWorld)
.asEnemy("rat")
.withPosition(5, 5)
.withStats({ hp: 10, maxHp: 10 } as any)
.build();
// Verify it exists
let actors = [...accessor.getAllActors()];
expect(actors.length).toBe(1);
expect(actors[0].id).toBe(id);
// Destroy it
ecsWorld.destroyEntity(id);
// Verify it is gone
actors = [...accessor.getAllActors()];
expect(actors.length).toBe(0);
});
});

View File

@@ -1,4 +1,4 @@
import { type Vec2, type Stats, type ActorType, type EnemyAIState, type EntityId } from "../../core/types";
import { type Vec2, type Stats, type ActorType, type EnemyAIState, type EntityId, type Inventory, type Equipment, type Item } from "../../core/types";
export interface PositionComponent extends Vec2 {}
@@ -98,10 +98,13 @@ export interface DestructibleComponent {
* For items laying on the ground that can be picked up.
*/
export interface GroundItemComponent {
itemId: string; // Reference to item definition
quantity: number; // Stack size
item: Item;
}
export interface InventoryComponent extends Inventory {}
export interface EquipmentComponent extends Equipment {}
export type ComponentMap = {
// Core components
position: PositionComponent;
@@ -120,6 +123,8 @@ export type ComponentMap = {
combat: CombatComponent;
destructible: DestructibleComponent;
groundItem: GroundItemComponent;
inventory: InventoryComponent;
equipment: EquipmentComponent;
};
export type ComponentType = keyof ComponentMap;

View File

@@ -1,7 +1,7 @@
import { type World, type Vec2, type EntityId } from "../../core/types";
import { isBlocked } from "../world/world-logic";
import { raycast } from "../../core/math";
import { EntityManager } from "../EntityManager";
import { type EntityAccessor } from "../EntityAccessor";
export interface ProjectileResult {
path: Vec2[];
@@ -16,7 +16,7 @@ export function traceProjectile(
world: World,
start: Vec2,
target: Vec2,
entityManager: EntityManager,
accessor: EntityAccessor | undefined,
shooterId?: EntityId
): ProjectileResult {
const points = raycast(start.x, start.y, target.x, target.y);
@@ -28,9 +28,13 @@ export function traceProjectile(
const p = points[i];
// Check for blocking
if (isBlocked(world, p.x, p.y, entityManager)) {
if (accessor && isBlocked(world, p.x, p.y, accessor)) {
// Check if we hit a combatant
const actors = entityManager.getActorsAt(p.x, p.y);
let actors: any[] = [];
if (accessor) {
actors = accessor.getActorsAt(p.x, p.y);
}
const enemy = actors.find(a => a.category === "combatant" && a.id !== shooterId);
if (enemy) {
@@ -56,10 +60,10 @@ export function traceProjectile(
* Finds the closest visible enemy to a given position.
*/
export function getClosestVisibleEnemy(
world: World,
origin: Vec2,
seenTiles: Set<string> | boolean[] | Uint8Array, // Support various visibility structures
width?: number // Required if seenTiles is a flat array
width?: number, // Required if seenTiles is a flat array
accessor?: EntityAccessor
): Vec2 | null {
let closestDistSq = Infinity;
let closestPos: Vec2 | null = null;
@@ -76,7 +80,9 @@ export function getClosestVisibleEnemy(
}
};
for (const actor of world.actors.values()) {
const enemies = accessor ? accessor.getEnemies() : [];
for (const actor of enemies) {
if (actor.category !== "combatant" || actor.isPlayer) continue;
// Check visibility

View File

@@ -1,54 +1,54 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { traceProjectile } from '../CombatLogic';
import type { World } from '../../../core/types';
import { EntityManager } from '../../EntityManager';
import type { World, EntityId } from '../../../core/types';
import { EntityAccessor } from '../../EntityAccessor';
import { TileType } from '../../../core/terrain';
import { ECSWorld } from '../../ecs/World';
describe('CombatLogic', () => {
// Mock World
const mockWorld: World = {
width: 10,
height: 10,
tiles: new Array(100).fill(TileType.EMPTY),
actors: new Map(),
exit: { x: 9, y: 9 }
};
let mockWorld: World;
let ecsWorld: ECSWorld;
let accessor: EntityAccessor;
// Helper to set wall
const setWall = (x: number, y: number) => {
mockWorld.tiles[y * mockWorld.width + x] = TileType.WALL;
};
// Helper to clear world
const clearWorld = () => {
mockWorld.tiles.fill(TileType.EMPTY);
mockWorld.actors.clear();
};
// Mock EntityManager
const mockEntityManager = {
getActorsAt: (x: number, y: number) => {
return [...mockWorld.actors.values()].filter(a => a.pos.x === x && a.pos.y === y);
}
} as unknown as EntityManager;
beforeEach(() => {
clearWorld();
mockWorld = {
width: 10,
height: 10,
tiles: new Array(100).fill(TileType.EMPTY),
exit: { x: 9, y: 9 }
};
ecsWorld = new ECSWorld();
// Shooter ID 1
accessor = new EntityAccessor(mockWorld, 1 as EntityId, ecsWorld);
});
function syncActor(actor: any) {
ecsWorld.addComponent(actor.id as EntityId, "position", actor.pos);
if (actor.category === 'combatant') {
ecsWorld.addComponent(actor.id as EntityId, "actorType", { type: actor.type });
ecsWorld.addComponent(actor.id as EntityId, "stats", { hp: 10 } as any);
if (actor.isPlayer) ecsWorld.addComponent(actor.id as EntityId, "player", {});
} else if (actor.category === 'item_drop') {
ecsWorld.addComponent(actor.id as EntityId, "groundItem", { item: actor.item || {} });
}
}
describe('traceProjectile', () => {
it('should travel full path if no obstacles', () => {
const start = { x: 0, y: 0 };
const end = { x: 5, y: 0 };
const result = traceProjectile(mockWorld, start, end, mockEntityManager);
const result = traceProjectile(mockWorld, start, end, accessor);
expect(result.blockedPos).toEqual(end);
expect(result.hitActorId).toBeUndefined();
// Path should be (0,0) -> (1,0) -> (2,0) -> (3,0) -> (4,0) -> (5,0)
// But raycast implementation includes start?
// CombatLogic logic: "skip start" -> loop i=1
// So result.path is full array from raycast.
expect(result.path).toHaveLength(6);
});
@@ -57,7 +57,7 @@ describe('CombatLogic', () => {
const end = { x: 5, y: 0 };
setWall(3, 0); // Wall at (3,0)
const result = traceProjectile(mockWorld, start, end, mockEntityManager);
const result = traceProjectile(mockWorld, start, end, accessor);
expect(result.blockedPos).toEqual({ x: 2, y: 0 });
expect(result.hitActorId).toBeUndefined();
@@ -68,17 +68,17 @@ describe('CombatLogic', () => {
const end = { x: 5, y: 0 };
// Place enemy at (3,0)
const enemyId = 2;
mockWorld.actors.set(enemyId, {
const enemyId = 2 as EntityId;
const enemy = {
id: enemyId,
type: 'rat',
category: 'combatant',
pos: { x: 3, y: 0 },
isPlayer: false
// ... other props mocked if needed
} as any);
};
syncActor(enemy);
const result = traceProjectile(mockWorld, start, end, mockEntityManager, 1); // Shooter 1
const result = traceProjectile(mockWorld, start, end, accessor, 1 as EntityId); // Shooter 1
expect(result.blockedPos).toEqual({ x: 3, y: 0 });
expect(result.hitActorId).toBe(enemyId);
@@ -89,15 +89,16 @@ describe('CombatLogic', () => {
const end = { x: 5, y: 0 };
// Shooter at start
mockWorld.actors.set(1, {
id: 1,
const shooter = {
id: 1 as EntityId,
type: 'player',
category: 'combatant',
pos: { x: 0, y: 0 },
isPlayer: true
} as any);
};
syncActor(shooter);
const result = traceProjectile(mockWorld, start, end, mockEntityManager, 1);
const result = traceProjectile(mockWorld, start, end, accessor, 1 as EntityId);
// Should not hit self
expect(result.hitActorId).toBeUndefined();
@@ -109,13 +110,15 @@ describe('CombatLogic', () => {
const end = { x: 5, y: 0 };
// Item at (3,0)
mockWorld.actors.set(99, {
id: 99,
const item = {
id: 99 as EntityId,
category: 'item_drop',
pos: { x: 3, y: 0 },
} as any);
item: { name: 'Test Item' }
};
syncActor(item);
const result = traceProjectile(mockWorld, start, end, mockEntityManager);
const result = traceProjectile(mockWorld, start, end, accessor);
// Should pass through item
expect(result.blockedPos).toEqual(end);

View File

@@ -1,35 +1,36 @@
import { describe, it, expect, beforeEach } from "vitest";
import { ItemManager } from "../../../scenes/systems/ItemManager";
import { EntityManager } from "../../EntityManager";
import type { World, CombatantActor, RangedWeaponItem } from "../../../core/types";
import type { World, CombatantActor, RangedWeaponItem, EntityId } from "../../../core/types";
import { EntityAccessor } from "../../EntityAccessor";
import { ECSWorld } from "../../ecs/World";
import { createRangedWeapon, createAmmo } from "../../../core/config/Items";
// Mock World and EntityManager
const mockWorld: World = {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
actors: new Map(),
exit: { x: 9, y: 9 }
};
describe("Fireable Weapons & Ammo System", () => {
let entityManager: EntityManager;
let accessor: EntityAccessor;
let itemManager: ItemManager;
let player: CombatantActor;
let ecsWorld: ECSWorld;
let world: World;
beforeEach(() => {
entityManager = new EntityManager(mockWorld);
itemManager = new ItemManager(mockWorld, entityManager);
world = {
width: 10,
height: 10,
tiles: new Array(100).fill(0),
exit: { x: 9, y: 9 }
};
ecsWorld = new ECSWorld();
accessor = new EntityAccessor(world, 1 as EntityId, ecsWorld);
itemManager = new ItemManager(world, accessor, ecsWorld);
player = {
id: 1,
id: 1 as EntityId,
pos: { x: 0, y: 0 },
category: "combatant",
type: "player",
isPlayer: true,
speed: 1,
speed: 100,
energy: 0,
stats: {
maxHp: 100, hp: 100,
@@ -43,55 +44,68 @@ describe("Fireable Weapons & Ammo System", () => {
},
inventory: { gold: 0, items: [] },
equipment: {}
};
mockWorld.actors.clear();
mockWorld.actors.set(player.id, player);
} as any;
// Sync player to ECS
ecsWorld.addComponent(player.id, "position", player.pos);
ecsWorld.addComponent(player.id, "player", {});
ecsWorld.addComponent(player.id, "stats", player.stats);
ecsWorld.addComponent(player.id, "actorType", { type: "player" });
ecsWorld.addComponent(player.id, "inventory", player.inventory!);
ecsWorld.addComponent(player.id, "energy", { current: 0, speed: 100 });
// Avoid ID collisions between manually added player (ID 1) and spawned entities
ecsWorld.setNextId(10);
});
it("should stack ammo correctly", () => {
const playerActor = accessor.getPlayer()!;
// Spawn Ammo pack 1
const ammo1 = createAmmo("ammo_9mm", 10);
itemManager.spawnItem(ammo1, { x: 0, y: 0 });
// Pickup
itemManager.tryPickup(player);
expect(player.inventory!.items.length).toBe(1);
expect(player.inventory!.items[0].quantity).toBe(10);
itemManager.tryPickup(playerActor);
expect(playerActor.inventory!.items.length).toBe(1);
expect(playerActor.inventory!.items[0].quantity).toBe(10);
// Spawn Ammo pack 2
const ammo2 = createAmmo("ammo_9mm", 5);
itemManager.spawnItem(ammo2, { x: 0, y: 0 });
// Pickup (should merge)
itemManager.tryPickup(player);
expect(player.inventory!.items.length).toBe(1); // Still 1 stack
expect(player.inventory!.items[0].quantity).toBe(15);
itemManager.tryPickup(playerActor);
expect(playerActor.inventory!.items.length).toBe(1); // Still 1 stack
expect(playerActor.inventory!.items[0].quantity).toBe(15);
});
it("should consume ammo from weapon when fired", () => {
const playerActor = accessor.getPlayer()!;
// Create pistol using factory (already has currentAmmo initialized)
const pistol = createRangedWeapon("pistol");
player.inventory!.items.push(pistol);
playerActor.inventory!.items.push(pistol);
// Sanity Check - currentAmmo is now top-level
expect(pistol.currentAmmo).toBe(6);
expect(pistol.stats.magazineSize).toBe(6);
// Simulate Firing (logic mimic from GameScene)
if (pistol.currentAmmo > 0) {
pistol.currentAmmo--;
if (pistol.currentAmmo! > 0) {
pistol.currentAmmo!--;
}
expect(pistol.currentAmmo).toBe(5);
});
it("should reload weapon using inventory ammo", () => {
const playerActor = accessor.getPlayer()!;
const pistol = createRangedWeapon("pistol");
pistol.currentAmmo = 0; // Empty
player.inventory!.items.push(pistol);
playerActor.inventory!.items.push(pistol);
const ammo = createAmmo("ammo_9mm", 10);
player.inventory!.items.push(ammo);
playerActor.inventory!.items.push(ammo);
// Logic mimic from GameScene
const needed = pistol.stats.magazineSize - pistol.currentAmmo; // 6
@@ -105,12 +119,13 @@ describe("Fireable Weapons & Ammo System", () => {
});
it("should handle partial reload if not enough ammo", () => {
const playerActor = accessor.getPlayer()!;
const pistol = createRangedWeapon("pistol");
pistol.currentAmmo = 0;
player.inventory!.items.push(pistol);
playerActor.inventory!.items.push(pistol);
const ammo = createAmmo("ammo_9mm", 3); // Only 3 bullets
player.inventory!.items.push(ammo);
playerActor.inventory!.items.push(ammo);
// Logic mimic
const needed = pistol.stats.magazineSize - pistol.currentAmmo; // 6
@@ -124,16 +139,17 @@ describe("Fireable Weapons & Ammo System", () => {
});
it("should deep clone on spawn so pistols remain independent", () => {
const playerActor = accessor.getPlayer()!;
const pistol1 = createRangedWeapon("pistol");
// Spawn 1
itemManager.spawnItem(pistol1, {x:0, y:0});
const picked1 = itemManager.tryPickup(player)! as RangedWeaponItem;
const picked1 = itemManager.tryPickup(playerActor)! as RangedWeaponItem;
// Spawn 2
const pistol2 = createRangedWeapon("pistol");
itemManager.spawnItem(pistol2, {x:0, y:0});
const picked2 = itemManager.tryPickup(player)! as RangedWeaponItem;
const picked2 = itemManager.tryPickup(playerActor)! as RangedWeaponItem;
expect(picked1).not.toBe(picked2);
expect(picked1.stats).not.toBe(picked2.stats); // Critical!

View File

@@ -1,12 +1,17 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { applyAction } from '../simulation';
import type { World, CombatantActor, Action } from '../../../core/types';
import type { World, CombatantActor, Action, EntityId } from '../../../core/types';
import { TileType } from '../../../core/terrain';
import { GAME_CONFIG } from '../../../core/config/GameConfig';
import { EntityAccessor } from '../../EntityAccessor';
import { ECSWorld } from '../../ecs/World';
describe('Movement Blocking Behavior', () => {
let world: World;
let player: CombatantActor;
let accessor: EntityAccessor;
let ecsWorld: ECSWorld;
beforeEach(() => {
// minimalist world setup
@@ -14,7 +19,6 @@ describe('Movement Blocking Behavior', () => {
width: 3,
height: 3,
tiles: new Array(9).fill(TileType.GRASS),
actors: new Map(),
exit: { x: 2, y: 2 }
};
@@ -22,7 +26,7 @@ describe('Movement Blocking Behavior', () => {
world.tiles[1] = TileType.WALL;
player = {
id: 1,
id: 1 as EntityId,
type: 'player',
category: 'combatant',
isPlayer: true,
@@ -32,12 +36,19 @@ describe('Movement Blocking Behavior', () => {
stats: { ...GAME_CONFIG.player.initialStats }
};
world.actors.set(player.id, player);
ecsWorld = new ECSWorld();
ecsWorld.addComponent(player.id, "position", player.pos);
ecsWorld.addComponent(player.id, "stats", player.stats);
ecsWorld.addComponent(player.id, "actorType", { type: player.type });
ecsWorld.addComponent(player.id, "player", {});
ecsWorld.addComponent(player.id, "energy", { current: player.energy, speed: player.speed });
accessor = new EntityAccessor(world, player.id, ecsWorld);
});
it('should return move-blocked event when moving into a wall', () => {
const action: Action = { type: 'move', dx: 1, dy: 0 }; // Try to move right into wall at (1,0)
const events = applyAction(world, player.id, action);
const events = applyAction(world, player.id, action, accessor);
expect(events).toHaveLength(1);
expect(events[0]).toMatchObject({
@@ -50,7 +61,7 @@ describe('Movement Blocking Behavior', () => {
it('should return moved event when moving into empty space', () => {
const action: Action = { type: 'move', dx: 0, dy: 1 }; // Move down to (0,1) - valid
const events = applyAction(world, player.id, action);
const events = applyAction(world, player.id, action, accessor);
expect(events).toHaveLength(1);
expect(events[0]).toMatchObject({

View File

@@ -3,24 +3,24 @@ import type { World, EntityId, Action, SimEvent, Actor, CombatantActor, Collecti
import { isBlocked, tryDestructTile } from "../world/world-logic";
import { isDestructibleByWalk } from "../../core/terrain";
import { GAME_CONFIG } from "../../core/config/GameConfig";
import { type EntityManager } from "../EntityManager";
import { type EntityAccessor } from "../EntityAccessor";
import { AISystem } from "../ecs/AISystem";
import { Prefabs } from "../ecs/Prefabs";
export function applyAction(w: World, actorId: EntityId, action: Action, em?: EntityManager): SimEvent[] {
const actor = w.actors.get(actorId);
export function applyAction(w: World, actorId: EntityId, action: Action, accessor: EntityAccessor): SimEvent[] {
const actor = accessor.getActor(actorId);
if (!actor) return [];
const events: SimEvent[] = [];
switch (action.type) {
case "move":
events.push(...handleMove(w, actor, action, em));
events.push(...handleMove(w, actor, action, accessor));
break;
case "attack":
events.push(...handleAttack(w, actor, action, em));
events.push(...handleAttack(w, actor, action, accessor));
break;
case "throw":
// Throwing consumes a turn but visuals are handled by the renderer/scene directly
// so we do NOT emit a "waited" event.
break;
case "wait":
default:
@@ -28,19 +28,16 @@ export function applyAction(w: World, actorId: EntityId, action: Action, em?: En
break;
}
// Note: Energy is now managed by ROT.Scheduler, no need to deduct manually
return events;
}
function handleExpCollection(w: World, player: Actor, events: SimEvent[], em?: EntityManager) {
function handleExpCollection(player: Actor, events: SimEvent[], accessor: EntityAccessor) {
if (player.category !== "combatant") return;
const orbs = [...w.actors.values()].filter(a =>
const actorsAtPos = accessor.getActorsAt(player.pos.x, player.pos.y);
const orbs = actorsAtPos.filter(a =>
a.category === "collectible" &&
a.type === "exp_orb" &&
a.pos.x === player.pos.x &&
a.pos.y === player.pos.y
a.type === "exp_orb"
) as CollectibleActor[];
for (const orb of orbs) {
@@ -55,8 +52,7 @@ function handleExpCollection(w: World, player: Actor, events: SimEvent[], em?: E
});
checkLevelUp(player, events);
if (em) em.removeActor(orb.id);
else w.actors.delete(orb.id);
accessor.removeActor(orb.id);
}
}
@@ -91,47 +87,26 @@ function checkLevelUp(player: CombatantActor, events: SimEvent[]) {
}
function handleMove(w: World, actor: Actor, action: { dx: number; dy: number }, em?: EntityManager): SimEvent[] {
function handleMove(w: World, actor: Actor, action: { dx: number; dy: number }, accessor: EntityAccessor): SimEvent[] {
const from = { ...actor.pos };
const nx = actor.pos.x + action.dx;
const ny = actor.pos.y + action.dy;
if (em) {
const moved = em.movement.move(actor.id, action.dx, action.dy);
if (moved) {
const to = { ...actor.pos };
const events: SimEvent[] = [{ type: "moved", actorId: actor.id, from, to }];
const tileIdx = ny * w.width + nx;
const tile = w.tiles[tileIdx];
if (isDestructibleByWalk(tile)) {
tryDestructTile(w, nx, ny);
}
if (actor.category === "combatant" && actor.isPlayer) {
handleExpCollection(w, actor, events, em);
}
return events;
if (!isBlocked(w, nx, ny, accessor)) {
actor.pos.x = nx;
actor.pos.y = ny;
const to = { ...actor.pos };
const events: SimEvent[] = [{ type: "moved", actorId: actor.id, from, to }];
const tileIdx = ny * w.width + nx;
if (isDestructibleByWalk(w.tiles[tileIdx])) {
tryDestructTile(w, nx, ny);
}
} else {
// Fallback for cases without EntityManager (e.g. tests)
if (!isBlocked(w, nx, ny)) {
actor.pos.x = nx;
actor.pos.y = ny;
const to = { ...actor.pos };
const events: SimEvent[] = [{ type: "moved", actorId: actor.id, from, to }];
const tileIdx = ny * w.width + nx;
if (isDestructibleByWalk(w.tiles[tileIdx])) {
tryDestructTile(w, nx, ny);
}
if (actor.category === "combatant" && actor.isPlayer) {
handleExpCollection(w, actor, events);
}
return events;
if (actor.category === "combatant" && actor.isPlayer) {
handleExpCollection(actor, events, accessor);
}
return events;
}
return [{ type: "move-blocked", actorId: actor.id, x: nx, y: ny }];
@@ -139,8 +114,8 @@ function handleMove(w: World, actor: Actor, action: { dx: number; dy: number },
function handleAttack(w: World, actor: Actor, action: { targetId: EntityId }, em?: EntityManager): SimEvent[] {
const target = w.actors.get(action.targetId);
function handleAttack(_w: World, actor: Actor, action: { targetId: EntityId }, accessor: EntityAccessor): SimEvent[] {
const target = accessor.getActor(action.targetId);
if (target && target.category === "combatant" && actor.category === "combatant") {
const events: SimEvent[] = [{ type: "attacked", attackerId: actor.id, targetId: action.targetId }];
@@ -149,7 +124,6 @@ function handleAttack(w: World, actor: Actor, action: { targetId: EntityId }, em
const hitRoll = Math.random() * 100;
if (hitRoll > hitChance) {
// Miss!
events.push({
type: "dodged",
targetId: action.targetId,
@@ -173,17 +147,15 @@ function handleAttack(w: World, actor: Actor, action: { targetId: EntityId }, em
const blockRoll = Math.random() * 100;
let isBlock = false;
if (blockRoll < target.stats.blockChance) {
dmg = Math.floor(dmg * 0.5); // Block reduces damage by 50%
dmg = Math.floor(dmg * 0.5);
isBlock = true;
}
target.stats.hp -= dmg;
// Aggression on damage: if target is enemy and attacker is player (or vice versa), alert them
if (target.stats.hp > 0 && target.category === "combatant" && !target.isPlayer) {
// Switch to pursuing immediately
target.aiState = "pursuing";
target.alertedAt = Date.now(); // Reset alert timer if any
target.alertedAt = Date.now();
if (actor.pos) {
target.lastKnownPlayerPos = { ...actor.pos };
}
@@ -224,28 +196,18 @@ function handleAttack(w: World, actor: Actor, action: { targetId: EntityId }, em
y: target.pos.y,
victimType: target.type as ActorType
});
if (em) em.removeActor(target.id);
else w.actors.delete(target.id);
accessor.removeActor(target.id);
// Spawn EXP Orb
const enemyDef = (GAME_CONFIG.enemies as any)[target.type || ""];
const orbId = em ? em.getNextId() : Math.max(0, ...w.actors.keys(), target.id) + 1;
const expAmount = enemyDef?.expValue || 0;
const orb: CollectibleActor = {
id: orbId,
category: "collectible",
type: "exp_orb",
pos: { ...target.pos },
expAmount: enemyDef?.expValue || 0
};
if (em) em.addActor(orb);
else w.actors.set(orbId, orb);
events.push({ type: "orb-spawned", orbId, x: target.pos.x, y: target.pos.y });
const ecsWorld = accessor.context;
if (ecsWorld) {
const orbId = Prefabs.expOrb(ecsWorld, target.pos.x, target.pos.y, expAmount);
events.push({ type: "orb-spawned", orbId, x: target.pos.x, y: target.pos.y });
}
}
return events;
}
@@ -260,12 +222,13 @@ function handleAttack(w: World, actor: Actor, action: { targetId: EntityId }, em
* - Alerted: Brief period after spotting player (shows "!")
* - Pursuing: Chase player while in FOV or toward last known position
*/
export function decideEnemyAction(_w: World, enemy: CombatantActor, player: CombatantActor, em?: EntityManager): { action: Action; justAlerted: boolean } {
if (em) {
const result = em.ai.update(enemy.id, player.id);
export function decideEnemyAction(w: World, enemy: CombatantActor, player: CombatantActor, accessor: EntityAccessor): { action: Action; justAlerted: boolean } {
const ecsWorld = accessor.context;
if (ecsWorld) {
const aiSystem = new AISystem(ecsWorld, w, accessor);
const result = aiSystem.update(enemy.id, player.id);
// Sync ECS component state back to Actor object for compatibility with tests and old logic
const aiComp = em.ecsWorld.getComponent(enemy.id, "ai");
const aiComp = ecsWorld.getComponent(enemy.id, "ai");
if (aiComp) {
enemy.aiState = aiComp.state;
enemy.alertedAt = aiComp.alertedAt;
@@ -275,8 +238,6 @@ export function decideEnemyAction(_w: World, enemy: CombatantActor, player: Comb
return result;
}
// Fallback for tests or cases without EntityManager
// [Existing decideEnemyAction logic could be kept here as fallback, or just return wait]
return { action: { type: "wait" }, justAlerted: false };
}
@@ -284,81 +245,42 @@ export function decideEnemyAction(_w: World, enemy: CombatantActor, player: Comb
* Speed-based scheduler using rot-js: runs until it's the player's turn and the game needs input.
* Returns enemy events accumulated along the way.
*/
export function stepUntilPlayerTurn(w: World, playerId: EntityId, em?: EntityManager): { awaitingPlayerId: EntityId; events: SimEvent[] } {
// Energy Threshold
export function stepUntilPlayerTurn(w: World, playerId: EntityId, accessor: EntityAccessor): { awaitingPlayerId: EntityId; events: SimEvent[] } {
const THRESHOLD = 100;
// Ensure player exists
const player = w.actors.get(playerId) as CombatantActor;
if (!player || player.category !== "combatant") throw new Error("Player missing or invalid");
const player = accessor.getCombatant(playerId);
if (!player) throw new Error("Player missing or invalid");
const events: SimEvent[] = [];
// If player already has enough energy (from previous accumulation), return immediately to let them act
// NOTE: We do NOT deduct player energy here. The player's action will cost energy in the next turn processing or we expect the caller to have deducted it?
// Actually, standard roguelike loop:
// 1. Player acts. Deduct cost.
// 2. Loop game until Player has energy >= Threshold.
// Since this function is called AFTER user input (Player just acted), we assume Player needs to recover energy.
// BUT, we should check if we need to deduct energy first?
// The caller just applied an action. We should probably deduct energy for that action BEFORE entering the loop?
// For now, let's assume the player is at < 100 energy and needs to wait.
// Wait, if we don't deduct energy, the player stays at high energy?
// The caller doesn't manage energy. WE manage energy.
// Implicitly, the player just spent 100 energy to trigger this call.
// So we should deduct it from the player NOW.
if (player.energy >= THRESHOLD) {
player.energy -= THRESHOLD;
}
while (true) {
// If player has enough energy to act, return control to user
if (player.energy >= THRESHOLD) {
return { awaitingPlayerId: playerId, events };
}
// Give energy to everyone
for (const actor of w.actors.values()) {
const actors = [...accessor.getAllActors()];
for (const actor of actors) {
if (actor.category === "combatant") {
actor.energy += actor.speed;
}
}
// Process turns for everyone who has enough energy (except player, who breaks the loop)
// We sort by energy to give priority to those who have waited longest/are fastest?
// ROT.Scheduler uses a priority queue. Here we can iterate.
// Iterating map values is insertion order.
// Ideally we'd duplicate the list to sort it, but for performance let's simple iterate.
// We need to loop multiple times if someone has A LOT of energy (e.g. speed 200 vs speed 50)
// But typically we step 1 tick.
// Simpler approach:
// Process all actors with energy >= THRESHOLD.
// If multiple have >= THRESHOLD, who goes first?
// Usually the one with highest energy.
// Let's protect against infinite loops if someone has infinite speed.
let actionsTaken = 0;
while (true) {
const eligibleActors = [...w.actors.values()].filter(
a => a.category === "combatant" && a.energy >= THRESHOLD && !a.isPlayer
) as CombatantActor[];
const eligibleActors = accessor.getEnemies().filter(a => a.energy >= THRESHOLD);
if (eligibleActors.length === 0) break;
// Sort by energy descending
eligibleActors.sort((a, b) => b.energy - a.energy);
const actor = eligibleActors[0];
// Actor takes a turn
actor.energy -= THRESHOLD;
// Decide logic
const decision = decideEnemyAction(w, actor, player, em);
const decision = decideEnemyAction(w, actor, player, accessor);
if (decision.justAlerted) {
events.push({
@@ -369,15 +291,14 @@ export function stepUntilPlayerTurn(w: World, playerId: EntityId, em?: EntityMan
});
}
events.push(...applyAction(w, actor.id, decision.action, em));
events.push(...applyAction(w, actor.id, decision.action, accessor));
// Check if player died
if (!w.actors.has(playerId)) {
if (!accessor.isPlayerAlive()) {
return { awaitingPlayerId: null as any, events };
}
actionsTaken++;
if (actionsTaken > 1000) break; // Emergency break
if (actionsTaken > 1000) break;
}
}
}

View File

@@ -0,0 +1,134 @@
import { type CombatantActor, type Item, type Equipment } from "../../core/types";
/**
* Equipment slot keys matching the Equipment interface.
*/
export type EquipmentSlotKey = keyof Equipment;
/**
* Map of item types to valid equipment slot keys.
*/
const ITEM_TYPE_TO_SLOTS: Record<string, EquipmentSlotKey[]> = {
Weapon: ["mainHand", "offHand"],
BodyArmour: ["bodyArmour"],
Helmet: ["helmet"],
Gloves: ["gloves"],
Boots: ["boots"],
Ring: ["ringLeft", "ringRight"],
Belt: ["belt"],
Amulet: ["amulet"],
Offhand: ["offHand"],
};
/**
* Checks if an item can be equipped in the specified slot.
*/
export function isItemValidForSlot(item: Item | undefined, slotKey: string): boolean {
if (!item || !item.type) return false;
const validSlots = ITEM_TYPE_TO_SLOTS[item.type];
return validSlots?.includes(slotKey as EquipmentSlotKey) ?? false;
}
/**
* Applies or removes item stats to/from a player.
* @param player - The player actor to modify
* @param item - The item with stats to apply
* @param isAdding - True to add stats, false to remove
*/
export function applyItemStats(player: CombatantActor, item: Item, isAdding: boolean): void {
if (!("stats" in item) || !item.stats) return;
const modifier = isAdding ? 1 : -1;
const stats = item.stats as Record<string, number | undefined>;
// Primary stats
if (stats.defense) player.stats.defense += stats.defense * modifier;
if (stats.attack) player.stats.attack += stats.attack * modifier;
// Max HP with current HP adjustment
if (stats.maxHp) {
const diff = stats.maxHp * modifier;
player.stats.maxHp += diff;
player.stats.hp = Math.min(player.stats.maxHp, player.stats.hp + (isAdding ? diff : 0));
}
// Max Mana with current mana adjustment
if (stats.maxMana) {
const diff = stats.maxMana * modifier;
player.stats.maxMana += diff;
player.stats.mana = Math.min(player.stats.maxMana, player.stats.mana + (isAdding ? diff : 0));
}
// Secondary stats
if (stats.critChance) player.stats.critChance += stats.critChance * modifier;
if (stats.accuracy) player.stats.accuracy += stats.accuracy * modifier;
if (stats.evasion) player.stats.evasion += stats.evasion * modifier;
if (stats.blockChance) player.stats.blockChance += stats.blockChance * modifier;
}
/**
* De-equips an item from the specified slot, removing stats and returning to inventory.
* @returns The de-equipped item, or null if slot was empty
*/
export function deEquipItem(
player: CombatantActor,
slotKey: EquipmentSlotKey
): Item | null {
if (!player.equipment) return null;
const item = (player.equipment as Record<string, Item | undefined>)[slotKey];
if (!item) return null;
// Remove from equipment
delete (player.equipment as Record<string, Item | undefined>)[slotKey];
// Remove stats
applyItemStats(player, item, false);
// Add back to inventory
if (!player.inventory) player.inventory = { gold: 0, items: [] };
player.inventory.items.push(item);
return item;
}
/**
* Equips an item to the specified slot, handling swaps if needed.
* @returns Object with success status and optional message
*/
export function equipItem(
player: CombatantActor,
item: Item,
slotKey: EquipmentSlotKey
): { success: boolean; swappedItem?: Item; message?: string } {
// Validate slot
if (!isItemValidForSlot(item, slotKey)) {
return { success: false, message: "Cannot equip there!" };
}
// Remove from inventory
if (!player.inventory) return { success: false, message: "No inventory" };
const itemIdx = player.inventory.items.findIndex(it => it.id === item.id);
if (itemIdx === -1) return { success: false, message: "Item not in inventory" };
// Handle swap if slot is occupied
if (!player.equipment) player.equipment = {};
const oldItem = (player.equipment as Record<string, Item | undefined>)[slotKey];
let swappedItem: Item | undefined;
if (oldItem) {
swappedItem = deEquipItem(player, slotKey) ?? undefined;
}
// Move to equipment (re-find index after potential swap)
const newIdx = player.inventory.items.findIndex(it => it.id === item.id);
if (newIdx !== -1) {
player.inventory.items.splice(newIdx, 1);
}
(player.equipment as Record<string, Item | undefined>)[slotKey] = item;
// Apply stats
applyItemStats(player, item, true);
return { success: true, swappedItem };
}

View File

@@ -0,0 +1,231 @@
import { describe, it, expect, beforeEach } from "vitest";
import {
isItemValidForSlot,
applyItemStats,
deEquipItem,
equipItem,
} from "../EquipmentService";
import type { CombatantActor, Item, WeaponItem, ArmourItem } from "../../../core/types";
// Helper to create a mock player
function createMockPlayer(overrides: Partial<CombatantActor> = {}): CombatantActor {
return {
id: 1,
pos: { x: 0, y: 0 },
category: "combatant",
isPlayer: true,
type: "player",
speed: 100,
energy: 0,
stats: {
maxHp: 20,
hp: 20,
maxMana: 10,
mana: 10,
attack: 5,
defense: 2,
level: 1,
exp: 0,
expToNextLevel: 10,
critChance: 5,
critMultiplier: 150,
accuracy: 90,
lifesteal: 0,
evasion: 5,
blockChance: 0,
luck: 0,
statPoints: 0,
skillPoints: 0,
strength: 10,
dexterity: 10,
intelligence: 10,
passiveNodes: [],
},
inventory: { gold: 0, items: [] },
equipment: {},
...overrides,
};
}
function createSword(): WeaponItem {
return {
id: "sword_1",
name: "Iron Sword",
type: "Weapon",
weaponType: "melee",
textureKey: "items",
spriteIndex: 0,
stats: { attack: 3 },
};
}
function createArmour(): ArmourItem {
return {
id: "armour_1",
name: "Leather Armor",
type: "BodyArmour",
textureKey: "items",
spriteIndex: 1,
stats: { defense: 2 },
};
}
describe("EquipmentService", () => {
describe("isItemValidForSlot", () => {
it("returns true for weapon in mainHand", () => {
expect(isItemValidForSlot(createSword(), "mainHand")).toBe(true);
});
it("returns true for weapon in offHand", () => {
expect(isItemValidForSlot(createSword(), "offHand")).toBe(true);
});
it("returns false for weapon in bodyArmour slot", () => {
expect(isItemValidForSlot(createSword(), "bodyArmour")).toBe(false);
});
it("returns true for BodyArmour in bodyArmour slot", () => {
expect(isItemValidForSlot(createArmour(), "bodyArmour")).toBe(true);
});
it("returns false for undefined item", () => {
expect(isItemValidForSlot(undefined, "mainHand")).toBe(false);
});
it("returns false for unknown slot", () => {
expect(isItemValidForSlot(createSword(), "unknownSlot")).toBe(false);
});
});
describe("applyItemStats", () => {
let player: CombatantActor;
beforeEach(() => {
player = createMockPlayer();
});
it("adds attack stat when isAdding is true", () => {
const sword = createSword();
applyItemStats(player, sword, true);
expect(player.stats.attack).toBe(8); // 5 + 3
});
it("removes attack stat when isAdding is false", () => {
const sword = createSword();
player.stats.attack = 8;
applyItemStats(player, sword, false);
expect(player.stats.attack).toBe(5);
});
it("adds defense stat when isAdding is true", () => {
const armour = createArmour();
applyItemStats(player, armour, true);
expect(player.stats.defense).toBe(4); // 2 + 2
});
it("handles items without stats", () => {
const itemWithoutStats = { id: "coin", name: "Coin", type: "Currency" } as Item;
applyItemStats(player, itemWithoutStats, true);
expect(player.stats.attack).toBe(5); // unchanged
});
});
describe("deEquipItem", () => {
let player: CombatantActor;
let sword: WeaponItem;
beforeEach(() => {
sword = createSword();
player = createMockPlayer({
equipment: { mainHand: sword },
inventory: { gold: 0, items: [] },
});
player.stats.attack = 8; // Sword already equipped
});
it("removes item from equipment slot", () => {
deEquipItem(player, "mainHand");
expect(player.equipment?.mainHand).toBeUndefined();
});
it("returns the de-equipped item", () => {
const result = deEquipItem(player, "mainHand");
expect(result?.id).toBe("sword_1");
});
it("adds item back to inventory", () => {
deEquipItem(player, "mainHand");
expect(player.inventory?.items.length).toBe(1);
expect(player.inventory?.items[0].id).toBe("sword_1");
});
it("removes item stats from player", () => {
deEquipItem(player, "mainHand");
expect(player.stats.attack).toBe(5); // Back to base
});
it("returns null for empty slot", () => {
const result = deEquipItem(player, "offHand");
expect(result).toBeNull();
});
});
describe("equipItem", () => {
let player: CombatantActor;
let sword: WeaponItem;
beforeEach(() => {
sword = createSword();
player = createMockPlayer({
inventory: { gold: 0, items: [sword] },
equipment: {},
});
});
it("equips item to valid slot", () => {
const result = equipItem(player, sword, "mainHand");
expect(result.success).toBe(true);
expect(player.equipment?.mainHand?.id).toBe("sword_1");
});
it("removes item from inventory", () => {
equipItem(player, sword, "mainHand");
expect(player.inventory?.items.length).toBe(0);
});
it("applies item stats", () => {
equipItem(player, sword, "mainHand");
expect(player.stats.attack).toBe(8); // 5 + 3
});
it("fails for invalid slot", () => {
const result = equipItem(player, sword, "bodyArmour");
expect(result.success).toBe(false);
expect(result.message).toBe("Cannot equip there!");
});
it("swaps existing item", () => {
const sword2: WeaponItem = {
id: "sword_2",
name: "Steel Sword",
type: "Weapon",
weaponType: "melee",
textureKey: "items",
spriteIndex: 0,
stats: { attack: 5 },
};
player.inventory!.items.push(sword2);
// Equip first sword
equipItem(player, sword, "mainHand");
expect(player.stats.attack).toBe(8);
// Equip second sword (should swap)
const result = equipItem(player, sword2, "mainHand");
expect(result.success).toBe(true);
expect(result.swappedItem?.id).toBe("sword_1");
expect(player.equipment?.mainHand?.id).toBe("sword_2");
expect(player.stats.attack).toBe(10); // 5 base + 5 new sword
});
});
});

View File

@@ -0,0 +1,41 @@
import { describe, it, expect } from 'vitest';
import { generateWorld } from '../generator';
import { GAME_CONFIG } from '../../../core/config/GameConfig';
describe('World Generator Stacking Debug', () => {
it('should not spawn multiple enemies on the same tile', () => {
const runState = {
stats: { ...GAME_CONFIG.player.initialStats },
inventory: { gold: 0, items: [] }
};
// Run multiple times to catch sporadic rng issues
for (let i = 0; i < 50; i++) {
const floor = 1 + (i % 10);
const { ecsWorld } = generateWorld(floor, runState);
// Get all enemies
const aiEntities = ecsWorld.getEntitiesWith("ai");
const positions = new Set<string>();
const duplicates: string[] = [];
for (const entityId of aiEntities) {
const pos = ecsWorld.getComponent(entityId, "position");
if (pos) {
const key = `${pos.x},${pos.y}`;
if (positions.has(key)) {
duplicates.push(key);
}
positions.add(key);
}
}
if (duplicates.length > 0) {
console.error(`Found duplicates on iteration ${i} (floor ${floor}):`, duplicates);
}
expect(duplicates.length).toBe(0);
}
});
});

View File

@@ -1,4 +1,4 @@
import { type World, type EntityId, type RunState, type Tile, type Actor, type Vec2 } from "../../core/types";
import { type World, type EntityId, type RunState, type Tile, type Vec2 } from "../../core/types";
import { TileType } from "../../core/terrain";
import { idx } from "./world-logic";
import { GAME_CONFIG } from "../../core/config/GameConfig";
@@ -13,6 +13,7 @@ import { seededRandom } from "../../core/math";
import * as ROT from "rot-js";
import { ECSWorld } from "../ecs/World";
import { Prefabs } from "../ecs/Prefabs";
import { EntityBuilder } from "../ecs/EntityBuilder";
interface Room {
@@ -34,6 +35,9 @@ export function generateWorld(floor: number, runState: RunState): { world: World
const tiles: Tile[] = new Array(width * height).fill(TileType.WALL);
const random = seededRandom(floor * 12345);
// Create ECS World first
const ecsWorld = new ECSWorld(); // Starts at ID 1 by default
// Set ROT's RNG seed for consistent dungeon generation
ROT.RNG.setSeed(floor * 12345);
@@ -45,35 +49,34 @@ export function generateWorld(floor: number, runState: RunState): { world: World
const playerX = firstRoom.x + Math.floor(firstRoom.width / 2);
const playerY = firstRoom.y + Math.floor(firstRoom.height / 2);
const actors = new Map<EntityId, Actor>();
const playerId = 1;
actors.set(playerId, {
id: playerId,
category: "combatant",
isPlayer: true,
type: "player",
pos: { x: playerX, y: playerY },
speed: GAME_CONFIG.player.speed,
stats: { ...runState.stats },
inventory: {
// Create Player Entity in ECS
const runInventory = {
gold: runState.inventory.gold,
items: [
...runState.inventory.items,
// Add starting items for testing if empty
...(runState.inventory.items.length === 0 ? [
createConsumable("health_potion", 2),
createMeleeWeapon("iron_sword", "sharp"), // Sharp sword variant
createMeleeWeapon("iron_sword", "sharp"),
createConsumable("throwing_dagger", 3),
createRangedWeapon("pistol"),
createArmour("leather_armor", "heavy"), // Heavy armour variant
createUpgradeScroll(2) // 2 Upgrade scrolls
createArmour("leather_armor", "heavy"),
createUpgradeScroll(2)
] : [])
]
},
energy: 0
});
};
const playerId = EntityBuilder.create(ecsWorld)
.asPlayer()
.withPosition(playerX, playerY)
// RunState stats override default player stats
.withStats(runState.stats)
.withInventory(runInventory)
.withEnergy(GAME_CONFIG.player.speed)
.build();
// No more legacy Actors Map
// Place exit in last room
const lastRoom = rooms[rooms.length - 1];
const exit: Vec2 = {
@@ -81,10 +84,10 @@ export function generateWorld(floor: number, runState: RunState): { world: World
y: lastRoom.y + Math.floor(lastRoom.height / 2)
};
placeEnemies(floor, rooms, actors, random);
placeEnemies(floor, rooms, ecsWorld, random);
// Create ECS world and place traps
const ecsWorld = new ECSWorld();
// Place traps (using same ecsWorld)
const occupiedPositions = new Set<string>();
occupiedPositions.add(`${playerX},${playerY}`); // Don't place traps on player start
occupiedPositions.add(`${exit.x},${exit.y}`); // Don't place traps on exit
@@ -103,7 +106,7 @@ export function generateWorld(floor: number, runState: RunState): { world: World
tiles[playerY * width + playerX] = TileType.EMPTY;
return {
world: { width, height, tiles, actors, exit },
world: { width, height, tiles, exit },
playerId,
ecsWorld
};
@@ -368,8 +371,7 @@ function decorate(width: number, height: number, tiles: Tile[], random: () => nu
}
}
function placeEnemies(floor: number, rooms: Room[], actors: Map<EntityId, Actor>, random: () => number): void {
let enemyId = 2;
function placeEnemies(floor: number, rooms: Room[], ecsWorld: ECSWorld, random: () => number): void {
const numEnemies = GAME_CONFIG.enemyScaling.baseCount + floor * GAME_CONFIG.enemyScaling.baseCountPerFloor;
const enemyTypes = Object.keys(GAME_CONFIG.enemies);
@@ -394,43 +396,23 @@ function placeEnemies(floor: number, rooms: Room[], actors: Map<EntityId, Actor>
const scaledHp = enemyDef.baseHp + floor * GAME_CONFIG.enemyScaling.hpPerFloor;
const scaledAttack = enemyDef.baseAttack + Math.floor(floor / 2) * GAME_CONFIG.enemyScaling.attackPerTwoFloors;
actors.set(enemyId, {
id: enemyId,
category: "combatant",
isPlayer: false,
type,
pos: { x: ex, y: ey },
speed: enemyDef.minSpeed + Math.floor(random() * (enemyDef.maxSpeed - enemyDef.minSpeed)),
stats: {
maxHp: scaledHp + Math.floor(random() * 4),
hp: scaledHp + Math.floor(random() * 4),
maxMana: 0,
mana: 0,
attack: scaledAttack + Math.floor(random() * 2),
defense: enemyDef.baseDefense,
level: 0,
exp: 0,
expToNextLevel: 0,
statPoints: 0,
skillPoints: 0,
strength: 0,
dexterity: 0,
intelligence: 0,
critChance: 0,
critMultiplier: 100,
accuracy: 80,
lifesteal: 0,
evasion: 0,
blockChance: 0,
luck: 0,
passiveNodes: []
},
energy: 0
});
const speed = enemyDef.minSpeed + Math.floor(random() * (enemyDef.maxSpeed - enemyDef.minSpeed));
// Create Enemy in ECS
EntityBuilder.create(ecsWorld)
.asEnemy(type)
.withPosition(ex, ey)
.withStats({
maxHp: scaledHp + Math.floor(random() * 4),
hp: scaledHp + Math.floor(random() * 4),
attack: scaledAttack + Math.floor(random() * 2),
defense: enemyDef.baseDefense,
})
.withEnergy(speed) // Configured speed
// Note: Other stats like crit/evasion are defaults from EntityBuilder or BaseStats
.build();
occupiedPositions.add(k);
enemyId++;
break;
}
}

View File

@@ -1,6 +1,6 @@
import type { World, Vec2 } from "../../core/types";
import { inBounds, isWall, isBlocked, idx } from "./world-logic";
import { type EntityManager } from "../EntityManager";
import { type EntityAccessor } from "../EntityAccessor";
import * as ROT from "rot-js";
/**
@@ -16,14 +16,14 @@ export function findPathAStar(
seen: Uint8Array,
start: Vec2,
end: Vec2,
options: { ignoreBlockedTarget?: boolean; ignoreSeen?: boolean; em?: EntityManager } = {}
options: { ignoreBlockedTarget?: boolean; ignoreSeen?: boolean; accessor?: EntityAccessor } = {}
): Vec2[] {
// Validate target
if (!inBounds(w, end.x, end.y)) return [];
if (isWall(w, end.x, end.y)) return [];
// Check if target is blocked (unless ignoring)
if (!options.ignoreBlockedTarget && isBlocked(w, end.x, end.y, options.em)) return [];
if (!options.ignoreBlockedTarget && isBlocked(w, end.x, end.y, options.accessor)) return [];
// Check if target is unseen (unless ignoring)
if (!options.ignoreSeen && seen[idx(w, end.x, end.y)] !== 1) return [];
@@ -44,7 +44,7 @@ export function findPathAStar(
if (!options.ignoreSeen && seen[idx(w, x, y)] !== 1) return false;
// Check actor blocking
if (isBlocked(w, x, y, options.em)) return false;
if (options.accessor && isBlocked(w, x, y, options.accessor)) return false;
return true;
};

View File

@@ -1,6 +1,6 @@
import type { World, EntityId } from "../../core/types";
import type { World } from "../../core/types";
import { isBlocking, isDestructible, getDestructionResult } from "../../core/terrain";
import { type EntityManager } from "../EntityManager";
import { type EntityAccessor } from "../EntityAccessor";
export function inBounds(w: World, x: number, y: number): boolean {
@@ -37,26 +37,19 @@ export function tryDestructTile(w: World, x: number, y: number): boolean {
return false;
}
export function isBlocked(w: World, x: number, y: number, em?: EntityManager): boolean {
export function isBlocked(w: World, x: number, y: number, accessor: EntityAccessor | undefined): boolean {
if (!inBounds(w, x, y)) return true;
if (isBlockingTile(w, x, y)) return true;
if (em) {
const actors = em.getActorsAt(x, y);
// Only combatants block movement
return actors.some(a => a.category === "combatant");
}
for (const a of w.actors.values()) {
if (a.pos.x === x && a.pos.y === y && a.category === "combatant") return true;
}
return false;
if (!accessor) return false;
const actors = accessor.getActorsAt(x, y);
return actors.some(a => a.category === "combatant");
}
export function isPlayerOnExit(w: World, playerId: EntityId): boolean {
const p = w.actors.get(playerId);
export function isPlayerOnExit(w: World, accessor: EntityAccessor): boolean {
const p = accessor.getPlayer();
if (!p) return false;
return p.pos.x === w.exit.x && p.pos.y === w.exit.y;
}