diff --git a/engine/Scene.ts b/engine/Scene.ts index b9ad138..115547a 100644 --- a/engine/Scene.ts +++ b/engine/Scene.ts @@ -6,7 +6,7 @@ import { } from "#/engine/Entity.ts"; import { type Graph, getNeighbours } from "#/engine/Graph.ts"; import { useWithAsyncCache } from "#/engine/lib/cache.ts"; -import MapProxy from "#/engine/lib/MapProxy.ts"; +import MapArray from "#/engine/lib/MapArray.ts"; export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void; export type PreProcess = () => void; @@ -24,7 +24,7 @@ export type SceneData = { entities: Array>; }; -export type ComponentsMap = Map>; +export type ComponentsMap = Map>; export type Scene = { data: SceneData; @@ -113,7 +113,7 @@ const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => { const localEntityProcessMap = new Map(); sceneEntityProcessMap.set(name, localEntityProcessMap); - const componentsMap = new Map>(); + const componentsMap = new Map>(); sceneComponentsMap.set(name, componentsMap); await Promise.all( @@ -136,9 +136,9 @@ const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => { ), ); if (componentsMap.has(key)) { - componentsMap.get(key)!.map.set(id, boxedValue); + componentsMap.get(key)!.set(id, boxedValue); } else { - componentsMap.set(key, new MapProxy([[id, boxedValue]])); + componentsMap.set(key, new MapArray([[id, boxedValue]])); } } }, @@ -179,6 +179,6 @@ export async function setScene(name: string): Promise { } } -export default function (name: string): MapProxy { - return currentScene.components.get(name) as MapProxy; +export default function (name: string): MapArray { + return currentScene.components.get(name) as MapArray; } diff --git a/engine/lib/MapArray.ts b/engine/lib/MapArray.ts new file mode 100644 index 0000000..d848e37 --- /dev/null +++ b/engine/lib/MapArray.ts @@ -0,0 +1,37 @@ +import type { Box } from "#/engine/Box.ts"; + +export default class MapArray { + map: Map>; + array: Array<[K, Box]>; + + constructor(iterable?: Iterable<[K, Box]>) { + this.map = new Map(iterable); + this.array = iterable ? [...iterable] : []; + } + + delete(key: K): void { + if (this.map.delete(key)) { + this.array[this.array.findIndex((e) => e[0] === key)] = + this.array[this.array.length - 1]; + this.array.pop(); + } + } + + set(key: K, value: Box): void { + if (this.map.has(key)) { + this.array.find((e) => e[0] === key)![1] = value; + } else { + this.array.push([key, value]); + } + + this.map.set(key, value); + } + + get(key: K): Box | undefined { + return this.map.get(key); + } + + entries(): Array<[K, Box]> { + return this.array; + } +} diff --git a/engine/lib/MapProxy.ts b/engine/lib/MapProxy.ts deleted file mode 100644 index 2f9f601..0000000 --- a/engine/lib/MapProxy.ts +++ /dev/null @@ -1,25 +0,0 @@ -import type { Box } from "#/engine/Box.ts"; - -export default class MapProxy { - map: Map>; - - constructor(iterable?: Iterable]>) { - this.map = new Map(iterable); - } - - get(key: K): Box | undefined { - return this.map.get(key); - } - - entries(): MapIterator<[K, Box]> { - return this.map.entries(); - } - - keys(): MapIterator { - return this.map.keys(); - } - - values(): MapIterator> { - return this.map.values(); - } -} diff --git a/engine/system/drawSprites.ts b/engine/system/drawSprites.ts index 806face..a70c109 100644 --- a/engine/system/drawSprites.ts +++ b/engine/system/drawSprites.ts @@ -8,13 +8,13 @@ sceneChangeSet.add((sceneData) => { drawables.length = 0; const sprites = [ - ...sceneComponentsMap.get(sceneData.name)!.get("sprite")!.values(), + ...sceneComponentsMap.get(sceneData.name)!.get("sprite")!.map.values(), ] as Array>; sceneComponentsMap .get(sceneData.name)! .get("position")! - .values() + .map.values() .forEach((box, index) => { drawables.push({ position: box as Box,