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 = $("position").get(id)!.value; const velocity = $("velocity").get(id)!.value; const collider = $("collider").get(id)!.value; const stuff = $("stuff").get(id)!; const sprite = $("sprite").get(id)!; const animation = $("animation").get(id)!; velocity.x = stuff.value * 0.5; position.x += velocity.x; 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; } });