From eef8adfa5fb061ad5b8faa8478a5d57b1b5542e0 Mon Sep 17 00:00:00 2001 From: Schluffe Date: Fri, 6 Mar 2026 23:03:33 +0100 Subject: [PATCH] wIp --- engine/Entity.ts | 6 +- engine/Scene.ts | 166 +++++++++------------------------ game/scenes/testScene/scene.ts | 15 ++- 3 files changed, 54 insertions(+), 133 deletions(-) diff --git a/engine/Entity.ts b/engine/Entity.ts index 0d8048c..4128f84 100644 --- a/engine/Entity.ts +++ b/engine/Entity.ts @@ -1,13 +1,13 @@ export type Process = (id: string, delta: number) => void; export const entityProcessMap = new Map(); -export const entityBlueprintsMap = new Map>(); +export const entityBlueprintMap = new Map>(); export function defineEntity( name: string, - blueprints: T, + blueprint: T, ): (fn: Process) => void { - entityBlueprintsMap.set(name, new Map(Object.entries(blueprints))); + entityBlueprintMap.set(name, new Map(Object.entries(blueprint))); return (fn) => { entityProcessMap.set(name, fn); diff --git a/engine/Scene.ts b/engine/Scene.ts index 24bb1a7..6394b98 100644 --- a/engine/Scene.ts +++ b/engine/Scene.ts @@ -1,21 +1,22 @@ -import { type Box, box } from "#/engine/Box.ts"; -import { type Drawable, draw, isDrawable } from "#/engine/Drawable.ts"; -import { entityBlueprintsMap, entityProcessMap } from "#/engine/Entity.ts"; +import { entityBlueprintMap, entityProcessMap } from "#/engine/Entity.ts"; import { type Graph, getNeighbours } from "#/engine/Graph.ts"; import { useWithAsyncCache } from "#/engine/lib/cache.ts"; import type MapProxy from "#/engine/lib/MapProxy.ts"; -import { mulberry32 } from "#/engine/lib/mulberry32.ts"; -import { animateSprite, type SpritePlayback } from "#/engine/Sprite.ts"; export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void; export type PreProcess = () => void; export type PostProcess = () => void; +export type EntityData = { + name: string; + init: T; +}; + export type SceneData = { name: string; width: number; height: number; - entities: string[]; + entities: Array>; }; export type ComponentsMap = Map>; @@ -38,41 +39,6 @@ export const scenePostProcessMap = new Map(); export const sceneChangeSet = new Set(); export const sceneGraph: Graph = new Map(); -export const [loadScene, sceneCache] = useWithAsyncCache( - async (name: string) => { - await import(`#/game/scenes/${name}/scene.ts`); - - const data = sceneDataMap.get(name)!; - const entityInstanceMap = sceneEntiyMap.get(name)!; - entityInstanceMap.clear(); - - await Promise.all( - data.entities.map(async (entityData: EntityData, index: number) => { - await import(`#/game/entities/${entityData.name}/entity.ts`); - - if (!entityInstanceMap.has(entityData.name)) { - entityInstanceMap.set(entityData.name, { - process: entityProcessMap.get(entityData.name) ?? (() => {}), - instances: [], - }); - } - - try { - entityInstanceMap.get(entityData.name)!.instances.push( - structuredClone({ - ...entityMap.get(entityData.name)!, - ...entityData, - id: index, - }), - ); - } catch (error) { - console.error(entityData.name, error); - } - }), - ); - }, -); - export const currentScene: Scene = { data: { name: "", @@ -81,29 +47,12 @@ export const currentScene: Scene = { entities: [], }, process: () => {}, - entityMap: new Map(), + components: new Map(), }; function processEntities(delta: number): void { - for (const entry of currentScene.entityMap.values()) { - for (const instance of entry.instances) { - entry.process(instance, delta); - } - } -} - -function processSystems(ctx: CanvasRenderingContext2D, delta: number): void { - const drawables = sceneInstancesDrawMap.get(currentScene.data.name)!; - const animationsMap = sceneInstancesAnimationMap.get(currentScene.data.name)!; - - for (const drawable of drawables.sort( - (a, b) => a.position.y - b.position.y, - )) { - // this dosn't belong here - if (animationsMap.has(drawable.id)) { - animateSprite(animationsMap.get(drawable.id)!.value, delta); - } - draw(ctx, drawable); + for (const entity of currentScene.data.entities) { + entityProcessMap.get(entity.name)!("", delta); // TODO } } @@ -116,17 +65,28 @@ function linkScenes(a: string, b: string): void { } export function defineScene(data: SceneData): { - linkScenes: ( - names: T, - ) => (name: T[number]) => Promise; process: (fn: Process) => void; preProcess: (fn: PreProcess) => void; postProcess: (fn: PostProcess) => void; + linkScenes: ( + names: T, + ) => (name: T[number]) => Promise; } { sceneDataMap.set(data.name, data); - sceneEntiyMap.set(data.name, new Map()); return { + process(fn) { + sceneProcessMap.set(data.name, (ctx, delta) => { + fn(ctx, delta); + processEntities(delta); + }); + }, + preProcess(fn) { + scenePreProcessMap.set(data.name, fn); + }, + postProcess(fn) { + scenePostProcessMap.set(data.name, fn); + }, linkScenes(ids) { for (const id of ids) { linkScenes(data.name, id); @@ -136,22 +96,24 @@ export function defineScene(data: SceneData): { return setScene(id); }; }, - process(fn) { - sceneProcessMap.set(data.name, (ctx, delta) => { - fn(ctx, delta); - processEntities(delta); - processSystems(ctx, delta); - }); - }, - preProcess(fn) { - scenePreProcessMap.set(data.name, fn); - }, - postProcess(fn) { - scenePostProcessMap.set(data.name, fn); - }, }; } +const [loadScene, sceneCache] = useWithAsyncCache(async (name: string) => { + await import(`#/game/scenes/${name}/scene.ts`); + + const data = sceneDataMap.get(name)!; + + await Promise.all( + data.entities.map( + async (entityData: EntityData, index: number) => { + await import(`#/game/entities/${entityData.name}/entity.ts`); + // TODO + }, + ), + ); +}); + export async function setScene(name: string): Promise { await loadScene(name); for (const neighbour of getNeighbours(sceneGraph, name)) { @@ -162,39 +124,15 @@ export async function setScene(name: string): Promise { const data = sceneDataMap.get(name)!; const process = sceneProcessMap.get(name)!; - const entityInstanceMap = sceneEntiyMap.get(name)!; - - const drawableInstances: Array = []; - sceneInstancesDrawMap.set(data.name, drawableInstances); - - const animateInstances: AnimationMap = new Map(); - sceneInstancesAnimationMap.set(data.name, animateInstances); - - for (const entity of entityInstanceMap.values()) { - for (const instance of entity.instances) { - if (isDrawable(instance)) { - drawableInstances.push(instance); - } - if ( - (instance as { id: number; animation: object }).animation !== undefined - ) { - animateInstances.set( - instance.id, - (instance as { id: number; animation: Box }) - .animation, // TODO - ); - } - } - } const preProcess = scenePreProcessMap.get(name); if (preProcess) { - preProcess(sceneEntiyMap.get(name)!); + preProcess(); } const postProcess = scenePostProcessMap.get(currentScene.data.name); if (postProcess) { - postProcess(sceneEntiyMap.get(name)!); + postProcess(); } currentScene.data = data; @@ -202,27 +140,11 @@ export async function setScene(name: string): Promise { currentScene.process = process; - currentScene.entityMap = entityInstanceMap; - - for (const handler of sceneChangeSet.values()) { - handler(data, currentScene.data); + for (const callback of sceneChangeSet.values()) { + callback(data, currentScene.data); } } -const next = mulberry32(54850268); - -export function def(name: string): (key: string, value: T) => void { - const id = next().toString(16).slice(2); - defineComponent("name").set(id, name); - - return (key, value) => { - const components = defineComponent(key); - components.set(id, value); - - console.log(componentMap); - }; -} - export default function (name: string): MapProxy { return currentScene.components.get(name) as MapProxy; } diff --git a/game/scenes/testScene/scene.ts b/game/scenes/testScene/scene.ts index b0a06c3..2053b03 100644 --- a/game/scenes/testScene/scene.ts +++ b/game/scenes/testScene/scene.ts @@ -1,24 +1,23 @@ import { createCircle, strokeCircle } from "#/engine/Circle.ts"; import { pointer } from "#/engine/Pointer.ts"; import { defineScene } from "#/engine/Scene.ts"; -import { spriteSheetMap } from "#/engine/Sprite.ts"; import { createVector } from "#/engine/Vector"; import data from "./data.json"; -const { process, preProcess, linkScenes } = defineScene(data); +const { process, preProcess, postProcess, linkScenes } = defineScene(data); linkScenes(["fooScene"] as const); -preProcess((entityInstanceMap) => { - const entry = entityInstanceMap.get("hero")!; - const instance = entry.instances[0]; - // const sheet = spriteSheetMap.get(instance.animation.src)!; +preProcess(() => { + console.log("pre: ", data.name); +}); - instance.position.x = data.width - 16; - instance.position.y = data.height - 16; +postProcess(() => { + console.log("post: ", data.name); }); const cirle = createCircle(createVector(), 4); + process((ctx, _delta) => { if (!pointer.isDown) { cirle.position.x = pointer.position.x;