Grid-based movement and collisions working
This commit is contained in:
@@ -1,72 +1,133 @@
|
|||||||
|
import standard;
|
||||||
import node;
|
import node;
|
||||||
|
|
||||||
//constants
|
//constants
|
||||||
var SPEED: int const = 3;
|
var SPEED: int const = 3;
|
||||||
|
|
||||||
|
var SPRITE_WIDTH: int const = 64;
|
||||||
|
var SPRITE_HEIGHT: int const = 64;
|
||||||
|
|
||||||
|
|
||||||
//variables
|
//variables
|
||||||
var parent: opaque = null; //cache the parent for quick access
|
var parent: opaque = null; //cache the parent for quick access
|
||||||
var posX: int = 50;
|
|
||||||
var posY: int = 50;
|
|
||||||
var WIDTH: int const = 128;
|
|
||||||
var HEIGHT: int const = 128;
|
|
||||||
|
|
||||||
var xspeed: int = 0;
|
var gridX: int = 1; //position on the game grid
|
||||||
var yspeed: int = 0;
|
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 inputX: int = 0; //cache the keyboard input
|
||||||
|
var inputY: int = 0;
|
||||||
|
|
||||||
|
|
||||||
//polyfills - animating different cycles on one image
|
//polyfills - animating different cycles on one image
|
||||||
var SPRITE_WIDTH: int const = 32;
|
|
||||||
var SPRITE_HEIGHT: int const = 32;
|
|
||||||
var stepCount: int = 0;
|
var stepCount: int = 0;
|
||||||
var stepDelay: int = 10;
|
|
||||||
|
|
||||||
fn faceDown(node: opaque) {
|
fn faceDown(node: opaque) {
|
||||||
node.setNodeRect(0, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
|
node.setNodeRect(0, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceUp(node: opaque) {
|
fn faceUp(node: opaque) {
|
||||||
node.setNodeRect(32 * 4, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
|
node.setNodeRect(32 * 4, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceLeft(node: opaque) {
|
fn faceLeft(node: opaque) {
|
||||||
node.setNodeRect(32 * 12, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
|
node.setNodeRect(32 * 12, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceRight(node: opaque) {
|
fn faceRight(node: opaque) {
|
||||||
node.setNodeRect(32 * 8, 0, SPRITE_WIDTH, SPRITE_HEIGHT);
|
node.setNodeRect(32 * 8, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//polyfills - move these to standard
|
||||||
|
fn normalize(x): int {
|
||||||
|
if (x > 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (x < 0) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//lifecycle functions
|
//lifecycle functions
|
||||||
fn onLoad(node: opaque) {
|
fn onLoad(node: opaque) {
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onInit(node: opaque) {
|
|
||||||
parent = node.getParentNode();
|
|
||||||
node.loadTexture("sprites:/lejana.png");
|
node.loadTexture("sprites:/lejana.png");
|
||||||
node.faceDown();
|
node.faceDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onStep(node: opaque) {
|
fn onInit(node: opaque) {
|
||||||
posX += xspeed;
|
parent = node.getParentNode();
|
||||||
posY += yspeed;
|
}
|
||||||
|
|
||||||
//animate after X steps
|
fn onStep(node: opaque) {
|
||||||
if (xspeed == 0 && yspeed == 0) {
|
//process input when aligned to a grid
|
||||||
|
if (realX / SPRITE_WIDTH == gridX && realY / SPRITE_HEIGHT == gridY && motionX == 0 && motionY == 0) {
|
||||||
|
//disallow wall phasing
|
||||||
|
if (parent.callNodeFn("getCollisionAt", gridX + inputX, gridY) != true) {
|
||||||
|
inputX = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parent.callNodeFn("getCollisionAt", gridX, gridY + inputY) != true) {
|
||||||
|
inputY = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
//disallow diagonal movement
|
||||||
|
if (abs(inputX) != 0) {
|
||||||
|
gridX += inputX;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
gridY += inputY;
|
||||||
|
}
|
||||||
|
|
||||||
|
//facing
|
||||||
|
if (inputY > 0) {
|
||||||
|
node.faceDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inputY < 0) {
|
||||||
|
node.faceUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inputX > 0) {
|
||||||
|
node.faceRight();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inputX < 0) {
|
||||||
|
node.faceLeft();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//animation
|
||||||
|
if (motionX == 0 && motionY == 0) {
|
||||||
stepCount = 0;
|
stepCount = 0;
|
||||||
node.setCurrentNodeFrame(0);
|
node.setCurrentNodeFrame(0);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
if (++stepCount >= stepDelay) {
|
if (++stepCount >= 10) {
|
||||||
node.incrementCurrentNodeFrame();
|
node.incrementCurrentNodeFrame();
|
||||||
stepCount = 0;
|
stepCount = 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//calc movement
|
||||||
|
var distX = gridX * SPRITE_WIDTH - realX;
|
||||||
|
var distY = gridY * SPRITE_HEIGHT - realY;
|
||||||
|
|
||||||
|
motionX = normalize(distX);
|
||||||
|
motionY = normalize(distY);
|
||||||
|
|
||||||
|
//make movement
|
||||||
|
realX += abs(distX) > SPEED ? SPEED * motionX : distX;
|
||||||
|
realY += abs(distY) > SPEED ? SPEED * motionY : distY;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
fn onFree(node: opaque) {
|
||||||
@@ -80,75 +141,55 @@ fn onDraw(node: opaque) {
|
|||||||
if (parent != null) {
|
if (parent != null) {
|
||||||
px = parent.callNodeFn("getX");
|
px = parent.callNodeFn("getX");
|
||||||
py = parent.callNodeFn("getY");
|
py = parent.callNodeFn("getY");
|
||||||
|
|
||||||
|
px = px != null ? px : 0;
|
||||||
|
py = py != null ? py : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
node.drawNode(posX + px, posY + py, WIDTH, HEIGHT);
|
node.drawNode(realX + px, realY + py, SPRITE_WIDTH, SPRITE_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
//event functions
|
//event functions
|
||||||
fn onKeyDown(node: opaque, event: string) {
|
fn onKeyDown(node: opaque, event: string) {
|
||||||
if (event == "character_up") {
|
if (event == "character_up") {
|
||||||
yspeed -= SPEED;
|
inputY -= 1;
|
||||||
faceUp(node);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_down") {
|
if (event == "character_down") {
|
||||||
yspeed += SPEED;
|
inputY += 1;
|
||||||
faceDown(node);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_left") {
|
if (event == "character_left") {
|
||||||
xspeed -= SPEED;
|
inputX -= 1;
|
||||||
faceLeft(node);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_right") {
|
if (event == "character_right") {
|
||||||
xspeed += SPEED;
|
inputX += 1;
|
||||||
faceRight(node);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onKeyUp(node: opaque, event: string) {
|
fn onKeyUp(node: opaque, event: string) {
|
||||||
if (event == "character_up" && yspeed < 0) {
|
if (event == "character_up" && inputY < 0) {
|
||||||
yspeed = 0;
|
inputY = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_down" && yspeed > 0) {
|
if (event == "character_down" && inputY > 0) {
|
||||||
yspeed = 0;
|
inputY = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_left" && xspeed < 0) {
|
if (event == "character_left" && inputX < 0) {
|
||||||
xspeed = 0;
|
inputX = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event == "character_right" && xspeed > 0) {
|
if (event == "character_right" && inputX > 0) {
|
||||||
xspeed = 0;
|
inputX = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onMouseMotion(node: opaque, x: int, y: int, xrel: int, yrel: int) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
|
||||||
//jump to pos
|
|
||||||
posX = x - WIDTH / 2;
|
|
||||||
posY = y - HEIGHT / 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseButtonUp(node: opaque, x: int, y: int, button: string) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseWheel(node: opaque, xrel: int, yrel: int) {
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import standard;
|
||||||
|
import node;
|
||||||
|
|
||||||
|
var tilemap: opaque = null;
|
||||||
|
var player: opaque = null;
|
||||||
|
|
||||||
|
var collisionMap: [[bool]] = null; //cache this, since it won't change during a level
|
||||||
|
|
||||||
|
//lifecycle functions
|
||||||
|
fn onLoad(node: opaque) {
|
||||||
|
tilemap = node.loadChild("scripts:/gameplay/tilemap.toy");
|
||||||
|
player = node.loadChild("scripts:/gameplay/lejana.toy");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn onInit(node: opaque) {
|
||||||
|
tilemap.callNodeFn("generateFromSeed", clock().hash(), 8, 8);
|
||||||
|
collisionMap = tilemap.callNodeFn("getCollisionMap");
|
||||||
|
}
|
||||||
|
|
||||||
|
//utils - polyfills
|
||||||
|
fn loadChild(parent: opaque, fname: string) {
|
||||||
|
var child: opaque = loadNode(fname);
|
||||||
|
parent.pushNode(child);
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
//connective functions
|
||||||
|
fn getCollisionAt(node: opaque, x: int, y: int) {
|
||||||
|
if (collisionMap == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return collisionMap[x][y];
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,9 +2,6 @@ import standard;
|
|||||||
import node;
|
import node;
|
||||||
|
|
||||||
//consts
|
//consts
|
||||||
var ROOM_WIDTH: int const = 8;
|
|
||||||
var ROOM_HEIGHT: int const = 8;
|
|
||||||
|
|
||||||
var TILE_WIDTH: int const = 64;
|
var TILE_WIDTH: int const = 64;
|
||||||
var TILE_HEIGHT: int const = 64;
|
var TILE_HEIGHT: int const = 64;
|
||||||
|
|
||||||
@@ -13,27 +10,27 @@ var tilemap: [[string]] = null;
|
|||||||
var collisions: [[bool]] = null;
|
var collisions: [[bool]] = null;
|
||||||
|
|
||||||
var tileset: [string : [int]] = [
|
var tileset: [string : [int]] = [
|
||||||
"pillar": [0, 0, 1],
|
"pillar": [0, 0, 0],
|
||||||
|
|
||||||
"floor-0": [0, 1, 0],
|
"floor-0": [0, 1, 1],
|
||||||
"floor-1": [1, 1, 0],
|
"floor-1": [1, 1, 1],
|
||||||
"floor-2": [2, 1, 0],
|
"floor-2": [2, 1, 1],
|
||||||
"floor-3": [3, 1, 0],
|
"floor-3": [3, 1, 1],
|
||||||
|
|
||||||
"wall-t": [0, 2, 1],
|
"wall-t": [0, 2, 0],
|
||||||
"wall-b": [1, 2, 1],
|
"wall-b": [1, 2, 0],
|
||||||
"wall-l": [2, 2, 1],
|
"wall-l": [2, 2, 0],
|
||||||
"wall-r": [3, 2, 1],
|
"wall-r": [3, 2, 0],
|
||||||
|
|
||||||
"corner-tl": [0, 3, 1],
|
"corner-tl": [0, 3, 0],
|
||||||
"corner-tr": [1, 3, 1],
|
"corner-tr": [1, 3, 0],
|
||||||
"corner-bl": [2, 3, 1],
|
"corner-bl": [2, 3, 0],
|
||||||
"corner-br": [3, 3, 1],
|
"corner-br": [3, 3, 0],
|
||||||
|
|
||||||
"edge-tl": [0, 4, 1],
|
"edge-tl": [0, 4, 0],
|
||||||
"edge-tr": [1, 4, 1],
|
"edge-tr": [1, 4, 0],
|
||||||
"edge-bl": [2, 4, 1],
|
"edge-bl": [2, 4, 0],
|
||||||
"edge-br": [3, 4, 1]
|
"edge-br": [3, 4, 0]
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
@@ -45,34 +42,41 @@ var camH = 720;
|
|||||||
|
|
||||||
|
|
||||||
//lifecycle functions
|
//lifecycle functions
|
||||||
fn onInit(node: opaque) {
|
fn onLoad(node: opaque) {
|
||||||
node.loadTexture("sprites:/tileset.png");
|
node.loadTexture("sprites:/tileset.png");
|
||||||
|
|
||||||
tilemap = generateTilemap(clock().hash(), ROOM_WIDTH, ROOM_HEIGHT);
|
|
||||||
collisions = bakeCollisionMap(tilemap, ROOM_WIDTH, ROOM_HEIGHT);
|
|
||||||
|
|
||||||
print tilemap;
|
|
||||||
print collisions;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
fn onDraw(node: opaque) {
|
||||||
|
if (tilemap == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
//calc the region to render
|
//calc the region to render
|
||||||
var lowerX: int = round((camX - camW/2.0) / TILE_WIDTH);
|
var lowerX: int = round((camX - camW/2.0) / TILE_WIDTH);
|
||||||
var upperX: int = round((camX - camW*1.5) / TILE_WIDTH);
|
var upperX: int = round((camX - camW*1.5) / TILE_WIDTH);
|
||||||
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, ROOM_WIDTH);
|
upperX = min(upperX < 0 ? abs(upperX) : 0, rwidth);
|
||||||
lowerY = max(0, lowerY);
|
lowerY = max(0, lowerY);
|
||||||
upperY = min(upperY < 0 ? abs(upperY) : 0, ROOM_HEIGHT);
|
upperY = min(upperY < 0 ? abs(upperY) : 0, rheight);
|
||||||
|
|
||||||
|
|
||||||
//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[tilemap[i][j]];
|
var pos = tileset[tilemap[i][j]];
|
||||||
node.setNodeRect(pos[0] * 16, pos[1] * 16, 16, 16);
|
node.setNodeRect(pos[0] * 16, pos[1] * 16, 16, 16);
|
||||||
|
|
||||||
@@ -83,6 +87,14 @@ fn onDraw(node: opaque) {
|
|||||||
|
|
||||||
|
|
||||||
//utils functions for map generation
|
//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);
|
||||||
|
|
||||||
|
print tilemap;
|
||||||
|
print collisions;
|
||||||
|
}
|
||||||
|
|
||||||
fn generateTilemap(seed: int, width: int, height: int) {
|
fn generateTilemap(seed: int, width: int, height: int) {
|
||||||
import random;
|
import random;
|
||||||
var rng: opaque = createRandomGenerator(seed);
|
var rng: opaque = createRandomGenerator(seed);
|
||||||
@@ -152,3 +164,7 @@ fn bakeCollisionMap(tilemap: [[string]], width: int, height: int) {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn getCollisionMap(node: opaque) {
|
||||||
|
return collisions;
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
initWindow("Skyland", 1080, 720, false);
|
initWindow("Skyland", 1080, 720, false);
|
||||||
|
|
||||||
//kick off the logic of the scene graph
|
//kick off the logic of the scene graph
|
||||||
loadRootNode("scripts:/gameplay/tilemap.toy");
|
loadRootNode("scripts:/gameplay/scene.toy");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Globals go here
|
//Globals go here
|
||||||
|
|||||||
@@ -750,7 +750,7 @@ static int nativeDrawNode(Toy_Interpreter* interpreter, Toy_LiteralArray* argume
|
|||||||
static int nativeCallNodeFn(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
static int nativeCallNodeFn(Toy_Interpreter* interpreter, Toy_LiteralArray* arguments) {
|
||||||
//checks
|
//checks
|
||||||
if (arguments->count < 2) {
|
if (arguments->count < 2) {
|
||||||
interpreter->errorOutput("Too few arguments passed to callNode\n");
|
interpreter->errorOutput("Too few arguments passed to callNodeFn\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -797,7 +797,7 @@ static int nativeCallNodeFn(Toy_Interpreter* interpreter, Toy_LiteralArray* argu
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!TOY_IS_OPAQUE(nodeLiteral) || !TOY_IS_STRING(fnName)) {
|
if (!TOY_IS_OPAQUE(nodeLiteral) || !TOY_IS_STRING(fnName)) {
|
||||||
interpreter->errorOutput("Incorrect argument type passed to callNode\n");
|
interpreter->errorOutput("Incorrect argument type passed to callNodeFn\n");
|
||||||
Toy_freeLiteral(nodeLiteral);
|
Toy_freeLiteral(nodeLiteral);
|
||||||
Toy_freeLiteral(fnName);
|
Toy_freeLiteral(fnName);
|
||||||
return -1;
|
return -1;
|
||||||
|
|||||||
Reference in New Issue
Block a user