diff --git a/engine/Entity.ts b/engine/Entity.ts index 53c9ceb..e06f822 100644 --- a/engine/Entity.ts +++ b/engine/Entity.ts @@ -1,11 +1,9 @@ import { createVector, type Vector } from "#/lib/Vector.ts"; export type Entity = { - [x: string]: any; id: string; position: Vector; - state: T; -}; +} & T; export type Animate = ( entity: Entity, @@ -21,7 +19,7 @@ export const entityProcessMap = new Map>(); export function useEntity( id: string, - state: T, + data: T, ): { animate: (fn: Animate) => void; process: (fn: Process) => void; @@ -29,7 +27,7 @@ export function useEntity( entityMap.set(id, { id, position: createVector(), - state, + ...data, }); return { diff --git a/engine/Scene.ts b/engine/Scene.ts index c85f3e8..f6b8656 100644 --- a/engine/Scene.ts +++ b/engine/Scene.ts @@ -99,24 +99,23 @@ export async function loadScene(name: string): Promise { const data = sceneDataMap.get(name)!; currentScene.data = data; - for (const entry of data.entities) { - await import(`#/entities/${entry.id}/index.ts`); + for (const entityData of data.entities) { + await import(`#/entities/${entityData.id}/index.ts`); - const e = entityMap.get(entry.id) as Entity; - const entity: Entity = { - id: e.id, - position: entry.position, - state: structuredClone(e.state), + const entity = entityMap.get(entityData.id) as Entity; + const instance: Entity = { + ...structuredClone(entity), + position: entityData.position, }; - if (currentScene.entityMap.has(e.id)) { - const pool = currentScene.entityMap.get(e.id); - pool!.entities.push(entity); + if (currentScene.entityMap.has(entity.id)) { + const entry = currentScene.entityMap.get(entity.id); + entry!.entities.push(instance); } else { - currentScene.entityMap.set(e.id, { - animate: entityAnimateMap.get(entry.id)!, - process: entityProcessMap.get(entry.id)!, - entities: [entity], + currentScene.entityMap.set(entity.id, { + animate: entityAnimateMap.get(entityData.id) ?? (() => {}), + process: entityProcessMap.get(entityData.id) ?? (() => {}), + entities: [instance], }); } } diff --git a/entities/testEntity/index.ts b/entities/testEntity/index.ts index f16f77d..7053c77 100644 --- a/entities/testEntity/index.ts +++ b/entities/testEntity/index.ts @@ -22,7 +22,7 @@ const { animate, process } = useEntity<{ stuff: number; }>("testEntity", { animations: { - idle: createAnimation(2, 250), + idle: createAnimation(2, 500), walk: createAnimation(2, 250), }, currentAnimation: "idle", @@ -32,8 +32,8 @@ const { animate, process } = useEntity<{ animate((entity, ctx, delta) => { animateSprite( ctx, - sprites[entity.state.currentAnimation], - entity.state.animations[entity.state.currentAnimation], + sprites[entity.currentAnimation], + entity.animations[entity.currentAnimation], entity.position.x, entity.position.y, delta, @@ -41,12 +41,12 @@ animate((entity, ctx, delta) => { }); process((entity, delta) => { - entity.position.x += entity.state.stuff * 0.05 * delta; + entity.position.x += entity.stuff * 0.05 * delta; if (entity.position.x > viewport.canvas.width - 16) { - entity.state.stuff = -1; - entity.state.currentAnimation = "walk"; + entity.stuff = -1; + entity.currentAnimation = "walk"; } else if (entity.position.x < 0) { - entity.state.stuff = 1; - entity.state.currentAnimation = "idle"; + entity.stuff = 1; + entity.currentAnimation = "idle"; } });