oden/game/main.ts

80 lines
1.7 KiB
TypeScript

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";
/// A nice looping frame counter.
let clock = 0;
let world: World | undefined = undefined;
let level: Level | undefined = undefined;
// Note zelda overworld is 16x8 screens
// zelda screen is 16x11 tiles
// from a feeling point of view this is sufficient, apparently :D
function load_assets() {
// Start this load, but then...
let map_load = load_world("./overworld.ldtk").then((w) => {
print("World loaded at", since_start());
world = w;
// Assume we start at 0,0
level = world.levels.find((l) => l.world_x == 0 && l.world_y == 0);
if (!level) {
throw new Error("UNABLE TO FIND LEVEL AT 0,0: CANNOT START");
}
// TODO: SPAWN ACTORS BASED ON LEVEL.
});
Promise.all([map_load]).then(() => {
print("All are loaded.");
});
}
const 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)));
}
export function suspend() {
return { clock };
}
export function resume() {}
export function update() {
clock = (clock + 1) % 20160;
for (const actor of actors) {
actor.update();
}
for (const actor of actors) {
actor.update_physics();
}
// TODO: Bonks
// TODO: Transients
}
export function draw() {
cls(0.1, 0.2, 0.3);
if (level != undefined) {
draw_level(level, 0, 0);
}
for (const actor of actors) {
actor.draw(clock);
}
// print("FRAME TIME:", since_last_frame());
}