now it works with n
This commit is contained in:
+11
-20
@@ -1,6 +1,5 @@
|
|||||||
import { loadImage } from "#/lib/loadImage.ts";
|
import type { Sprite } from "#/lib/Sprite.ts";
|
||||||
import { createSprite, 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 = {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -16,9 +15,12 @@ export type EntityInstance = {
|
|||||||
sprite: Sprite;
|
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(
|
export function createEntity(
|
||||||
id: string,
|
id: string,
|
||||||
@@ -29,25 +31,15 @@ export function createEntity(
|
|||||||
return { id, width, height, src };
|
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 entityDrawMap = new Map<string, Draw>();
|
||||||
export const entityProcessMap = new Map<string, Process>();
|
export const entityProcessMap = new Map<string, Process>();
|
||||||
|
|
||||||
export async function useEntity(data: Entity): Promise<{
|
export function useEntity(data: Entity): {
|
||||||
draw: (fn: Draw) => void;
|
draw: (fn: Draw) => void;
|
||||||
process: (fn: Process) => void;
|
process: (fn: Process) => void;
|
||||||
entity: EntityInstance;
|
} {
|
||||||
}> {
|
entityMap.set(data.id, data);
|
||||||
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);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
draw(fn: Draw): void {
|
draw(fn: Draw): void {
|
||||||
@@ -56,6 +48,5 @@ export async function useEntity(data: Entity): Promise<{
|
|||||||
process(fn: Process): void {
|
process(fn: Process): void {
|
||||||
entityProcessMap.set(data.id, fn);
|
entityProcessMap.set(data.id, fn);
|
||||||
},
|
},
|
||||||
entity,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-9
@@ -1,3 +1,6 @@
|
|||||||
|
import { loadImage } from "#/lib/loadImage.ts";
|
||||||
|
import { createSprite } from "#/lib/Sprite.ts";
|
||||||
|
import { createVector, type Vector } from "#/lib/Vector.ts";
|
||||||
import {
|
import {
|
||||||
type Draw as EntityDraw,
|
type Draw as EntityDraw,
|
||||||
type EntityInstance,
|
type EntityInstance,
|
||||||
@@ -11,7 +14,10 @@ export type SceneData = {
|
|||||||
id: string;
|
id: string;
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
entities: string[];
|
entities: Array<{
|
||||||
|
id: string;
|
||||||
|
position: Vector;
|
||||||
|
}>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type Scene = {
|
export type Scene = {
|
||||||
@@ -45,14 +51,14 @@ export const currentScene: Scene = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function drawEntities(ctx: CanvasRenderingContext2D): void {
|
function drawEntities(ctx: CanvasRenderingContext2D): void {
|
||||||
for (const entity of currentScene.entityPool) {
|
for (const entry of currentScene.entityPool) {
|
||||||
entity.draw(ctx);
|
entry.draw(entry.entity, ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function processEntities(delta: number): void {
|
function processEntities(delta: number): void {
|
||||||
for (const entity of currentScene.entityPool) {
|
for (const entry of currentScene.entityPool) {
|
||||||
entity.process(delta);
|
entry.process(entry.entity, delta);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,12 +95,20 @@ export async function loadScene(name: string): Promise<void> {
|
|||||||
currentScene.data = data;
|
currentScene.data = data;
|
||||||
|
|
||||||
for (const entity of data.entities) {
|
for (const entity of data.entities) {
|
||||||
await import(`#/entities/${entity}/index.ts`);
|
await import(`#/entities/${entity.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
|
||||||
|
};
|
||||||
|
|
||||||
currentScene.entityPool.push({
|
currentScene.entityPool.push({
|
||||||
draw: entityDrawMap.get(entity)!,
|
draw: entityDrawMap.get(entity.id)!,
|
||||||
process: entityProcessMap.get(entity)!,
|
process: entityProcessMap.get(entity.id)!,
|
||||||
entity: entityMap.get(entity)!,
|
entity: entityInstance,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
import { useEntity } from "#/engine/Entity.ts";
|
import { type EntityInstance, useEntity } from "#/engine/Entity.ts";
|
||||||
import { drawSprite } from "#/lib/Sprite.ts";
|
import { drawSprite } from "#/lib/Sprite.ts";
|
||||||
import data from "./data.json";
|
import data from "./data.json";
|
||||||
|
|
||||||
const { draw, process, entity } = await useEntity(data);
|
const { draw, process } = useEntity(data);
|
||||||
|
|
||||||
draw((ctx: CanvasRenderingContext2D) => {
|
draw((entity: EntityInstance, ctx: CanvasRenderingContext2D) => {
|
||||||
drawSprite(ctx, entity.sprite, 0, 0, 16, 16);
|
drawSprite(
|
||||||
|
ctx,
|
||||||
|
entity.sprite,
|
||||||
|
entity.position.x,
|
||||||
|
entity.position.y,
|
||||||
|
entity.width,
|
||||||
|
entity.height,
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
process((_delta) => {});
|
process((_entity, _delta) => {});
|
||||||
|
|||||||
@@ -2,5 +2,20 @@
|
|||||||
"id": "testScene",
|
"id": "testScene",
|
||||||
"width": 48,
|
"width": 48,
|
||||||
"height": 48,
|
"height": 48,
|
||||||
"entities": ["testEntity"]
|
"entities": [
|
||||||
|
{
|
||||||
|
"id": "testEntity",
|
||||||
|
"position": {
|
||||||
|
"x": 0,
|
||||||
|
"y": 0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "testEntity",
|
||||||
|
"position": {
|
||||||
|
"x": 16,
|
||||||
|
"y": 16
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user