fantasy-console/index.ts

52 lines
942 B
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-02 17:06:54 -07:00
import { codeSheet } 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";
2023-05-01 18:42:55 -07:00
2023-05-02 17:06:54 -07:00
const game = codeSheet(0);
2023-04-29 15:16:35 -07:00
2023-05-01 11:12:08 -07:00
game.init();
2023-04-29 20:07:06 -07:00
2023-05-03 16:47:09 -07:00
let mode: "play" | "edit" | "repl" = "repl";
addToContext("play", () => {mode = "play"});
2023-05-03 15:17:27 -07:00
clearScreen();
2023-05-02 18:17:31 -07:00
2023-05-01 18:42:55 -07:00
await mainloop((_t) => {
2023-05-01 11:12:08 -07:00
// TODO: use t
2023-05-03 16:47:09 -07:00
if (keyPressed(K.ESCAPE)) {
console.log('pressed escape');
if (mode === "play") {
resetRepl();
}
if (mode === "edit") {
clearScreen();
}
mode = ({
play: "repl",
edit: "repl",
repl: "edit",
} as const)[mode];
} else {
if (mode === "play") {
game.update();
game.draw();
frame();
} else if (mode === "repl") {
repl.update();
repl.draw();
frame();
} else if (mode === "edit") {
clearScreen();
frame();
}
2023-05-02 18:17:31 -07:00
}
refreshKeyboard();
2023-04-29 14:34:26 -07:00
});