fantasy-console/sheet.ts
2023-05-06 14:49:46 -07:00

42 lines
1.0 KiB
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>>,
} | {
sheet_type: "none",
value: null,
}
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 getCodeSheet = (sheet: number) => {
const {sheet_type, value} = getSheet(sheet);
if (sheet_type !== "code") {
throw "Trying to use a non-code sheet as code."
}
return value;
}
export const getSpriteSheet = (sheet: number) => {
const {sheet_type, value} = getSheet(sheet);
if (sheet_type !== "spritesheet") {
throw "Trying to use a non-sprite sheet as a spritesheet."
}
return value;
}
// addToContext("code", codeSheet);