[oden][game] Multiple screens, logging, pre/post, bluescreen
Better blue screens and also logging and whatnot
This commit is contained in:
parent
95d626c15f
commit
93d4e3eb91
8 changed files with 1201 additions and 244 deletions
|
|
@ -2,7 +2,8 @@ import { load_texture } from "./assets";
|
|||
import { btn, Button } from "./input";
|
||||
import { Vec2, new_v2, vadd, vsub, vnorm, vmul } from "./vector";
|
||||
import { color, stroke, circle, spr, use_texture, Texture } from "./graphics";
|
||||
import { has_collision, Level } from "./level";
|
||||
import { has_collision, Level, World } from "./level";
|
||||
import { log } from "./log";
|
||||
|
||||
export interface ActorProps {
|
||||
id: string;
|
||||
|
|
@ -50,7 +51,15 @@ export class Actor {
|
|||
this.props = props;
|
||||
}
|
||||
|
||||
update() {}
|
||||
pre_update(_world: World) {}
|
||||
|
||||
post_update(_world: World) {}
|
||||
|
||||
update(world: World) {
|
||||
this.pre_update(world);
|
||||
this.update_physics(world.current_level);
|
||||
this.post_update(world);
|
||||
}
|
||||
|
||||
// IDEAS: Make gameplay logic at 30fps and render updates at 60fps? Does
|
||||
// this mean we do some "update" in render?
|
||||
|
|
@ -159,7 +168,7 @@ export class Robo extends Actor {
|
|||
});
|
||||
}
|
||||
|
||||
update() {
|
||||
pre_update() {
|
||||
// Acceleration from input
|
||||
let a = new_v2(0);
|
||||
if (btn(Button.Up)) {
|
||||
|
|
@ -178,6 +187,50 @@ export class Robo extends Actor {
|
|||
this.props.velocity = vadd(this.props.velocity, vmul(a, 0.07));
|
||||
}
|
||||
|
||||
post_update(world: World) {
|
||||
const level = world.current_level;
|
||||
const props = this.props;
|
||||
const w = level.cw - 1;
|
||||
const h = level.ch - 1;
|
||||
|
||||
log("robo position", "cx", props.cx, "cy", props.cy, "w", w, "h", h);
|
||||
let next_level = null;
|
||||
if (props.cx > w || (props.cx == w && props.xr > 0.8)) {
|
||||
const iid = level.neighbors.e;
|
||||
next_level = iid && world.level_map.get(iid);
|
||||
if (next_level) {
|
||||
props.cx = 0;
|
||||
props.xr = 0.2;
|
||||
}
|
||||
} else if (props.cy > h || (props.cy == h && props.yr > 0.8)) {
|
||||
const iid = level.neighbors.s;
|
||||
next_level = iid && world.level_map.get(iid);
|
||||
if (next_level) {
|
||||
props.cy = 0;
|
||||
props.yr = 0.2;
|
||||
}
|
||||
} else if (props.cx < 0 || (props.cx == 0 && props.xr < 0.2)) {
|
||||
const iid = level.neighbors.w;
|
||||
next_level = iid && world.level_map.get(iid);
|
||||
if (next_level) {
|
||||
props.cx = next_level.cw - 1;
|
||||
props.xr = 0.8;
|
||||
}
|
||||
} else if (props.cy < 0 || (props.cy == 0 && props.yr < 0.2)) {
|
||||
const iid = level.neighbors.n;
|
||||
next_level = iid && world.level_map.get(iid);
|
||||
if (next_level) {
|
||||
props.cy = next_level.ch - 1;
|
||||
props.yr = 0.8;
|
||||
}
|
||||
}
|
||||
|
||||
if (next_level) {
|
||||
log("vwoop", next_level.iid);
|
||||
world.current_level = next_level; // Yikes.
|
||||
}
|
||||
}
|
||||
|
||||
draw(clock: number) {
|
||||
if (this.bot_sprite != undefined) {
|
||||
use_texture(this.bot_sprite);
|
||||
|
|
|
|||
|
|
@ -2,8 +2,9 @@
|
|||
// javascript. Dealing with the level code is really nice in javascript
|
||||
// but there's a bunch of weird stuff that really feels lower level.
|
||||
import { load_texture } from "./assets";
|
||||
import { Texture, print, spr, use_texture } from "./graphics";
|
||||
import { Texture, spr, use_texture } from "./graphics";
|
||||
import { load_string } from "./io";
|
||||
import { log } from "./log";
|
||||
|
||||
// TODO: Use io-ts? YIKES.
|
||||
|
||||
|
|
@ -43,6 +44,7 @@ export interface EntityLayer {
|
|||
}
|
||||
|
||||
export interface Level {
|
||||
iid: string;
|
||||
world_x: number;
|
||||
world_y: number;
|
||||
width: number;
|
||||
|
|
@ -52,11 +54,19 @@ export interface Level {
|
|||
tile_layers: TileLayer[];
|
||||
entities: Entity[];
|
||||
values: { [key: string]: number[] };
|
||||
neighbors: {
|
||||
n: string | null;
|
||||
s: string | null;
|
||||
e: string | null;
|
||||
w: string | null;
|
||||
};
|
||||
}
|
||||
|
||||
export interface World {
|
||||
levels: Level[];
|
||||
level_map: Map<string, Level>;
|
||||
tilesets: Map<number, TileSet>;
|
||||
current_level: Level;
|
||||
}
|
||||
|
||||
interface LDTKTilesetDef {
|
||||
|
|
@ -78,7 +88,15 @@ async function load_tileset(def: LDTKTilesetDef): Promise<TileSet> {
|
|||
tags.set(et.enumValueId, et.tileIds);
|
||||
}
|
||||
|
||||
print("Loaded tileset", def.uid, "from", relPath, "as ID", texture.id());
|
||||
log(
|
||||
"map_load",
|
||||
"Loaded tileset",
|
||||
def.uid,
|
||||
"from",
|
||||
relPath,
|
||||
"as ID",
|
||||
texture.id()
|
||||
);
|
||||
return { id: def.uid, texture, tags };
|
||||
}
|
||||
|
||||
|
|
@ -179,15 +197,18 @@ function is_intgrid_layer_instance(
|
|||
}
|
||||
|
||||
type LDTKLevel = {
|
||||
iid: string;
|
||||
worldX: number;
|
||||
worldY: number;
|
||||
pxWid: number;
|
||||
pxHei: number;
|
||||
layerInstances: LDTKLayerInstance[];
|
||||
__neighbours: { levelIid: string; dir: "n" | "s" | "e" | "w" }[];
|
||||
};
|
||||
|
||||
function load_level(tile_sets: Map<number, TileSet>, def: LDTKLevel): Level {
|
||||
const result: Level = {
|
||||
iid: def.iid,
|
||||
world_x: def.worldX,
|
||||
world_y: def.worldY,
|
||||
width: def.pxWid,
|
||||
|
|
@ -197,6 +218,7 @@ function load_level(tile_sets: Map<number, TileSet>, def: LDTKLevel): Level {
|
|||
tile_layers: [],
|
||||
entities: [],
|
||||
values: {},
|
||||
neighbors: { n: null, s: null, e: null, w: null },
|
||||
};
|
||||
|
||||
for (const li of def.layerInstances) {
|
||||
|
|
@ -220,6 +242,18 @@ function load_level(tile_sets: Map<number, TileSet>, def: LDTKLevel): Level {
|
|||
}
|
||||
|
||||
result.tile_layers = result.tile_layers.reverse();
|
||||
const neighbors = result.neighbors;
|
||||
for (const n of def.__neighbours) {
|
||||
if (n.dir == "n") {
|
||||
neighbors.n = n.levelIid;
|
||||
} else if (n.dir == "s") {
|
||||
neighbors.s = n.levelIid;
|
||||
} else if (n.dir == "e") {
|
||||
neighbors.e = n.levelIid;
|
||||
} else if (n.dir == "w") {
|
||||
neighbors.w = n.levelIid;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -255,7 +289,7 @@ function is_ldtk_map(map: unknown): map is LDTKMap {
|
|||
}
|
||||
|
||||
export async function load_world(path: string): Promise<World> {
|
||||
print("Loading map:", path);
|
||||
log("map_load", "Loading map:", path);
|
||||
const blob = await load_string(path);
|
||||
const map = JSON.parse(blob);
|
||||
|
||||
|
|
@ -274,7 +308,17 @@ export async function load_world(path: string): Promise<World> {
|
|||
}
|
||||
|
||||
const levels = map.levels.map((l: any) => load_level(tilesets, l));
|
||||
return { levels, tilesets };
|
||||
const level_map = new Map();
|
||||
for (const level of levels) {
|
||||
level_map.set(level.iid, level);
|
||||
}
|
||||
|
||||
const current_level = levels.find((l) => l.world_x == 0 && l.world_y == 0);
|
||||
if (!current_level) {
|
||||
throw new Error("UNABLE TO FIND LEVEL AT 0,0: CANNOT START");
|
||||
}
|
||||
|
||||
return { levels, level_map, tilesets, current_level };
|
||||
}
|
||||
|
||||
export function has_collision(level: Level, cx: number, cy: number): boolean {
|
||||
|
|
|
|||
32
game/log.ts
32
game/log.ts
|
|
@ -1,17 +1,35 @@
|
|||
import { color, print } from "./graphics";
|
||||
|
||||
const lines: string[] = [];
|
||||
interface LogEntry {
|
||||
frame_age: number;
|
||||
line: string;
|
||||
}
|
||||
|
||||
export function log(...args: unknown[]) {
|
||||
// const line = args.join(" ");
|
||||
lines.push(args.join(" "));
|
||||
const lines: Map<string, LogEntry> = new Map();
|
||||
|
||||
export function log(key: string, ...args: unknown[]) {
|
||||
const entry = {
|
||||
frame_age: 60,
|
||||
line: key + " " + args.join(" "),
|
||||
};
|
||||
lines.set(key, entry);
|
||||
}
|
||||
|
||||
export function draw_log() {
|
||||
color(1, 1, 1, 1);
|
||||
let line_y = 3;
|
||||
for (const line of lines) {
|
||||
print(3, line_y, line);
|
||||
line_y += 8;
|
||||
|
||||
const keys = [...lines.keys()].sort();
|
||||
for (const key of keys) {
|
||||
const entry = lines.get(key);
|
||||
if (entry) {
|
||||
// Life is too short for me to convince typescript.
|
||||
print(3, line_y, entry.line);
|
||||
line_y += 8;
|
||||
entry.frame_age -= 1;
|
||||
if (entry.frame_age <= 0) {
|
||||
lines.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
game/main.ts
26
game/main.ts
|
|
@ -1,7 +1,7 @@
|
|||
import { cls, get_dimensions, scale } from "./graphics";
|
||||
import { since_start } from "./time";
|
||||
import { new_v2 } from "./vector";
|
||||
import { load_world, World, Level, draw_level } from "./level";
|
||||
import { load_world, World, draw_level } from "./level";
|
||||
import {
|
||||
Actor,
|
||||
ActorProps,
|
||||
|
|
@ -16,7 +16,6 @@ import { log, draw_log } from "./log";
|
|||
let clock = 0;
|
||||
|
||||
let world: World | undefined = undefined;
|
||||
let level: Level | undefined = undefined;
|
||||
let actors: Actor[] = [];
|
||||
|
||||
// Note zelda overworld is 16x8 screens
|
||||
|
|
@ -26,14 +25,11 @@ let actors: Actor[] = [];
|
|||
function start_load_assets() {
|
||||
// Start this load, but then...
|
||||
load_world("./overworld.ldtk").then((w) => {
|
||||
log("World loaded at", since_start());
|
||||
log("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");
|
||||
}
|
||||
const level = world.current_level;
|
||||
|
||||
// TODO: SPAWN ACTORS BASED ON LEVEL.
|
||||
actors.length = 0;
|
||||
|
|
@ -63,7 +59,7 @@ interface Snapshot {
|
|||
}
|
||||
|
||||
export function suspend(): Snapshot {
|
||||
log("Suspend! ", actors.length, "actors");
|
||||
log("suspend_status", "Suspend ", actors.length, "actors");
|
||||
return {
|
||||
clock,
|
||||
actors: actors.map((a) => {
|
||||
|
|
@ -76,23 +72,19 @@ export function resume(snapshot: Snapshot | undefined) {
|
|||
if (snapshot) {
|
||||
clock = snapshot.clock || 0;
|
||||
actors = snapshot.actors.map((s) => spawn_actor(s.type, s.props));
|
||||
log("Resume! ", actors.length, "actors");
|
||||
log("resume_status", "Resume ", actors.length, "actors");
|
||||
}
|
||||
}
|
||||
|
||||
export function update() {
|
||||
if (!level) {
|
||||
if (!world) {
|
||||
return;
|
||||
}
|
||||
|
||||
clock = (clock + 1) % 20160;
|
||||
|
||||
for (const actor of actors) {
|
||||
actor.update();
|
||||
}
|
||||
|
||||
for (const actor of actors) {
|
||||
actor.update_physics(level);
|
||||
actor.update(world);
|
||||
}
|
||||
|
||||
// TODO: Bonks
|
||||
|
|
@ -109,8 +101,8 @@ export function draw() {
|
|||
);
|
||||
scale(s);
|
||||
|
||||
if (level != undefined) {
|
||||
draw_level(level, 0, 0);
|
||||
if (world != undefined) {
|
||||
draw_level(world.current_level, 0, 0);
|
||||
}
|
||||
|
||||
for (const actor of actors) {
|
||||
|
|
|
|||
Binary file not shown.
1226
game/overworld.ldtk
1226
game/overworld.ldtk
File diff suppressed because it is too large
Load diff
Binary file not shown.
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
Loading…
Add table
Add a link
Reference in a new issue