Files
nage/engine/Entity.ts
T
2026-02-05 22:26:34 +01:00

46 lines
986 B
TypeScript

import type { Animation } from "./Animation.ts";
import { createVector, type Vector } from "./Vector.ts";
export type Indirect = never;
export type Entity<T> = {
name: string;
position: Vector;
animation: Animation;
} & T;
export type Animate<T> = (
entity: Entity<T>,
ctx: CanvasRenderingContext2D,
delta: number,
) => void;
export type Process<T> = (entity: Entity<T>, delta: number) => void;
export const entityMap = new Map<string, Entity<unknown>>();
export const entityAnimateMap = new Map<string, Animate<Indirect>>();
export const entityProcessMap = new Map<string, Process<Indirect>>();
export function defineEntity<T extends { animation: Animation }>(
name: string,
state: T,
): {
animate: (fn: Animate<T>) => void;
process: (fn: Process<T>) => void;
} {
entityMap.set(name, {
name,
position: createVector(),
...state,
});
return {
animate(fn) {
entityAnimateMap.set(name, fn);
},
process(fn) {
entityProcessMap.set(name, fn);
},
};
}