Streamline stuff
This commit is contained in:
+3
-5
@@ -1,11 +1,9 @@
|
||||
import { createVector, type Vector } from "#/lib/Vector.ts";
|
||||
|
||||
export type Entity<T> = {
|
||||
[x: string]: any;
|
||||
id: string;
|
||||
position: Vector;
|
||||
state: T;
|
||||
};
|
||||
} & T;
|
||||
|
||||
export type Animate<T> = (
|
||||
entity: Entity<T>,
|
||||
@@ -21,7 +19,7 @@ export const entityProcessMap = new Map<string, Process<never>>();
|
||||
|
||||
export function useEntity<T>(
|
||||
id: string,
|
||||
state: T,
|
||||
data: T,
|
||||
): {
|
||||
animate: (fn: Animate<T>) => void;
|
||||
process: (fn: Process<T>) => void;
|
||||
@@ -29,7 +27,7 @@ export function useEntity<T>(
|
||||
entityMap.set(id, {
|
||||
id,
|
||||
position: createVector(),
|
||||
state,
|
||||
...data,
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
+13
-14
@@ -99,24 +99,23 @@ export async function loadScene(name: string): Promise<void> {
|
||||
const data = sceneDataMap.get(name)!;
|
||||
currentScene.data = data;
|
||||
|
||||
for (const entry of data.entities) {
|
||||
await import(`#/entities/${entry.id}/index.ts`);
|
||||
for (const entityData of data.entities) {
|
||||
await import(`#/entities/${entityData.id}/index.ts`);
|
||||
|
||||
const e = entityMap.get(entry.id) as Entity<unknown>;
|
||||
const entity: Entity<unknown> = {
|
||||
id: e.id,
|
||||
position: entry.position,
|
||||
state: structuredClone(e.state),
|
||||
const entity = entityMap.get(entityData.id) as Entity<unknown>;
|
||||
const instance: Entity<unknown> = {
|
||||
...structuredClone(entity),
|
||||
position: entityData.position,
|
||||
};
|
||||
|
||||
if (currentScene.entityMap.has(e.id)) {
|
||||
const pool = currentScene.entityMap.get(e.id);
|
||||
pool!.entities.push(entity);
|
||||
if (currentScene.entityMap.has(entity.id)) {
|
||||
const entry = currentScene.entityMap.get(entity.id);
|
||||
entry!.entities.push(instance);
|
||||
} else {
|
||||
currentScene.entityMap.set(e.id, {
|
||||
animate: entityAnimateMap.get(entry.id)!,
|
||||
process: entityProcessMap.get(entry.id)!,
|
||||
entities: [entity],
|
||||
currentScene.entityMap.set(entity.id, {
|
||||
animate: entityAnimateMap.get(entityData.id) ?? (() => {}),
|
||||
process: entityProcessMap.get(entityData.id) ?? (() => {}),
|
||||
entities: [instance],
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user