74 lines
1.5 KiB
TypeScript
74 lines
1.5 KiB
TypeScript
import { defineEntity } from "#/engine/Entity.ts";
|
|
import {
|
|
createSpriteAnimation,
|
|
type Sprite,
|
|
type SpriteAnimation,
|
|
useSpriteSheetSrc,
|
|
} from "#/engine/Sprite.ts";
|
|
import { createVector, type Vector } from "#/engine/Vector.ts";
|
|
import { viewport } from "#/engine/Viewport.ts";
|
|
import charIdle from "#/game/assets/char-idle.png";
|
|
import charWalk from "#/game/assets/char-walk.png";
|
|
|
|
const sheets = {
|
|
idle: await useSpriteSheetSrc(charIdle, 2, 4),
|
|
walk: await useSpriteSheetSrc(charWalk, 4, 4),
|
|
};
|
|
|
|
const sprites = {
|
|
idle: {
|
|
src: sheets.idle,
|
|
xStart: 0,
|
|
yStart: 0,
|
|
width: 16,
|
|
height: 16,
|
|
},
|
|
walk: {
|
|
src: sheets.walk,
|
|
xStart: 0,
|
|
yStart: 0,
|
|
width: 16,
|
|
height: 16,
|
|
},
|
|
};
|
|
|
|
const animations = {
|
|
idle: createSpriteAnimation(sprites.idle, 500, 2),
|
|
walk: createSpriteAnimation(sprites.walk, 250, 2),
|
|
};
|
|
|
|
const process = defineEntity<{
|
|
position: Vector;
|
|
animation: SpriteAnimation;
|
|
animations: {
|
|
idle: SpriteAnimation;
|
|
walk: SpriteAnimation;
|
|
};
|
|
sprite: Sprite;
|
|
sprites: {
|
|
idle: Sprite;
|
|
walk: Sprite;
|
|
};
|
|
stuff: number;
|
|
}>("testEntity", {
|
|
position: createVector(),
|
|
animation: animations.idle,
|
|
animations,
|
|
sprite: sprites.idle,
|
|
sprites,
|
|
stuff: 1,
|
|
});
|
|
|
|
process((entity, _delta) => {
|
|
entity.position.x += entity.stuff * 0.5;
|
|
if (entity.position.x > viewport.canvas.width - 16) {
|
|
entity.stuff = -1;
|
|
entity.animation = entity.animations.walk;
|
|
entity.sprite = entity.sprites.walk;
|
|
} else if (entity.position.x < 0) {
|
|
entity.stuff = 1;
|
|
entity.animation = entity.animations.idle;
|
|
entity.sprite = entity.sprites.idle;
|
|
}
|
|
});
|