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_HEIGHT: int const = 32;
var MAP_WIDTH: int const = 16;
var MAP_HEIGHT: int const = 16;
var tilemap: opaque = null;
var player: opaque = null;
@@ -12,7 +15,7 @@ var enemies: [opaque] = [];
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;
@@ -27,7 +30,7 @@ fn onLoad(node: opaque) {
}
fn onInit(node: opaque) {
node.generateLevel(clock().hash(), 16, 16);
node.generateLevel(clock().hash(), MAP_WIDTH, MAP_HEIGHT);
}
fn onStep(node: opaque) {
@@ -76,7 +79,7 @@ fn generateLevel(node: opaque, seed: int, width: int, height: int) {
collisionMap = tilemap.callNodeFn("getCollisionMap");
//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++) {
var d = node.loadChild("scripts:/gameplay/drone.toy");
d.initNode();
@@ -88,7 +91,7 @@ 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 / 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;
y = rng.generateRandomNumber() % height;
}
@@ -117,7 +120,7 @@ fn getCollisionAt(node: opaque, x: int, y: int) {
}
//default
return collisionMap[x][y];
return collisionMap[y * MAP_WIDTH + x];
}
fn runAI(node: opaque) {