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
+1
View File
@@ -1,6 +1,7 @@
import { createVector, type Vector } from "#/lib/Vector.ts"; import { createVector, type Vector } from "#/lib/Vector.ts";
export type Entity<T> = { export type Entity<T> = {
[x: string]: any;
id: string; id: string;
position: Vector; position: Vector;
state: T; state: T;
+32 -34
View File
@@ -1,54 +1,52 @@
import { type Entity, useEntity } from "#/engine/Entity.ts"; import { useEntity } from "#/engine/Entity.ts";
import { viewport } from "#/engine/Viewport.ts"; import { viewport } from "#/engine/Viewport.ts";
import {
type Animation,
animateSprite,
createAnimation,
} from "#/lib/Animation.ts";
import { loadImage } from "#/lib/loadImage.ts"; import { loadImage } from "#/lib/loadImage.ts";
import { createSprite, drawSprite, type Sprite } from "#/lib/Sprite.ts"; import { createSprite } from "#/lib/Sprite.ts";
const spriteIdle = createSprite(await loadImage("/assets/char-idle.png"), 2, 4); const sprites = {
const spriteWalk = createSprite(await loadImage("/assets/char-walk.png"), 4, 4); idle: createSprite(await loadImage("/assets/char-idle.png"), 2, 4),
spriteWalk.yIndex = 2; walk: createSprite(await loadImage("/assets/char-walk.png"), 4, 4),
};
function animateSprite( const { animate, process } = useEntity<{
ctx: CanvasRenderingContext2D,
sprite: Sprite,
entity: Entity<any>,
delta: number,
): void {
entity.state.animations.walk.frameDelta += delta;
if (
entity.state.animations.walk.frameDelta >
entity.state.animations.walk.frameTime
) {
entity.state.animations.walk.frameDelta =
entity.state.animations.walk.frameDelta %
entity.state.animations.walk.frameTime;
spriteWalk.xIndex += 1;
if (spriteWalk.xIndex >= spriteWalk.xFrames) {
spriteWalk.xIndex = 0;
}
}
drawSprite(ctx, spriteWalk, entity.position.x, entity.position.y);
}
const { animate, process } = useEntity("testEntity", {
animations: { animations: {
walk: { idle: Animation;
frameTime: 222, walk: Animation;
frameDelta: 0, };
}, currentAnimation: "idle" | "walk";
stuff: number;
}>("testEntity", {
animations: {
idle: createAnimation(2, 250),
walk: createAnimation(2, 250),
}, },
currentAnimation: "idle",
stuff: 1, stuff: 1,
}); });
animate((entity, ctx, delta) => { animate((entity, ctx, delta) => {
animateSprite(ctx, spriteIdle, entity, delta); animateSprite(
ctx,
sprites[entity.state.currentAnimation],
entity.state.animations[entity.state.currentAnimation],
entity.position.x,
entity.position.y,
delta,
);
}); });
process((entity, delta) => { process((entity, delta) => {
entity.position.x += entity.state.stuff * 0.05 * delta; entity.position.x += entity.state.stuff * 0.05 * delta;
if (entity.position.x > viewport.canvas.width - 16) { if (entity.position.x > viewport.canvas.width - 16) {
entity.state.stuff = -1; entity.state.stuff = -1;
entity.state.currentAnimation = "walk";
} else if (entity.position.x < 0) { } else if (entity.position.x < 0) {
entity.state.stuff = 1; entity.state.stuff = 1;
entity.state.currentAnimation = "idle";
} }
}); });
+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;
}
}
}
+8 -8
View File
@@ -4,8 +4,6 @@ export type Sprite = {
yFrames: number; yFrames: number;
frameWidth: number; frameWidth: number;
frameHeight: number; frameHeight: number;
xIndex: number;
yIndex: number;
}; };
export function createSprite( export function createSprite(
@@ -19,26 +17,28 @@ export function createSprite(
yFrames, yFrames,
frameWidth: image.width / xFrames, frameWidth: image.width / xFrames,
frameHeight: image.height / yFrames, frameHeight: image.height / yFrames,
xIndex: 0,
yIndex: 0,
}; };
} }
export function drawSprite( export function drawSprite(
ctx: CanvasRenderingContext2D, ctx: CanvasRenderingContext2D,
sprite: Sprite, sprite: Sprite,
xIndex: number,
yIndex: number,
x: number, x: number,
y: number, y: number,
width?: number,
height?: number,
): void { ): void {
ctx.drawImage( ctx.drawImage(
sprite.image, sprite.image,
sprite.xIndex * sprite.frameWidth, xIndex * sprite.frameWidth,
sprite.yIndex * sprite.frameHeight, yIndex * sprite.frameHeight,
sprite.frameWidth, sprite.frameWidth,
sprite.frameHeight, sprite.frameHeight,
x, x,
y, y,
sprite.frameWidth, width || sprite.frameWidth,
sprite.frameHeight, height || sprite.frameHeight,
); );
} }