[game] Big actor refactor, spawn actors from world
This commit is contained in:
parent
4042cd28a4
commit
3af0bb4002
4 changed files with 281 additions and 107 deletions
100
game/actor.ts
100
game/actor.ts
|
|
@ -3,8 +3,7 @@ import { btn, Button } from "./input";
|
|||
import { Vec2, new_v2, vadd, vnorm, vmul } from "./vector";
|
||||
import { spr, use_texture, Texture } from "./graphics";
|
||||
|
||||
interface ActorSnapshot {
|
||||
__type__: string;
|
||||
export interface ActorProps {
|
||||
velocity: Vec2;
|
||||
friction: number;
|
||||
id: string;
|
||||
|
|
@ -12,23 +11,33 @@ interface ActorSnapshot {
|
|||
bounds: Vec2;
|
||||
}
|
||||
|
||||
export class Actor {
|
||||
velocity: Vec2 = new_v2(0);
|
||||
friction: number = 0.6;
|
||||
id: string;
|
||||
position: Vec2;
|
||||
bounds: Vec2;
|
||||
export function new_actor_props(
|
||||
id: string,
|
||||
position: Vec2,
|
||||
bounds: Vec2
|
||||
): ActorProps {
|
||||
return {
|
||||
velocity: new_v2(0),
|
||||
friction: 0.6,
|
||||
id,
|
||||
position,
|
||||
bounds,
|
||||
};
|
||||
}
|
||||
|
||||
constructor(id: string, position: Vec2, bounds: Vec2) {
|
||||
this.id = id;
|
||||
this.position = position;
|
||||
this.bounds = bounds;
|
||||
export class Actor {
|
||||
type: ActorType;
|
||||
props: ActorProps;
|
||||
|
||||
constructor(type: ActorType, props: ActorProps) {
|
||||
this.type = type;
|
||||
this.props = props;
|
||||
}
|
||||
|
||||
update() {}
|
||||
|
||||
update_physics() {
|
||||
const velocity = vmul(this.velocity, this.friction);
|
||||
const velocity = vmul(this.props.velocity, this.props.friction);
|
||||
// Zero if we're smaller than some epsilon.
|
||||
if (Math.abs(velocity.x) < 0.01) {
|
||||
velocity.x = 0;
|
||||
|
|
@ -37,36 +46,22 @@ export class Actor {
|
|||
velocity.y = 0;
|
||||
}
|
||||
|
||||
const new_position = vadd(this.position, velocity);
|
||||
const new_position = vadd(this.props.position, velocity);
|
||||
|
||||
// TODO: Collision detection
|
||||
// const { w, h } = this.bounds;
|
||||
|
||||
this.velocity = velocity;
|
||||
this.position = new_position;
|
||||
this.props.velocity = velocity;
|
||||
this.props.position = new_position;
|
||||
}
|
||||
|
||||
draw(_clock: number) {}
|
||||
|
||||
bonk(_other: Actor) {}
|
||||
|
||||
snapshot(): ActorSnapshot {
|
||||
return { ...this, __type__: "??" };
|
||||
}
|
||||
|
||||
assign_snapshot(s: ActorSnapshot) {
|
||||
this.id = s.id;
|
||||
this.position = s.position;
|
||||
this.bounds = s.bounds;
|
||||
this.velocity = s.velocity;
|
||||
this.friction = s.friction;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
const robo_info = {
|
||||
width: 32,
|
||||
height: 32,
|
||||
bounds: new_v2(32),
|
||||
sprite: "./bot.png",
|
||||
animations: [
|
||||
{ start: 0, length: 1, speed: 20 },
|
||||
|
|
@ -74,15 +69,11 @@ const robo_info = {
|
|||
],
|
||||
};
|
||||
|
||||
interface RoboSnapshot extends ActorSnapshot {
|
||||
__type__: "robo";
|
||||
}
|
||||
|
||||
export class Robo extends Actor {
|
||||
bot_sprite: Texture | undefined = undefined;
|
||||
|
||||
constructor(pos: Vec2) {
|
||||
super("robo", pos, new_v2(robo_info.width, robo_info.height));
|
||||
constructor(props: ActorProps) {
|
||||
super("robo", props);
|
||||
load_texture(robo_info.sprite).then((texture) => {
|
||||
this.bot_sprite = texture;
|
||||
});
|
||||
|
|
@ -104,43 +95,36 @@ export class Robo extends Actor {
|
|||
a.x += 1;
|
||||
}
|
||||
vnorm(a);
|
||||
this.velocity = vadd(this.velocity, vmul(a, 1.5));
|
||||
this.props.velocity = vadd(this.props.velocity, vmul(a, 1.5));
|
||||
}
|
||||
|
||||
draw(clock: number) {
|
||||
if (this.bot_sprite != undefined) {
|
||||
use_texture(this.bot_sprite);
|
||||
|
||||
const vel = this.velocity;
|
||||
const vel = this.props.velocity;
|
||||
const moving = vel.x != 0 || vel.y != 0;
|
||||
const anim = robo_info.animations[moving ? 1 : 0];
|
||||
|
||||
const { x: w, y: h } = this.bounds;
|
||||
const { x, y } = this.position;
|
||||
const { x: w, y: h } = this.props.bounds;
|
||||
const { x, y } = this.props.position;
|
||||
|
||||
const frame = (anim.start + ((clock / anim.speed) % anim.length)) >> 0;
|
||||
spr(x, y, w, h, frame * w, 0, 32, 32);
|
||||
}
|
||||
}
|
||||
|
||||
snapshot(): RoboSnapshot {
|
||||
return { ...super.snapshot(), __type__: "robo" };
|
||||
}
|
||||
|
||||
assign_snapshot(s: RoboSnapshot) {
|
||||
super.assign_snapshot(s);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
const SNAPSHOT_TABLE: { [key: string]: (s: any) => Actor } = {
|
||||
robo: (s: any) => new Robo(new_v2(0)).assign_snapshot(s),
|
||||
const ACTOR_TABLE = {
|
||||
robo: (s: ActorProps) => new Robo(s),
|
||||
};
|
||||
|
||||
export function actor_from_snapshot(s: ActorSnapshot): Actor {
|
||||
const f = SNAPSHOT_TABLE[s.__type__];
|
||||
if (f == undefined) {
|
||||
throw new Error("No handler for " + s.__type__);
|
||||
}
|
||||
return f(s);
|
||||
export type ActorType = keyof typeof ACTOR_TABLE;
|
||||
|
||||
export function is_actor_type(type: string): type is ActorType {
|
||||
return ACTOR_TABLE.hasOwnProperty(type);
|
||||
}
|
||||
|
||||
export function spawn_actor(type: ActorType, props: ActorProps): Actor {
|
||||
return ACTOR_TABLE[type](props);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue