This commit is contained in:
kolaflash
2025-11-27 22:53:17 +01:00
parent af48493e44
commit b4ca15900e
5 changed files with 75 additions and 79 deletions
+25 -35
View File
@@ -1,52 +1,42 @@
import type { Sprite } from "#/lib/Sprite.ts";
import type { Vector } from "#/lib/Vector.ts";
import { createVector, type Vector } from "#/lib/Vector.ts";
export type Entity = {
export type Entity<T> = {
id: string;
src: string;
width: number;
height: number;
};
export type EntityInstance = {
position: Vector;
width: number;
height: number;
sprite: Sprite;
state: T;
};
export type Draw = (
entity: EntityInstance,
export type Animate<T> = (
entity: Entity<T>,
ctx: CanvasRenderingContext2D,
delta: number,
) => void;
export type Process = (entity: EntityInstance, delta: number) => void;
export type Process<T> = (entity: Entity<T>, delta: number) => void;
export function createEntity(
export const entityMap = new Map<string, Entity<unknown>>();
export const entityAnimateMap = new Map<string, Animate<never>>();
export const entityProcessMap = new Map<string, Process<never>>();
export function useEntity<T>(
id: string,
src: string,
width: number = 16,
height: number = 16,
): Entity {
return { id, width, height, src };
}
export const entityMap = new Map<string, Entity>();
export const entityDrawMap = new Map<string, Draw>();
export const entityProcessMap = new Map<string, Process>();
export function useEntity(data: Entity): {
draw: (fn: Draw) => void;
process: (fn: Process) => void;
state: T,
): {
animate: (fn: Animate<T>) => void;
process: (fn: Process<T>) => void;
} {
entityMap.set(data.id, data);
entityMap.set(id, {
id,
position: createVector(),
state,
});
return {
draw(fn: Draw): void {
entityDrawMap.set(data.id, fn);
animate(fn: Animate<T>): void {
entityAnimateMap.set(id, fn);
},
process(fn: Process): void {
entityProcessMap.set(data.id, fn);
process(fn: Process<T>): void {
entityProcessMap.set(id, fn);
},
};
}