[game] Use bounds from animation for gfx, position is at anchor
This commit is contained in:
parent
3af0bb4002
commit
2388acaa94
1 changed files with 7 additions and 12 deletions
|
|
@ -1,27 +1,21 @@
|
||||||
import { load_texture } from "./assets";
|
import { load_texture } from "./assets";
|
||||||
import { btn, Button } from "./input";
|
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";
|
import { spr, use_texture, Texture } from "./graphics";
|
||||||
|
|
||||||
export interface ActorProps {
|
export interface ActorProps {
|
||||||
|
position: Vec2;
|
||||||
velocity: Vec2;
|
velocity: Vec2;
|
||||||
friction: number;
|
friction: number;
|
||||||
id: string;
|
id: string;
|
||||||
position: Vec2;
|
|
||||||
bounds: Vec2;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function new_actor_props(
|
export function new_actor_props(id: string, position: Vec2): ActorProps {
|
||||||
id: string,
|
|
||||||
position: Vec2,
|
|
||||||
bounds: Vec2
|
|
||||||
): ActorProps {
|
|
||||||
return {
|
return {
|
||||||
velocity: new_v2(0),
|
velocity: new_v2(0),
|
||||||
friction: 0.6,
|
friction: 0.6,
|
||||||
id,
|
id,
|
||||||
position,
|
position,
|
||||||
bounds,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,7 +55,8 @@ export class Actor {
|
||||||
}
|
}
|
||||||
|
|
||||||
const robo_info = {
|
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",
|
sprite: "./bot.png",
|
||||||
animations: [
|
animations: [
|
||||||
{ start: 0, length: 1, speed: 20 },
|
{ start: 0, length: 1, speed: 20 },
|
||||||
|
|
@ -106,8 +101,8 @@ export class Robo extends Actor {
|
||||||
const moving = vel.x != 0 || vel.y != 0;
|
const moving = vel.x != 0 || vel.y != 0;
|
||||||
const anim = robo_info.animations[moving ? 1 : 0];
|
const anim = robo_info.animations[moving ? 1 : 0];
|
||||||
|
|
||||||
const { x: w, y: h } = this.props.bounds;
|
const { x: w, y: h } = robo_info.bounds;
|
||||||
const { x, y } = this.props.position;
|
const { x, y } = vsub(this.props.position, robo_info.anchor);
|
||||||
|
|
||||||
const frame = (anim.start + ((clock / anim.speed) % anim.length)) >> 0;
|
const frame = (anim.start + ((clock / anim.speed) % anim.length)) >> 0;
|
||||||
spr(x, y, w, h, frame * w, 0, 32, 32);
|
spr(x, y, w, h, frame * w, 0, 32, 32);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue