Adjusted scale
This commit is contained in:
2
Toy
2
Toy
Submodule Toy updated: 76ddd5703e...f869c9425a
@@ -2,11 +2,14 @@ import standard;
|
||||
import node;
|
||||
|
||||
//constants
|
||||
var SPEED: int const = 3;
|
||||
var SPEED: int const = 2;
|
||||
|
||||
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
|
||||
@@ -71,13 +74,13 @@ fn onInit(node: opaque) {
|
||||
|
||||
fn onStep(node: opaque) {
|
||||
//process input when aligned to a grid
|
||||
if (realX / SPRITE_WIDTH == gridX && realY / SPRITE_HEIGHT == gridY && motionX == 0 && motionY == 0) {
|
||||
if (realX / TILE_WIDTH == gridX && realY / TILE_HEIGHT == gridY && motionX == 0 && motionY == 0) {
|
||||
//disallow wall phasing
|
||||
if (parent.callNodeFn("getCollisionAt", gridX + inputX, gridY) != true) {
|
||||
if (inputX != 0 && parent.callNodeFn("getCollisionAt", gridX + inputX, gridY) != true) {
|
||||
inputX = 0;
|
||||
}
|
||||
|
||||
if (parent.callNodeFn("getCollisionAt", gridX, gridY + inputY) != true) {
|
||||
if (inputY != 0 && parent.callNodeFn("getCollisionAt", gridX, gridY + inputY) != true) {
|
||||
inputY = 0;
|
||||
}
|
||||
|
||||
@@ -119,8 +122,8 @@ fn onStep(node: opaque) {
|
||||
}
|
||||
|
||||
//calc movement
|
||||
var distX = gridX * SPRITE_WIDTH - realX;
|
||||
var distY = gridY * SPRITE_HEIGHT - realY;
|
||||
var distX = gridX * TILE_WIDTH - realX;
|
||||
var distY = gridY * TILE_HEIGHT - realY;
|
||||
|
||||
motionX = normalize(distX);
|
||||
motionY = normalize(distY);
|
||||
@@ -146,7 +149,7 @@ fn onDraw(node: opaque) {
|
||||
py = py != null ? py : 0;
|
||||
}
|
||||
|
||||
node.drawNode(realX + px, realY + py, SPRITE_WIDTH, SPRITE_HEIGHT);
|
||||
node.drawNode(realX + px - SPRITE_WIDTH / 4, realY + py - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
|
||||
}
|
||||
|
||||
//event functions
|
||||
|
||||
@@ -6,6 +6,9 @@ var player: opaque = null;
|
||||
|
||||
var collisionMap: [[bool]] = null; //cache this, since it won't change during a level
|
||||
|
||||
var stepCounter: int = 0;
|
||||
var drawCounter: int = 0;
|
||||
|
||||
//lifecycle functions
|
||||
fn onLoad(node: opaque) {
|
||||
tilemap = node.loadChild("scripts:/gameplay/tilemap.toy");
|
||||
@@ -13,10 +16,22 @@ fn onLoad(node: opaque) {
|
||||
}
|
||||
|
||||
fn onInit(node: opaque) {
|
||||
tilemap.callNodeFn("generateFromSeed", clock().hash(), 8, 8);
|
||||
tilemap.callNodeFn("generateFromSeed", clock().hash(), 16, 16);
|
||||
collisionMap = tilemap.callNodeFn("getCollisionMap");
|
||||
}
|
||||
|
||||
fn onStep(node: opaque) {
|
||||
if (++stepCounter >= 60) {
|
||||
print "FPS: " + string drawCounter + " / 60";
|
||||
stepCounter = 0;
|
||||
drawCounter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn onDraw(node: opaque) {
|
||||
drawCounter++;
|
||||
}
|
||||
|
||||
//utils - polyfills
|
||||
fn loadChild(parent: opaque, fname: string) {
|
||||
var child: opaque = loadNode(fname);
|
||||
|
||||
@@ -2,11 +2,13 @@ import standard;
|
||||
import node;
|
||||
|
||||
//consts
|
||||
var TILE_WIDTH: int const = 64;
|
||||
var TILE_HEIGHT: int const = 64;
|
||||
var TILE_WIDTH: int const = 32;
|
||||
var TILE_HEIGHT: int const = 32;
|
||||
|
||||
//map between the identity and position on the sprite sheet
|
||||
var tilemap: [[string]] = null;
|
||||
var rawmap: [[string]] = null;
|
||||
|
||||
var tilemap: [[[int]]] = null;
|
||||
var collisions: [[bool]] = null;
|
||||
|
||||
var tileset: [string : [int]] = [
|
||||
@@ -77,7 +79,7 @@ fn onDraw(node: opaque) {
|
||||
//draw the tilemap
|
||||
for (var j = lowerY; j < upperY; j++) {
|
||||
for (var i = lowerX; i < upperX; i++) {
|
||||
var pos = tileset[tilemap[i][j]];
|
||||
var pos = tileset[ rawmap[i][j] ];
|
||||
node.setNodeRect(pos[0] * 16, pos[1] * 16, 16, 16);
|
||||
|
||||
node.drawNode(i * TILE_WIDTH, j * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
|
||||
@@ -88,14 +90,16 @@ fn onDraw(node: opaque) {
|
||||
|
||||
//utils functions for map generation
|
||||
fn generateFromSeed(node: opaque, seed: int, width: int, height: int) {
|
||||
tilemap = generateTilemap(seed, width, height);
|
||||
collisions = bakeCollisionMap(tilemap, width, height);
|
||||
rawmap = generateRawTilemap(seed, width, height);
|
||||
|
||||
tilemap = bakeTilemap(rawmap, width, height);
|
||||
collisions = bakeCollisionMap(rawmap, width, height);
|
||||
|
||||
print tilemap;
|
||||
print collisions;
|
||||
}
|
||||
|
||||
fn generateTilemap(seed: int, width: int, height: int) {
|
||||
fn generateRawTilemap(seed: int, width: int, height: int) {
|
||||
import random;
|
||||
var rng: opaque = createRandomGenerator(seed);
|
||||
|
||||
@@ -165,6 +169,30 @@ fn bakeCollisionMap(tilemap: [[string]], width: int, height: int) {
|
||||
return result;
|
||||
}
|
||||
|
||||
fn bakeTilemap(tilemap: [[string]], width: int, height: int) {
|
||||
//generate an empty grid
|
||||
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++) {
|
||||
result.push(row);
|
||||
}
|
||||
|
||||
//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[i][j][0] = (tileset[ tilemap[i][j] ][0]);
|
||||
result[i][j][1] = (tileset[ tilemap[i][j] ][1]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
fn getCollisionMap(node: opaque) {
|
||||
return collisions;
|
||||
}
|
||||
BIN
assets/sprites/drone.png
Normal file
BIN
assets/sprites/drone.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.4 KiB |
Reference in New Issue
Block a user