Added water-based step counter

This commit is contained in:
2023-03-07 21:28:16 +11:00
parent 25dadb9bfd
commit 3052b2a265
13 changed files with 124 additions and 69 deletions

View File

@@ -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) {

View File

@@ -1,6 +1 @@
//this file is a polyfill TODO: fix this
fn onDraw(node: opaque) {
//debug draw at the origin
node.drawNode(0, 0);
}

View File

@@ -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);
}

View File

@@ -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);
}

View File

@@ -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];
}

View File

@@ -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);
}
}
}

View File

@@ -0,0 +1,9 @@
import node;
fn customOnDraw(node: opaque, parentX: int, parentY: int) {
node.drawNode(parentX, parentY);
}
fn onFree(node: opaque) {
node.freeNodeTexture();
}

View File

@@ -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);
}
}
}

View File

@@ -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");

Binary file not shown.

After

Width:  |  Height:  |  Size: 160 B