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 { Vector } from "#/lib/Vector.ts";
|
||||||
import {
|
import {
|
||||||
|
type Animate as AnimateEntity,
|
||||||
type Entity,
|
type Entity,
|
||||||
type Animate as EntityDraw,
|
|
||||||
type Process as EntityProcess,
|
|
||||||
entityAnimateMap,
|
entityAnimateMap,
|
||||||
entityMap,
|
entityMap,
|
||||||
entityProcessMap,
|
entityProcessMap,
|
||||||
|
type Process as ProcessEntity,
|
||||||
} from "./Entity.ts";
|
} from "./Entity.ts";
|
||||||
|
|
||||||
export type SceneData = {
|
export type SceneData = {
|
||||||
@@ -20,11 +20,16 @@ export type SceneData = {
|
|||||||
|
|
||||||
export type Scene = {
|
export type Scene = {
|
||||||
data: SceneData;
|
data: SceneData;
|
||||||
entityPool: Array<{
|
entityPool: Map<
|
||||||
draw: EntityDraw<never>;
|
string,
|
||||||
process: EntityProcess<never>;
|
{
|
||||||
entity: Entity<never>;
|
hooks: {
|
||||||
}>;
|
animate: AnimateEntity<never>;
|
||||||
|
process: ProcessEntity<never>;
|
||||||
|
};
|
||||||
|
entities: Array<Entity<unknown>>;
|
||||||
|
}
|
||||||
|
>;
|
||||||
process: Process;
|
process: Process;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -45,18 +50,22 @@ export const currentScene: Scene = {
|
|||||||
entities: [],
|
entities: [],
|
||||||
},
|
},
|
||||||
process: () => {},
|
process: () => {},
|
||||||
entityPool: [],
|
entityPool: new Map(),
|
||||||
};
|
};
|
||||||
|
|
||||||
function animateEntities(ctx: CanvasRenderingContext2D, delta: number): void {
|
function animateEntities(ctx: CanvasRenderingContext2D, delta: number): void {
|
||||||
for (const entry of currentScene.entityPool) {
|
for (const entry of currentScene.entityPool.values()) {
|
||||||
entry.draw(entry.entity, ctx, delta);
|
for (const entity of entry.entities) {
|
||||||
|
entry.hooks.animate(entity as Entity<never>, ctx, delta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function processEntities(delta: number): void {
|
function processEntities(delta: number): void {
|
||||||
for (const entry of currentScene.entityPool) {
|
for (const entry of currentScene.entityPool.values()) {
|
||||||
entry.process(entry.entity, delta);
|
for (const entity of entry.entities) {
|
||||||
|
entry.hooks.process(entity as Entity<never>, delta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +76,7 @@ export function useScene(data: SceneData): {
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
process(fn: Process): void {
|
process(fn: Process): void {
|
||||||
sceneProcessMap.set(data.id, (ctx, delta) => {
|
sceneProcessMap.set(data.id, (ctx, delta): void => {
|
||||||
animateEntities(ctx, delta);
|
animateEntities(ctx, delta);
|
||||||
processEntities(delta);
|
processEntities(delta);
|
||||||
fn(ctx, delta);
|
fn(ctx, delta);
|
||||||
@@ -95,19 +104,26 @@ export async function loadScene(name: string): Promise<void> {
|
|||||||
for (const entry of data.entities) {
|
for (const entry of data.entities) {
|
||||||
await import(`#/entities/${entry.id}/index.ts`);
|
await import(`#/entities/${entry.id}/index.ts`);
|
||||||
|
|
||||||
const e = entityMap.get(entry.id) as Entity<never>;
|
const e = entityMap.get(entry.id) as Entity<unknown>;
|
||||||
const entity: Entity<never> = {
|
const entity: Entity<unknown> = {
|
||||||
id: e.id,
|
id: e.id,
|
||||||
position: entry.position,
|
position: entry.position,
|
||||||
state: structuredClone(e.state),
|
state: structuredClone(e.state),
|
||||||
};
|
};
|
||||||
|
|
||||||
currentScene.entityPool.push({
|
if (currentScene.entityPool.has(e.id)) {
|
||||||
draw: entityAnimateMap.get(entry.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)!,
|
process: entityProcessMap.get(entry.id)!,
|
||||||
entity: entity,
|
},
|
||||||
|
entities: [entity],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const process = sceneProcessMap.get(name)!;
|
const process = sceneProcessMap.get(name)!;
|
||||||
currentScene.process = process;
|
currentScene.process = process;
|
||||||
|
|||||||
Reference in New Issue
Block a user