Streamline stuff

This commit is contained in:
kolaflash
2025-12-01 20:07:19 +01:00
parent bc83534826
commit 16ac19ff5e
3 changed files with 24 additions and 27 deletions
+3 -5
View File
@@ -1,11 +1,9 @@
import { createVector, type Vector } from "#/lib/Vector.ts"; import { createVector, type Vector } from "#/lib/Vector.ts";
export type Entity<T> = { export type Entity<T> = {
[x: string]: any;
id: string; id: string;
position: Vector; position: Vector;
state: T; } & T;
};
export type Animate<T> = ( export type Animate<T> = (
entity: Entity<T>, entity: Entity<T>,
@@ -21,7 +19,7 @@ export const entityProcessMap = new Map<string, Process<never>>();
export function useEntity<T>( export function useEntity<T>(
id: string, id: string,
state: T, data: T,
): { ): {
animate: (fn: Animate<T>) => void; animate: (fn: Animate<T>) => void;
process: (fn: Process<T>) => void; process: (fn: Process<T>) => void;
@@ -29,7 +27,7 @@ export function useEntity<T>(
entityMap.set(id, { entityMap.set(id, {
id, id,
position: createVector(), position: createVector(),
state, ...data,
}); });
return { return {
+13 -14
View File
@@ -99,24 +99,23 @@ export async function loadScene(name: string): Promise<void> {
const data = sceneDataMap.get(name)!; const data = sceneDataMap.get(name)!;
currentScene.data = data; currentScene.data = data;
for (const entry of data.entities) { for (const entityData of data.entities) {
await import(`#/entities/${entry.id}/index.ts`); await import(`#/entities/${entityData.id}/index.ts`);
const e = entityMap.get(entry.id) as Entity<unknown>; const entity = entityMap.get(entityData.id) as Entity<unknown>;
const entity: Entity<unknown> = { const instance: Entity<unknown> = {
id: e.id, ...structuredClone(entity),
position: entry.position, position: entityData.position,
state: structuredClone(e.state),
}; };
if (currentScene.entityMap.has(e.id)) { if (currentScene.entityMap.has(entity.id)) {
const pool = currentScene.entityMap.get(e.id); const entry = currentScene.entityMap.get(entity.id);
pool!.entities.push(entity); entry!.entities.push(instance);
} else { } else {
currentScene.entityMap.set(e.id, { currentScene.entityMap.set(entity.id, {
animate: entityAnimateMap.get(entry.id)!, animate: entityAnimateMap.get(entityData.id) ?? (() => {}),
process: entityProcessMap.get(entry.id)!, process: entityProcessMap.get(entityData.id) ?? (() => {}),
entities: [entity], entities: [instance],
}); });
} }
} }
+8 -8
View File
@@ -22,7 +22,7 @@ const { animate, process } = useEntity<{
stuff: number; stuff: number;
}>("testEntity", { }>("testEntity", {
animations: { animations: {
idle: createAnimation(2, 250), idle: createAnimation(2, 500),
walk: createAnimation(2, 250), walk: createAnimation(2, 250),
}, },
currentAnimation: "idle", currentAnimation: "idle",
@@ -32,8 +32,8 @@ const { animate, process } = useEntity<{
animate((entity, ctx, delta) => { animate((entity, ctx, delta) => {
animateSprite( animateSprite(
ctx, ctx,
sprites[entity.state.currentAnimation], sprites[entity.currentAnimation],
entity.state.animations[entity.state.currentAnimation], entity.animations[entity.currentAnimation],
entity.position.x, entity.position.x,
entity.position.y, entity.position.y,
delta, delta,
@@ -41,12 +41,12 @@ animate((entity, ctx, delta) => {
}); });
process((entity, delta) => { process((entity, delta) => {
entity.position.x += entity.state.stuff * 0.05 * delta; entity.position.x += entity.stuff * 0.05 * delta;
if (entity.position.x > viewport.canvas.width - 16) { if (entity.position.x > viewport.canvas.width - 16) {
entity.state.stuff = -1; entity.stuff = -1;
entity.state.currentAnimation = "walk"; entity.currentAnimation = "walk";
} else if (entity.position.x < 0) { } else if (entity.position.x < 0) {
entity.state.stuff = 1; entity.stuff = 1;
entity.state.currentAnimation = "idle"; entity.currentAnimation = "idle";
} }
}); });