run 11
This commit is contained in:
+25
-35
@@ -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);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
+20
-23
@@ -1,11 +1,9 @@
|
||||
import { loadImage } from "#/lib/loadImage.ts";
|
||||
import { createSprite } from "#/lib/Sprite.ts";
|
||||
import { createVector, type Vector } from "#/lib/Vector.ts";
|
||||
import type { Vector } from "#/lib/Vector.ts";
|
||||
import {
|
||||
type Draw as EntityDraw,
|
||||
type EntityInstance,
|
||||
type Entity,
|
||||
type Animate as EntityDraw,
|
||||
type Process as EntityProcess,
|
||||
entityDrawMap,
|
||||
entityAnimateMap,
|
||||
entityMap,
|
||||
entityProcessMap,
|
||||
} from "./Entity.ts";
|
||||
@@ -23,9 +21,9 @@ export type SceneData = {
|
||||
export type Scene = {
|
||||
data: SceneData;
|
||||
entityPool: Array<{
|
||||
draw: EntityDraw;
|
||||
process: EntityProcess;
|
||||
entity: EntityInstance;
|
||||
draw: EntityDraw<never>;
|
||||
process: EntityProcess<never>;
|
||||
entity: Entity<never>;
|
||||
}>;
|
||||
process: Process;
|
||||
};
|
||||
@@ -50,9 +48,9 @@ export const currentScene: Scene = {
|
||||
entityPool: [],
|
||||
};
|
||||
|
||||
function drawEntities(ctx: CanvasRenderingContext2D): void {
|
||||
function animateEntities(ctx: CanvasRenderingContext2D, delta: number): void {
|
||||
for (const entry of currentScene.entityPool) {
|
||||
entry.draw(entry.entity, ctx);
|
||||
entry.draw(entry.entity, ctx, delta);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +68,7 @@ export function useScene(data: SceneData): {
|
||||
return {
|
||||
process(fn: Process): void {
|
||||
sceneProcessMap.set(data.id, (ctx, delta) => {
|
||||
drawEntities(ctx);
|
||||
animateEntities(ctx, delta);
|
||||
processEntities(delta);
|
||||
fn(ctx, delta);
|
||||
});
|
||||
@@ -94,21 +92,20 @@ export async function loadScene(name: string): Promise<void> {
|
||||
const data = sceneDataMap.get(name)!;
|
||||
currentScene.data = data;
|
||||
|
||||
for (const entity of data.entities) {
|
||||
await import(`#/entities/${entity.id}/index.ts`);
|
||||
for (const entry of data.entities) {
|
||||
await import(`#/entities/${entry.id}/index.ts`);
|
||||
|
||||
const e = entityMap.get(entity.id)!;
|
||||
const entityInstance: EntityInstance = {
|
||||
position: entity.position,
|
||||
width: e.width,
|
||||
height: e.height,
|
||||
sprite: createSprite(await loadImage(e.src), 2, 4), // FIXME
|
||||
const e = entityMap.get(entry.id) as Entity<never>;
|
||||
const entity: Entity<never> = {
|
||||
id: e.id,
|
||||
position: entry.position,
|
||||
state: structuredClone(e.state),
|
||||
};
|
||||
|
||||
currentScene.entityPool.push({
|
||||
draw: entityDrawMap.get(entity.id)!,
|
||||
process: entityProcessMap.get(entity.id)!,
|
||||
entity: entityInstance,
|
||||
draw: entityAnimateMap.get(entry.id)!,
|
||||
process: entityProcessMap.get(entry.id)!,
|
||||
entity: entity,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user