[game] Use bounds from animation for gfx, position is at anchor

This commit is contained in:
John Doty 2023-08-20 17:38:21 -07:00
parent 3af0bb4002
commit 2388acaa94

View file

@ -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);