Added node scaling, and a few utility functions
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user