From c7be05701566391724a9b48174cd3b020efc5bb0 Mon Sep 17 00:00:00 2001 From: kolaflash Date: Wed, 7 Jan 2026 15:07:47 +0100 Subject: [PATCH] TIL --- engine/Scene.ts | 2 +- entities/barEntity/index.ts | 10 +++++++++- entities/testEntity/index.ts | 2 +- lib/Animation.ts | 12 ++++-------- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/engine/Scene.ts b/engine/Scene.ts index 13ad680..84a2e93 100644 --- a/engine/Scene.ts +++ b/engine/Scene.ts @@ -128,9 +128,9 @@ export function useScene(data: SceneData): { }, process(fn) { sceneProcessMap.set(data.name, (ctx, delta) => { - animateEntities(ctx, delta); processEntities(delta); fn(ctx, delta); + animateEntities(ctx, delta); }); }, }; diff --git a/entities/barEntity/index.ts b/entities/barEntity/index.ts index a0d80ac..f77ee2a 100644 --- a/entities/barEntity/index.ts +++ b/entities/barEntity/index.ts @@ -6,6 +6,7 @@ const idle: Sprite = await fromSrc("/assets/hero/idle.png", 5, 4); const { animate, process } = useEntity("barEntity", { animation: createAnimation(0, 200), + stuff: 1, }); animate((entity, ctx, delta) => { @@ -19,4 +20,11 @@ animate((entity, ctx, delta) => { ); }); -process((entity, delta) => {}); +process((entity, delta) => { + entity.position.x += entity.stuff * 0.5; + if (entity.position.x > 64) { + entity.stuff = -1; + } else if (entity.position.x < 0) { + entity.stuff = 1; + } +}); diff --git a/entities/testEntity/index.ts b/entities/testEntity/index.ts index 5530722..19b8251 100644 --- a/entities/testEntity/index.ts +++ b/entities/testEntity/index.ts @@ -43,7 +43,7 @@ animate((entity, ctx, delta) => { }); process((entity, delta) => { - entity.position.x += entity.stuff * 0.05 * delta; + entity.position.x += entity.stuff * 0.5; if (entity.position.x > viewport.canvas.width - 16) { entity.stuff = -1; entity.currentAnimation = "walk"; diff --git a/lib/Animation.ts b/lib/Animation.ts index 9333faa..39d35d4 100644 --- a/lib/Animation.ts +++ b/lib/Animation.ts @@ -24,15 +24,11 @@ export function animateSprite( y: number, delta: number, ): void { - drawSprite(ctx, sprite, animation.xIndex, animation.yIndex, x, y); - - animation.frameDelta += delta; - if (animation.frameDelta >= animation.frameTime) { - animation.frameDelta = animation.frameDelta - animation.frameTime; - - animation.xIndex += 1; - if (animation.xIndex >= sprite.xFrames) { + if ((animation.frameDelta += delta) > animation.frameTime) { + animation.frameDelta = 0; + if ((animation.xIndex += 1) >= sprite.xFrames) { animation.xIndex = 0; } } + drawSprite(ctx, sprite, animation.xIndex, animation.yIndex, x, y); }