well, that works
This commit is contained in:
+42
-1
@@ -1,3 +1,12 @@
|
||||
import {
|
||||
type Draw as EntityDraw,
|
||||
type EntityInstance,
|
||||
type Process as EntityProcess,
|
||||
entityDrawMap,
|
||||
entityMap,
|
||||
entityProcessMap,
|
||||
} from "./Entity.ts";
|
||||
|
||||
export type SceneData = {
|
||||
id: string;
|
||||
width: number;
|
||||
@@ -7,6 +16,11 @@ export type SceneData = {
|
||||
|
||||
export type Scene = {
|
||||
data: SceneData;
|
||||
entityPool: Array<{
|
||||
draw: EntityDraw;
|
||||
process: EntityProcess;
|
||||
entity: EntityInstance;
|
||||
}>;
|
||||
process: Process;
|
||||
};
|
||||
|
||||
@@ -27,8 +41,21 @@ export const currentScene: Scene = {
|
||||
entities: [],
|
||||
},
|
||||
process: () => {},
|
||||
entityPool: [],
|
||||
};
|
||||
|
||||
function drawEntities(ctx: CanvasRenderingContext2D): void {
|
||||
for (const entity of currentScene.entityPool) {
|
||||
entity.draw(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
function processEntities(delta: number): void {
|
||||
for (const entity of currentScene.entityPool) {
|
||||
entity.process(delta);
|
||||
}
|
||||
}
|
||||
|
||||
export function useScene(data: SceneData): {
|
||||
process: (fn: Process) => void;
|
||||
} {
|
||||
@@ -36,7 +63,11 @@ export function useScene(data: SceneData): {
|
||||
|
||||
return {
|
||||
process(fn: Process): void {
|
||||
sceneProcessMap.set(data.id, fn);
|
||||
sceneProcessMap.set(data.id, (ctx, delta) => {
|
||||
drawEntities(ctx);
|
||||
processEntities(delta);
|
||||
fn(ctx, delta);
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -57,6 +88,16 @@ export async function loadScene(name: string): Promise<void> {
|
||||
const data = sceneDataMap.get(name)!;
|
||||
currentScene.data = data;
|
||||
|
||||
for (const entity of data.entities) {
|
||||
await import(`#/entities/${entity}/index.ts`);
|
||||
|
||||
currentScene.entityPool.push({
|
||||
draw: entityDrawMap.get(entity)!,
|
||||
process: entityProcessMap.get(entity)!,
|
||||
entity: entityMap.get(entity)!,
|
||||
});
|
||||
}
|
||||
|
||||
const process = sceneProcessMap.get(name)!;
|
||||
currentScene.process = process;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user