From fdc8f97aeec655ad4b4b9ec866f85abcff3ca453 Mon Sep 17 00:00:00 2001 From: dylan <> Date: Tue, 2 May 2023 17:06:54 -0700 Subject: [PATCH] some more cart infrastructure --- builtins.ts | 8 +++++--- cart.ts | 11 +++++++++++ cart_tools.ts | 31 ------------------------------- cart_unpacked.json | 6 +++++- game.ts | 19 ------------------- index.ts | 5 ++--- sheet.ts | 38 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 57 deletions(-) create mode 100644 cart.ts delete mode 100644 cart_tools.ts delete mode 100644 game.ts create mode 100644 sheet.ts diff --git a/builtins.ts b/builtins.ts index 27399a5..1384160 100644 --- a/builtins.ts +++ b/builtins.ts @@ -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; \ No newline at end of file diff --git a/cart.ts b/cart.ts new file mode 100644 index 0000000..29adc11 --- /dev/null +++ b/cart.ts @@ -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; +} \ No newline at end of file diff --git a/cart_tools.ts b/cart_tools.ts deleted file mode 100644 index e3370ae..0000000 --- a/cart_tools.ts +++ /dev/null @@ -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); -} \ No newline at end of file diff --git a/cart_unpacked.json b/cart_unpacked.json index f2ef3e8..ad30e81 100644 --- a/cart_unpacked.json +++ b/cart_unpacked.json @@ -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" } ] \ No newline at end of file diff --git a/game.ts b/game.ts deleted file mode 100644 index 744c60c..0000000 --- a/game.ts +++ /dev/null @@ -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"); - }, -} \ No newline at end of file diff --git a/index.ts b/index.ts index 60445a5..1b107fc 100644 --- a/index.ts +++ b/index.ts @@ -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(); diff --git a/sheet.ts b/sheet.ts new file mode 100644 index 0000000..f683e30 --- /dev/null +++ b/sheet.ts @@ -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; + }, +}); \ No newline at end of file