minor performance upgrade
This commit is contained in:
+7
-7
@@ -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<EntityData<unknown>>;
|
||||
};
|
||||
|
||||
export type ComponentsMap = Map<string, MapProxy<string, unknown>>;
|
||||
export type ComponentsMap = Map<string, MapArray<string, unknown>>;
|
||||
|
||||
export type Scene = {
|
||||
data: SceneData;
|
||||
@@ -113,7 +113,7 @@ const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => {
|
||||
const localEntityProcessMap = new Map<string, EntityProcess>();
|
||||
sceneEntityProcessMap.set(name, localEntityProcessMap);
|
||||
|
||||
const componentsMap = new Map<string, MapProxy<string, unknown>>();
|
||||
const componentsMap = new Map<string, MapArray<string, unknown>>();
|
||||
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<void> {
|
||||
}
|
||||
}
|
||||
|
||||
export default function <T>(name: string): MapProxy<string, T> {
|
||||
return currentScene.components.get(name) as MapProxy<string, T>;
|
||||
export default function <T>(name: string): MapArray<string, T> {
|
||||
return currentScene.components.get(name) as MapArray<string, T>;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import type { Box } from "#/engine/Box.ts";
|
||||
|
||||
export default class MapArray<K, V> {
|
||||
map: Map<K, Box<V>>;
|
||||
array: Array<[K, Box<V>]>;
|
||||
|
||||
constructor(iterable?: Iterable<[K, Box<V>]>) {
|
||||
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<V>): 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<V> | undefined {
|
||||
return this.map.get(key);
|
||||
}
|
||||
|
||||
entries(): Array<[K, Box<V>]> {
|
||||
return this.array;
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
import type { Box } from "#/engine/Box.ts";
|
||||
|
||||
export default class MapProxy<K, V> {
|
||||
map: Map<K, Box<V>>;
|
||||
|
||||
constructor(iterable?: Iterable<readonly [K, Box<V>]>) {
|
||||
this.map = new Map(iterable);
|
||||
}
|
||||
|
||||
get(key: K): Box<V> | undefined {
|
||||
return this.map.get(key);
|
||||
}
|
||||
|
||||
entries(): MapIterator<[K, Box<V>]> {
|
||||
return this.map.entries();
|
||||
}
|
||||
|
||||
keys(): MapIterator<K> {
|
||||
return this.map.keys();
|
||||
}
|
||||
|
||||
values(): MapIterator<Box<V>> {
|
||||
return this.map.values();
|
||||
}
|
||||
}
|
||||
@@ -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<Box<Sprite>>;
|
||||
|
||||
sceneComponentsMap
|
||||
.get(sceneData.name)!
|
||||
.get("position")!
|
||||
.values()
|
||||
.map.values()
|
||||
.forEach((box, index) => {
|
||||
drawables.push({
|
||||
position: box as Box<Vector>,
|
||||
|
||||
Reference in New Issue
Block a user