Implemented node child sorting

This commit is contained in:
2023-06-15 03:10:30 +10:00
parent bfb985a08e
commit 1172ad50b4
11 changed files with 235 additions and 31 deletions

View File

@@ -35,6 +35,7 @@
<None Include="assets\scripts\gameplay\text.toy" /> <None Include="assets\scripts\gameplay\text.toy" />
<None Include="assets\scripts\gameplay\tilemap.toy" /> <None Include="assets\scripts\gameplay\tilemap.toy" />
<None Include="assets\scripts\init.toy" /> <None Include="assets\scripts\init.toy" />
<None Include="assets\scripts\sorting_test.toy" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion> <VCProjectVersion>17.0</VCProjectVersion>

View File

@@ -1 +1,5 @@
//this file is a polyfill TODO: fix this //this file is a polyfill TODO: fix this
fn getRealPos(node: opaque) {
return [0, 0];
}

View File

@@ -120,8 +120,9 @@ fn onFree(node: opaque) {
node.freeNodeTexture(); node.freeNodeTexture();
} }
fn customOnDraw(node: opaque, parentX: int, parentY: int) { fn onDraw(node: opaque) {
node.drawNode(realX + parentX - SPRITE_WIDTH / 4, realY + parentY - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT); var camera = parent.callNodeFn("getCameraPos");
node.drawNode(realX + camera[0] - SPRITE_WIDTH / 4, realY + camera[1] - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
} }

View File

@@ -197,8 +197,9 @@ fn onFree(node: opaque) {
node.freeNodeTexture(); node.freeNodeTexture();
} }
fn customOnDraw(node: opaque, parentX: int, parentY: int) { fn onDraw(node: opaque) {
node.drawNode(realX + parentX - SPRITE_WIDTH / 4, realY + parentY - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT); var camera = parent.callNodeFn("getCameraPos");
node.drawNode(realX + camera[0] - SPRITE_WIDTH / 4, realY + camera[1] - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
} }

View File

@@ -9,6 +9,9 @@ var TILE_HEIGHT: int const = 32;
var MAP_WIDTH: int const = 16; var MAP_WIDTH: int const = 16;
var MAP_HEIGHT: int const = 16; var MAP_HEIGHT: int const = 16;
var CAMERA_X: int const = 0;
var CAMERA_Y: int const = 32;
var tilemap: opaque = null; var tilemap: opaque = null;
var player: opaque = null; var player: opaque = null;
@@ -16,8 +19,6 @@ var stepCounter: opaque = null;
var enemies: [opaque] = []; var enemies: [opaque] = [];
var entities: [opaque] = null; //full list of entities
var collisionMap: [bool] = null; //cache this, since it won't change during a level var collisionMap: [bool] = null; //cache this, since it won't change during a level
var rng: opaque = null; var rng: opaque = null;
@@ -25,9 +26,12 @@ var rng: opaque = null;
//lifecycle functions //lifecycle functions
fn onLoad(node: opaque) { fn onLoad(node: opaque) {
tilemap = node.loadChild("scripts:/gameplay/tilemap.toy"); var empty = node.loadChild("scripts:/empty.toy");
tilemap = empty.loadChild("scripts:/gameplay/tilemap.toy");
stepCounter = empty.loadChild("scripts:/gameplay/step-counter.toy");
player = node.loadChild("scripts:/gameplay/lejana.toy"); player = node.loadChild("scripts:/gameplay/lejana.toy");
stepCounter = node.loadChild("scripts:/gameplay/step-counter.toy");
} }
fn onInit(node: opaque) { fn onInit(node: opaque) {
@@ -39,17 +43,12 @@ fn onFree(node: opaque) {
rng.freeRandomGenerator(); rng.freeRandomGenerator();
} }
fn onStep(node: opaque) { //fn onStep(node: opaque) {
entities = entities.sort(depthComparator); // //
} //}
fn onDraw(node: opaque) { fn onDraw(node: opaque) {
//call each child's custom draw fn (with parent shifting) node.sortChildrenNode(depthComparator);
tilemap.callNodeFn("customOnDraw", 0, 32);
for (var i = 0; i < entities.length(); i++) {
entities[i].callNodeFn("customOnDraw", 0, 32);
}
stepCounter.callNodeFn("customOnDraw", 0, 0, TILE_WIDTH * MAP_WIDTH, 32); stepCounter.callNodeFn("customOnDraw", 0, 0, TILE_WIDTH * MAP_WIDTH, 32);
} }
@@ -64,8 +63,7 @@ fn loadChild(parent: opaque, fname: string) {
fn generateLevel(node: opaque, seed: int, width: int, height: int) { fn generateLevel(node: opaque, seed: int, width: int, height: int) {
//reset the array //reset the array
enemies = []; enemies = [];
entities = [player]; //assume the player exists already
if (rng != null) rng.freeRandomGenerator(); if (rng != null) rng.freeRandomGenerator();
rng = createRandomGenerator(seed); rng = createRandomGenerator(seed);
@@ -83,7 +81,6 @@ fn generateLevel(node: opaque, seed: int, width: int, height: int) {
d.initNode(); d.initNode();
enemies.push(d); enemies.push(d);
entities.push(d);
var x = 0; var x = 0;
var y = 0; var y = 0;
@@ -134,8 +131,15 @@ fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth
var rhsPos = rhs.callNodeFn("getRealPos"); var rhsPos = rhs.callNodeFn("getRealPos");
if (lhsPos[1] == rhsPos[1]) { //BUGFIX: prevent z-fighting if (lhsPos[1] == rhsPos[1]) { //BUGFIX: prevent z-fighting
if (lhsPos[0] == rhsPos[0]) {
return true;
}
return lhsPos[0] < rhsPos[0]; return lhsPos[0] < rhsPos[0];
} }
return lhsPos[1] < rhsPos[1]; return lhsPos[1] < rhsPos[1];
}
fn getCameraPos(node: opaque) {
return [CAMERA_X, CAMERA_Y];
} }

View File

@@ -1,5 +1,7 @@
import node; import node;
//this is a child of the step counter, which simply renders text
fn customOnDraw(node: opaque, parentX: int, parentY: int) { fn customOnDraw(node: opaque, parentX: int, parentY: int) {
node.drawNode(parentX, parentY); node.drawNode(parentX, parentY);
} }

View File

@@ -41,7 +41,10 @@ var tileset: [string : [int]] = [
]; ];
//debug vars var parent: opaque = null;
//debug vars - what are these for?
var camX = 0; var camX = 0;
var camY = 0; var camY = 0;
var camW = 1080; var camW = 1080;
@@ -53,10 +56,20 @@ fn onLoad(node: opaque) {
node.loadNodeTexture("sprites:/tileset.png"); node.loadNodeTexture("sprites:/tileset.png");
} }
fn customOnDraw(node: opaque, parentX: int, parentY: int) { fn onInit(node: opaque) {
//find root
parent = node;
while (parent.getParentNode() != null) {
parent = parent.getParentNode();
}
}
fn onDraw(node: opaque) {
if (tilemap == null) { if (tilemap == null) {
return; return;
} }
var camera = parent.callNodeFn("getCameraPos");
//calc the region to render //calc the region to render
var lowerX: int = round((camX - camW/2.0) / TILE_WIDTH); var lowerX: int = round((camX - camW/2.0) / TILE_WIDTH);
@@ -77,7 +90,7 @@ fn customOnDraw(node: opaque, parentX: int, parentY: int) {
for (var i = lowerX; i < upperX; i++) { for (var i = lowerX; i < upperX; i++) {
node.setNodeRect(tilemap[j * mapWidth * 2 + i * 2] * 16, tilemap[j * mapWidth * 2 + i * 2 + 1] * 16, 16, 16); node.setNodeRect(tilemap[j * mapWidth * 2 + i * 2] * 16, tilemap[j * mapWidth * 2 + i * 2 + 1] * 16, 16, 16);
node.drawNode(i * TILE_WIDTH + parentX, j * TILE_HEIGHT + parentY, TILE_WIDTH, TILE_HEIGHT); node.drawNode(i * TILE_WIDTH + camera[0], j * TILE_HEIGHT + camera[1], TILE_WIDTH, TILE_HEIGHT);
} }
} }
} }

