From c2dce421b37eb9a58572bdd79f7fce65240aa234 Mon Sep 17 00:00:00 2001 From: Schluffe Date: Sun, 1 Mar 2026 23:34:45 +0100 Subject: [PATCH] WIP --- engine/Component.ts | 9 +++++++++ engine/Scene.ts | 18 +++++++++++++++++- game/entities/hero/entity.ts | 7 ++++++- 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 engine/Component.ts diff --git a/engine/Component.ts b/engine/Component.ts new file mode 100644 index 0000000..9f04dae --- /dev/null +++ b/engine/Component.ts @@ -0,0 +1,9 @@ +export const componentMap = new Map(); + +export function defineComponent(key: string): Map { + if (!componentMap.has(key)) { + componentMap.set(key, new Map()); + } + + return componentMap.get(key)!; +} diff --git a/engine/Scene.ts b/engine/Scene.ts index b818765..c88bde3 100644 --- a/engine/Scene.ts +++ b/engine/Scene.ts @@ -1,4 +1,4 @@ -import type { Box } from "#/engine/Box.ts"; +import { type Box, box } from "#/engine/Box.ts"; import { type Drawable, draw, isDrawable } from "#/engine/Drawable.ts"; import { entityMap, @@ -8,6 +8,8 @@ import { import { type Graph, getNeighbours } from "#/engine/Graph.ts"; import { useWithAsyncCache } from "#/engine/lib/cache.ts"; import { animateSprite, type SpriteAnimation } from "#/engine/Sprite.ts"; +import { componentMap, defineComponent } from "./Component"; +import { mulberry32 } from "./lib/mulberry32"; export type EntityData = { name: string; @@ -229,3 +231,17 @@ export async function setScene(name: string): Promise { handler(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); + }; +} diff --git a/game/entities/hero/entity.ts b/game/entities/hero/entity.ts index 7fe265f..5e1976c 100644 --- a/game/entities/hero/entity.ts +++ b/game/entities/hero/entity.ts @@ -1,9 +1,10 @@ import { type Box, box } from "#/engine/Box.ts"; import { type Circle, createCircle } from "#/engine/Circle.ts"; import { moveAndCollide } from "#/engine/Collision.ts"; -import { defineEntity } from "#/engine/Entity.ts"; +import { defineEntity, type Process } from "#/engine/Entity.ts"; import { keyboardInput } from "#/engine/Keyboard.ts"; import { pointer } from "#/engine/Pointer.ts"; +import { def } from "#/engine/Scene.ts"; import { createSpriteAnimation, type Sprite, @@ -39,6 +40,10 @@ const state: Hero = { animation: box(createSpriteAnimation(sprite, 500, 2)), }; +const $ = def("hero"); +$("position", createVector()); +$("process", (_id, _delta) => {}); + const process = defineEntity("hero", state); process((entity, delta) => {