[game] Big actor refactor, spawn actors from world

This commit is contained in:
John Doty 2023-08-20 08:25:51 -07:00
parent 4042cd28a4
commit 3af0bb4002
4 changed files with 281 additions and 107 deletions

View file

@ -2,13 +2,21 @@ 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, ActorSnapshot, actor_from_snapshot } from "./actor";
import {
Actor,
ActorProps,
ActorType,
new_actor_props,
is_actor_type,
spawn_actor,
} from "./actor";
/// A nice looping frame counter.
let clock = 0;
let world: World | undefined = undefined;
let level: Level | undefined = undefined;
let actors: Actor[] = [];
// Note zelda overworld is 16x8 screens
// zelda screen is 16x11 tiles
@ -27,6 +35,19 @@ function load_assets() {
}
// TODO: SPAWN ACTORS BASED ON LEVEL.
actors.length = 0;
for (const entity_layer of level.entity_layers) {
for (const entity of entity_layer.entities) {
if (is_actor_type(entity.type)) {
const [x, y] = entity.px;
const [w, h] = entity.bounds;
const props = new_actor_props(entity.id, new_v2(x, y), new_v2(w, h));
actors.push(spawn_actor(entity.type, props));
} else {
print("WARNING: Ignoring entity of type", entity.type);
}
}
}
});
Promise.all([map_load]).then(() => {
@ -34,32 +55,31 @@ function load_assets() {
});
}
let actors: Actor[] = [];
// TODO: Build a system whereby the signatures of the fundamental functions can be checked.
export function init() {
print("Hello world!");
load_assets();
actors.push(new Robo(new_v2(10, 10)));
}
interface Snapshot {
clock: number;
actors: ActorSnapshot[];
actors: { type: ActorType; props: ActorProps }[];
}
export function suspend(): Snapshot {
return {
clock,
actors: actors.map((a) => a.snapshot()),
actors: actors.map((a) => {
return { type: a.type, props: a.props };
}),
};
}
export function resume(snapshot: Snapshot | undefined) {
if (snapshot) {
clock = snapshot.clock || 0;
actors = snapshot.actors.map((a) => actor_from_snapshot(a));
actors = snapshot.actors.map((s) => spawn_actor(s.type, s.props));
}
}