WIP
This commit is contained in:
+7
-2
@@ -28,8 +28,13 @@ async function fetchBrowserData(dir: string): Promise<BrowserFolder> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mapSceneEntities(sceneData: SceneData): BrowserFolder {
|
function mapSceneEntities(sceneData: SceneData): BrowserFolder {
|
||||||
const entites = sceneData.entities.reduce((root, entiry, index) => {
|
const sets = {};
|
||||||
root.set(`${entiry.id}:${index}`, `${entiry.id}:${index}`);
|
const entites = sceneData.entities.reduce((root, entiry) => {
|
||||||
|
sets[entiry.id] = sets[entiry.id] === undefined ? 0 : sets[entiry.id] + 1;
|
||||||
|
root.set(
|
||||||
|
`${entiry.id}:${sets[entiry.id]}`,
|
||||||
|
`${entiry.id}:${sets[entiry.id]}`,
|
||||||
|
);
|
||||||
return root;
|
return root;
|
||||||
}, new Map() as BrowserFolder);
|
}, new Map() as BrowserFolder);
|
||||||
|
|
||||||
|
|||||||
+22
-3
@@ -76,7 +76,9 @@ function linkScenes(a: string, b: string): void {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function useScene(data: SceneData): {
|
export function useScene(data: SceneData): {
|
||||||
linkScenes: (ids: string[]) => () => Promise<void>;
|
linkScenes: <T extends readonly string[]>(
|
||||||
|
ids: T,
|
||||||
|
) => (id: T[number]) => Promise<void>;
|
||||||
process: (fn: Process) => void;
|
process: (fn: Process) => void;
|
||||||
} {
|
} {
|
||||||
sceneDataMap.set(data.id, data);
|
sceneDataMap.set(data.id, data);
|
||||||
@@ -88,7 +90,9 @@ export function useScene(data: SceneData): {
|
|||||||
linkScenes(data.id, id);
|
linkScenes(data.id, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
return async () => {};
|
return async (id) => {
|
||||||
|
return changeScene(id);
|
||||||
|
};
|
||||||
},
|
},
|
||||||
process(fn) {
|
process(fn) {
|
||||||
sceneProcessMap.set(data.id, (ctx, delta) => {
|
sceneProcessMap.set(data.id, (ctx, delta) => {
|
||||||
@@ -100,12 +104,27 @@ export function useScene(data: SceneData): {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function changeScene(id: string): Promise<void> {
|
||||||
|
const data = sceneDataMap.get(id)!;
|
||||||
|
const process = sceneProcessMap.get(id)!;
|
||||||
|
const entityInstanceMap = sceneEntiyMap.get(id)!;
|
||||||
|
for (const neighbour of getNeighbours(sceneGraph, id)) {
|
||||||
|
if (!sceneCache.has(neighbour)) {
|
||||||
|
sceneCache.set(neighbour, loadScene(neighbour));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentScene.data = data;
|
||||||
|
currentScene.process = process;
|
||||||
|
currentScene.entityInstanceMap = entityInstanceMap;
|
||||||
|
}
|
||||||
|
|
||||||
export async function loadScene(id: string): Promise<void> {
|
export async function loadScene(id: string): Promise<void> {
|
||||||
await import(`#/scenes/${id}/index.ts`);
|
await import(`#/scenes/${id}/index.ts`);
|
||||||
|
|
||||||
const data = sceneDataMap.get(id)!;
|
const data = sceneDataMap.get(id)!;
|
||||||
const process = sceneProcessMap.get(id)!;
|
const process = sceneProcessMap.get(id)!;
|
||||||
const entityInstanceMap = sceneEntiyMap.get(id)!;
|
const entityInstanceMap = sceneEntiyMap.get(id)!;
|
||||||
|
entityInstanceMap.clear();
|
||||||
|
|
||||||
Promise.all(
|
Promise.all(
|
||||||
data.entities.map(async (entity: Entity<unknown>) => {
|
data.entities.map(async (entity: Entity<unknown>) => {
|
||||||
@@ -121,7 +140,7 @@ export async function loadScene(id: string): Promise<void> {
|
|||||||
|
|
||||||
const instance: Entity<unknown> = {
|
const instance: Entity<unknown> = {
|
||||||
...structuredClone(entityMap.get(entity.id)!),
|
...structuredClone(entityMap.get(entity.id)!),
|
||||||
position: entity.position,
|
...structuredClone(entity),
|
||||||
};
|
};
|
||||||
|
|
||||||
entityInstanceMap.get(entity.id)!.instances.push(instance);
|
entityInstanceMap.get(entity.id)!.instances.push(instance);
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
import { useScene } from "#/engine/Scene.ts";
|
import { useScene } from "#/engine/Scene.ts";
|
||||||
import data from "./data.json";
|
import data from "./data.json";
|
||||||
|
|
||||||
const { process } = useScene(data);
|
const { process, linkScenes } = useScene(data);
|
||||||
|
|
||||||
|
// const changeScene = linkScenes(["testScene"] as const);
|
||||||
|
|
||||||
|
// changeScene("testScene");
|
||||||
|
|
||||||
process((_ctx, _delta) => {});
|
process((_ctx, _delta) => {});
|
||||||
|
|||||||
Reference in New Issue
Block a user