[game] Save and restore actors

This commit is contained in:
John Doty 2023-08-19 22:47:13 -07:00
parent efd6884d0b
commit 4042cd28a4
2 changed files with 65 additions and 5 deletions

View file

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