From d400863db504c8da6d1c10362882d3c97e26b98e Mon Sep 17 00:00:00 2001 From: kolaflash Date: Thu, 27 Nov 2025 23:54:12 +0100 Subject: [PATCH] Ich glaub ich sollte ins Bett gehen. --- engine/Scene.ts | 56 +++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/engine/Scene.ts b/engine/Scene.ts index 174c2a1..b328628 100644 --- a/engine/Scene.ts +++ b/engine/Scene.ts @@ -1,11 +1,11 @@ import type { Vector } from "#/lib/Vector.ts"; import { + type Animate as AnimateEntity, type Entity, - type Animate as EntityDraw, - type Process as EntityProcess, entityAnimateMap, entityMap, entityProcessMap, + type Process as ProcessEntity, } from "./Entity.ts"; export type SceneData = { @@ -20,11 +20,16 @@ export type SceneData = { export type Scene = { data: SceneData; - entityPool: Array<{ - draw: EntityDraw; - process: EntityProcess; - entity: Entity; - }>; + entityPool: Map< + string, + { + hooks: { + animate: AnimateEntity; + process: ProcessEntity; + }; + entities: Array>; + } + >; process: Process; }; @@ -45,18 +50,22 @@ export const currentScene: Scene = { entities: [], }, process: () => {}, - entityPool: [], + entityPool: new Map(), }; function animateEntities(ctx: CanvasRenderingContext2D, delta: number): void { - for (const entry of currentScene.entityPool) { - entry.draw(entry.entity, ctx, delta); + for (const entry of currentScene.entityPool.values()) { + for (const entity of entry.entities) { + entry.hooks.animate(entity as Entity, ctx, delta); + } } } function processEntities(delta: number): void { - for (const entry of currentScene.entityPool) { - entry.process(entry.entity, delta); + for (const entry of currentScene.entityPool.values()) { + for (const entity of entry.entities) { + entry.hooks.process(entity as Entity, delta); + } } } @@ -67,7 +76,7 @@ export function useScene(data: SceneData): { return { process(fn: Process): void { - sceneProcessMap.set(data.id, (ctx, delta) => { + sceneProcessMap.set(data.id, (ctx, delta): void => { animateEntities(ctx, delta); processEntities(delta); fn(ctx, delta); @@ -95,18 +104,25 @@ export async function loadScene(name: string): Promise { for (const entry of data.entities) { await import(`#/entities/${entry.id}/index.ts`); - const e = entityMap.get(entry.id) as Entity; - const entity: Entity = { + const e = entityMap.get(entry.id) as Entity; + const entity: Entity = { id: e.id, position: entry.position, state: structuredClone(e.state), }; - currentScene.entityPool.push({ - draw: entityAnimateMap.get(entry.id)!, - process: entityProcessMap.get(entry.id)!, - entity: entity, - }); + if (currentScene.entityPool.has(e.id)) { + const pool = currentScene.entityPool.get(e.id); + pool!.entities.push(entity); + } else { + currentScene.entityPool.set(e.id, { + hooks: { + animate: entityAnimateMap.get(entry.id)!, + process: entityProcessMap.get(entry.id)!, + }, + entities: [entity], + }); + } } const process = sceneProcessMap.get(name)!;