2023-05-01 11:12:08 -07:00
|
|
|
import {
|
|
|
|
setPixelsInRect,
|
|
|
|
clearScreen,
|
2023-05-04 20:14:48 -07:00
|
|
|
fillRect,
|
2023-05-10 00:06:08 -07:00
|
|
|
cameraPos,
|
2023-05-01 11:12:08 -07:00
|
|
|
} from "./window.ts";
|
2023-05-08 21:39:08 -07:00
|
|
|
import { Font, font } from "./font.ts";
|
2023-05-06 17:18:49 -07:00
|
|
|
import { keyDown, keyPressed, keyReleased } from "./keyboard.ts";
|
2023-05-06 11:35:02 -07:00
|
|
|
import { addToContext, runCode } from "./runcode.ts";
|
2023-05-04 20:14:48 -07:00
|
|
|
import { resetRepl } from "./repl.ts";
|
2023-05-05 11:52:08 -07:00
|
|
|
import { COLOR } from "./colors.ts";
|
2023-05-09 23:50:27 -07:00
|
|
|
import { getSheet, getCodeSheet, getMapSheet } from "./sheet.ts";
|
2023-05-06 15:12:42 -07:00
|
|
|
import { saveCart, loadCart } from "./cart.ts";
|
2023-05-10 00:06:08 -07:00
|
|
|
import { outlineRect } from "./util.ts";
|
2023-05-01 11:12:08 -07:00
|
|
|
|
2023-05-06 14:49:46 -07:00
|
|
|
let spritesheet: number | null = null;
|
|
|
|
|
2023-05-09 23:50:27 -07:00
|
|
|
export const getSpritesheet = () => {
|
|
|
|
return spritesheet;
|
|
|
|
}
|
|
|
|
|
2023-05-06 14:49:46 -07:00
|
|
|
export const useSpritesheet = (sheet: number) => {
|
|
|
|
spritesheet = sheet;
|
|
|
|
}
|
|
|
|
|
2023-05-04 20:14:48 -07:00
|
|
|
export const drawSprite = (x: number, y: number, spr: number) => {
|
2023-05-06 14:49:46 -07:00
|
|
|
if (!spritesheet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const {sheet_type, value: sprites} = getSheet(spritesheet);
|
2023-05-05 16:02:23 -07:00
|
|
|
if (sheet_type !== "spritesheet") {
|
|
|
|
throw "Trying to run a non-code sheet as code."
|
|
|
|
}
|
2023-05-01 11:12:08 -07:00
|
|
|
setPixelsInRect(x, y, 8, sprites[spr]);
|
|
|
|
}
|
|
|
|
|
2023-05-05 16:39:51 -07:00
|
|
|
export const drawIcon = (x: number, y: number, icon: Array<number>, color: number) => {
|
|
|
|
setPixelsInRect(x, y, 8, icon.map(n => n*color));
|
|
|
|
}
|
|
|
|
|
2023-05-08 21:39:08 -07:00
|
|
|
export const measureCharFont = (char: string, fnt: Font) => {
|
|
|
|
return (fnt.chars[char]?.length ?? 0)/fnt.height;
|
2023-05-01 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
2023-05-08 21:39:08 -07:00
|
|
|
export const drawCharFont = (x: number, y: number, char: string, fnt: Font, color: number) => {
|
|
|
|
const w = measureCharFont(char, fnt);
|
|
|
|
if (!fnt.chars[char]) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
setPixelsInRect(x, y, w, fnt.chars[char].map(n => n*color));
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const drawTextFont = (x: number, y: number, text: string, fnt: Font, color?: number) => {
|
|
|
|
let dx = 0;
|
|
|
|
[...text].forEach((char) => {
|
|
|
|
dx += 1+drawCharFont(x+dx, y, char, fnt, color ?? COLOR.WHITE);
|
|
|
|
});
|
|
|
|
return dx-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const measureTextFont = (text: string, fnt: Font) => {
|
|
|
|
let w = 0;
|
|
|
|
[...text].forEach((char) => {
|
|
|
|
w += measureCharFont(char, fnt)+1;
|
2023-05-01 11:12:08 -07:00
|
|
|
});
|
2023-05-08 21:39:08 -07:00
|
|
|
return Math.max(0, w-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const drawText = (x: number, y: number, text: string, color?: number) => {
|
|
|
|
return drawTextFont(x, y, text, font, color);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const measureText = (text: string) => {
|
|
|
|
return measureTextFont(text, font);
|
2023-05-01 11:12:08 -07:00
|
|
|
}
|
|
|
|
|
2023-05-10 00:06:08 -07:00
|
|
|
export const camera = (x: number, y: number) => {
|
|
|
|
cameraPos.x = x;
|
|
|
|
cameraPos.y = y;
|
|
|
|
};
|
|
|
|
|
2023-05-01 18:42:55 -07:00
|
|
|
const faux = {
|
2023-05-09 23:06:09 -07:00
|
|
|
// Graphics
|
2023-05-04 20:14:48 -07:00
|
|
|
cls: () => {
|
|
|
|
resetRepl();
|
|
|
|
clearScreen();
|
|
|
|
},
|
2023-05-10 00:06:08 -07:00
|
|
|
camera,
|
2023-05-06 14:49:46 -07:00
|
|
|
sprsht: useSpritesheet,
|
2023-05-04 20:14:48 -07:00
|
|
|
spr: drawSprite,
|
|
|
|
txt: drawText,
|
2023-05-10 00:06:08 -07:00
|
|
|
rectfill: fillRect,
|
|
|
|
rect: outlineRect,
|
2023-05-09 23:50:27 -07:00
|
|
|
map: (mapSheet: number, tileX: number, tileY: number, screenX: number, screenY: number, tileW: number, tileH: number) => {
|
|
|
|
const originalSpritesheet = getSpritesheet() ?? 0;
|
|
|
|
getMapSheet(mapSheet).forEach(([sprSheet, spr], i) => {
|
|
|
|
const x = i%64;
|
|
|
|
const y = Math.floor(i/64);
|
|
|
|
if (x >= tileX && y >= tileY && x < tileX + tileW && y < tileY + tileH) {
|
|
|
|
useSpritesheet(sprSheet);
|
|
|
|
drawSprite(screenX + (x-tileX)*8, screenY + (y-tileY)*8, spr);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
useSpritesheet(originalSpritesheet);
|
|
|
|
},
|
|
|
|
// Map
|
|
|
|
mgetsht: (mapSheet: number, x: number, y: number) => {
|
|
|
|
if (x < 0 || x >= 64 || y < 0 || y >= 64) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return getMapSheet(mapSheet)[y*64+x][0];
|
|
|
|
},
|
|
|
|
mgetspr: (mapSheet: number, x: number, y: number) => {
|
|
|
|
if (x < 0 || x >= 64 || y < 0 || y >= 64) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return getMapSheet(mapSheet)[y*64+x][1];
|
|
|
|
},
|
|
|
|
mset: (mapSheet: number, x: number, y: number, sprSheet: number, spr: number) => {
|
|
|
|
if (x < 0 || x >= 64 || y < 0 || y >= 64) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
getMapSheet(mapSheet)[y*64+x] = [sprSheet, spr];
|
|
|
|
},
|
2023-05-09 23:06:09 -07:00
|
|
|
// Input
|
2023-05-06 17:18:49 -07:00
|
|
|
btn: keyDown,
|
|
|
|
btnp: keyPressed,
|
|
|
|
btnr: keyReleased,
|
2023-05-09 23:06:09 -07:00
|
|
|
// Cart
|
|
|
|
save: saveCart,
|
|
|
|
load: loadCart,
|
|
|
|
// JS
|
|
|
|
Array,
|
|
|
|
BigInt: BigInt,
|
|
|
|
Boolean,
|
|
|
|
Date,
|
|
|
|
Error,
|
|
|
|
Function,
|
|
|
|
Infinity: Infinity,
|
|
|
|
JSON: JSON,
|
|
|
|
Map,
|
|
|
|
NaN: NaN,
|
|
|
|
Number,
|
|
|
|
Object,
|
|
|
|
Promise,
|
|
|
|
Proxy,
|
|
|
|
Reflect: Reflect,
|
|
|
|
RegExp,
|
|
|
|
Set,
|
|
|
|
String,
|
|
|
|
Symbol: Symbol,
|
|
|
|
WeakMap,
|
|
|
|
WeakRef,
|
|
|
|
WeakSet,
|
|
|
|
isFinite,
|
|
|
|
isNaN,
|
|
|
|
// Math
|
|
|
|
max: Math.max,
|
|
|
|
min: Math.min,
|
|
|
|
floor: Math.floor,
|
|
|
|
ceil: Math.ceil,
|
|
|
|
sin: Math.sin,
|
|
|
|
cos: Math.cos,
|
|
|
|
atan2: Math.atan2,
|
|
|
|
sqrt: Math.sqrt,
|
|
|
|
abs: Math.abs,
|
|
|
|
rand: Math.random,
|
|
|
|
// Other
|
2023-05-06 11:35:02 -07:00
|
|
|
code: (n: number) => {
|
|
|
|
return runCode(getCodeSheet(n));
|
|
|
|
},
|
2023-05-03 15:17:27 -07:00
|
|
|
log: console.log,
|
2023-05-01 18:42:55 -07:00
|
|
|
};
|
|
|
|
|
2023-05-03 15:17:27 -07:00
|
|
|
for (const key in faux) {
|
|
|
|
addToContext(key, faux[key as keyof typeof faux]);
|
|
|
|
}
|
|
|
|
|
2023-05-01 18:42:55 -07:00
|
|
|
export default faux;
|