This commit is contained in:
Schluffe
2026-03-05 23:36:33 +01:00
parent c2dce421b3
commit c6bb4abac0
12 changed files with 131 additions and 144 deletions
+2
View File
@@ -17,10 +17,12 @@ export function strokeCircle(
circle: Circle, circle: Circle,
color: string = "#fff", color: string = "#fff",
alpha: number = 1.0, alpha: number = 1.0,
lineWidth: number = 1,
): void { ): void {
ctx.save(); ctx.save();
ctx.strokeStyle = color; ctx.strokeStyle = color;
ctx.globalAlpha = alpha; ctx.globalAlpha = alpha;
ctx.lineWidth = lineWidth;
ctx.beginPath(); ctx.beginPath();
ctx.arc(circle.position.x, circle.position.y, circle.radius, 0, 2 * Math.PI); ctx.arc(circle.position.x, circle.position.y, circle.radius, 0, 2 * Math.PI);
ctx.stroke(); ctx.stroke();
+2 -8
View File
@@ -1,9 +1,3 @@
export const componentMap = new Map(); import type { Box } from "#/engine/Box.ts";
export function defineComponent<T>(key: string): Map<string, T> { export type Component<T> = Record<string, Box<T>>;
if (!componentMap.has(key)) {
componentMap.set(key, new Map());
}
return componentMap.get(key)!;
}
+7 -12
View File
@@ -1,18 +1,13 @@
export type Entity<T extends object> = { export type Process = (id: string, delta: number) => void;
name: string;
state: T;
};
export type Process<T extends object> = (instance: T, delta: number) => void; export const entityProcessMap = new Map<string, Process>();
export const entityBlueprintsMap = new Map<string, Map<string, unknown>>();
export const entityMap = new Map(); export function defineEntity<T extends { [key: string]: unknown }>(
export const entityProcessMap = new Map();
export function defineEntity<T extends object>(
name: string, name: string,
state: T, blueprints: T,
): (fn: Process<T>) => void { ): (fn: Process) => void {
entityMap.set(name, state); entityBlueprintsMap.set(name, new Map(Object.entries(blueprints)));
return (fn) => { return (fn) => {
entityProcessMap.set(name, fn); entityProcessMap.set(name, fn);
+2
View File
@@ -31,10 +31,12 @@ export function strokeRect(
rect: Rect, rect: Rect,
color: string = "#fff", color: string = "#fff",
alpha: number = 1.0, alpha: number = 1.0,
lineWidth: number = 1,
): void { ): void {
ctx.save(); ctx.save();
ctx.strokeStyle = color; ctx.strokeStyle = color;
ctx.globalAlpha = alpha; ctx.globalAlpha = alpha;
ctx.lineWidth = lineWidth;
ctx.strokeRect( ctx.strokeRect(
rect.position.x - 0.5, rect.position.x - 0.5,
rect.position.y - 0.5, rect.position.y - 0.5,
+21 -40
View File
@@ -1,66 +1,43 @@
import { type Box, box } from "#/engine/Box.ts"; import { type Box, box } from "#/engine/Box.ts";
import { type Drawable, draw, isDrawable } from "#/engine/Drawable.ts"; import { type Drawable, draw, isDrawable } from "#/engine/Drawable.ts";
import { import { entityBlueprintsMap, entityProcessMap } from "#/engine/Entity.ts";
entityMap,
entityProcessMap,
type Process as ProcessEntity,
} from "#/engine/Entity.ts";
import { type Graph, getNeighbours } from "#/engine/Graph.ts"; import { type Graph, getNeighbours } from "#/engine/Graph.ts";
import { useWithAsyncCache } from "#/engine/lib/cache.ts"; import { useWithAsyncCache } from "#/engine/lib/cache.ts";
import { animateSprite, type SpriteAnimation } from "#/engine/Sprite.ts"; import type MapProxy from "#/engine/lib/MapProxy.ts";
import { componentMap, defineComponent } from "./Component"; import { mulberry32 } from "#/engine/lib/mulberry32.ts";
import { mulberry32 } from "./lib/mulberry32"; import { animateSprite, type SpritePlayback } from "#/engine/Sprite.ts";
export type EntityData = { export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void;
name: string; export type PreProcess = () => void;
init: object; export type PostProcess = () => void;
};
export type SceneData = { export type SceneData = {
name: string; name: string;
width: number; width: number;
height: number; height: number;
entities: Array<EntityData>; entities: string[];
}; };
export type EntityMap = Map< export type ComponentsMap = Map<string, MapProxy<string, unknown>>;
string,
{
process: ProcessEntity<{ id: number }>;
instances: Array<{ id: number }>;
}
>;
export type AnimationMap = Map<number, Box<SpriteAnimation>>;
export type Scene = { export type Scene = {
data: SceneData; data: SceneData;
entityMap: EntityMap; components: ComponentsMap;
process: Process; process: Process;
}; };
export type Process = (ctx: CanvasRenderingContext2D, delta: number) => void; export type SceneChange = (to: SceneData, from: SceneData) => void;
export type PreProcess = (entityMap: EntityMap) => void;
export type PostProcess = (entityMap: EntityMap) => void;
export type SceneChangeHandler = (to: SceneData, from: SceneData) => void;
export const sceneDataMap = new Map<string, SceneData>();
export const sceneProcessMap = new Map<string, Process>(); export const sceneProcessMap = new Map<string, Process>();
export const sceneComponentsMap = new Map<string, ComponentsMap>();
export const scenePreProcessMap = new Map<string, PreProcess>(); export const scenePreProcessMap = new Map<string, PreProcess>();
export const scenePostProcessMap = new Map<string, PostProcess>(); export const scenePostProcessMap = new Map<string, PostProcess>();
export const sceneChangedHandlers = new Set<SceneChangeHandler>(); export const sceneChangeSet = new Set<SceneChange>();
export const sceneDataMap = new Map<string, SceneData>();
export const sceneEntiyMap = new Map<string, EntityMap>();
export const sceneGraph: Graph<string> = new Map(); export const sceneGraph: Graph<string> = new Map();
const sceneInstancesDrawMap = new Map<
string,
Array<Drawable & { id: number }>
>();
const sceneInstancesAnimationMap = new Map<string, AnimationMap>();
export const [loadScene, sceneCache] = useWithAsyncCache( export const [loadScene, sceneCache] = useWithAsyncCache(
async (name: string) => { async (name: string) => {
await import(`#/game/scenes/${name}/scene.ts`); await import(`#/game/scenes/${name}/scene.ts`);
@@ -203,7 +180,7 @@ export async function setScene(name: string): Promise<void> {
) { ) {
animateInstances.set( animateInstances.set(
instance.id, instance.id,
(instance as { id: number; animation: Box<SpriteAnimation> }) (instance as { id: number; animation: Box<SpritePlayback> })
.animation, // TODO .animation, // TODO
); );
} }
@@ -227,7 +204,7 @@ export async function setScene(name: string): Promise<void> {
currentScene.entityMap = entityInstanceMap; currentScene.entityMap = entityInstanceMap;
for (const handler of sceneChangedHandlers.values()) { for (const handler of sceneChangeSet.values()) {
handler(data, currentScene.data); handler(data, currentScene.data);
} }
} }
@@ -245,3 +222,7 @@ export function def(name: string): <T>(key: string, value: T) => void {
console.log(componentMap); console.log(componentMap);
}; };
} }
export default function <T>(name: string): MapProxy<string, T> {
return currentScene.components.get(name) as MapProxy<string, T>;
}
+7 -2
View File
@@ -61,12 +61,17 @@ export function setSegmentIntersection(
export function drawSegment( export function drawSegment(
segment: Segment, segment: Segment,
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
color: string, color: string = "#fff",
alpha: number = 1.0,
lineWidth: number = 1,
): void { ): void {
ctx.lineWidth = 2; ctx.save();
ctx.strokeStyle = color; ctx.strokeStyle = color;
ctx.globalAlpha = alpha;
ctx.lineWidth = lineWidth;
ctx.beginPath(); ctx.beginPath();
ctx.moveTo(segment[0].x, segment[0].y); ctx.moveTo(segment[0].x, segment[0].y);
ctx.lineTo(segment[1].x, segment[1].y); ctx.lineTo(segment[1].x, segment[1].y);
ctx.stroke(); ctx.stroke();
ctx.restore();
} }
+32 -17
View File
@@ -16,8 +16,7 @@ export type SpriteSheet = {
frameHeight: number; frameHeight: number;
}; };
export type SpriteAnimation = { export type SpritePlayback = {
sprite: Sprite;
xIndex: number; xIndex: number;
yIndex: number; yIndex: number;
frameTime: number; frameTime: number;
@@ -26,7 +25,7 @@ export type SpriteAnimation = {
// It's not possible to crate a structured clone of a HTMLImageElement and it would be a // It's not possible to crate a structured clone of a HTMLImageElement and it would be a
// waste of memory anyways since we use the HTMLImageElement only as reference for draw calls. // waste of memory anyways since we use the HTMLImageElement only as reference for draw calls.
// Therefore the SpriteSheet will stored by it's image source. // Therefore the SpriteSheet will stored and referenced by it's image source.
export const spriteSheetMap = new Map<string, SpriteSheet>(); export const spriteSheetMap = new Map<string, SpriteSheet>();
export function createSpriteSheet( export function createSpriteSheet(
@@ -43,7 +42,23 @@ export function createSpriteSheet(
}; };
} }
export async function useSpriteSheetSrc( export function createSprite(
src: string,
xStartFrame: number = 0,
yStartFrame: number = 0,
): Sprite {
const spriteSheet = spriteSheetMap.get(src)!;
return {
src,
xStart: xStartFrame * spriteSheet.frameWidth,
yStart: yStartFrame * spriteSheet.frameHeight,
width: spriteSheet.frameWidth,
height: spriteSheet.frameHeight,
};
}
export async function defineSpriteSheet(
src: string, src: string,
xFrames: number, xFrames: number,
yFrames: number, yFrames: number,
@@ -56,15 +71,11 @@ export async function useSpriteSheetSrc(
return src; return src;
} }
export function createSpriteAnimation( export function createSpritePlayback(
sprite: Sprite,
frameTime: number, frameTime: number,
yIndex: number, yIndex: number,
): SpriteAnimation { ): SpritePlayback {
sprite.yStart = yIndex * spriteSheetMap.get(sprite.src)!.frameHeight;
return { return {
sprite,
xIndex: 0, xIndex: 0,
yIndex, yIndex,
frameTime, frameTime,
@@ -95,14 +106,18 @@ export function drawSprite(
); );
} }
export function animateSprite(animation: SpriteAnimation, delta: number): void { export function animateSprite(
const sheet = spriteSheetMap.get(animation.sprite.src)!; sprite: Sprite,
if ((animation.frameDelta += delta) > animation.frameTime) { playback: SpritePlayback,
animation.frameDelta = 0; delta: number,
if ((animation.xIndex += 1) >= sheet.xFrames) { ): void {
animation.xIndex = 0; const sheet = spriteSheetMap.get(sprite.src)!;
if ((playback.frameDelta += delta) > playback.frameTime) {
playback.frameDelta = 0;
if ((playback.xIndex += 1) >= sheet.xFrames) {
playback.xIndex = 0;
} }
} }
animation.sprite.xStart = animation.xIndex * sheet.frameWidth; sprite.xStart = playback.xIndex * sheet.frameWidth;
} }
+2 -2
View File
@@ -11,7 +11,7 @@ import type { Entity } from "#/engine/Entity.ts";
import { import {
currentScene, currentScene,
type SceneData, type SceneData,
sceneChangedHandlers, sceneChangeSet,
sceneGraph, sceneGraph,
setScene, setScene,
} from "#/engine/Scene.ts"; } from "#/engine/Scene.ts";
@@ -86,7 +86,7 @@ const DevTools: Component<{ gameContainer: HTMLElement }> = ({
await setScene(name); await setScene(name);
}; };
sceneChangedHandlers.add((data) => { sceneChangeSet.add((data) => {
setActiveEntity(null); setActiveEntity(null);
setSceneData(data); setSceneData(data);
setEntities(mapSceneEntities(data)); setEntities(mapSceneEntities(data));
+13
View File
@@ -0,0 +1,13 @@
import type { Box } from "#/engine/Box.ts";
export default class MapProxy<K, V> {
map: Map<K, Box<V>>;
constructor() {
this.map = new Map();
}
get(key: K): V | undefined {
return this.map.get(key)?.value;
}
}
+30 -50
View File
@@ -1,70 +1,50 @@
import { type Box, box } from "#/engine/Box.ts"; // import { createCircle } from "#/engine/Circle.ts";
import { type Circle, createCircle } from "#/engine/Circle.ts"; // import { moveAndCollide } from "#/engine/Collision.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 { keyboardInput } from "#/engine/Keyboard.ts";
import { pointer } from "#/engine/Pointer.ts"; import { pointer } from "#/engine/Pointer.ts";
import { def } from "#/engine/Scene.ts"; import $ from "#/engine/Scene.ts";
import { import {
createSpriteAnimation, createSprite,
type Sprite, createSpritePlayback,
type SpriteAnimation, defineSpriteSheet,
useSpriteSheetSrc,
} from "#/engine/Sprite.ts"; } from "#/engine/Sprite.ts";
import { createVector, type Vector } from "#/engine/Vector.ts"; import { createVector, type Vector } from "#/engine/Vector.ts";
import charIdle from "#/game/assets/char-idle.png"; import charIdle from "#/game/assets/char-idle.png";
export type Hero = {
position: Vector;
velocity: Vector;
body: Circle;
sprite: Sprite;
animation: Box<SpriteAnimation>;
};
const speed = 0.05; const speed = 0.05;
const idleSheet = await useSpriteSheetSrc(charIdle, 2, 4); const idleSrc = await defineSpriteSheet(charIdle, 2, 4);
const sprite = {
src: idleSheet,
xStart: 0,
yStart: 0,
width: 16,
height: 16,
};
const state: Hero = { const process = defineEntity("hero", {
position: createVector(), position: createVector(),
sprite: createSprite(idleSrc),
playback: createSpritePlayback(500, 2),
// body: createCircle(createVector(), 4),
velocity: createVector(), velocity: createVector(),
body: createCircle(createVector(), 4), });
sprite,
animation: box(createSpriteAnimation(sprite, 500, 2)),
};
const $ = def("hero"); process((id, delta) => {
$<Vector>("position", createVector()); const position = $<Vector>("position").get(id)!;
$<Process>("process", (_id, _delta) => {}); const velocity = velocityMap.get(id)!;
// const body = bodyMap.get(id);
const process = defineEntity("hero", state);
process((entity, delta) => {
if (pointer.isDown) { if (pointer.isDown) {
entity.position.x = pointer.position.x; position.x = pointer.position.x;
entity.position.y = pointer.position.y; position.y = pointer.position.y;
} }
entity.velocity.x -= keyboardInput.arrowLeft ? speed : 0; velocity.x -= keyboardInput.arrowLeft ? speed : 0;
entity.velocity.x += keyboardInput.arrowRight ? speed : 0; velocity.x += keyboardInput.arrowRight ? speed : 0;
entity.velocity.y -= keyboardInput.arrowUp ? speed : 0; velocity.y -= keyboardInput.arrowUp ? speed : 0;
entity.velocity.y += keyboardInput.arrowDown ? speed : 0; velocity.y += keyboardInput.arrowDown ? speed : 0;
entity.position.x = Math.round(entity.velocity.x * delta + entity.position.x); // const collision = moveAndCollide(body, velocity, delta);
entity.position.y = Math.round(entity.velocity.y * delta + entity.position.y); // slideOnCollision(velocity, collision);
// slide(velocity, collision.normal);
entity.velocity.x = 0; position.x = Math.round(velocity.x * delta + position.x);
entity.velocity.y = 0; position.y = Math.round(velocity.y * delta + position.y);
moveAndCollide(entity.body, entity.velocity); velocity.x = 0;
velocity.y = 0;
// entity.body.position.x = entity.body.offset.x + entity.position.x;
// entity.body.position.y = entity.body.offset.y + entity.position.y;
}); });
+3 -3
View File
@@ -1,10 +1,10 @@
import { box } from "#/engine/Box.ts"; import { box } from "#/engine/Box.ts";
import { defineEntity } from "#/engine/Entity.ts"; import { defineEntity } from "#/engine/Entity.ts";
import { createSpriteAnimation, useSpriteSheetSrc } from "#/engine/Sprite.ts"; import { createSpritePlayback, defineSpriteSheet } from "#/engine/Sprite.ts";
import { createVector } from "#/engine/Vector.ts"; import { createVector } from "#/engine/Vector.ts";
import portal from "#/game/assets/portal.png"; import portal from "#/game/assets/portal.png";
const portalSheet = await useSpriteSheetSrc(portal, 3, 1); const portalSheet = await defineSpriteSheet(portal, 3, 1);
const sprite = { const sprite = {
src: portalSheet, src: portalSheet,
xStart: 0, xStart: 0,
@@ -16,5 +16,5 @@ const sprite = {
defineEntity("portal", { defineEntity("portal", {
position: createVector(64, 64), position: createVector(64, 64),
sprite, sprite,
animation: box(createSpriteAnimation(sprite, 400, 0)), animation: box(createSpritePlayback(sprite, 400, 0)),
}); });
+10 -10
View File
@@ -1,10 +1,10 @@
import { type Box, box } from "#/engine/Box.ts"; import { type Box, box } from "#/engine/Box.ts";
import { defineEntity } from "#/engine/Entity.ts"; import { defineEntity } from "#/engine/Entity.ts";
import { import {
createSpriteAnimation, createSpritePlayback,
defineSpriteSheet,
type Sprite, type Sprite,
type SpriteAnimation, type SpritePlayback,
useSpriteSheetSrc,
} from "#/engine/Sprite.ts"; } from "#/engine/Sprite.ts";
import { createVector, type Vector } from "#/engine/Vector.ts"; import { createVector, type Vector } from "#/engine/Vector.ts";
import { viewport } from "#/engine/Viewport.ts"; import { viewport } from "#/engine/Viewport.ts";
@@ -12,8 +12,8 @@ import charIdle from "#/game/assets/char-idle.png";
import charWalk from "#/game/assets/char-walk.png"; import charWalk from "#/game/assets/char-walk.png";
const sheets = { const sheets = {
idle: await useSpriteSheetSrc(charIdle, 2, 4), idle: await defineSpriteSheet(charIdle, 2, 4),
walk: await useSpriteSheetSrc(charWalk, 4, 4), walk: await defineSpriteSheet(charWalk, 4, 4),
}; };
const sprites = { const sprites = {
@@ -34,16 +34,16 @@ const sprites = {
}; };
const animations = { const animations = {
idle: createSpriteAnimation(sprites.idle, 500, 2), idle: createSpritePlayback(sprites.idle, 500, 2),
walk: createSpriteAnimation(sprites.walk, 250, 2), walk: createSpritePlayback(sprites.walk, 250, 2),
}; };
const process = defineEntity<{ const process = defineEntity<{
position: Vector; position: Vector;
animation: Box<SpriteAnimation>; animation: Box<SpritePlayback>;
animations: { animations: {
idle: SpriteAnimation; idle: SpritePlayback;
walk: SpriteAnimation; walk: SpritePlayback;
}; };
sprite: Sprite; sprite: Sprite;
sprites: { sprites: {