Implemented node child sorting
This commit is contained in:
@@ -1 +1,5 @@
|
||||
//this file is a polyfill TODO: fix this
|
||||
|
||||
fn getRealPos(node: opaque) {
|
||||
return [0, 0];
|
||||
}
|
||||
@@ -120,8 +120,9 @@ fn onFree(node: opaque) {
|
||||
node.freeNodeTexture();
|
||||
}
|
||||
|
||||
fn customOnDraw(node: opaque, parentX: int, parentY: int) {
|
||||
node.drawNode(realX + parentX - SPRITE_WIDTH / 4, realY + parentY - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
|
||||
fn onDraw(node: opaque) {
|
||||
var camera = parent.callNodeFn("getCameraPos");
|
||||
node.drawNode(realX + camera[0] - SPRITE_WIDTH / 4, realY + camera[1] - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -197,8 +197,9 @@ fn onFree(node: opaque) {
|
||||
node.freeNodeTexture();
|
||||
}
|
||||
|
||||
fn customOnDraw(node: opaque, parentX: int, parentY: int) {
|
||||
node.drawNode(realX + parentX - SPRITE_WIDTH / 4, realY + parentY - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
|
||||
fn onDraw(node: opaque) {
|
||||
var camera = parent.callNodeFn("getCameraPos");
|
||||
node.drawNode(realX + camera[0] - SPRITE_WIDTH / 4, realY + camera[1] - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@ var TILE_HEIGHT: int const = 32;
|
||||
var MAP_WIDTH: 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 player: opaque = null;
|
||||
@@ -16,8 +19,6 @@ var stepCounter: opaque = null;
|
||||
|
||||
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 rng: opaque = null;
|
||||
@@ -25,9 +26,12 @@ var rng: opaque = null;
|
||||
|
||||
//lifecycle functions
|
||||
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");
|
||||
stepCounter = node.loadChild("scripts:/gameplay/step-counter.toy");
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
@@ -39,17 +43,12 @@ fn onFree(node: opaque) {
|
||||
rng.freeRandomGenerator();
|
||||
}
|
||||
|
||||
fn onStep(node: opaque) {
|
||||
entities = entities.sort(depthComparator);
|
||||
}
|
||||
//fn onStep(node: opaque) {
|
||||
// //
|
||||
//}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
//call each child's custom draw fn (with parent shifting)
|
||||
tilemap.callNodeFn("customOnDraw", 0, 32);
|
||||
for (var i = 0; i < entities.length(); i++) {
|
||||
entities[i].callNodeFn("customOnDraw", 0, 32);
|
||||
}
|
||||
|
||||
node.sortChildrenNode(depthComparator);
|
||||
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) {
|
||||
//reset the array
|
||||
enemies = [];
|
||||
entities = [player]; //assume the player exists already
|
||||
|
||||
|
||||
if (rng != null) rng.freeRandomGenerator();
|
||||
rng = createRandomGenerator(seed);
|
||||
|
||||
@@ -83,7 +81,6 @@ fn generateLevel(node: opaque, seed: int, width: int, height: int) {
|
||||
d.initNode();
|
||||
|
||||
enemies.push(d);
|
||||
entities.push(d);
|
||||
|
||||
var x = 0;
|
||||
var y = 0;
|
||||
@@ -134,8 +131,15 @@ fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth
|
||||
var rhsPos = rhs.callNodeFn("getRealPos");
|
||||
|
||||
if (lhsPos[1] == rhsPos[1]) { //BUGFIX: prevent z-fighting
|
||||
if (lhsPos[0] == rhsPos[0]) {
|
||||
return true;
|
||||
}
|
||||
return lhsPos[0] < rhsPos[0];
|
||||
}
|
||||
|
||||
return lhsPos[1] < rhsPos[1];
|
||||
}
|
||||
|
||||
fn getCameraPos(node: opaque) {
|
||||
return [CAMERA_X, CAMERA_Y];
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
import node;
|
||||
|
||||
//this is a child of the step counter, which simply renders text
|
||||
|
||||
fn customOnDraw(node: opaque, parentX: int, parentY: int) {
|
||||
node.drawNode(parentX, parentY);
|
||||
}
|
||||
|
||||
@@ -41,7 +41,10 @@ var tileset: [string : [int]] = [
|
||||
];
|
||||
|
||||
|
||||
//debug vars
|
||||
var parent: opaque = null;
|
||||
|
||||
|
||||
//debug vars - what are these for?
|
||||
var camX = 0;
|
||||
var camY = 0;
|
||||
var camW = 1080;
|
||||
@@ -53,10 +56,20 @@ fn onLoad(node: opaque) {
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
|
||||
var camera = parent.callNodeFn("getCameraPos");
|
||||
|
||||
//calc the region to render
|
||||
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++) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
34
assets/scripts/sorting_test.toy
Normal file
34
assets/scripts/sorting_test.toy
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user