Some animations

This commit is contained in:
kolaflash
2025-12-01 19:21:57 +01:00
parent ad9ab70d05
commit bc83534826
4 changed files with 79 additions and 42 deletions
+38
View File
@@ -0,0 +1,38 @@
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 {
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) {
animation.xIndex = 0;
}
}
}