Added sorting layers to nodes

This commit is contained in:
2023-06-29 13:47:35 +10:00
parent 51a0c94ba2
commit 2d6ad5efcc
10 changed files with 39 additions and 44 deletions

2
Box

Submodule Box updated: 888b7e27ba...ac7dd38bdb

View File

@@ -15,12 +15,12 @@ fn onLoad(node: opaque) {
fn onInit(node: opaque) { fn onInit(node: opaque) {
print "onInit() called"; print "onInit() called";
node.loadNodeTexture("sprites:/little_plane.png"); node.loadNodeTexture("airport:/little_plane.png");
} }
fn onStep(node: opaque) { // fn onStep(node: opaque) {
//TODO: move // //
} // }
fn onFree(node: opaque) { fn onFree(node: opaque) {
print "onFree() called"; print "onFree() called";
@@ -30,18 +30,8 @@ fn onFree(node: opaque) {
fn onDraw(node: opaque) { fn onDraw(node: opaque) {
// print "onDraw() called"; // print "onDraw() called";
var parent: opaque = node.getParentNode();
var px = 0; node.drawNode(node.getNodeWorldPositionX(), node.getNodeWorldPositionY(), WIDTH, HEIGHT);
var py = 0;
//TODO: get world position
if (parent != null) {
px = parent.getNodePositionX();
py = parent.getNodePositionY();
}
node.drawNode(node.getNodePositionX() + px, node.getNodePositionY() + py, WIDTH, HEIGHT);
} }
//event functions //event functions

View File

@@ -28,14 +28,11 @@
mapInputEventToKeyDown("character_attack", "space"); //event, keysym mapInputEventToKeyDown("character_attack", "space"); //event, keysym
//TODO: escape to kill the game
//this function must always be called, or the engine won't run //this function must always be called, or the engine won't run
initWindow("Airport", 800, 600, false); //TODO: custom FPS setting initWindow("Airport", 800, 600, false);
//kick off the logic of the scene graph //kick off the logic of the scene graph
loadRootNode("scripts:/airplane.toy"); loadRootNode("airport:/airplane.toy");
} }
//Globals go here //Globals go here

View File

@@ -103,8 +103,8 @@ fn onDraw(node: opaque) {
var originOffsetY: int = node.getNodeRectH() * int scaleY / 2; var originOffsetY: int = node.getNodeRectH() * int scaleY / 2;
node.drawNode( node.drawNode(
floor(posX * scaleX) - originOffsetX, floor(posX * scaleX) - originOffsetX + globalCameraX,
floor(posY * scaleY) - originOffsetY, floor(posY * scaleY) - originOffsetY + globalCameraY,
floor(node.getNodeRectW() * scaleX), floor(node.getNodeRectW() * scaleX),
floor(node.getNodeRectH() * scaleY) floor(node.getNodeRectH() * scaleY)
); );

View File

@@ -169,12 +169,12 @@ fn onDraw(node: opaque) {
var scaleY: float = node.getNodeWorldScaleY(); var scaleY: float = node.getNodeWorldScaleY();
//this offset is because the sprite cell for lejana is twice as big as the sprite cell for the floor tiles //this offset is because the sprite cell for lejana is twice as big as the sprite cell for the floor tiles
var originOffsetX: int = node.getNodeRectW() * int scaleX / 4; var originOffsetX: int = player.getNodeRectW() / 2;
var originOffsetY: int = node.getNodeRectH() * int scaleY / 2; var originOffsetY: int = player.getNodeRectH();
node.drawNode( node.drawNode(
floor(posX * scaleX) - originOffsetX, floor(posX * scaleX) - originOffsetX + globalCameraX,
floor(posY * scaleY) - originOffsetY, floor(posY * scaleY) - originOffsetY + globalCameraY,
floor(node.getNodeRectW() * scaleX), floor(node.getNodeRectW() * scaleX),
floor(node.getNodeRectH() * scaleY) floor(node.getNodeRectH() * scaleY)
); );

View File

@@ -21,13 +21,9 @@ fn onInit(node: opaque) {
//load and initialize the UI (depends on map stuff) //load and initialize the UI (depends on map stuff)
stepCounter = node.loadChild("scripts:/gameplay/step-counter.toy"); stepCounter = node.loadChild("scripts:/gameplay/step-counter.toy");
stepCounter.setNodeRect( stepCounter.setNodeRect(0, 0, CAMERA_SCREEN_W, 32);
0,
0,
floor(TILE_PIXEL_WIDTH * MAP_GRID_WIDTH * tilemap.getNodeWorldScaleX()), //width - stretches along the top of the tilemap
stepCounter.getNodeRectH() //default height
);
stepCounter.callNodeFn("setMaxSteps", 100); stepCounter.callNodeFn("setMaxSteps", 100);
stepCounter.setNodeLayer(100);
//offset the scene node, so it shows the map outside separate from the UI //offset the scene node, so it shows the map outside separate from the UI
node.setNodePositionY(16); node.setNodePositionY(16);
@@ -47,6 +43,17 @@ fn onFree(node: opaque) {
fn onDraw(node: opaque) { fn onDraw(node: opaque) {
//use the onDraw to sort - skip too many steps //use the onDraw to sort - skip too many steps
node.sortChildrenNode(depthComparator); node.sortChildrenNode(depthComparator);
var scaleX: float = player.getNodeWorldScaleX();
var scaleY: float = player.getNodeWorldScaleY();
//this offset is because the sprite cell for lejana is twice as big as the sprite cell for the floor tiles
var originOffsetX: int = player.getNodeRectW() / 2;
var originOffsetY: int = player.getNodeRectH() / 2;
//use the room scene's onDraw to set the global camera position
globalCameraX = floor((CAMERA_SCREEN_W / 2) - (player.getNodeWorldPositionX() * scaleX)) - originOffsetX;
globalCameraY = floor((CAMERA_SCREEN_H / 2) - (player.getNodeWorldPositionY() * scaleY)) - originOffsetY;
} }
//gameplay functions //gameplay functions

View File

@@ -41,6 +41,7 @@ fn alterRemainingSteps(node: opaque, increment: int) {
fn loadChild(parent: opaque, fname: string) { fn loadChild(parent: opaque, fname: string) {
var child: opaque = loadNode(fname); var child: opaque = loadNode(fname);
parent.pushNode(child); parent.pushNode(child);
child.setNodeLayer(parent.getNodeLayer());
return child; return child;
} }

View File

@@ -76,8 +76,8 @@ fn onDraw(node: opaque) {
//draw to the screen //draw to the screen
node.drawNode( node.drawNode(
floor((i * TILE_PIXEL_WIDTH + posX) * scaleX), floor((i * TILE_PIXEL_WIDTH + posX) * scaleX) + globalCameraX,
floor((j * TILE_PIXEL_HEIGHT + posY) * scaleY), floor((j * TILE_PIXEL_HEIGHT + posY) * scaleY) + globalCameraY,
floor(TILE_PIXEL_WIDTH * scaleX), floor(TILE_PIXEL_WIDTH * scaleX),
floor(TILE_PIXEL_HEIGHT * scaleY) floor(TILE_PIXEL_HEIGHT * scaleY)
); );

View File

@@ -5,10 +5,15 @@ var TILE_PIXEL_HEIGHT: int const = 16;
var MAP_GRID_WIDTH: int const = 16; var MAP_GRID_WIDTH: int const = 16;
var MAP_GRID_HEIGHT: int const = 16; var MAP_GRID_HEIGHT: int const = 16;
var CAMERA_SCREEN_W: int const = 800;
var CAMERA_SCREEN_H: int const = 600;
var CAMERA_SCALE_X: float const = 2.0; var CAMERA_SCALE_X: float const = 2.0;
var CAMERA_SCALE_Y: float const = 2.0; var CAMERA_SCALE_Y: float const = 2.0;
//this is a very odd pattern...
var globalCameraX: int = 0;
var globalCameraY: int = 0;
//A quirk of the setup is that anything defined in the root of `init.toy` becomes a global object //A quirk of the setup is that anything defined in the root of `init.toy` becomes a global object
//To resolve that, the configuration is inside a block scope //To resolve that, the configuration is inside a block scope
@@ -45,12 +50,7 @@ var CAMERA_SCALE_Y: float const = 2.0;
//this function must always be called, or the engine won't run //this function must always be called, or the engine won't run
initWindow( initWindow("Skylands", CAMERA_SCREEN_W, CAMERA_SCREEN_H, false); //TODO: custom FPS setting
"Airport",
floor(TILE_PIXEL_WIDTH * MAP_GRID_WIDTH * CAMERA_SCALE_X),
floor(TILE_PIXEL_HEIGHT * MAP_GRID_HEIGHT * CAMERA_SCALE_Y) + 32, //32 is UI space
false);
//TODO: custom FPS setting
//kick off the logic of the scene graph //kick off the logic of the scene graph
loadRootNode("scripts:/gameplay/scene.toy"); loadRootNode("scripts:/gameplay/scene.toy");

View File

@@ -1,5 +1,4 @@
#include "box_engine.h" #include "box_engine.h"
#include "toy_drive_system.h"
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
//debugging tools //debugging tools
@@ -8,9 +7,10 @@ int main(int argc, char* argv[]) {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
#endif//win32 && debug #endif//win32 && debug
//the drive system uses a LiteralDictionary, which must be initialized with this //the drive system maps filepaths to "drives", which specifies which folders can be accessed by the scripts
Toy_initDriveSystem(); Toy_initDriveSystem();
// Toy_setDrivePath("airport", "assets/airport-demo"); //DEBUG
Toy_setDrivePath("scripts", "assets/scripts"); Toy_setDrivePath("scripts", "assets/scripts");
Toy_setDrivePath("sprites", "assets/sprites"); Toy_setDrivePath("sprites", "assets/sprites");
Toy_setDrivePath("fonts", "assets/fonts"); Toy_setDrivePath("fonts", "assets/fonts");