some more cart infrastructure

This commit is contained in:
dylan 2023-05-02 17:06:54 -07:00
parent a7b675d541
commit fdc8f97aee
7 changed files with 61 additions and 57 deletions

View File

@ -3,6 +3,7 @@ import {
clearScreen,
} from "./window.ts";
import { font } from "./font.ts";
// import { codeSheet } from "./sheet.ts";
// deno-fmt-ignore
const sprites = [
@ -53,9 +54,10 @@ const drawText = (x: number, y: number, text: string) => {
}
const faux = {
clearScreen,
drawSprite,
drawText,
clear_screen: clearScreen,
draw_sprite: drawSprite,
draw_text: drawText,
// code_sheet: codeSheet,
};
export default faux;

11
cart.ts Normal file
View File

@ -0,0 +1,11 @@
import fakeCart from "./cart_unpacked.json" assert { type: "json" };
const cart = fakeCart;
export const loadCart = (_name: string) => {
return;
}
export const getCart = () => {
return cart;
}

View File

@ -1,31 +0,0 @@
import faux from "./builtins.ts";
export type SheetType = "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts";
export const parseCodeSheet = (sheet: string) => {
try {
new Function(sheet);
} catch (err) {
throw err;
}
// deno-lint-ignore no-explicit-any
const G: any = {...faux, faux, log: console.log};
const context = new Proxy(G, {
get: (target, prop) => {
return target[prop];
},
set: (target, prop, value) => {
target[prop] = value;
return true;
},
has: () => {
return true;
},
});
const fn = new Function("context", `
with (context) {
${sheet}
}
`);
return fn(context);
}

View File

@ -1,6 +1,10 @@
[
{
"sheet_type": "code",
"value": "return {init: () => {}, update: () => {}, draw: () => {clearScreen(); drawText(0, 0, 'hello world')}}"
"value": "x = code_sheet(1);\nreturn {init: () => {y = 0}, update: () => {y += speed; if (y > 127) {y = -6}}, draw: () => {clear_screen(); draw_text(x, y, 'hello world')}}"
},
{
"sheet_type": "code",
"value": "speed = 2; return 8"
}
]

19
game.ts
View File

@ -1,19 +0,0 @@
import faux from "./builtins.ts";
let y = 0;
export default {
init() {
},
update() {
y++;
if (y>127) {
y=-5;
}
},
draw() {
faux.clearScreen();
faux.drawText(0, y, "hello, world");
},
}

View File

@ -2,10 +2,9 @@ import {
mainloop,
frame,
} from "./window.ts";
import cart from "./cart_unpacked.json" assert { type: "json" };
import { parseCodeSheet } from "./cart_tools.ts";
import { codeSheet } from "./sheet.ts";
const game = parseCodeSheet(cart[0].value);
const game = codeSheet(0);
game.init();

38
sheet.ts Normal file
View File

@ -0,0 +1,38 @@
import faux from "./builtins.ts";
import { getCart } from "./cart.ts";
export type SheetType = "code" | "spritesheet" | "map" | "sfx" | "patterns" | "fonts";
const getSheet = (n: number) => {
return getCart()[n].value;
}
export const codeSheet = (sheet: number) => {
const code = getSheet(sheet);
try {
new Function(code);
} catch (err) {
throw err;
}
const fn = new Function("context", `
with (context) {
${code}
}
`);
return fn(context);
}
// deno-lint-ignore no-explicit-any
const G: any = {...faux, faux, log: console.log, code_sheet: codeSheet};
const context = new Proxy(G, {
get: (target, prop) => {
return target[prop];
},
set: (target, prop, value) => {
target[prop] = value;
return true;
},
has: () => {
return true;
},
});