No errors, well done sir!

This commit is contained in:
Schluffe
2026-03-08 14:11:33 +01:00
parent 75bbe67730
commit 2ba2fa3d32
2 changed files with 38 additions and 9 deletions
+36 -7
View File
@@ -1,7 +1,12 @@
import { entityBlueprintMap, entityProcessMap } from "#/engine/Entity.ts"; import { type Box, box } from "#/engine/Box.ts";
import {
type Process as EntityProcess,
entityBlueprintMap,
entityProcessMap,
} from "#/engine/Entity.ts";
import { type Graph, getNeighbours } from "#/engine/Graph.ts"; import { type Graph, getNeighbours } from "#/engine/Graph.ts";
import { useWithAsyncCache } from "#/engine/lib/cache.ts"; import { useWithAsyncCache } from "#/engine/lib/cache.ts";
import type MapProxy from "#/engine/lib/MapProxy.ts"; import MapProxy from "#/engine/lib/MapProxy.ts";
export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void; export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void;
export type PreProcess = () => void; export type PreProcess = () => void;
@@ -46,13 +51,15 @@ export const currentScene: Scene = {
height: 0, height: 0,
entities: [], entities: [],
}, },
process: () => {},
components: new Map(), components: new Map(),
process: () => {},
}; };
function processEntities(delta: number): void { function processEntities(delta: number): void {
for (const entity of currentScene.data.entities) { for (const [id, processBox] of sceneComponentsMap
entityProcessMap.get(entity.name)!("", delta); // TODO .get(currentScene.data.name)!
.get("process")!.map) {
(processBox as Box<EntityProcess>).value(id, delta);
} }
} }
@@ -101,16 +108,36 @@ export function defineScene(data: SceneData): {
const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => { const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => {
await import(`#/game/scenes/${name}/scene.ts`); await import(`#/game/scenes/${name}/scene.ts`);
const data = sceneDataMap.get(name)!; const data = sceneDataMap.get(name)!;
const componentsMap = new Map<string, MapProxy<string, unknown>>();
sceneComponentsMap.set(name, componentsMap);
const entityProcessMapProxy = new MapProxy<string, EntityProcess>();
componentsMap.set("process", entityProcessMapProxy);
await Promise.all( await Promise.all(
data.entities.map( data.entities.map(
async (entityData: EntityData<unknown>, index: number) => { async (entityData: EntityData<unknown>, index: number) => {
await import(`#/game/entities/${entityData.name}/entity.ts`); await import(`#/game/entities/${entityData.name}/entity.ts`);
for (const blueprint of entityBlueprintMap const id = `${index}_${entityData.name}`;
const entityProcess = entityProcessMap.get(entityData.name);
if (entityProcess !== undefined) {
entityProcessMapProxy.map.set(id, box(entityProcess));
}
for (const [key, value] of entityBlueprintMap
.get(entityData.name)! .get(entityData.name)!
.entries()) { .entries()) {
if (componentsMap.has(key)) {
componentsMap.get(key)?.map.set(id, box(structuredClone(value)));
} else {
componentsMap.set(
key,
new MapProxy([[id, box(structuredClone(value))]]),
);
}
} }
}, },
), ),
@@ -126,6 +153,7 @@ export async function setScene(name: string): Promise<void> {
} }
const data = sceneDataMap.get(name)!; const data = sceneDataMap.get(name)!;
const components = sceneComponentsMap.get(name)!;
const process = sceneProcessMap.get(name)!; const process = sceneProcessMap.get(name)!;
const preProcess = scenePreProcessMap.get(name); const preProcess = scenePreProcessMap.get(name);
@@ -141,6 +169,7 @@ export async function setScene(name: string): Promise<void> {
currentScene.data = data; currentScene.data = data;
self.dispatchEvent(new Event("resize")); self.dispatchEvent(new Event("resize"));
currentScene.components = components;
currentScene.process = process; currentScene.process = process;
for (const callback of sceneChangeSet.values()) { for (const callback of sceneChangeSet.values()) {
+2 -2
View File
@@ -3,8 +3,8 @@ import type { Box } from "#/engine/Box.ts";
export default class MapProxy<K, V> { export default class MapProxy<K, V> {
map: Map<K, Box<V>>; map: Map<K, Box<V>>;
constructor() { constructor(iterable?: Iterable<readonly [K, Box<V>]>) {
this.map = new Map(); this.map = new Map(iterable);
} }
get(key: K): Box<V> | undefined { get(key: K): Box<V> | undefined {