Added node scaling, and a few utility functions

This commit is contained in:
2023-06-24 21:30:33 +10:00
parent d83ef10c1f
commit 0671a89d43
18 changed files with 377 additions and 588 deletions

View File

@@ -2,40 +2,23 @@ import standard;
import node;
import random;
//constants
var SPEED: int const = 4;
var SPRITE_WIDTH: int const = 64;
var SPRITE_HEIGHT: int const = 64;
var TILE_WIDTH: int const = 32;
var TILE_HEIGHT: int const = 32;
//variables
var parent: opaque = null; //cache the parent for quick access
var gridX: int = 1; //position on the game grid
var gridY: int = 1;
var realX: int = 0; //will change until realX = gridX * SPRITE_WIDTH
var realY: int = 0;
var motionX: int = 0; //normalized movement direction
var motionY: int = 0;
var gridPositionX: int = 1;
var gridPositionY: int = 1;
var direction: int = null; //BUGFIX: animation not looping properly
var stepAI: int = 0;
//polyfills - animating different cycles on one image
var stepCount: int = 0;
//polyfills - animating different cycles on one image
var walkAnimationCounter: int = 0;
fn faceDown(node: opaque) {
if (direction == 0) return;
direction = 0;
node.setNodeRect(0, 0, 32, 32);
node.setNodeFrames(2);
walkAnimationCounter = 12;
}
fn faceUp(node: opaque) {
@@ -43,6 +26,7 @@ fn faceUp(node: opaque) {
direction = 1;
node.setNodeRect(32 * 2, 0, 32, 32);
node.setNodeFrames(2);
walkAnimationCounter = 12;
}
fn faceRight(node: opaque) {
@@ -50,6 +34,7 @@ fn faceRight(node: opaque) {
direction = 2;
node.setNodeRect(32 * 4, 0, 32, 32);
node.setNodeFrames(2);
walkAnimationCounter = 12;
}
fn faceLeft(node: opaque) {
@@ -57,34 +42,25 @@ fn faceLeft(node: opaque) {
direction = 3;
node.setNodeRect(32 * 6, 0, 32, 32);
node.setNodeFrames(2);
walkAnimationCounter = 12;
}
//accessors & mutators
fn setGridPos(node: opaque, x: int, y: int) {
gridX = x;
gridY = y;
fn setGridPosition(node: opaque, x: int, y: int) {
gridPositionX = x;
gridPositionY = y;
if (realX == null) {
realX = gridX * TILE_WIDTH;
}
if (realY == null) {
realY = gridY * TILE_HEIGHT;
}
node.setNodePositionX(floor( gridPositionX * node.getNodeRectW() / node.getNodeScaleX() ));
node.setNodePositionY(floor( gridPositionY * node.getNodeRectH() / node.getNodeScaleY() ));
}
fn setRealPos(node: opaque, x: int, y: int) {
realX = x;
realY = y;
fn getGridPositionX(node: opaque) {
return gridPositionX;
}
fn getGridPos(node: opaque) {
return [gridX, gridY];
}
fn getRealPos(node: opaque) {
return [realX, realY];
fn getGridPositionY(node: opaque) {
return gridPositionY;
}
@@ -92,28 +68,23 @@ fn getRealPos(node: opaque) {
fn onLoad(node: opaque) {
node.loadNodeTexture("sprites:/drone.png");
node.faceDown();
}
fn onInit(node: opaque) {
parent = node.getParentNode();
node.setNodeScaleX(CAMERA_SCALE_X);
node.setNodeScaleY(CAMERA_SCALE_Y);
}
fn onStep(node: opaque) {
if (++stepCount >= 5) {
if (++walkAnimationCounter >= 12) {
node.incrementCurrentNodeFrame();
stepCount = 0;
walkAnimationCounter = 0;
}
//calc movement
var distX = gridX * TILE_WIDTH - realX;
var distY = gridY * TILE_HEIGHT - realY;
//move in realspace
var distX = gridPositionX * TILE_PIXEL_WIDTH - node.getNodePositionX();
var distY = gridPositionY * TILE_PIXEL_HEIGHT - node.getNodePositionY();
motionX = normalize(distX);
motionY = normalize(distY);
//make movement
realX += abs(distX) > SPEED ? SPEED * motionX : distX;
realY += abs(distY) > SPEED ? SPEED * motionY : distY;
node.setNodeMotionX( normalize(distX) );
node.setNodeMotionY( normalize(distY) );
}
fn onFree(node: opaque) {
@@ -121,10 +92,23 @@ fn onFree(node: opaque) {
}
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);
}
var posX: int = node.getNodeWorldPositionX();
var posY: int = node.getNodeWorldPositionY();
var scaleX: float = node.getNodeWorldScaleX();
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
var originOffsetX: int = node.getNodeRectW() * int scaleX / 4;
var originOffsetY: int = node.getNodeRectH() * int scaleY / 2;
node.drawNode(
floor(posX * scaleX) - originOffsetX,
floor(posY * scaleY) - originOffsetY,
floor(node.getNodeRectW() * scaleX),
floor(node.getNodeRectH() * scaleY)
);
}
//gameplay functions
fn runAI(node: opaque, rng: opaque) {
@@ -155,9 +139,9 @@ fn runAI(node: opaque, rng: opaque) {
node.faceLeft();
}
if (parent.callNodeFn("getCollisionAt", gridX + moveX, gridY + moveY)) {
gridX += moveX;
gridY += moveY;
if (node.getParentNode().callNodeFn("getWalkableAt", gridPositionX + moveX, gridPositionY + moveY)) {
gridPositionX += moveX;
gridPositionY += moveY;
}
}
}

View File

@@ -1,43 +1,26 @@
import standard;
import node;
//constants
var SPEED: int const = 4;
var SPRITE_WIDTH: int const = 64;
var SPRITE_HEIGHT: int const = 64;
var TILE_WIDTH: int const = 32;
var TILE_HEIGHT: int const = 32;
//variables
var parent: opaque = null; //cache the parent for quick access
var gridX: int = 1; //position on the game grid
var gridY: int = 1;
var realX: int = null; //will change until realX = gridX * SPRITE_WIDTH
var realY: int = null;
var motionX: int = 0; //normalized movement direction
var motionY: int = 0;
var gridPositionX: int = 1; //position on the game grid
var gridPositionY: int = 1;
var inputX: int = 0; //cache the keyboard input
var inputY: int = 0;
var direction: int = null; //BUGFIX: animation not looping properly
var enableMovementCounter: int = 30 * 3; //BUGFIX: freeze while drones reach their starting spot
var attackCounter: int = 0;
var enableMovementCounter: int = 60 * 3; //BUGFIX: freeze while drones reach their starting spot
//polyfills - animating different cycles on one image
var stepCount: int = 0;
var walkAnimationCounter: int = 0;
var attackAnimationCounter: int = 0;
//utils for facing different directions (idling)
fn faceDown(node: opaque) {
if (direction == 0) return;
direction = 0;
node.setNodeRect(0, 0, 32, 32);
node.setNodeFrames(4);
walkAnimationCounter = 12;
}
fn faceUp(node: opaque) {
@@ -45,6 +28,7 @@ fn faceUp(node: opaque) {
direction = 1;
node.setNodeRect(32 * 4, 0, 32, 32);
node.setNodeFrames(4);
walkAnimationCounter = 12;
}
fn faceRight(node: opaque) {
@@ -52,6 +36,7 @@ fn faceRight(node: opaque) {
direction = 2;
node.setNodeRect(32 * 8, 0, 32, 32);
node.setNodeFrames(4);
walkAnimationCounter = 12;
}
fn faceLeft(node: opaque) {
@@ -59,45 +44,33 @@ fn faceLeft(node: opaque) {
direction = 3;
node.setNodeRect(32 * 12, 0, 32, 32);
node.setNodeFrames(4);
walkAnimationCounter = 12;
}
//accessors & mutators
fn setGridPos(node: opaque, x: int, y: int) {
gridX = x;
gridY = y;
fn setGridPosition(node: opaque, x: int, y: int) {
gridPositionX = x;
gridPositionY = y;
if (realX == null) {
realX = gridX * TILE_WIDTH;
}
if (realY == null) {
realY = gridY * TILE_HEIGHT;
}
node.setNodePositionX(floor( gridPositionX * node.getNodeRectW() / node.getNodeScaleX() ));
node.setNodePositionY(floor( gridPositionY * node.getNodeRectH() / node.getNodeScaleY() ));
}
fn setRealPos(node: opaque, x: int, y: int) {
realX = x;
realY = y;
fn getGridPositionX(node: opaque) {
return gridPositionX;
}
fn getGridPos(node: opaque) {
return [gridX, gridY];
fn getGridPositionY(node: opaque) {
return gridPositionY;
}
fn getRealPos(node: opaque) {
return [realX, realY];
}
//lifecycle functions
fn onLoad(node: opaque) {
node.loadNodeTexture("sprites:/lejana.png");
node.loadNodeTexture("sprites:/lejana.png"); //NOTE: all of this script is mapped to this sprite sheet
node.faceDown();
}
fn onInit(node: opaque) {
parent = node.getParentNode();
node.setNodeScaleX(CAMERA_SCALE_X);
node.setNodeScaleY(CAMERA_SCALE_Y);
}
fn onStep(node: opaque) {
@@ -106,91 +79,82 @@ fn onStep(node: opaque) {
enableMovementCounter--;
return;
}
//process input when aligned to a grid
if (realX / TILE_WIDTH == gridX && realY / TILE_HEIGHT == gridY && motionX == 0 && motionY == 0) {
//facing
if (inputY > 0) {
node.faceDown();
}
if (inputY < 0) {
node.faceUp();
}
if (inputX > 0) {
node.faceRight();
}
if (inputX < 0) {
node.faceLeft();
}
//disallow wall phasing
if (inputX != 0 && parent.callNodeFn("getCollisionAt", gridX + inputX, gridY) != true) {
inputX = 0;
}
if (inputY != 0 && parent.callNodeFn("getCollisionAt", gridX, gridY + inputY) != true) {
inputY = 0;
}
//disallow diagonal movement
if (abs(inputX) != 0) {
gridX += inputX;
}
else {
gridY += inputY;
}
//trigger the world
if (inputX != 0 || inputY != 0) {
parent.callNodeFn("runAI");
}
}
//actually animate
if (++stepCount >= 5) {
if (motionX == 0 && motionY == 0 && inputX == 0 && inputY == 0) {
stepCount = 0;
}
if (attackCounter > 0) {
attackCounter--;
}
node.incrementCurrentNodeFrame();
}
//animation - standing still
if (attackCounter == 0 && stepCount == 0) {
//animation - start idling
if (attackAnimationCounter == 0) {
//move to standing state
if (node.getNodeRectY() != 0) {
node.setNodeRect(direction * 32 * 4, 0, 32, 32);
node.setNodeFrames(4);
walkAnimationCounter = 12;
}
}
//animation - attacking
if (attackCounter > 0 && stepCount == 0) {
//animation - start attacking
if (--attackAnimationCounter >= 0) {
//move to attacking state
if (node.getNodeRectY() != 32) {
node.setNodeRect(direction * 32 * 4, 32, 32, 32);
node.setNodeFrames(3);
stepCount = 0;
}
if ((attackAnimationCounter+1) % 4 == 0) { //+1 for a bugfix
node.incrementCurrentNodeFrame();
}
//skip out
return;
}
//calc movement
var distX = gridX * TILE_WIDTH - realX;
var distY = gridY * TILE_HEIGHT - realY;
//facing
if (inputY > 0) {
node.faceDown();
}
motionX = normalize(distX);
motionY = normalize(distY);
else if (inputY < 0) {
node.faceUp();
}
//make movement
realX += abs(distX) > SPEED ? SPEED * motionX : distX;
realY += abs(distY) > SPEED ? SPEED * motionY : distY;
else if (inputX > 0) {
node.faceRight();
}
else if (inputX < 0) {
node.faceLeft();
}
//BUGFIX for smooth animation fo
var lesser = 0;
if ((inputX != 0 || inputY != 0) && attackAnimationCounter == 0) {
lesser = 6;
}
//actually animate
if (--walkAnimationCounter <= lesser) {
node.incrementCurrentNodeFrame();
walkAnimationCounter = 12;
}
var parent = node.getParentNode(); //check for collisions from the parent
if (parent.callNodeFn("getWalkableAt", gridPositionX + inputX, gridPositionY + inputY) && abs(inputX) != abs(inputY)) {
//calc movement
gridPositionX += inputX;
gridPositionY += inputY;
parent.callNodeFn("runAI");
}
//move in realspace
var distX = gridPositionX * TILE_PIXEL_WIDTH - node.getNodePositionX();
var distY = gridPositionY * TILE_PIXEL_HEIGHT - node.getNodePositionY();
node.setNodeMotionX( normalize(distX) );
node.setNodeMotionY( normalize(distY) );
//reset input
inputX = 0;
inputY = 0;
}
fn onFree(node: opaque) {
@@ -198,8 +162,22 @@ fn onFree(node: opaque) {
}
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);
var posX: int = node.getNodeWorldPositionX();
var posY: int = node.getNodeWorldPositionY();
var scaleX: float = node.getNodeWorldScaleX();
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
var originOffsetX: int = node.getNodeRectW() * int scaleX / 4;
var originOffsetY: int = node.getNodeRectH() * int scaleY / 2;
node.drawNode(
floor(posX * scaleX) - originOffsetX,
floor(posY * scaleY) - originOffsetY,
floor(node.getNodeRectW() * scaleX),
floor(node.getNodeRectH() * scaleY)
);
}
@@ -210,8 +188,14 @@ fn onKeyDown(node: opaque, event: string) {
return;
}
//enable attack
if (event == "character_attack" && inputX == 0 && inputY == 0) {
attackCounter = 3;
attackAnimationCounter = 12;
return;
}
//if moving, don't take any more input
if (node.getNodeMotionX() != 0 || node.getNodeMotionY() != 0) {
return;
}
@@ -258,7 +242,6 @@ fn onKeyUp(node: opaque, event: string) {
}
}
//polyfills - move these to standard
fn normalize(x): int {
if (x > 0) {

View File

@@ -2,45 +2,42 @@ import standard;
import node;
import random;
//consts
var TILE_WIDTH: int const = 32;
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;
//persistent members
var tilemap: opaque = null;
var player: opaque = null;
var stepCounter: opaque = null;
var player: opaque = null;
var enemies: [opaque] = null;
var enemies: [opaque] = [];
var collisionMap: [bool] = null; //cache this, since it won't change during a level
var rng: opaque = null;
var collisionDataCache: [bool] = null;
var rng: opaque = null; //TODO: does this have an opaque tag?
//lifecycle functions
fn onLoad(node: opaque) {
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");
}
fn onInit(node: opaque) {
node.generateLevel(clock().hash(), MAP_WIDTH, MAP_HEIGHT);
//load the map stuff
tilemap = node.loadChild("scripts:/gameplay/tilemap.toy");
node.generateScene(clock().hash(), MAP_GRID_WIDTH, MAP_GRID_HEIGHT);
//load and initialize the UI (depends on map stuff)
stepCounter = node.loadChild("scripts:/gameplay/step-counter.toy");
stepCounter.setNodeRect(
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);
//offset the scene node, so it shows the map outside separate from the UI
node.setNodePositionY(16);
}
fn onFree(node: opaque) {
rng.freeRandomGenerator();
//cleanup
if (rng != null) {
rng.freeRandomGenerator();
}
}
//fn onStep(node: opaque) {
@@ -48,36 +45,32 @@ fn onFree(node: opaque) {
//}
fn onDraw(node: opaque) {
//use the onDraw to sort - skip too many steps
node.sortChildrenNode(depthComparator);
stepCounter.callNodeFn("customOnDraw", 0, 0, TILE_WIDTH * MAP_WIDTH, 32);
}
//utils - polyfills
fn loadChild(parent: opaque, fname: string) {
var child: opaque = loadNode(fname);
parent.pushNode(child);
return child;
}
//gameplay functions
fn generateLevel(node: opaque, seed: int, width: int, height: int) {
fn generateScene(node: opaque, seed: int, width: int, height: int) {
//reset the array
enemies = [];
if (rng != null) rng.freeRandomGenerator();
if (rng != null) {
rng.freeRandomGenerator();
}
rng = createRandomGenerator(seed);
//place player
player.callNodeFn("setGridPos", 1, 1);
//generate map
tilemap.callNodeFn("generateFromRng", rng, 16, 16);
collisionMap = tilemap.callNodeFn("getCollisionMap");
tilemap.callNodeFn("generateFromRng", rng, width, height);
collisionDataCache = tilemap.callNodeFn("getCollisionData");
//place player here, so drop dropping can avoid the player
player = node.loadChild("scripts:/gameplay/lejana.toy");
player.callNodeFn("setGridPosition", 1, 1);
//generate drones
var droneCount: int const = rng.generateRandomNumber() % 2 + 20;
for (var i = 0; i < droneCount; i++) {
var d = node.loadChild("scripts:/gameplay/drone.toy");
var d: opaque = node.loadChild("scripts:/gameplay/drone.toy");
d.initNode();
enemies.push(d);
@@ -86,36 +79,40 @@ fn generateLevel(node: opaque, seed: int, width: int, height: int) {
var y = 0;
//while generated spot is a collision, or too close to the player
while (x == 0 && y == 0 || node.getCollisionAt(x, y) == false || x < width / 6 * 2 && y < height / 6 * 2) {
while (x == 0 && y == 0 || node.getWalkableAt(x, y) == false || x < width / 6 * 2 && y < height / 6 * 2) {
x = rng.generateRandomNumber() % width;
y = rng.generateRandomNumber() % height;
}
d.callNodeFn("setGridPos", x, y);
d.callNodeFn("setRealPos", (width -1) * TILE_WIDTH, (height - 1) * TILE_HEIGHT);
d.callNodeFn("setGridPosition", x, y);
d.setNodePositionX(MAP_GRID_WIDTH * TILE_PIXEL_WIDTH);
d.setNodePositionY(MAP_GRID_HEIGHT * TILE_PIXEL_HEIGHT);
}
}
fn getCollisionAt(node: opaque, x: int, y: int) {
if (collisionMap == null || x == null || y == null) {
fn getWalkableAt(node: opaque, x: int, y: int) {
if (collisionDataCache == null || x == null || y == null) {
return false;
}
//entities
var pos = player.callNodeFn("getGridPos");
if (x == pos[0] && y == pos[1]) {
if (x == player.callNodeFn("getGridPositionX") && y == player.callNodeFn("getGridPositionY")) {
return false;
}
for (var i = 0; i < enemies.length(); i++) {
var pos = enemies[i].callNodeFn("getGridPos");
if (x == pos[0] && y == pos[1]) {
if (x == enemies[i].callNodeFn("getGridPositionX") && y == enemies[i].callNodeFn("getGridPositionY")) {
return false;
}
}
//outside the grid
if (y * MAP_GRID_WIDTH + x < 0 || y * MAP_GRID_WIDTH + x >= collisionDataCache.length()) {
return false;
}
//default
return collisionMap[y * MAP_WIDTH + x];
return collisionDataCache[y * MAP_GRID_WIDTH + x];
}
fn runAI(node: opaque) {
@@ -126,21 +123,28 @@ fn runAI(node: opaque) {
stepCounter.callNodeFn("alterRemainingSteps", -1);
}
fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth
var lhsPos = lhs.callNodeFn("getRealPos");
var rhsPos = rhs.callNodeFn("getRealPos");
//utils - polyfills
fn loadChild(parent: opaque, fname: string) {
//TODO: add this to the API proper
var child: opaque = loadNode(fname);
parent.pushNode(child);
return child;
}
if (lhsPos == null || rhsPos == null) { //BUGFIX: children without that function
fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth
var lhsPositionY = lhs.getNodeWorldPositionY();
var rhsPositionY = rhs.getNodeWorldPositionY();
if (lhsPositionY == null || rhsPositionY == null) { //BUGFIX: children without that function
return true;
}
if (lhsPos[1] == rhsPos[1]) { //BUGFIX: prevent z-fighting
return lhsPos[0] <= rhsPos[0];
if (lhsPositionY == rhsPositionY) { //BUGFIX: prevent z-fighting
var lhsPositionX = lhs.getNodeWorldPositionX();
var rhsPositionX = rhs.getNodeWorldPositionX();
return lhsPositionX < rhsPositionX;
}
return lhsPos[1] < rhsPos[1];
return lhsPositionY < rhsPositionY;
}
fn getCameraPos(node: opaque) {
return [CAMERA_X, CAMERA_Y];
}

View File

@@ -1,9 +1,19 @@
import node;
//utils
//members
var maxSteps: int = 0;
var remainingSteps: int = 0;
//accessors
fn getMaxSteps(node: opaque) {
return maxSteps;
}
fn getRemainingSteps(node: opaque) {
return remainingSteps;
}
//mutators
fn setMaxSteps(node: opaque, steps: int) {
maxSteps = steps;
remainingSteps = steps;
@@ -13,14 +23,7 @@ fn setRemainingSteps(node: opaque, steps: int) {
remainingSteps = steps;
}
fn getMaxSteps(node: opaque) {
return maxSteps;
}
fn getRemainingSteps(node: opaque) {
return remainingSteps;
}
//utility mutators
fn alterRemainingSteps(node: opaque, increment: int) {
remainingSteps += increment;
if (remainingSteps > maxSteps) {
@@ -47,21 +50,18 @@ fn onLoad(node: opaque) {
node
.loadChild("scripts:/gameplay/text.toy")
.setNodeText("fonts:/alphbeta.ttf", 32, "Water", 0, 60, 240, 255);
.setNodeText("fonts:/alphbeta.ttf", 32, "Water", 0, 60, 240, 255); //rgba
}
fn onFree(node: opaque) {
node.freeNodeTexture();
}
fn customOnDraw(node: opaque, x: int, y: int, w: int, h: int) {
fn onDraw(node: opaque) {
if (remainingSteps > 0 && maxSteps > 0) {
var tmp = float remainingSteps / maxSteps * w;
var tmp = float remainingSteps / maxSteps * node.getNodeRectW();
var h = node.getNodeRectH();
node.drawNode(x, y, int tmp, h);
for (var i: int = 0; i < node.getChildNodeCount(); i++) {
node.getChildNode(i).callNodeFn("customOnDraw", x, y);
}
node.drawNode(node.getNodePositionX(), node.getNodePositionY(), int tmp, h);
}
}

View File

@@ -2,10 +2,17 @@ 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);
fn onDraw(node: opaque) {
var posX: int = node.getNodePositionX();
var posY: int = node.getNodePositionY();
var scaleX: float = node.getNodeScaleX();
var scaleY: float = node.getNodeScaleY();
node.drawNode(floor(posX * scaleX), floor(posY * scaleY));
}
fn onFree(node: opaque) {
node.freeNodeTexture();
}
}
//TODO: polyfill?

View File

@@ -1,22 +1,8 @@
import standard;
import node;
//consts
var TILE_WIDTH: int const = 32;
var TILE_HEIGHT: int const = 32;
//vars
var mapWidth: int = 0;
var mapHeight: int = 0;
//map between the identity and position on the sprite sheet
var rawmap: [[string]] = null;
var tilemap: [int] = null; // [x0, y0, x1, y1, ...]
var collisions: [bool] = null;
var tileset: [string : [int]] = [
//constants mapped to the given file "tileset.png"
var tileset: [string : [int]] const = [
"pillar": [0, 0, 0],
"floor-0": [0, 1, 1],
@@ -40,57 +26,61 @@ var tileset: [string : [int]] = [
"edge-br": [3, 4, 0]
];
//raw string data
var rawmap: [[string]] = null;
var parent: opaque = null;
//baked and usable blobs of data
var tilemap: [int] = null; // [x0, y0, x1, y1, ...]
var collisions: [bool] = null;
//metadata about the above blobs
var tilemapGridWidth: int = 0;
var tilemapGridHeight: int = 0;
//debug vars - what are these for?
var camX = 0;
var camY = 0;
var camW = 1080;
var camH = 720;
//fast accessors
fn getTilemapData(node: opaque) {
return tilemap;
}
fn getCollisionData(node: opaque) {
return collisions;
}
//lifecycle functions
fn onLoad(node: opaque) {
node.loadNodeTexture("sprites:/tileset.png");
}
fn onInit(node: opaque) {
//find root
parent = node;
while (parent.getParentNode() != null) {
parent = parent.getParentNode();
}
node.setNodeScaleX(CAMERA_SCALE_X);
node.setNodeScaleY(CAMERA_SCALE_Y);
}
fn onDraw(node: opaque) {
if (tilemap == null) {
return;
}
var camera = parent.callNodeFn("getCameraPos");
assert tilemap, "tilemap is null";
//calc the region to render
var lowerX: int = round((camX - camW/2.0) / TILE_WIDTH);
var upperX: int = round((camX - camW*1.5) / TILE_WIDTH);
var lowerY: int = round((camY - camH/2.0) / TILE_HEIGHT);
var upperY: int = round((camY - camH*1.5) / TILE_HEIGHT);
var posX: int const = node.getNodeWorldPositionX();
var posY: int const = node.getNodeWorldPositionY();
//bounds check
lowerX = max(0, lowerX);
upperX = min(upperX < 0 ? abs(upperX) : 0, mapWidth);
lowerY = max(0, lowerY);
upperY = min(upperY < 0 ? abs(upperY) : 0, mapHeight);
var runner: int = 0;
//draw everything at twice the original size
var scaleX: float const = node.getNodeWorldScaleX();
var scaleY: float const = node.getNodeWorldScaleY();
//draw the tilemap
for (var j = lowerY; j < upperY; j++) {
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);
for (var j = 0; j < tilemapGridHeight; j++) {
for (var i = 0; i < tilemapGridWidth; i++) {
//set the rect of the node on the tilesheet - the "tilemap" var is a single blob of data
node.setNodeRect(
tilemap[j * tilemapGridWidth * 2 + i * 2] * TILE_PIXEL_WIDTH,
tilemap[j * tilemapGridWidth * 2 + i * 2 + 1] * TILE_PIXEL_HEIGHT,
TILE_PIXEL_WIDTH, TILE_PIXEL_HEIGHT
);
node.drawNode(i * TILE_WIDTH + camera[0], j * TILE_HEIGHT + camera[1], TILE_WIDTH, TILE_HEIGHT);
//draw to the screen
node.drawNode(
floor((i * TILE_PIXEL_WIDTH + posX) * scaleX),
floor((j * TILE_PIXEL_HEIGHT + posY) * scaleY),
floor(TILE_PIXEL_WIDTH * scaleX),
floor(TILE_PIXEL_HEIGHT * scaleY)
);
}
}
}
@@ -98,36 +88,38 @@ fn onDraw(node: opaque) {
//utils functions for map generation
fn generateFromRng(node: opaque, rng: opaque, width: int, height: int) {
rawmap = generateRawTilemap(rng, width, height);
//use debug room for now
rawmap = generateRawTilemapDebugRoom(rng, width, height);
tilemap = bakeTilemap(rawmap, width, height);
collisions = bakeCollisionMap(rawmap, width, height);
mapWidth = width;
mapHeight = height;
tilemapGridWidth = width;
tilemapGridHeight = height;
//debugging
print tilemap;
print collisions;
}
fn generateRawTilemap(rng: opaque, width: int, height: int) {
//"raw" tilemaps are not usable, they are just a 2d array of strings
fn generateRawTilemapDebugRoom(rng: opaque, width: int, height: int): [[string]] const {
import random;
//generate an empty grid
//generate a grid filled with only pillar tiles, as a starting point
var result: [[string]] = [];
var row: [string] = [];
for (var j: int = 0; j < height; j++) {
row.push("pillar");
}
for (var i: int = 0; i < width; i++) {
result.push(row);
}
//generate the contents of the grid - floor
//generate the walkable tiles of the grid - floor tiles
for (var j: int = 1; j < height - 1; j++) {
for (var i: int = 1; i < width - 1; i++) {
//select a random floor tile
//select a random floor tile (graphical effect only)
var x: int = rng.generateRandomNumber() % 4;
var t: string = "floor-" + string x;
@@ -153,45 +145,41 @@ fn generateRawTilemap(rng: opaque, width: int, height: int) {
result[0][height - 1] = "corner-bl";
result[width - 1][height - 1] = "corner-br";
//debug
//debugging
result[4][4] = "pillar";
result[width - 5][4] = "pillar";
result[4][height - 5] = "pillar";
result[width - 5][height - 5] = "pillar";
//return the raw result of strings
return result;
}
fn bakeCollisionMap(tilemap: [[string]], width: int, height: int) {
//generate an empty grid
var result: [bool] = [];
//extract the collision map
for (var j: int = 0; j < height; j++) {
for (var i: int = 0; i < width; i++) {
//almost - you still need one pair of parentheses
result.push(tileset[ tilemap[i][j] ][2] != 0);
}
}
return result;
}
fn bakeTilemap(tilemap: [[string]], width: int, height: int) {
//generate an empty grid
//"baking" converts raw tilemaps into usable blobs of data
fn bakeTilemap(tilemap: [[string]] const, width: int, height: int): [int] const {
var result: [int] = [];
//extract the position map
//extract the positions from the tileset
for (var j: int = 0; j < height; j++) {
for (var i: int = 0; i < width; i++) {
result.push(tileset[ tilemap[i][j] ][0]);
result.push(tileset[ tilemap[i][j] ][1]);
}
}
return result;
}
fn getCollisionMap(node: opaque) {
return collisions;
}
//this bakes the collision map as a separate object
fn bakeCollisionMap(tilemap: [[string]] const, width: int, height: int): [bool] const {
var result: [bool] = [];
//extract the walkable state
for (var j: int = 0; j < height; j++) {
for (var i: int = 0; i < width; i++) {
result.push(tileset[ tilemap[i][j] ][2] != 0);
}
}
return result;
}