Fix the animation issue

This commit is contained in:
Schluffe
2026-02-25 23:59:06 +01:00
parent 9ab90b579c
commit a0671e7953
4 changed files with 12 additions and 11 deletions
+5 -4
View File
@@ -28,7 +28,7 @@ export type EntityMap = Map<
} }
>; >;
export type AnimationMap = Map<number, SpriteAnimation>; export type AnimationMap = Map<number, { value: SpriteAnimation }>;
export type Scene = { export type Scene = {
data: SceneData; data: SceneData;
@@ -120,7 +120,7 @@ function processSystems(ctx: CanvasRenderingContext2D, delta: number): void {
(a, b) => a.position.y - b.position.y, (a, b) => a.position.y - b.position.y,
)) { )) {
if (animationsMap.has(drawable.id)) { if (animationsMap.has(drawable.id)) {
animateSprite(animationsMap.get(drawable.id)!, delta); animateSprite(animationsMap.get(drawable.id)!.value, delta);
} }
draw(ctx, drawable); draw(ctx, drawable);
} }
@@ -195,12 +195,13 @@ export async function setScene(name: string): Promise<void> {
drawableInstances.push(instance); drawableInstances.push(instance);
} }
if ( if (
(instance as { id: number; animation: SpriteAnimation }).animation !== (instance as { id: number; $animation: object }).$animation !==
undefined undefined
) { ) {
animateInstances.set( animateInstances.set(
instance.id, instance.id,
(instance as { id: number; animation: SpriteAnimation }).animation, // FIXME (instance as { id: number; $animation: { value: SpriteAnimation } })
.$animation, // TODO: unify that $ value component thing
); );
} }
} }
+2 -2
View File
@@ -17,7 +17,7 @@ export type Hero = {
velocity: Vector; velocity: Vector;
body: Circle; body: Circle;
sprite: Sprite; sprite: Sprite;
animation: SpriteAnimation; $animation: { value: SpriteAnimation };
}; };
const speed = 0.05; const speed = 0.05;
@@ -35,7 +35,7 @@ const state: Hero = {
velocity: createVector(), velocity: createVector(),
body: createCircle(createVector(), 4), body: createCircle(createVector(), 4),
sprite, sprite,
animation: createSpriteAnimation(sprite, 500, 2), $animation: { value: createSpriteAnimation(sprite, 500, 2) },
}; };
const process = defineEntity("hero", state); const process = defineEntity("hero", state);
+1 -1
View File
@@ -15,5 +15,5 @@ const sprite = {
defineEntity("portal", { defineEntity("portal", {
position: createVector(64, 64), position: createVector(64, 64),
sprite, sprite,
animation: createSpriteAnimation(sprite, 400, 0), $animation: { value: createSpriteAnimation(sprite, 400, 0) },
}); });
+4 -4
View File
@@ -39,7 +39,7 @@ const animations = {
const process = defineEntity<{ const process = defineEntity<{
position: Vector; position: Vector;
animation: SpriteAnimation; $animation: { value: SpriteAnimation };
animations: { animations: {
idle: SpriteAnimation; idle: SpriteAnimation;
walk: SpriteAnimation; walk: SpriteAnimation;
@@ -52,7 +52,7 @@ const process = defineEntity<{
stuff: number; stuff: number;
}>("testEntity", { }>("testEntity", {
position: createVector(), position: createVector(),
animation: animations.idle, $animation: { value: animations.idle },
animations, animations,
sprite: sprites.idle, sprite: sprites.idle,
sprites, sprites,
@@ -63,11 +63,11 @@ process((entity, _delta) => {
entity.position.x += entity.stuff * 0.5; entity.position.x += entity.stuff * 0.5;
if (entity.position.x > viewport.canvas.width - 16) { if (entity.position.x > viewport.canvas.width - 16) {
entity.stuff = -1; entity.stuff = -1;
entity.animation = entity.animations.walk; entity.$animation.value = entity.animations.walk;
entity.sprite = entity.sprites.walk; entity.sprite = entity.sprites.walk;
} else if (entity.position.x < 0) { } else if (entity.position.x < 0) {
entity.stuff = 1; entity.stuff = 1;
entity.animation = entity.animations.idle; entity.$animation.value = entity.animations.idle;
entity.sprite = entity.sprites.idle; entity.sprite = entity.sprites.idle;
} }
}); });