[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);
|
||||
}
|
||||
|
|
|
|||
23
game/main.ts
23
game/main.ts
|
|
@ -2,7 +2,7 @@ import { cls, print } from "./graphics";
|
|||
import { since_start } from "./time";
|
||||
import { new_v2 } from "./vector";
|
||||
import { load_world, World, Level, draw_level } from "./level";
|
||||
import { Actor, Robo } from "./actor";
|
||||
import { Actor, Robo, ActorSnapshot, actor_from_snapshot } from "./actor";
|
||||
|
||||
/// A nice looping frame counter.
|
||||
let clock = 0;
|
||||
|
|
@ -34,7 +34,7 @@ function load_assets() {
|
|||
});
|
||||
}
|
||||
|
||||
const actors: Actor[] = [];
|
||||
let actors: Actor[] = [];
|
||||
|
||||
// TODO: Build a system whereby the signatures of the fundamental functions can be checked.
|
||||
|
||||
|
|
@ -44,11 +44,24 @@ export function init() {
|
|||
actors.push(new Robo(new_v2(10, 10)));
|
||||
}
|
||||
|
||||
export function suspend() {
|
||||
return { clock };
|
||||
interface Snapshot {
|
||||
clock: number;
|
||||
actors: ActorSnapshot[];
|
||||
}
|
||||
|
||||
export function resume() {}
|
||||
export function suspend(): Snapshot {
|
||||
return {
|
||||
clock,
|
||||
actors: actors.map((a) => a.snapshot()),
|
||||
};
|
||||
}
|
||||
|
||||
export function resume(snapshot: Snapshot | undefined) {
|
||||
if (snapshot) {
|
||||
clock = snapshot.clock || 0;
|
||||
actors = snapshot.actors.map((a) => actor_from_snapshot(a));
|
||||
}
|
||||
}
|
||||
|
||||
export function update() {
|
||||
clock = (clock + 1) % 20160;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue