[game] Save and restore actors
This commit is contained in:
parent
efd6884d0b
commit
4042cd28a4
2 changed files with 65 additions and 5 deletions
|
|
@ -3,6 +3,15 @@ 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;
|
||||
velocity: Vec2;
|
||||
friction: number;
|
||||
id: string;
|
||||
position: Vec2;
|
||||
bounds: Vec2;
|
||||
}
|
||||
|
||||
export class Actor {
|
||||
velocity: Vec2 = new_v2(0);
|
||||
friction: number = 0.6;
|
||||
|
|
@ -40,6 +49,19 @@ export class Actor {
|
|||
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 = {
|
||||
|
|
@ -52,6 +74,10 @@ const robo_info = {
|
|||
],
|
||||
};
|
||||
|
||||
interface RoboSnapshot extends ActorSnapshot {
|
||||
__type__: "robo";
|
||||
}
|
||||
|
||||
export class Robo extends Actor {
|
||||
bot_sprite: Texture | undefined = undefined;
|
||||
|
||||
|
|
@ -96,4 +122,25 @@ export class Robo extends Actor {
|
|||
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),
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue