Ich glaub ich sollte ins Bett gehen.
This commit is contained in:
+34
-18
@@ -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<never>;
|
||||
process: EntityProcess<never>;
|
||||
entity: Entity<never>;
|
||||
}>;
|
||||
entityPool: Map<
|
||||
string,
|
||||
{
|
||||
hooks: {
|
||||
animate: AnimateEntity<never>;
|
||||
process: ProcessEntity<never>;
|
||||
};
|
||||
entities: Array<Entity<unknown>>;
|
||||
}
|
||||
>;
|
||||
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<never>, 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<never>, 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,19 +104,26 @@ export async function loadScene(name: string): Promise<void> {
|
||||
for (const entry of data.entities) {
|
||||
await import(`#/entities/${entry.id}/index.ts`);
|
||||
|
||||
const e = entityMap.get(entry.id) as Entity<never>;
|
||||
const entity: Entity<never> = {
|
||||
const e = entityMap.get(entry.id) as Entity<unknown>;
|
||||
const entity: Entity<unknown> = {
|
||||
id: e.id,
|
||||
position: entry.position,
|
||||
state: structuredClone(e.state),
|
||||
};
|
||||
|
||||
currentScene.entityPool.push({
|
||||
draw: entityAnimateMap.get(entry.id)!,
|
||||
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)!,
|
||||
entity: entity,
|
||||
},
|
||||
entities: [entity],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const process = sceneProcessMap.get(name)!;
|
||||
currentScene.process = process;
|
||||
|
||||
Reference in New Issue
Block a user