fantasy-console/sheet.ts

39 lines
1003 B
TypeScript
Raw Normal View History

2023-05-02 17:06:54 -07:00
import { getCart } from "./cart.ts";
2023-05-06 11:35:02 -07:00
// import { runCode, addToContext } from "./runcode.ts";
2023-05-02 17:06:54 -07:00
2023-05-05 14:59:52 -07:00
// "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"];
2023-05-02 17:06:54 -07:00
2023-05-05 11:52:08 -07:00
export const getSheet = (n: number) => {
2023-05-05 14:59:52 -07:00
return getCart()[n];
2023-05-02 17:06:54 -07:00
}
2023-05-05 11:52:08 -07:00
// deno-lint-ignore no-explicit-any
export const setSheet = (n: number, type: SheetType, value: any) => {
return getCart()[n] = {sheet_type: type, value};
}
2023-05-06 11:35:02 -07:00
export const getCodeSheet = (sheet: number) => {
2023-05-05 14:59:52 -07:00
const {sheet_type, value} = getSheet(sheet);
if (sheet_type !== "code") {
2023-05-06 11:35:02 -07:00
throw "Trying to use a non-code sheet as code."
2023-05-05 14:59:52 -07:00
}
2023-05-06 11:35:02 -07:00
return value;
2023-05-02 17:06:54 -07:00
}
2023-05-06 11:35:02 -07:00
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);