99 lines
2.2 KiB
TypeScript
99 lines
2.2 KiB
TypeScript
import { defineEntity } from "#/engine/Entity.ts";
|
|
import { createRect } from "#/engine/Rect.ts";
|
|
import $ from "#/engine/Scene.ts";
|
|
import {
|
|
createSpriteAnimation,
|
|
defineSpriteSheet,
|
|
type Sprite,
|
|
type SpriteAnimation,
|
|
} from "#/engine/Sprite.ts";
|
|
import {
|
|
type Collider,
|
|
createCollider,
|
|
Shape,
|
|
} from "#/engine/system/physics.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 srcs = {
|
|
idle: await defineSpriteSheet(charIdle, 2, 4),
|
|
walk: await defineSpriteSheet(charWalk, 4, 4),
|
|
};
|
|
|
|
const sprites = {
|
|
idle: {
|
|
src: srcs.idle,
|
|
xStart: 0,
|
|
yStart: 0,
|
|
width: 16,
|
|
height: 16,
|
|
offset: createVector(-8, -8),
|
|
},
|
|
walk: {
|
|
src: srcs.walk,
|
|
xStart: 0,
|
|
yStart: 0,
|
|
width: 16,
|
|
height: 16,
|
|
offset: createVector(-8, -8),
|
|
},
|
|
};
|
|
|
|
const animations = {
|
|
idle: createSpriteAnimation(500, 2),
|
|
walk: createSpriteAnimation(250, 2),
|
|
};
|
|
|
|
const process = defineEntity<{
|
|
position: Vector;
|
|
velocity: Vector;
|
|
collider: Collider;
|
|
animation: SpriteAnimation;
|
|
animations: {
|
|
idle: SpriteAnimation;
|
|
walk: SpriteAnimation;
|
|
};
|
|
sprite: Sprite;
|
|
sprites: {
|
|
idle: Sprite;
|
|
walk: Sprite;
|
|
};
|
|
stuff: number;
|
|
}>("testEntity", {
|
|
position: createVector(),
|
|
velocity: createVector(),
|
|
collider: createCollider(Shape.Rect, createRect(16, 16)),
|
|
animation: animations.idle,
|
|
animations: animations,
|
|
sprite: sprites.idle,
|
|
sprites,
|
|
stuff: 1,
|
|
});
|
|
|
|
process((id, delta) => {
|
|
const position = $<Vector>("position").get(id)!.value;
|
|
const velocity = $<Vector>("velocity").get(id)!.value;
|
|
const collider = $<Collider>("collider").get(id)!.value;
|
|
const stuff = $<number>("stuff").get(id)!;
|
|
const sprite = $<Sprite>("sprite").get(id)!;
|
|
const animation = $<SpriteAnimation>("animation").get(id)!;
|
|
|
|
velocity.x = stuff.value * 0.05;
|
|
position.x += velocity.x * delta;
|
|
|
|
// @ts-ignore
|
|
window.viewport = viewport;
|
|
|
|
if (position.x + collider.offset.x > viewport.canvas.width - 16) {
|
|
stuff.value = -1;
|
|
animation.value = animations.walk;
|
|
sprite.value = sprites.walk;
|
|
} else if (position.x + collider.offset.x < 0) {
|
|
stuff.value = 1;
|
|
animation.value = animations.idle;
|
|
sprite.value = sprites.idle;
|
|
}
|
|
});
|