Added onLoad() lifecycle function
This commit is contained in:
@@ -23,13 +23,13 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="assets\scripts\children_test.toy" />
|
||||
<None Include="assets\scripts\empty.toy" />
|
||||
<None Include="assets\scripts\entity.toy" />
|
||||
<None Include="assets\scripts\frames_test.toy" />
|
||||
<None Include="assets\scripts\init.toy" />
|
||||
<None Include="assets\scripts\scene.toy" />
|
||||
<None Include="assets\scripts\tilemap\layer-background.toy" />
|
||||
<None Include="assets\scripts\tilemap\layer-walls.toy" />
|
||||
<None Include="assets\scripts\tilemap\tile.toy" />
|
||||
<None Include="assets\scripts\tilemap\tilemap.toy" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
|
||||
@@ -1,24 +1,22 @@
|
||||
import node;
|
||||
|
||||
//util to generate and init a child node of a given parent
|
||||
fn makeChild(parent: opaque, fname: string) {
|
||||
//looks like a polyfill
|
||||
fn loadChild(parent: opaque, fname: string) {
|
||||
var child: opaque = loadNode(fname);
|
||||
parent.pushNode(child);
|
||||
child.initNode();
|
||||
return child;
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
fn onLoad(node: opaque) {
|
||||
for (var i = 0; i < 20; i++) {
|
||||
node.makeChild("scripts:/tilemap/tile.toy");
|
||||
node.loadChild("scripts:/empty.toy");
|
||||
}
|
||||
|
||||
node.freeChildNode(10);
|
||||
var n = node.getChildNode(10);
|
||||
|
||||
for (var i = 0; i < 20; i++) {
|
||||
//this would originally prune tombstones...
|
||||
node.makeChild("scripts:/tilemap/tile.toy");
|
||||
node.loadChild("scripts:/empty.toy");
|
||||
}
|
||||
|
||||
print node.getChildNodeCount();
|
||||
|
||||
1
assets/scripts/empty.toy
Normal file
1
assets/scripts/empty.toy
Normal file
@@ -0,0 +1 @@
|
||||
//this file is a polyfill
|
||||
@@ -1,17 +1,20 @@
|
||||
//the overarching scene
|
||||
import node;
|
||||
|
||||
//debugging tools
|
||||
var stepCounter: int = 0;
|
||||
var stepTracker: [int] = [0, 0, 0];
|
||||
|
||||
|
||||
//util to generate and init a child node of a given parent
|
||||
fn makeChild(parent: opaque, fname: string) {
|
||||
var child: opaque = loadNode(fname);
|
||||
parent.pushNode(child);
|
||||
child.initNode();
|
||||
return child;
|
||||
}
|
||||
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
//lifecycle functions
|
||||
fn onLoad(node: opaque) {
|
||||
//load the tile map node
|
||||
var tilemapNode = node.makeChild("scripts:/tilemap/tilemap.toy");
|
||||
|
||||
@@ -22,6 +25,22 @@ fn onInit(node: opaque) {
|
||||
//tilemapNode.callNodeFn("loadLayer", "layer-maze.toy");
|
||||
}
|
||||
|
||||
//debugging code
|
||||
fn onStep(node: opaque) {
|
||||
stepCounter++;
|
||||
}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
//round off - too many skips means it's crappy anyway
|
||||
if (stepCounter > stepTracker.length() - 1) {
|
||||
stepCounter = stepTracker.length() - 1;
|
||||
}
|
||||
|
||||
//This logic is just a debugging tool
|
||||
stepTracker[stepCounter] = stepTracker[stepCounter] + 1;
|
||||
print stepTracker;
|
||||
stepCounter = 0;
|
||||
}
|
||||
|
||||
//global accessors
|
||||
fn getTileWidth(node: opaque): int { return 64; }
|
||||
|
||||
@@ -4,24 +4,25 @@ import engine;
|
||||
import node;
|
||||
|
||||
|
||||
//util to generate and init a child node of a given parent
|
||||
fn makeChildSprite(parent: opaque, spriteName: string) {
|
||||
var child: opaque = loadNode("scripts:/tilemap/tile.toy");
|
||||
//TODO: replace with an empty node function (polyfill)
|
||||
fn loadChildEmpty(parent: opaque) {
|
||||
var child: opaque = loadNode("scripts:/empty.toy");
|
||||
parent.pushNode(child);
|
||||
child.initNode();
|
||||
|
||||
child.loadTexture("sprites:/" + spriteName);
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
fn onLoad(node: opaque) {
|
||||
//load the child node, with the tiling back image
|
||||
node.makeChildSprite("tile-background.png");
|
||||
node.loadChildEmpty();
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
node.getChildNode(0).loadTexture("sprites:/tile-background.png");
|
||||
}
|
||||
|
||||
|
||||
//drawing util
|
||||
fn drawLayer(node: opaque, camX, camY, camW, camH, depth) {
|
||||
//get the constants from root
|
||||
var root: opaque = getRootNode();
|
||||
|
||||
@@ -4,24 +4,25 @@ import engine;
|
||||
import node;
|
||||
|
||||
|
||||
//util to generate and init a child node of a given parent
|
||||
fn makeChildSprite(parent: opaque, spriteName: string) {
|
||||
var child: opaque = loadNode("scripts:/tilemap/tile.toy");
|
||||
//TODO: replace with an empty node function (polyfill)
|
||||
fn loadChildEmpty(parent: opaque) {
|
||||
var child: opaque = loadNode("scripts:/empty.toy");
|
||||
parent.pushNode(child);
|
||||
child.initNode();
|
||||
|
||||
child.loadTexture("sprites:/" + spriteName);
|
||||
|
||||
return child;
|
||||
}
|
||||
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
fn onLoad(node: opaque) {
|
||||
//load the child node, with the tiling back image
|
||||
node.makeChildSprite("tile-wall.png");
|
||||
node.loadChildEmpty();
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
node.getChildNode(0).loadTexture("sprites:/tile-wall.png");
|
||||
}
|
||||
|
||||
|
||||
//drawing util
|
||||
fn drawLayer(node: opaque, camX, camY, camW, camH, depth) {
|
||||
//get the constants from root
|
||||
var root: opaque = getRootNode();
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
//EMPTY
|
||||
|
||||
@@ -6,44 +6,25 @@ import node;
|
||||
var camX: float = 0;
|
||||
var camY: float = 0;
|
||||
|
||||
var stepCounter: int = 0;
|
||||
var stepTracker: [int] = [0, 0, 0, 0, 0, 0];
|
||||
|
||||
//util to generate and init a child node of a given parent
|
||||
fn makeChild(parent: opaque, fname: string) {
|
||||
//util to generate a child node of a given parent
|
||||
fn loadChild(parent: opaque, fname: string) {
|
||||
var child: opaque = loadNode(fname);
|
||||
parent.pushNode(child);
|
||||
child.initNode();
|
||||
return child;
|
||||
}
|
||||
|
||||
fn loadLayer(node: opaque, layerName: string) {
|
||||
//load the given layer as a child
|
||||
var layerNode = node.makeChild("scripts:/tilemap/" + layerName);
|
||||
var layerNode = node.loadChild("scripts:/tilemap/" + layerName);
|
||||
}
|
||||
|
||||
//lifecycle functions
|
||||
fn onStep(node: opaque) {
|
||||
stepCounter++;
|
||||
|
||||
camX--;
|
||||
camY--;
|
||||
}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
var tmp = stepCounter;
|
||||
print stepCounter;
|
||||
stepCounter = 0;
|
||||
|
||||
if (tmp > 5) {
|
||||
tmp = 5;
|
||||
}
|
||||
|
||||
print typeof (stepTracker[tmp] + 1);
|
||||
|
||||
//TODO: index[]++
|
||||
stepTracker[tmp] = stepTracker[tmp] + 1;
|
||||
print stepTracker;
|
||||
|
||||
var screenWidth: int const = getRootNode().callNodeFn("getScreenWidth");
|
||||
var screenHeight: int const = getRootNode().callNodeFn("getScreenHeight");
|
||||
|
||||
|
||||
@@ -63,12 +63,17 @@ void Box_initEngine() {
|
||||
Toy_injectNativeHook(&engine.interpreter, "node", Box_hookNode);
|
||||
Toy_injectNativeHook(&engine.interpreter, "input", Box_hookInput);
|
||||
|
||||
//run the init
|
||||
size_t size = 0;
|
||||
const unsigned char* source = Toy_readFile("./assets/scripts/init.toy", &size);
|
||||
const unsigned char* tb = Toy_compileString((const char*)source, &size);
|
||||
free((void*)source);
|
||||
|
||||
Toy_runInterpreter(&engine.interpreter, tb, size);
|
||||
|
||||
//init the node-tree
|
||||
Box_callRecursiveEngineNode(engine.rootNode, &engine.interpreter, "onInit", NULL);
|
||||
|
||||
}
|
||||
|
||||
void Box_freeEngine() {
|
||||
|
||||
@@ -25,7 +25,7 @@ static int nativeInitWindow(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
||||
}
|
||||
|
||||
if (arguments->count != 4) {
|
||||
fatalError("Incorrect number of arguments passed to initEngine\n");
|
||||
fatalError("Incorrect number of arguments passed to initWindow\n");
|
||||
}
|
||||
|
||||
//extract the arguments
|
||||
@@ -34,9 +34,29 @@ static int nativeInitWindow(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
||||
Toy_Literal screenWidth = Toy_popLiteralArray(arguments);
|
||||
Toy_Literal caption = Toy_popLiteralArray(arguments);
|
||||
|
||||
Toy_Literal captionIdn = caption;
|
||||
if (TOY_IS_IDENTIFIER(caption) && Toy_parseIdentifierToValue(interpreter, &caption)) {
|
||||
Toy_freeLiteral(captionIdn);
|
||||
}
|
||||
|
||||
Toy_Literal screenWidthIdn = screenWidth;
|
||||
if (TOY_IS_IDENTIFIER(screenWidth) && Toy_parseIdentifierToValue(interpreter, &screenWidth)) {
|
||||
Toy_freeLiteral(screenWidthIdn);
|
||||
}
|
||||
|
||||
Toy_Literal screenHeightIdn = screenHeight;
|
||||
if (TOY_IS_IDENTIFIER(screenHeight) && Toy_parseIdentifierToValue(interpreter, &screenHeight)) {
|
||||
Toy_freeLiteral(screenHeightIdn);
|
||||
}
|
||||
|
||||
Toy_Literal fscreenIdn = fscreen;
|
||||
if (TOY_IS_IDENTIFIER(fscreen) && Toy_parseIdentifierToValue(interpreter, &fscreen)) {
|
||||
Toy_freeLiteral(fscreenIdn);
|
||||
}
|
||||
|
||||
//check argument types
|
||||
if (!TOY_IS_STRING(caption) || !TOY_IS_INTEGER(screenWidth) || !TOY_IS_INTEGER(screenHeight) || !TOY_IS_BOOLEAN(fscreen)) {
|
||||
fatalError("Incorrect argument type passed to initEngine\n");
|
||||
fatalError("Incorrect argument type passed to initWindow\n");
|
||||
}
|
||||
|
||||
//init the window
|
||||
@@ -133,7 +153,7 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
|
||||
|
||||
//init the inner interpreter manually
|
||||
Toy_initLiteralArray(&inner.literalCache);
|
||||
inner.scope = Toy_pushScope(NULL);
|
||||
inner.scope = Toy_pushScope(interpreter->scope);
|
||||
inner.bytecode = tb;
|
||||
inner.length = size;
|
||||
inner.count = 0;
|
||||
@@ -148,16 +168,15 @@ static int nativeLoadRootNode(Toy_Interpreter* interpreter, Toy_LiteralArray* ar
|
||||
|
||||
Box_initEngineNode(rootNode, &inner, tb, size);
|
||||
|
||||
//init the new node (and ONLY this node)
|
||||
Box_callEngineNode(rootNode, &engine.interpreter, "onInit", NULL);
|
||||
//immediately call onLoad() after running the script - for loading other nodes
|
||||
Box_callEngineNode(rootNode, &engine.interpreter, "onLoad", NULL);
|
||||
|
||||
//NOW it's non-null
|
||||
engine.rootNode = rootNode;
|
||||
|
||||
//cleanup
|
||||
while(inner.scope) {
|
||||
inner.scope = Toy_popScope(inner.scope);
|
||||
}
|
||||
Toy_popScope(inner.scope);
|
||||
inner.scope = NULL;
|
||||
|
||||
Toy_freeLiteralArray(&inner.stack);
|
||||
Toy_freeLiteralArray(&inner.literalCache);
|
||||
|
||||
@@ -50,14 +50,14 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
|
||||
|
||||
Box_EngineNode* node = TOY_ALLOCATE(Box_EngineNode, 1);
|
||||
|
||||
//BUGFIX: make an inner-interpreter
|
||||
//BUGFIX: make an -interpreter
|
||||
Toy_Interpreter inner;
|
||||
|
||||
//init the inner interpreter manually
|
||||
Toy_initLiteralArray(&inner.literalCache);
|
||||
Toy_initLiteralArray(&inner.stack);
|
||||
inner.hooks = interpreter->hooks;
|
||||
inner.scope = Toy_pushScope(NULL);
|
||||
inner.scope = Toy_pushScope(interpreter->scope);
|
||||
inner.bytecode = tb;
|
||||
inner.length = size;
|
||||
inner.count = 0;
|
||||
@@ -70,6 +70,9 @@ static int nativeLoadNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
|
||||
|
||||
Box_initEngineNode(node, &inner, tb, size);
|
||||
|
||||
//immediately call onLoad() after running the script - for loading other nodes
|
||||
Box_callEngineNode(node, &engine.interpreter, "onLoad", NULL);
|
||||
|
||||
// return the node
|
||||
Toy_Literal nodeLiteral = TOY_TO_OPAQUE_LITERAL(node, node->tag);
|
||||
Toy_pushLiteralArray(&interpreter->stack, nodeLiteral);
|
||||
|
||||
Reference in New Issue
Block a user