Begin refactoring GameScene
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import type { World, CombatantActor, Item, ItemDropActor, Vec2 } from "../../core/types";
|
||||
import { EntityManager } from "../../engine/EntityManager";
|
||||
import { EntityAccessor } from "../../engine/EntityAccessor";
|
||||
import { type ECSWorld } from "../../engine/ecs/World";
|
||||
import { EntityBuilder } from "../../engine/ecs/EntityBuilder";
|
||||
|
||||
/**
|
||||
* Result of attempting to use an item
|
||||
@@ -16,26 +18,29 @@ export interface ItemUseResult {
|
||||
*/
|
||||
export class ItemManager {
|
||||
private world: World;
|
||||
private entityManager: EntityManager;
|
||||
private entityAccessor: EntityAccessor;
|
||||
private ecsWorld?: ECSWorld;
|
||||
|
||||
constructor(world: World, entityManager: EntityManager) {
|
||||
constructor(world: World, entityAccessor: EntityAccessor, ecsWorld?: ECSWorld) {
|
||||
this.world = world;
|
||||
this.entityManager = entityManager;
|
||||
this.entityAccessor = entityAccessor;
|
||||
this.ecsWorld = ecsWorld;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update references when world changes (e.g., new floor)
|
||||
*/
|
||||
updateWorld(world: World, entityManager: EntityManager): void {
|
||||
updateWorld(world: World, entityAccessor: EntityAccessor, ecsWorld?: ECSWorld): void {
|
||||
this.world = world;
|
||||
this.entityManager = entityManager;
|
||||
this.entityAccessor = entityAccessor;
|
||||
if (ecsWorld) this.ecsWorld = ecsWorld;
|
||||
}
|
||||
|
||||
/**
|
||||
* Spawn an item drop at the specified position
|
||||
*/
|
||||
spawnItem(item: Item, pos: Vec2): void {
|
||||
if (!this.world || !this.entityManager) return;
|
||||
if (!this.world || !this.ecsWorld) return;
|
||||
|
||||
// Deep clone item (crucial for items with mutable stats like ammo)
|
||||
const clonedItem = { ...item } as Item;
|
||||
@@ -43,15 +48,11 @@ export class ItemManager {
|
||||
(clonedItem as any).stats = { ...clonedItem.stats };
|
||||
}
|
||||
|
||||
const id = this.entityManager.getNextId();
|
||||
const drop: ItemDropActor = {
|
||||
id,
|
||||
pos: { x: pos.x, y: pos.y },
|
||||
category: "item_drop",
|
||||
item: clonedItem
|
||||
};
|
||||
|
||||
this.entityManager.addActor(drop);
|
||||
// ECS Path: Spawn using EntityBuilder
|
||||
EntityBuilder.create(this.ecsWorld)
|
||||
.withPosition(pos.x, pos.y)
|
||||
.asGroundItem(clonedItem)
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,15 +62,19 @@ export class ItemManager {
|
||||
tryPickup(player: CombatantActor): Item | null {
|
||||
if (!player || !player.inventory) return null;
|
||||
|
||||
const actors = this.entityManager.getActorsAt(player.pos.x, player.pos.y);
|
||||
const itemActor = actors.find((a): a is ItemDropActor => a.category === "item_drop");
|
||||
let itemActor: ItemDropActor | null = null;
|
||||
|
||||
// Use EntityAccessor to find item on the ground
|
||||
if (this.entityAccessor) {
|
||||
itemActor = this.entityAccessor.findItemDropAt(player.pos.x, player.pos.y);
|
||||
}
|
||||
|
||||
if (itemActor) {
|
||||
const item = itemActor.item;
|
||||
const result = this.addItem(player, item);
|
||||
|
||||
// Remove from world
|
||||
this.entityManager.removeActor(itemActor.id);
|
||||
this.entityAccessor.removeActor(itemActor.id);
|
||||
|
||||
console.log("Picked up:", item.name);
|
||||
return result;
|
||||
|
||||
Reference in New Issue
Block a user