From 2388acaa945b6a46b301506ebaa1ed4e2775ed08 Mon Sep 17 00:00:00 2001 From: John Doty Date: Sun, 20 Aug 2023 17:38:21 -0700 Subject: [PATCH] [game] Use bounds from animation for gfx, position is at anchor --- game/actor.ts | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/game/actor.ts b/game/actor.ts index 368c650c..afa775a3 100644 --- a/game/actor.ts +++ b/game/actor.ts @@ -1,27 +1,21 @@ import { load_texture } from "./assets"; import { btn, Button } from "./input"; -import { Vec2, new_v2, vadd, vnorm, vmul } from "./vector"; +import { Vec2, new_v2, vadd, vsub, vnorm, vmul } from "./vector"; import { spr, use_texture, Texture } from "./graphics"; export interface ActorProps { + position: Vec2; velocity: Vec2; friction: number; id: string; - position: Vec2; - bounds: Vec2; } -export function new_actor_props( - id: string, - position: Vec2, - bounds: Vec2 -): ActorProps { +export function new_actor_props(id: string, position: Vec2): ActorProps { return { velocity: new_v2(0), friction: 0.6, id, position, - bounds, }; } @@ -61,7 +55,8 @@ export class Actor { } const robo_info = { - bounds: new_v2(32), + anchor: new_v2(16, 24), // Distance from upper-left of sprite. + bounds: new_v2(32), // Width/height of sprite. sprite: "./bot.png", animations: [ { start: 0, length: 1, speed: 20 }, @@ -106,8 +101,8 @@ export class Robo extends Actor { const moving = vel.x != 0 || vel.y != 0; const anim = robo_info.animations[moving ? 1 : 0]; - const { x: w, y: h } = this.props.bounds; - const { x, y } = this.props.position; + const { x: w, y: h } = robo_info.bounds; + const { x, y } = vsub(this.props.position, robo_info.anchor); const frame = (anim.start + ((clock / anim.speed) % anim.length)) >> 0; spr(x, y, w, h, frame * w, 0, 32, 32);