Improve animation handling

This commit is contained in:
kolaflash
2026-01-17 14:52:42 +01:00
parent 9441e09d07
commit 74a77fa281
8 changed files with 84 additions and 40 deletions
-34
View File
@@ -1,34 +0,0 @@
import { drawSprite, type Sprite } from "#/lib/Sprite.ts";
export type Animation = {
xIndex: number;
yIndex: number;
frameTime: number;
frameDelta: number;
};
export function createAnimation(yIndex: number, frameTime: number): Animation {
return {
xIndex: 0,
yIndex,
frameTime,
frameDelta: 0,
};
}
export function animateSprite(
ctx: CanvasRenderingContext2D,
sprite: Sprite,
animation: Animation,
x: number,
y: number,
delta: number,
): void {
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);
}