fantasy-console/sheet.ts
2023-05-05 14:59:52 -07:00

31 lines
793 B
TypeScript

import { getCart } from "./cart.ts";
import { runCode, addToContext } from "./runcode.ts";
// "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts"
export type Sheet = {
sheet_type: "code",
value: string,
} | {
sheet_type: "spritesheet",
value: Array<Array<number>>,
}
export type SheetType = Sheet["sheet_type"];
export const getSheet = (n: number) => {
return getCart()[n];
}
// deno-lint-ignore no-explicit-any
export const setSheet = (n: number, type: SheetType, value: any) => {
return getCart()[n] = {sheet_type: type, value};
}
export const codeSheet = (sheet: number) => {
const {sheet_type, value} = getSheet(sheet);
if (sheet_type !== "code") {
throw "Trying to run a non-code sheet as code."
}
return runCode(value);
}
addToContext("code_sheet", codeSheet);