From 3052b2a265e0f1d91c53235ee7686a07d74dcf50 Mon Sep 17 00:00:00 2001 From: Ratstail91 Date: Tue, 7 Mar 2023 21:28:16 +1100 Subject: [PATCH] Added water-based step counter --- Airport.vcxproj | 2 + assets/scripts/airplane.toy | 4 +- assets/scripts/empty.toy | 5 -- assets/scripts/gameplay/drone.toy | 19 ++----- assets/scripts/gameplay/lejana.toy | 19 ++----- assets/scripts/gameplay/scene.toy | 38 +++++++------ assets/scripts/gameplay/step-counter.toy | 67 +++++++++++++++++++++++ assets/scripts/gameplay/text.toy | 9 +++ assets/scripts/gameplay/tilemap.toy | 6 +- assets/scripts/init.toy | 2 +- assets/sprites/stepcounter.png | Bin 0 -> 160 bytes box/box_engine.c | 4 +- box/lib_node.c | 18 +++--- 13 files changed, 124 insertions(+), 69 deletions(-) create mode 100644 assets/scripts/gameplay/step-counter.toy create mode 100644 assets/scripts/gameplay/text.toy create mode 100644 assets/sprites/stepcounter.png diff --git a/Airport.vcxproj b/Airport.vcxproj index d0a5119..e241f2d 100644 --- a/Airport.vcxproj +++ b/Airport.vcxproj @@ -31,6 +31,8 @@ + + diff --git a/assets/scripts/airplane.toy b/assets/scripts/airplane.toy index efecfeb..3ddcb36 100644 --- a/assets/scripts/airplane.toy +++ b/assets/scripts/airplane.toy @@ -31,7 +31,7 @@ fn onInit(node: opaque) { print "onInit() called"; parent = node.getParentNode(); - node.loadTexture("sprites:/little_plane.png"); + node.loadNodeTexture("sprites:/little_plane.png"); } fn onStep(node: opaque) { @@ -44,7 +44,7 @@ fn onStep(node: opaque) { fn onFree(node: opaque) { print "onFree() called"; - node.freeTexture(); + node.freeNodeTexture(); } fn onDraw(node: opaque) { diff --git a/assets/scripts/empty.toy b/assets/scripts/empty.toy index 8776059..79bacfa 100644 --- a/assets/scripts/empty.toy +++ b/assets/scripts/empty.toy @@ -1,6 +1 @@ //this file is a polyfill TODO: fix this - -fn onDraw(node: opaque) { - //debug draw at the origin - node.drawNode(0, 0); -} \ No newline at end of file diff --git a/assets/scripts/gameplay/drone.toy b/assets/scripts/gameplay/drone.toy index 35fac16..31bebb6 100644 --- a/assets/scripts/gameplay/drone.toy +++ b/assets/scripts/gameplay/drone.toy @@ -90,7 +90,7 @@ fn getRealPos(node: opaque) { //lifecycle functions fn onLoad(node: opaque) { - node.loadTexture("sprites:/drone.png"); + node.loadNodeTexture("sprites:/drone.png"); node.faceDown(); } @@ -117,22 +117,11 @@ fn onStep(node: opaque) { } fn onFree(node: opaque) { - node.freeTexture(); + node.freeNodeTexture(); } -fn customOnDraw(node: opaque) { - var px = 0; - var py = 0; - - if (parent != null) { - px = parent.callNodeFn("getX"); - py = parent.callNodeFn("getY"); - - px = px != null ? px : 0; - py = py != null ? py : 0; - } - - node.drawNode(realX + px - SPRITE_WIDTH / 4, realY + py - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT); +fn customOnDraw(node: opaque, parentX: int, parentY: int) { + node.drawNode(realX + parentX - SPRITE_WIDTH / 4, realY + parentY - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT); } diff --git a/assets/scripts/gameplay/lejana.toy b/assets/scripts/gameplay/lejana.toy index 0c6d387..091b93c 100644 --- a/assets/scripts/gameplay/lejana.toy +++ b/assets/scripts/gameplay/lejana.toy @@ -92,7 +92,7 @@ fn getRealPos(node: opaque) { //lifecycle functions fn onLoad(node: opaque) { - node.loadTexture("sprites:/lejana.png"); + node.loadNodeTexture("sprites:/lejana.png"); node.faceDown(); } @@ -173,22 +173,11 @@ fn onStep(node: opaque) { } fn onFree(node: opaque) { - node.freeTexture(); + node.freeNodeTexture(); } -fn customOnDraw(node: opaque) { - var px = 0; - var py = 0; - - if (parent != null) { - px = parent.callNodeFn("getX"); - py = parent.callNodeFn("getY"); - - px = px != null ? px : 0; - py = py != null ? py : 0; - } - - node.drawNode(realX + px - SPRITE_WIDTH / 4, realY + py - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT); +fn customOnDraw(node: opaque, parentX: int, parentY: int) { + node.drawNode(realX + parentX - SPRITE_WIDTH / 4, realY + parentY - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT); } diff --git a/assets/scripts/gameplay/scene.toy b/assets/scripts/gameplay/scene.toy index cac5b48..bd3534b 100644 --- a/assets/scripts/gameplay/scene.toy +++ b/assets/scripts/gameplay/scene.toy @@ -12,6 +12,7 @@ var MAP_HEIGHT: int const = 16; var tilemap: opaque = null; var player: opaque = null; +var stepCounter: opaque = null; var enemies: [opaque] = []; @@ -22,24 +23,19 @@ var collisionMap: [bool] = null; //cache this, since it won't change during a le var rng: opaque = null; //debugging tools -var stepCounter: int = 0; -var drawCounter: int = 0; - -var textNode: opaque = null; +var debugStepCounter: int = 0; +var debugDrawCounter: int = 0; //lifecycle functions fn onLoad(node: opaque) { tilemap = node.loadChild("scripts:/gameplay/tilemap.toy"); player = node.loadChild("scripts:/gameplay/lejana.toy"); - textNode = node.loadChild("scripts:/empty.toy"); + stepCounter = node.loadChild("scripts:/gameplay/step-counter.toy"); } fn onInit(node: opaque) { node.generateLevel(clock().hash(), MAP_WIDTH, MAP_HEIGHT); - - //debugging -// textNode.setNodeText("fonts:/alphbeta.ttf", 48, "Hello world", 128, 0, 0, 255); - textNode.setNodeText("fonts:/Ancient God.ttf", 48, "Hello world", 128, 0, 0, 255); + stepCounter.callNodeFn("setMaxSteps", 100); } fn onFree(node: opaque) { @@ -47,23 +43,25 @@ fn onFree(node: opaque) { } fn onStep(node: opaque) { - if (++stepCounter >= 30) { - print "FPS: " + string drawCounter + " / 30"; - stepCounter = 0; - drawCounter = 0; + if (++debugStepCounter >= 30) { + print "FPS: " + string debugDrawCounter + " / 30"; + debugStepCounter = 0; + debugDrawCounter = 0; } entities = entities.sort(depthComparator); } fn onDraw(node: opaque) { - drawCounter++; + debugDrawCounter++; - //call each child's custom draw fn - tilemap.callNodeFn("customOnDraw"); + //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"); + entities[i].callNodeFn("customOnDraw", 0, 32); } + + stepCounter.callNodeFn("customOnDraw", 0, 0, TILE_WIDTH * MAP_WIDTH, 32); } //utils - polyfills @@ -138,11 +136,17 @@ fn runAI(node: opaque) { for (var i = 0; i < enemies.length(); i++) { enemies[i].callNodeFn("runAI", rng); } + + stepCounter.callNodeFn("alterRemainingSteps", -1); } fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth var lhsPos = lhs.callNodeFn("getRealPos"); var rhsPos = rhs.callNodeFn("getRealPos"); + if (lhsPos[1] == rhsPos[1]) { //BUGFIX: prevent z-fighting + return lhsPos[0] < rhsPos[0]; + } + return lhsPos[1] < rhsPos[1]; } \ No newline at end of file diff --git a/assets/scripts/gameplay/step-counter.toy b/assets/scripts/gameplay/step-counter.toy new file mode 100644 index 0000000..3aaeeab --- /dev/null +++ b/assets/scripts/gameplay/step-counter.toy @@ -0,0 +1,67 @@ +import node; + +//utils +var maxSteps: int = 0; +var remainingSteps: int = 0; + +fn setMaxSteps(node: opaque, steps: int) { + maxSteps = steps; + remainingSteps = steps; +} + +fn setRemainingSteps(node: opaque, steps: int) { + remainingSteps = steps; +} + +fn getMaxSteps(node: opaque) { + return maxSteps; +} + +fn getRemainingSteps(node: opaque) { + return remainingSteps; +} + +fn alterRemainingSteps(node: opaque, increment: int) { + remainingSteps += increment; + if (remainingSteps > maxSteps) { + remainingSteps = maxSteps; + } + + if (remainingSteps < 0) { + remainingSteps = 0; + } + + return remainingSteps; +} + +//utils - polyfills +fn loadChild(parent: opaque, fname: string) { + var child: opaque = loadNode(fname); + parent.pushNode(child); + return child; +} + +//lifecycle functions +fn onLoad(node: opaque) { + node.loadNodeTexture("sprites:/stepcounter.png"); + + node + .loadChild("scripts:/gameplay/text.toy") + .setNodeText("fonts:/alphbeta.ttf", 32, "Water", 0, 60, 240, 255); +} + +fn onFree(node: opaque) { + node.freeNodeTexture(); +} + +fn customOnDraw(node: opaque, x: int, y: int, w: int, h: int) { + if (remainingSteps > 0 && maxSteps > 0) { + var tmp = float remainingSteps / maxSteps * w; + + node.drawNode(x, y, int tmp, h); + + for (var i: int = 0; i < node.getChildNodeCount(); i++) { + node.getChildNode(i).callNodeFn("customOnDraw", x, y); + } + } +} \ No newline at end of file diff --git a/assets/scripts/gameplay/text.toy b/assets/scripts/gameplay/text.toy new file mode 100644 index 0000000..6b5c647 --- /dev/null +++ b/assets/scripts/gameplay/text.toy @@ -0,0 +1,9 @@ +import node; + +fn customOnDraw(node: opaque, parentX: int, parentY: int) { + node.drawNode(parentX, parentY); +} + +fn onFree(node: opaque) { + node.freeNodeTexture(); +} \ No newline at end of file diff --git a/assets/scripts/gameplay/tilemap.toy b/assets/scripts/gameplay/tilemap.toy index 95641de..314fb09 100644 --- a/assets/scripts/gameplay/tilemap.toy +++ b/assets/scripts/gameplay/tilemap.toy @@ -50,10 +50,10 @@ var camH = 720; //lifecycle functions fn onLoad(node: opaque) { - node.loadTexture("sprites:/tileset.png"); + node.loadNodeTexture("sprites:/tileset.png"); } -fn customOnDraw(node: opaque) { +fn customOnDraw(node: opaque, parentX: int, parentY: int) { if (tilemap == null) { return; } @@ -77,7 +77,7 @@ fn customOnDraw(node: opaque) { 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, j * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); + node.drawNode(i * TILE_WIDTH + parentX, j * TILE_HEIGHT + parentY, TILE_WIDTH, TILE_HEIGHT); } } } diff --git a/assets/scripts/init.toy b/assets/scripts/init.toy index cf15d34..e7377a2 100644 --- a/assets/scripts/init.toy +++ b/assets/scripts/init.toy @@ -31,7 +31,7 @@ //this function must always be called, or the engine won't run - initWindow("Skyland", 32*16, 32*16, false); //TODO: custom FPS setting + initWindow("Skyland", 32*16, 32*16 + 32, false); //TODO: custom FPS setting //kick off the logic of the scene graph loadRootNode("scripts:/gameplay/scene.toy"); diff --git a/assets/sprites/stepcounter.png b/assets/sprites/stepcounter.png new file mode 100644 index 0000000000000000000000000000000000000000..3b03f0b18bf46a8c51b908a9ede8a2db9ec3f60b GIT binary patch literal 160 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnE1|*BCs=fdz#^NA%Cx&(BWL^R}Ea{HEjtmSN z`?>!lvI6;>1s;*b3=DinK$vl=HlH+5(A3k#F(ktM?YV`F3<^9)4(Lz%+x}u&!i|MZ q-JN?+y_count != 2) { - interpreter->errorOutput("Incorrect number of arguments passed to loadTexture\n"); + interpreter->errorOutput("Incorrect number of arguments passed to loadNodeTexture\n"); return -1; } @@ -352,7 +352,7 @@ static int nativeLoadTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arg //check argument types if (!TOY_IS_STRING(drivePathLiteral) || !TOY_IS_OPAQUE(nodeLiteral)) { - interpreter->errorOutput("Incorrect argument type passed to loadTexture\n"); + interpreter->errorOutput("Incorrect argument type passed to loadNodeTexture\n"); Toy_freeLiteral(drivePathLiteral); Toy_freeLiteral(nodeLiteral); return -1; @@ -389,9 +389,9 @@ static int nativeLoadTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arg return 0; } -static int nativeFreeTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { +static int nativeFreeNodeTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) { if (arguments->count != 1) { - interpreter->errorOutput("Incorrect number of arguments passed to freeTexture\n"); + interpreter->errorOutput("Incorrect number of arguments passed to freeNodeTexture\n"); return -1; } @@ -405,7 +405,7 @@ static int nativeFreeTexture(Toy_Interpreter* interpreter, Toy_LiteralArray* arg //check argument types if (!TOY_IS_OPAQUE(nodeLiteral)) { - interpreter->errorOutput("Incorrect argument type passed to freeTexture\n"); + interpreter->errorOutput("Incorrect argument type passed to freeNodeTexture\n"); Toy_freeLiteral(nodeLiteral); return -1; } @@ -970,8 +970,8 @@ int Box_hookNode(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Liter {"freeChildNode", nativeFreeChildNode}, {"getParentNode", nativeGetParentNode}, {"getChildNodeCount", nativeGetChildNodeCount}, - {"loadTexture", nativeLoadTexture}, - {"freeTexture", nativeFreeTexture}, + {"loadNodeTexture", nativeLoadNodeTexture}, + {"freeNodeTexture", nativeFreeNodeTexture}, {"setNodeRect", nativeSetNodeRect}, //get rect {"setNodeFrames", nativeSetNodeFrames}, @@ -983,7 +983,7 @@ int Box_hookNode(Toy_Interpreter* interpreter, Toy_Literal identifier, Toy_Liter {"setNodeText", nativeSetNodeText}, {"callNodeFn", nativeCallNodeFn}, - //TODO: get rect, get node var, create empty node + //TODO: get rect, get node var, create empty node, set node color (tinting) {NULL, NULL}, };