fantasy-console/index.ts

65 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-04-28 20:01:48 -07:00
import {
mainloop,
2023-04-29 15:16:35 -07:00
frame,
2023-05-03 15:17:27 -07:00
clearScreen,
2023-04-29 15:16:35 -07:00
} from "./window.ts";
2023-05-06 11:35:02 -07:00
import { runCode } from "./runcode.ts";
import { getCodeSheet } from "./sheet.ts";
2023-05-03 16:47:09 -07:00
import { refreshKeyboard, keyPressed, K } from "./keyboard.ts";
import { repl, resetRepl } from "./repl.ts";
import { addToContext } from "./runcode.ts";
import { editmode } from "./editmode.ts";
2023-05-05 16:02:23 -07:00
import { refreshMouse } from "./mouse.ts";
2023-05-10 00:06:08 -07:00
import { camera } from "./builtins.ts";
2023-05-01 18:42:55 -07:00
2023-05-05 12:21:14 -07:00
// deno-lint-ignore no-explicit-any
let game: any = null;
2023-04-29 20:07:06 -07:00
2023-05-03 16:47:09 -07:00
let mode: "play" | "edit" | "repl" = "repl";
2023-05-05 12:21:14 -07:00
addToContext("play", () => {
mode = "play";
2023-05-06 11:35:02 -07:00
game = runCode(getCodeSheet(0));
2023-05-05 12:21:14 -07:00
game.init();
});
2023-05-03 15:17:27 -07:00
clearScreen();
2023-05-02 18:17:31 -07:00
2023-05-06 10:54:27 -07:00
await mainloop(async (_t) => {
2023-05-01 11:12:08 -07:00
// TODO: use t
2023-05-03 16:47:09 -07:00
if (keyPressed(K.ESCAPE)) {
const modeTo = ({
play: "repl",
edit: "repl",
repl: "edit",
} as const)[mode];
2023-05-03 16:47:09 -07:00
if (mode === "play") {
resetRepl();
}
if (mode === "edit") {
clearScreen();
}
mode = modeTo;
2023-05-03 16:47:09 -07:00
} else {
if (mode === "play") {
2023-05-05 12:21:14 -07:00
if (game) {
game.update();
game.draw();
}
2023-05-03 16:47:09 -07:00
frame();
} else if (mode === "repl") {
repl.update();
2023-05-10 00:06:08 -07:00
camera(0, 0);
2023-05-03 16:47:09 -07:00
repl.draw();
frame();
} else if (mode === "edit") {
2023-05-06 10:54:27 -07:00
await editmode.update();
2023-05-10 00:06:08 -07:00
camera(0, 0);
editmode.draw();
2023-05-03 16:47:09 -07:00
frame();
}
2023-05-02 18:17:31 -07:00
}
refreshKeyboard();
2023-05-05 16:02:23 -07:00
refreshMouse();
2023-05-06 10:54:27 -07:00
}, false);