Flattened map arrays to increase FPS

This commit is contained in:
2023-03-07 04:53:04 +11:00
parent 14524c261b
commit 3588846bb4
2 changed files with 35 additions and 45 deletions

View File

@@ -5,6 +5,9 @@ import node;
var TILE_WIDTH: int const = 32; var TILE_WIDTH: int const = 32;
var TILE_HEIGHT: int const = 32; var TILE_HEIGHT: int const = 32;
var MAP_WIDTH: int const = 16;
var MAP_HEIGHT: int const = 16;
var tilemap: opaque = null; var tilemap: opaque = null;
var player: opaque = null; var player: opaque = null;
@@ -12,7 +15,7 @@ var enemies: [opaque] = [];
var entities: [opaque] = null; //full list of entities var entities: [opaque] = null; //full list of entities
var collisionMap: [[bool]] = null; //cache this, since it won't change during a level var collisionMap: [bool] = null; //cache this, since it won't change during a level
var rng: opaque = null; var rng: opaque = null;
@@ -27,7 +30,7 @@ fn onLoad(node: opaque) {
} }
fn onInit(node: opaque) { fn onInit(node: opaque) {
node.generateLevel(clock().hash(), 16, 16); node.generateLevel(clock().hash(), MAP_WIDTH, MAP_HEIGHT);
} }
fn onStep(node: opaque) { fn onStep(node: opaque) {
@@ -76,7 +79,7 @@ fn generateLevel(node: opaque, seed: int, width: int, height: int) {
collisionMap = tilemap.callNodeFn("getCollisionMap"); collisionMap = tilemap.callNodeFn("getCollisionMap");
//generate drones //generate drones
var droneCount: int const = rng.generateRandomNumber() % 2 + 2; var droneCount: int const = rng.generateRandomNumber() % 2 + 20;
for (var i = 0; i < droneCount; i++) { for (var i = 0; i < droneCount; i++) {
var d = node.loadChild("scripts:/gameplay/drone.toy"); var d = node.loadChild("scripts:/gameplay/drone.toy");
d.initNode(); d.initNode();
@@ -88,7 +91,7 @@ fn generateLevel(node: opaque, seed: int, width: int, height: int) {
var y = 0; var y = 0;
//while generated spot is a collision, or too close to the player //while generated spot is a collision, or too close to the player
while ((x == 0 && y == 0) || node.getCollisionAt(x, y) == false || (x < (width / 2) && y < (height / 2))) { //TODO: toy bug - no short-circuiting? while ((x == 0 && y == 0) || node.getCollisionAt(x, y) == false || (x < (width / 3 * 2) && y < (height / 3 * 2))) { //TODO: toy bug - no short-circuiting?
x = rng.generateRandomNumber() % width; x = rng.generateRandomNumber() % width;
y = rng.generateRandomNumber() % height; y = rng.generateRandomNumber() % height;
} }
@@ -117,7 +120,7 @@ fn getCollisionAt(node: opaque, x: int, y: int) {
} }
//default //default
return collisionMap[x][y]; return collisionMap[y * MAP_WIDTH + x];
} }
fn runAI(node: opaque) { fn runAI(node: opaque) {

View File

@@ -5,11 +5,16 @@ import node;
var TILE_WIDTH: int const = 32; var TILE_WIDTH: int const = 32;
var TILE_HEIGHT: 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 //map between the identity and position on the sprite sheet
var rawmap: [[string]] = null; var rawmap: [[string]] = null;
var tilemap: [[[int]]] = null; var tilemap: [int] = null; // [x0, y0, x1, y1, ...]
var collisions: [[bool]] = null; var collisions: [bool] = null;
var tileset: [string : [int]] = [ var tileset: [string : [int]] = [
"pillar": [0, 0, 0], "pillar": [0, 0, 0],
@@ -59,28 +64,18 @@ fn customOnDraw(node: opaque) {
var lowerY: int = round((camY - camH/2.0) / TILE_HEIGHT); var lowerY: int = round((camY - camH/2.0) / TILE_HEIGHT);
var upperY: int = round((camY - camH*1.5) / TILE_HEIGHT); var upperY: int = round((camY - camH*1.5) / TILE_HEIGHT);
var rwidth = 0;
var rheight = 0;
if (tilemap.length() != 0) {
rwidth = tilemap.length();
if (tilemap[0].length() != 0) {
rheight = (tilemap[0].length()); //TODO: Some kind of Toy bug here, use parentheses
}
}
//bounds check //bounds check
lowerX = max(0, lowerX); lowerX = max(0, lowerX);
upperX = min(upperX < 0 ? abs(upperX) : 0, rwidth); upperX = min(upperX < 0 ? abs(upperX) : 0, mapWidth);
lowerY = max(0, lowerY); lowerY = max(0, lowerY);
upperY = min(upperY < 0 ? abs(upperY) : 0, rheight); upperY = min(upperY < 0 ? abs(upperY) : 0, mapHeight);
var runner: int = 0;
//draw the tilemap //draw the tilemap
for (var j = lowerY; j < upperY; j++) { for (var j = lowerY; j < upperY; j++) {
for (var i = lowerX; i < upperX; i++) { for (var i = lowerX; i < upperX; i++) {
var pos = tileset[ rawmap[i][j] ]; node.setNodeRect(tilemap[j * mapWidth * 2 + i * 2] * 16, tilemap[j * mapWidth * 2 + i * 2 + 1] * 16, 16, 16);
node.setNodeRect(pos[0] * 16, pos[1] * 16, 16, 16);
node.drawNode(i * TILE_WIDTH, j * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); node.drawNode(i * TILE_WIDTH, j * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
} }
@@ -95,6 +90,9 @@ fn generateFromRng(node: opaque, rng: opaque, width: int, height: int) {
tilemap = bakeTilemap(rawmap, width, height); tilemap = bakeTilemap(rawmap, width, height);
collisions = bakeCollisionMap(rawmap, width, height); collisions = bakeCollisionMap(rawmap, width, height);
mapWidth = width;
mapHeight = height;
print tilemap; print tilemap;
print collisions; print collisions;
} }
@@ -142,26 +140,24 @@ fn generateRawTilemap(rng: opaque, width: int, height: int) {
result[0][height - 1] = "corner-bl"; result[0][height - 1] = "corner-bl";
result[width - 1][height - 1] = "corner-br"; result[width - 1][height - 1] = "corner-br";
//debug
result[4][4] = "pillar";
result[width - 5][4] = "pillar";
result[4][height - 5] = "pillar";
result[width - 5][height - 5] = "pillar";
return result; return result;
} }
fn bakeCollisionMap(tilemap: [[string]], width: int, height: int) { fn bakeCollisionMap(tilemap: [[string]], width: int, height: int) {
//generate an empty grid //generate an empty grid
var result: [[bool]] = []; var result: [bool] = [];
var row: [bool] = [];
for (var j: int = 0; j < height; j++) {
row.push(false);
}
for (var i: int = 0; i < width; i++) {
result.push(row);
}
//extract the collision map //extract the collision map
for (var j: int = 0; j < height; j++) { for (var j: int = 0; j < height; j++) {
for (var i: int = 0; i < width; i++) { for (var i: int = 0; i < width; i++) {
//almost - you still need one pair of parentheses //almost - you still need one pair of parentheses
result[i][j] = (tileset[ tilemap[i][j] ][2] != 0); result.push(tileset[ tilemap[i][j] ][2] != 0);
} }
} }
@@ -170,22 +166,13 @@ fn bakeCollisionMap(tilemap: [[string]], width: int, height: int) {
fn bakeTilemap(tilemap: [[string]], width: int, height: int) { fn bakeTilemap(tilemap: [[string]], width: int, height: int) {
//generate an empty grid //generate an empty grid
var result: [[[int]]] = []; var result: [int] = [];
var row: [[int]] = [];
for (var j: int = 0; j < height; j++) {
row.push([0,0]);
}
for (var i: int = 0; i < width; i++) { //extract the position map
result.push(row);
}
//extract the collision map
for (var j: int = 0; j < height; j++) { for (var j: int = 0; j < height; j++) {
for (var i: int = 0; i < width; i++) { for (var i: int = 0; i < width; i++) {
//almost - you still need one pair of parentheses result.push(tileset[ tilemap[i][j] ][0]);
result[i][j][0] = (tileset[ tilemap[i][j] ][0]); result.push(tileset[ tilemap[i][j] ][1]);
result[i][j][1] = (tileset[ tilemap[i][j] ][1]);
} }
} }