now it works with n
This commit is contained in:
+11
-20
@@ -1,6 +1,5 @@
|
||||
import { loadImage } from "#/lib/loadImage.ts";
|
||||
import { createSprite, type Sprite } from "#/lib/Sprite.ts";
|
||||
import { createVector, type Vector } from "#/lib/Vector.ts";
|
||||
import type { Sprite } from "#/lib/Sprite.ts";
|
||||
import type { Vector } from "#/lib/Vector.ts";
|
||||
|
||||
export type Entity = {
|
||||
id: string;
|
||||
@@ -16,9 +15,12 @@ export type EntityInstance = {
|
||||
sprite: Sprite;
|
||||
};
|
||||
|
||||
export type Draw = (ctx: CanvasRenderingContext2D) => void;
|
||||
export type Draw = (
|
||||
entity: EntityInstance,
|
||||
ctx: CanvasRenderingContext2D,
|
||||
) => void;
|
||||
|
||||
export type Process = (delta: number) => void;
|
||||
export type Process = (entity: EntityInstance, delta: number) => void;
|
||||
|
||||
export function createEntity(
|
||||
id: string,
|
||||
@@ -29,25 +31,15 @@ export function createEntity(
|
||||
return { id, width, height, src };
|
||||
}
|
||||
|
||||
export const entityMap = new Map<string, EntityInstance>();
|
||||
export const entityMap = new Map<string, Entity>();
|
||||
export const entityDrawMap = new Map<string, Draw>();
|
||||
export const entityProcessMap = new Map<string, Process>();
|
||||
|
||||
export async function useEntity(data: Entity): Promise<{
|
||||
export function useEntity(data: Entity): {
|
||||
draw: (fn: Draw) => void;
|
||||
process: (fn: Process) => void;
|
||||
entity: EntityInstance;
|
||||
}> {
|
||||
const sprite = createSprite(await loadImage(data.src), 2, 4);
|
||||
|
||||
const entity: EntityInstance = {
|
||||
position: createVector(),
|
||||
width: data.width,
|
||||
height: data.height,
|
||||
sprite,
|
||||
};
|
||||
|
||||
entityMap.set(data.id, entity);
|
||||
} {
|
||||
entityMap.set(data.id, data);
|
||||
|
||||
return {
|
||||
draw(fn: Draw): void {
|
||||
@@ -56,6 +48,5 @@ export async function useEntity(data: Entity): Promise<{
|
||||
process(fn: Process): void {
|
||||
entityProcessMap.set(data.id, fn);
|
||||
},
|
||||
entity,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user