View File

@@ -0,0 +1,34 @@
import node;
//generate a number of child nodes
fn onInit(node: opaque) {
node.loadChild("scripts:/empty.toy", 3);
node.loadChild("scripts:/empty.toy", 2);
node.loadChild("scripts:/empty.toy", 1);
node.loadChild("scripts:/empty.toy", 4);
node.loadChild("scripts:/empty.toy", 5);
node.freeChildNode(3);
}
fn onStep(node) {
node.sortChildrenNode(lessThan);
}
fn lessThan(lhs, rhs) {
var a = lhs.callNodeFn("getValue");
var b = rhs.callNodeFn("getValue");
return a < b;
}
//utils - polyfills
fn loadChild(parent: opaque, fname: string, value) {
var child: opaque = loadNode(fname);
child.callNodeFn("setValue", value);
parent.pushNode(child);
return child;
}

View File

@@ -111,6 +111,115 @@ void Box_freeChildNode(Box_Node* node, int index) {
node->children[index] = NULL; node->children[index] = NULL;
} }
static void swapUtil(Box_Node** lhs, Box_Node** rhs) {
Box_Node* tmp = *lhs;
*lhs = *rhs;
*rhs = tmp;
}
//copied from lib_standard.c
static void recursiveLiteralQuicksortUtil(Toy_Interpreter* interpreter, Box_Node** ptr, int count, Toy_Literal fnCompare) {
//base case
if (count <= 1) {
return;
}
int runner = 0;
//iterate through the array
for (int checker = 0; checker < count - 1; checker++) {
//if node is null, it is always sorted to the end
if (ptr[checker] == NULL) {
swapUtil(&ptr[checker], &ptr[checker + 1]);
runner++;
continue;
}
if (ptr[checker + 1] == NULL) {
runner++;
continue;
}
Toy_LiteralArray arguments;
Toy_LiteralArray returns;
Toy_initLiteralArray(&arguments);
Toy_initLiteralArray(&returns);
Toy_pushLiteralArray(&arguments, TOY_TO_OPAQUE_LITERAL(ptr[checker], OPAQUE_TAG_NODE));
Toy_pushLiteralArray(&arguments, TOY_TO_OPAQUE_LITERAL(ptr[checker + 1], OPAQUE_TAG_NODE));
Toy_callLiteralFn(interpreter, fnCompare, &arguments, &returns);
Toy_Literal lessThan = Toy_popLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
if (TOY_IS_TRUTHY(lessThan)) {
swapUtil(&ptr[runner++], &ptr[checker]);
}
Toy_freeLiteral(lessThan);
}
//"shift everything up" so the pivot is in the middle
swapUtil(&ptr[runner], &ptr[count - 1]);
//recurse on each end
if (runner > 0) {
recursiveLiteralQuicksortUtil(interpreter, &ptr[0], runner, fnCompare);
}
if (runner < count) {
recursiveLiteralQuicksortUtil(interpreter, &ptr[runner + 1], count - runner - 1, fnCompare);
}
}
BOX_API void Box_sortChildrenNode(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal fnCompare) {
//check that this node's children aren't already sorted
bool sorted = true;
for (int checker = 0; checker < node->count - 1 && sorted; checker++) {
//NULL (tombstone) is always considered unsorted
if (node->children[checker] == NULL || node->children[checker + 1] == NULL) {
sorted = false;
break;
}
Toy_LiteralArray arguments;
Toy_LiteralArray returns;
Toy_initLiteralArray(&arguments);
Toy_initLiteralArray(&returns);
Toy_pushLiteralArray(&arguments, TOY_TO_OPAQUE_LITERAL(node->children[checker], OPAQUE_TAG_NODE));
Toy_pushLiteralArray(&arguments, TOY_TO_OPAQUE_LITERAL(node->children[checker + 1], OPAQUE_TAG_NODE));
Toy_callLiteralFn(interpreter, fnCompare, &arguments, &returns);
Toy_Literal lessThan = Toy_popLiteralArray(&returns);
Toy_freeLiteralArray(&arguments);
Toy_freeLiteralArray(&returns);
if (!TOY_IS_TRUTHY(lessThan)) {
sorted = false;
}
Toy_freeLiteral(lessThan);
}
//sort the children
if (!sorted) {
recursiveLiteralQuicksortUtil(interpreter, node->children, node->count, fnCompare);
}
//re-count the newly-sorted children
for (int i = node->count - 1; node->children[i] == NULL; i--) {
node->count--;
}
}
Toy_Literal Box_callNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args) { Toy_Literal Box_callNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args) {
Toy_Literal ret = TOY_TO_NULL_LITERAL; Toy_Literal ret = TOY_TO_NULL_LITERAL;
@@ -287,6 +396,6 @@ void Box_setTextNode(Box_Node* node, TTF_Font* font, const char* text, SDL_Color
void Box_drawNode(Box_Node* node, SDL_Rect dest) { void Box_drawNode(Box_Node* node, SDL_Rect dest) {
if (!node->texture) return; if (!node->texture) return;
SDL_Rect src = node->rect; SDL_Rect src = node->rect;
src.x += src.w * node->currentFrame; //TODO: improve this src.x += src.w * node->currentFrame;
SDL_RenderCopy(engine.renderer, node->texture, &src, &dest); SDL_RenderCopy(engine.renderer, node->texture, &src, &dest);
} }

View File

@@ -9,13 +9,9 @@
//forward declare //forward declare
typedef struct Box_private_node Box_Node; typedef struct Box_private_node Box_Node;
// typedef void (*Box_NodeCallback)(void*);
//the node object, which forms a tree //the node object, which forms a tree
typedef struct Box_private_node { typedef struct Box_private_node {
//function for releasing memory NOTE: removed, because it's not needed with only 1 node type - I've left them commented out because I might need them soon
// Box_NodeCallback freeMemory;
//toy functions, stored in a dict for flexibility //toy functions, stored in a dict for flexibility
Toy_LiteralDictionary* functions; Toy_LiteralDictionary* functions;
@@ -36,10 +32,10 @@ typedef struct Box_private_node {
//rendering-specific features //rendering-specific features
SDL_Texture* texture; SDL_Texture* texture;
SDL_Rect rect; SDL_Rect rect; //rendered rect
int frames; int frames; //horizontal-strip based animations
int currentFrame; int currentFrame;
} Box_Node; //TODO: rename this? } Box_Node;
BOX_API void Box_initNode(Box_Node* node, Toy_Interpreter* interpreter, const unsigned char* tb, size_t size); //run bytecode, then grab all top-level function literals BOX_API void Box_initNode(Box_Node* node, Toy_Interpreter* interpreter, const unsigned char* tb, size_t size); //run bytecode, then grab all top-level function literals
BOX_API void Box_pushNode(Box_Node* node, Box_Node* child); //push to the array (prune tombstones when expanding/copying) BOX_API void Box_pushNode(Box_Node* node, Box_Node* child); //push to the array (prune tombstones when expanding/copying)
@@ -48,6 +44,8 @@ BOX_API void Box_freeNode(Box_Node* node); //free this node and all children
BOX_API Box_Node* Box_getChildNode(Box_Node* node, int index); BOX_API Box_Node* Box_getChildNode(Box_Node* node, int index);
BOX_API void Box_freeChildNode(Box_Node* node, int index); BOX_API void Box_freeChildNode(Box_Node* node, int index);
BOX_API void Box_sortChildrenNode(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal fnCompare);
BOX_API Toy_Literal Box_callNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args); BOX_API Toy_Literal Box_callNodeLiteral(Box_Node* node, Toy_Interpreter* interpreter, Toy_Literal key, Toy_LiteralArray* args);
BOX_API Toy_Literal Box_callNode(Box_Node* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args); //call "fnName" on this node, and only this node, if it exists BOX_API Toy_Literal Box_callNode(Box_Node* node, Toy_Interpreter* interpreter, const char* fnName, Toy_LiteralArray* args); //call "fnName" on this node, and only this node, if it exists

View File

@@ -257,6 +257,42 @@ static int nativeFreeChildNode(Toy_Interpreter* interpreter, Toy_LiteralArray* a
return 0; return 0;
} }
static int nativeSortChildrenNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
if (arguments->count != 2) {
interpreter->errorOutput("Incorrect number of arguments passed to sortChildrenNode\n");
return -1;
}
Toy_Literal fnLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeLiteral = Toy_popLiteralArray(arguments);
Toy_Literal nodeLiteralIdn = nodeLiteral; //annoying
if (TOY_IS_IDENTIFIER(nodeLiteral) && Toy_parseIdentifierToValue(interpreter, &nodeLiteral)) {
Toy_freeLiteral(nodeLiteralIdn);
}
Toy_Literal fnLiteralIdn = fnLiteral; //annoying
if (TOY_IS_IDENTIFIER(fnLiteral) && Toy_parseIdentifierToValue(interpreter, &fnLiteral)) {
Toy_freeLiteral(fnLiteralIdn);
}
//check argument types
if (!TOY_IS_OPAQUE(nodeLiteral) || !TOY_IS_FUNCTION(fnLiteral)) {
interpreter->errorOutput("Incorrect argument type passed to sortChildrenNode\n");
Toy_freeLiteral(nodeLiteral);
return -1;
}
Box_Node* node = TOY_AS_OPAQUE(nodeLiteral);
Box_sortChildrenNode(node, interpreter, fnLiteral);
//cleanup
Toy_freeLiteral(nodeLiteral);
Toy_freeLiteral(fnLiteral);
return 0;
}
static int nativeGetParentNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { static int nativeGetParentNode(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
//checks //checks
if (arguments->count != 1) { if (arguments->count != 1) {
@@ -1099,6 +1135,7 @@ int Box_hookNode(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Liter
{"pushNode", nativePushNode}, {"pushNode", nativePushNode},
{"getChildNode", nativeGetChildNode}, {"getChildNode", nativeGetChildNode},
{"freeChildNode", nativeFreeChildNode}, {"freeChildNode", nativeFreeChildNode},
{"sortChildrenNode", nativeSortChildrenNode},
{"getParentNode", nativeGetParentNode}, {"getParentNode", nativeGetParentNode},
{"getChildNodeCount", nativeGetChildNodeCount}, {"getChildNodeCount", nativeGetChildNodeCount},
{"loadNodeTexture", nativeLoadNodeTexture}, {"loadNodeTexture", nativeLoadNodeTexture},