not sure its better or not
This commit is contained in:
+1
-1
@@ -30,7 +30,7 @@ import { GraphBrowser } from "./GraphBrowser.tsx";
|
|||||||
|
|
||||||
async function fetchScenes(): Promise<string[]> {
|
async function fetchScenes(): Promise<string[]> {
|
||||||
const indexRef: string[] = await (await fetch(`/api/scenes`)).json();
|
const indexRef: string[] = await (await fetch(`/api/scenes`)).json();
|
||||||
const items = indexRef.filter((value) => value.includes("index"));
|
const items = indexRef.filter((value) => value.includes("scene"));
|
||||||
|
|
||||||
return items.map((i) => i.substring(1).split("/")[0]);
|
return items.map((i) => i.substring(1).split("/")[0]);
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-3
@@ -20,16 +20,17 @@ export const entityAnimateMap = new Map<string, Animate<Indirect>>();
|
|||||||
export const entityProcessMap = new Map<string, Process<Indirect>>();
|
export const entityProcessMap = new Map<string, Process<Indirect>>();
|
||||||
|
|
||||||
export function useEntity<T extends object>(
|
export function useEntity<T extends object>(
|
||||||
name: string,
|
meta: { url: string },
|
||||||
data: T,
|
state: T,
|
||||||
): {
|
): {
|
||||||
animate: (fn: Animate<T>) => void;
|
animate: (fn: Animate<T>) => void;
|
||||||
process: (fn: Process<T>) => void;
|
process: (fn: Process<T>) => void;
|
||||||
} {
|
} {
|
||||||
|
const name = meta.url.match(/entities\/(.+)\/entity\.ts$/)![1];
|
||||||
entityMap.set(name, {
|
entityMap.set(name, {
|
||||||
name,
|
name,
|
||||||
position: createVector(),
|
position: createVector(),
|
||||||
...data,
|
...state,
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
+2
-2
@@ -48,7 +48,7 @@ export const sceneGraph: Graph<string> = new Map();
|
|||||||
|
|
||||||
export const [loadScene, sceneCache] = useWithAsyncCache(
|
export const [loadScene, sceneCache] = useWithAsyncCache(
|
||||||
async (name: string) => {
|
async (name: string) => {
|
||||||
await import(`#/scenes/${name}/index.ts`);
|
await import(`#/scenes/${name}/scene.ts`);
|
||||||
|
|
||||||
const data = sceneDataMap.get(name)!;
|
const data = sceneDataMap.get(name)!;
|
||||||
const entityInstanceMap = sceneEntiyMap.get(name)!;
|
const entityInstanceMap = sceneEntiyMap.get(name)!;
|
||||||
@@ -56,7 +56,7 @@ export const [loadScene, sceneCache] = useWithAsyncCache(
|
|||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
data.entities.map(async (entity: Entity<unknown>) => {
|
data.entities.map(async (entity: Entity<unknown>) => {
|
||||||
await import(`#/entities/${entity.name}/index.ts`);
|
await import(`#/entities/${entity.name}/entity.ts`);
|
||||||
|
|
||||||
if (!entityInstanceMap.has(entity.name)) {
|
if (!entityInstanceMap.has(entity.name)) {
|
||||||
entityInstanceMap.set(entity.name, {
|
entityInstanceMap.set(entity.name, {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { fromSrc, type Sprite } from "#/lib/Sprite.ts";
|
|||||||
|
|
||||||
const idle: Sprite = await fromSrc("/assets/hero/idle.png", 5, 4);
|
const idle: Sprite = await fromSrc("/assets/hero/idle.png", 5, 4);
|
||||||
|
|
||||||
const { animate, process } = useEntity("barEntity", {
|
const { animate, process } = useEntity(import.meta, {
|
||||||
animation: createAnimation(0, 200),
|
animation: createAnimation(0, 200),
|
||||||
stuff: 1,
|
stuff: 1,
|
||||||
});
|
});
|
||||||
@@ -5,7 +5,7 @@ import { fromSrc, type Sprite } from "#/lib/Sprite.ts";
|
|||||||
|
|
||||||
const idle: Sprite = await fromSrc(charIdle, 2, 4);
|
const idle: Sprite = await fromSrc(charIdle, 2, 4);
|
||||||
|
|
||||||
const { animate, process } = useEntity("fooEntity", {
|
const { animate, process } = useEntity(import.meta, {
|
||||||
animation: createAnimation(2, 500),
|
animation: createAnimation(2, 500),
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -22,7 +22,7 @@ const { animate, process } = useEntity<{
|
|||||||
};
|
};
|
||||||
currentAnimation: "idle" | "walk";
|
currentAnimation: "idle" | "walk";
|
||||||
stuff: number;
|
stuff: number;
|
||||||
}>("testEntity", {
|
}>(import.meta, {
|
||||||
animations: {
|
animations: {
|
||||||
idle: createAnimation(2, 500),
|
idle: createAnimation(2, 500),
|
||||||
walk: createAnimation(2, 250),
|
walk: createAnimation(2, 250),
|
||||||
Reference in New Issue
Block a user