fantasy-console/builtins.ts
2023-05-05 11:52:08 -07:00

79 lines
1.7 KiB
TypeScript

import {
setPixelsInRect,
clearScreen,
fillRect,
} from "./window.ts";
import { font } from "./font.ts";
// import { keyDown, keyPressed, keyReleased } from "./keyboard.ts";
import { addToContext } from "./runcode.ts";
import { resetRepl } from "./repl.ts";
import { COLOR } from "./colors.ts";
// deno-fmt-ignore
const sprites = [
[
2, 2, 2, 2, 2, 2, 2, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 2, 2, 2, 2, 2, 2, 2,
],
[
2, 2, 2, 2, 2, 2, 2, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 1, 1, 1, 1, 1, 1, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 3, 3, 1, 1, 3, 3, 2,
2, 2, 2, 2, 2, 2, 2, 2,
],
[
2, 2, 2, 2, 2, 2, 2, 2,
2, 4, 4, 4, 4, 5, 5, 2,
2, 4, 4, 4, 5, 5, 5, 2,
2, 4, 4, 5, 5, 5, 6, 2,
2, 4, 5, 5, 5, 6, 6, 2,
2, 5, 5, 5, 6, 6, 6, 2,
2, 5, 5, 6, 6, 6, 6, 2,
2, 2, 2, 2, 2, 2, 2, 2,
],
]
export const drawSprite = (x: number, y: number, spr: number) => {
setPixelsInRect(x, y, 8, sprites[spr]);
}
export const drawChar = (x: number, y: number, char: string, color: number) => {
setPixelsInRect(x, y, 4, font[char].map(n => n*color));
}
export const drawText = (x: number, y: number, text: string, color?: number) => {
[...text].forEach((char, i) => {
drawChar(x+4*i, y, char, color ?? COLOR.WHITE);
});
}
const faux = {
cls: () => {
resetRepl();
clearScreen();
},
spr: drawSprite,
txt: drawText,
rect: fillRect,
// key_down: keyDown,
// key_pressed: keyPressed,
// key_released: keyReleased,
log: console.log,
JSON: JSON,
};
for (const key in faux) {
addToContext(key, faux[key as keyof typeof faux]);
}
export default faux;