some more cart infrastructure
This commit is contained in:
parent
a7b675d541
commit
fdc8f97aee
@ -3,6 +3,7 @@ import {
|
|||||||
clearScreen,
|
clearScreen,
|
||||||
} from "./window.ts";
|
} from "./window.ts";
|
||||||
import { font } from "./font.ts";
|
import { font } from "./font.ts";
|
||||||
|
// import { codeSheet } from "./sheet.ts";
|
||||||
|
|
||||||
// deno-fmt-ignore
|
// deno-fmt-ignore
|
||||||
const sprites = [
|
const sprites = [
|
||||||
@ -53,9 +54,10 @@ const drawText = (x: number, y: number, text: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const faux = {
|
const faux = {
|
||||||
clearScreen,
|
clear_screen: clearScreen,
|
||||||
drawSprite,
|
draw_sprite: drawSprite,
|
||||||
drawText,
|
draw_text: drawText,
|
||||||
|
// code_sheet: codeSheet,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default faux;
|
export default faux;
|
11
cart.ts
Normal file
11
cart.ts
Normal 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;
|
||||||
|
}
|
@ -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);
|
|
||||||
}
|
|
@ -1,6 +1,10 @@
|
|||||||
[
|
[
|
||||||
{
|
{
|
||||||
"sheet_type": "code",
|
"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
19
game.ts
@ -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");
|
|
||||||
},
|
|
||||||
}
|
|
5
index.ts
5
index.ts
@ -2,10 +2,9 @@ import {
|
|||||||
mainloop,
|
mainloop,
|
||||||
frame,
|
frame,
|
||||||
} from "./window.ts";
|
} from "./window.ts";
|
||||||
import cart from "./cart_unpacked.json" assert { type: "json" };
|
import { codeSheet } from "./sheet.ts";
|
||||||
import { parseCodeSheet } from "./cart_tools.ts";
|
|
||||||
|
|
||||||
const game = parseCodeSheet(cart[0].value);
|
const game = codeSheet(0);
|
||||||
|
|
||||||
game.init();
|
game.init();
|
||||||
|
|
||||||
|
38
sheet.ts
Normal file
38
sheet.ts
Normal 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;
|
||||||
|
},
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user