Added node scaling, and a few utility functions
This commit is contained in:
2
Box
2
Box
Submodule Box updated: 78f6eb79fa...c330ba9831
42
assets/airport-demo/init.toy
Normal file
42
assets/airport-demo/init.toy
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
//A quirk of the setup is that anything defined in the root of `init.toy` becomes a global object
|
||||||
|
//To resolve that, the configuration is inside a block scope
|
||||||
|
{
|
||||||
|
import engine;
|
||||||
|
import input;
|
||||||
|
|
||||||
|
|
||||||
|
//input settings, mapping SDL2's virtual keys to event names
|
||||||
|
mapInputEventToKeyDown("character_up", "w"); //event, keysym
|
||||||
|
mapInputEventToKeyDown("character_left", "a"); //event, keysym
|
||||||
|
mapInputEventToKeyDown("character_down", "s"); //event, keysym
|
||||||
|
mapInputEventToKeyDown("character_right", "d"); //event, keysym
|
||||||
|
|
||||||
|
mapInputEventToKeyUp("character_up", "w"); //event, keysym
|
||||||
|
mapInputEventToKeyUp("character_left", "a"); //event, keysym
|
||||||
|
mapInputEventToKeyUp("character_down", "s"); //event, keysym
|
||||||
|
mapInputEventToKeyUp("character_right", "d"); //event, keysym
|
||||||
|
|
||||||
|
mapInputEventToKeyDown("character_up", "up"); //event, keysym
|
||||||
|
mapInputEventToKeyDown("character_left", "left"); //event, keysym
|
||||||
|
mapInputEventToKeyDown("character_down", "down"); //event, keysym
|
||||||
|
mapInputEventToKeyDown("character_right", "right"); //event, keysym
|
||||||
|
|
||||||
|
mapInputEventToKeyUp("character_up", "up"); //event, keysym
|
||||||
|
mapInputEventToKeyUp("character_left", "left"); //event, keysym
|
||||||
|
mapInputEventToKeyUp("character_down", "down"); //event, keysym
|
||||||
|
mapInputEventToKeyUp("character_right", "right"); //event, keysym
|
||||||
|
|
||||||
|
mapInputEventToKeyDown("character_attack", "space"); //event, keysym
|
||||||
|
|
||||||
|
//TODO: escape to kill the game
|
||||||
|
|
||||||
|
|
||||||
|
//this function must always be called, or the engine won't run
|
||||||
|
initWindow("Airport", 800, 600, false); //TODO: custom FPS setting
|
||||||
|
|
||||||
|
//kick off the logic of the scene graph
|
||||||
|
loadRootNode("scripts:/airplane.toy");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Globals go here
|
||||||
|
|
||||||
|
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 14 KiB |
@@ -1,65 +0,0 @@
|
|||||||
//the overarching scene
|
|
||||||
import engine;
|
|
||||||
import node;
|
|
||||||
|
|
||||||
//debugging tools
|
|
||||||
var stepCounter: int = 0;
|
|
||||||
var stepTracker: [int] = [0, 0, 0];
|
|
||||||
|
|
||||||
|
|
||||||
//util to generate and init a child node of a given parent
|
|
||||||
fn makeChild(parent: opaque, fname: string) {
|
|
||||||
var child: opaque = loadNode(fname);
|
|
||||||
parent.pushNode(child);
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
|
|
||||||
//lifecycle functions
|
|
||||||
fn onLoad(node: opaque) {
|
|
||||||
//load the tile map node
|
|
||||||
var tilemapNode = node.makeChild("scripts:/demo/tilemap/tilemap.toy");
|
|
||||||
|
|
||||||
tilemapNode.callNodeFn("loadLayer", "layer-background.toy");
|
|
||||||
tilemapNode.callNodeFn("loadLayer", "layer-walls.toy");
|
|
||||||
tilemapNode.callNodeFn("loadLayer", "layer-walls.toy");
|
|
||||||
//tilemapNode.callNodeFn("loadLayer", "layer-walls.toy"); //TODO: remove this
|
|
||||||
//tilemapNode.callNodeFn("loadLayer", "layer-maze.toy");
|
|
||||||
}
|
|
||||||
|
|
||||||
//debugging code
|
|
||||||
fn onStep(node: opaque) {
|
|
||||||
stepCounter++;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
|
||||||
//round off - too many skips means it's crappy anyway
|
|
||||||
if (stepCounter > stepTracker.length() - 1) {
|
|
||||||
stepCounter = stepTracker.length() - 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
//This logic is just a debugging tool
|
|
||||||
stepTracker[stepCounter] = stepTracker[stepCounter] + 1;
|
|
||||||
print stepTracker;
|
|
||||||
stepCounter = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onMouseButtonDown(node: opaque, x: int, y: int, button: string) {
|
|
||||||
//reload the scene on click
|
|
||||||
if (button == "left") {
|
|
||||||
loadRootNode("scripts:/demo/scene.toy");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//global accessors
|
|
||||||
fn getTileWidth(node: opaque): int { return 64; }
|
|
||||||
fn getTileHeight(node: opaque): int { return 64; }
|
|
||||||
|
|
||||||
fn getRoomWidth(node: opaque): int { return 10; }
|
|
||||||
fn getRoomHeight(node: opaque): int { return 10; }
|
|
||||||
|
|
||||||
fn getLevelXCount(node: opaque): int { return 9; }
|
|
||||||
fn getLevelYCount(node: opaque): int { return 9; }
|
|
||||||
|
|
||||||
//TODO: move this into the engine
|
|
||||||
fn getScreenWidth(node: opaque): int { return 1080; }
|
|
||||||
fn getScreenHeight(node: opaque): int { return 720; }
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
//this is one layer
|
|
||||||
import standard;
|
|
||||||
import engine;
|
|
||||||
import node;
|
|
||||||
|
|
||||||
|
|
||||||
//TODO: replace with an empty node function (polyfill)
|
|
||||||
fn loadChildEmpty(parent: opaque) {
|
|
||||||
var child: opaque = loadNode("scripts:/empty.toy");
|
|
||||||
parent.pushNode(child);
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn onLoad(node: opaque) {
|
|
||||||
//load the child node, with the tiling back image
|
|
||||||
node.loadChildEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onInit(node: opaque) {
|
|
||||||
node.getChildNode(0).loadTexture("sprites:/tile-background.png");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//drawing util
|
|
||||||
fn drawLayer(node: opaque, camX, camY, camW, camH, depth) {
|
|
||||||
//get the constants from root
|
|
||||||
var root: opaque = getRootNode();
|
|
||||||
|
|
||||||
var tileWidth: int const = root.callNodeFn("getTileWidth");
|
|
||||||
var tileHeight: int const = root.callNodeFn("getTileHeight");
|
|
||||||
|
|
||||||
var roomWidth: int const = root.callNodeFn("getRoomWidth");
|
|
||||||
var roomHeight: int const = root.callNodeFn("getRoomHeight");
|
|
||||||
|
|
||||||
var levelXCount: int const = root.callNodeFn("getLevelXCount");
|
|
||||||
var levelYCount: int const = root.callNodeFn("getLevelYCount");
|
|
||||||
|
|
||||||
//calc the modifier ratio to offset things
|
|
||||||
var mod: float = float tileWidth / (tileWidth - depth);
|
|
||||||
|
|
||||||
var tileWidth_mod: float = tileWidth * mod;
|
|
||||||
var tileHeight_mod: float = tileHeight * mod;
|
|
||||||
var camX_mod: float = (camX - camW) * mod + camW/2;
|
|
||||||
var camY_mod: float = (camY - camH) * mod + camH/2;
|
|
||||||
|
|
||||||
//calc the region to render
|
|
||||||
var lowerX: int = round((camX - camW/2.0) / tileWidth);
|
|
||||||
var upperX: int = round((camX - camW*1.5) / tileWidth);
|
|
||||||
var lowerY: int = round((camY - camH/2.0) / tileHeight);
|
|
||||||
var upperY: int = round((camY - camH*1.5) / tileHeight);
|
|
||||||
|
|
||||||
//bounds check
|
|
||||||
lowerX = max(0, abs(lowerX));
|
|
||||||
upperX = min(upperX < 0 ? abs(upperX) : 0, levelXCount * roomWidth);
|
|
||||||
lowerY = max(0, abs(lowerY));
|
|
||||||
upperY = min(upperY < 0 ? abs(upperY) : 0, levelYCount * roomHeight);
|
|
||||||
|
|
||||||
//render each tile
|
|
||||||
for (var j = lowerY; j <= upperY; j++) {
|
|
||||||
for (var i = lowerX; i <= upperX; i++) {
|
|
||||||
node.getChildNode(0).drawNode(round(camX_mod + i * tileWidth_mod), round(camY_mod + j * tileHeight_mod), round(tileWidth_mod), round(tileHeight_mod));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,69 +0,0 @@
|
|||||||
//this is one layer
|
|
||||||
import standard;
|
|
||||||
import engine;
|
|
||||||
import node;
|
|
||||||
|
|
||||||
|
|
||||||
//TODO: replace with an empty node function (polyfill)
|
|
||||||
fn loadChildEmpty(parent: opaque) {
|
|
||||||
var child: opaque = loadNode("scripts:/empty.toy");
|
|
||||||
parent.pushNode(child);
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn onLoad(node: opaque) {
|
|
||||||
//load the child node, with the tiling back image
|
|
||||||
node.loadChildEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onInit(node: opaque) {
|
|
||||||
node.getChildNode(0).loadTexture("sprites:/tile-wall.png");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//drawing util
|
|
||||||
fn drawLayer(node: opaque, camX, camY, camW, camH, depth) {
|
|
||||||
//get the constants from root
|
|
||||||
var root: opaque = getRootNode();
|
|
||||||
|
|
||||||
var tileWidth: int const = root.callNodeFn("getTileWidth");
|
|
||||||
var tileHeight: int const = root.callNodeFn("getTileHeight");
|
|
||||||
|
|
||||||
var roomWidth: int const = root.callNodeFn("getRoomWidth");
|
|
||||||
var roomHeight: int const = root.callNodeFn("getRoomHeight");
|
|
||||||
|
|
||||||
var levelXCount: int const = root.callNodeFn("getLevelXCount");
|
|
||||||
var levelYCount: int const = root.callNodeFn("getLevelYCount");
|
|
||||||
|
|
||||||
//calc the modifier ratio to offset things
|
|
||||||
var mod: float = float tileWidth / (tileWidth - depth);
|
|
||||||
|
|
||||||
var tileWidth_mod: int = round(tileWidth * mod);
|
|
||||||
var tileHeight_mod: int = round(tileHeight * mod);
|
|
||||||
var camX_mod: int = round((camX - camW) * mod + camW/2);
|
|
||||||
var camY_mod: int = round((camY - camH) * mod + camH/2);
|
|
||||||
|
|
||||||
//calc the region to render
|
|
||||||
var lowerX: int = round((camX - camW/2.0) / tileWidth);
|
|
||||||
var upperX: int = round((camX - camW*1.5) / tileWidth);
|
|
||||||
var lowerY: int = round((camY - camH/2.0) / tileHeight);
|
|
||||||
var upperY: int = round((camY - camH*1.5) / tileHeight);
|
|
||||||
|
|
||||||
//bounds check
|
|
||||||
lowerX = max(0, abs(lowerX));
|
|
||||||
upperX = min(upperX < 0 ? abs(upperX) : 0, levelXCount * roomWidth);
|
|
||||||
lowerY = max(0, abs(lowerY));
|
|
||||||
upperY = min(upperY < 0 ? abs(upperY) : 0, levelYCount * roomHeight);
|
|
||||||
|
|
||||||
//render each tile
|
|
||||||
for (var j = lowerY; j <= upperY; j++) {
|
|
||||||
for (var i = lowerX; i <= upperX; i++) {
|
|
||||||
if ( !(int i % int roomWidth == 0 || int i % int roomWidth == roomWidth - 1) && !(int j % int roomHeight == 0 || int j % int roomHeight == roomHeight - 1) ) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
node.getChildNode(0).drawNode(camX_mod + i * tileWidth_mod, camY_mod + j * tileHeight_mod, tileWidth_mod, tileHeight_mod);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
//this file manages the tilemap-related utilities
|
|
||||||
import standard;
|
|
||||||
import engine;
|
|
||||||
import node;
|
|
||||||
|
|
||||||
var camX: float = 0;
|
|
||||||
var camY: float = 0;
|
|
||||||
|
|
||||||
//util to generate a child node of a given parent
|
|
||||||
fn loadChild(parent: opaque, fname: string) {
|
|
||||||
var child: opaque = loadNode(fname);
|
|
||||||
parent.pushNode(child);
|
|
||||||
return child;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn loadLayer(node: opaque, layerName: string) {
|
|
||||||
//load the given layer as a child
|
|
||||||
var layerNode = node.loadChild("scripts:/demo/tilemap/" + layerName);
|
|
||||||
}
|
|
||||||
|
|
||||||
//lifecycle functions
|
|
||||||
fn onStep(node: opaque) {
|
|
||||||
camX--;
|
|
||||||
camY--;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
|
||||||
var screenWidth: int const = getRootNode().callNodeFn("getScreenWidth");
|
|
||||||
var screenHeight: int const = getRootNode().callNodeFn("getScreenHeight");
|
|
||||||
|
|
||||||
//iterate over each layer, passing in the screen dimensions
|
|
||||||
for (var c = 0; c < node.getChildNodeCount(); c++) {
|
|
||||||
node.getChildNode(c).callNodeFn("drawLayer", camX, camY, screenWidth, screenHeight, c * 4.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -2,40 +2,23 @@ import standard;
|
|||||||
import node;
|
import node;
|
||||||
import random;
|
import random;
|
||||||
|
|
||||||
//constants
|
|
||||||
var SPEED: int const = 4;
|
|
||||||
|
|
||||||
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
|
//variables
|
||||||
var parent: opaque = null; //cache the parent for quick access
|
var gridPositionX: int = 1;
|
||||||
|
var gridPositionY: int = 1;
|
||||||
var gridX: int = 1; //position on the game grid
|
|
||||||
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 direction: int = null; //BUGFIX: animation not looping properly
|
var direction: int = null; //BUGFIX: animation not looping properly
|
||||||
|
|
||||||
var stepAI: int = 0;
|
var stepAI: int = 0;
|
||||||
|
|
||||||
//polyfills - animating different cycles on one image
|
//polyfills - animating different cycles on one image
|
||||||
var stepCount: int = 0;
|
var walkAnimationCounter: int = 0;
|
||||||
|
|
||||||
fn faceDown(node: opaque) {
|
fn faceDown(node: opaque) {
|
||||||
if (direction == 0) return;
|
if (direction == 0) return;
|
||||||
direction = 0;
|
direction = 0;
|
||||||
node.setNodeRect(0, 0, 32, 32);
|
node.setNodeRect(0, 0, 32, 32);
|
||||||
node.setNodeFrames(2);
|
node.setNodeFrames(2);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceUp(node: opaque) {
|
fn faceUp(node: opaque) {
|
||||||
@@ -43,6 +26,7 @@ fn faceUp(node: opaque) {
|
|||||||
direction = 1;
|
direction = 1;
|
||||||
node.setNodeRect(32 * 2, 0, 32, 32);
|
node.setNodeRect(32 * 2, 0, 32, 32);
|
||||||
node.setNodeFrames(2);
|
node.setNodeFrames(2);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceRight(node: opaque) {
|
fn faceRight(node: opaque) {
|
||||||
@@ -50,6 +34,7 @@ fn faceRight(node: opaque) {
|
|||||||
direction = 2;
|
direction = 2;
|
||||||
node.setNodeRect(32 * 4, 0, 32, 32);
|
node.setNodeRect(32 * 4, 0, 32, 32);
|
||||||
node.setNodeFrames(2);
|
node.setNodeFrames(2);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceLeft(node: opaque) {
|
fn faceLeft(node: opaque) {
|
||||||
@@ -57,34 +42,25 @@ fn faceLeft(node: opaque) {
|
|||||||
direction = 3;
|
direction = 3;
|
||||||
node.setNodeRect(32 * 6, 0, 32, 32);
|
node.setNodeRect(32 * 6, 0, 32, 32);
|
||||||
node.setNodeFrames(2);
|
node.setNodeFrames(2);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//accessors & mutators
|
//accessors & mutators
|
||||||
fn setGridPos(node: opaque, x: int, y: int) {
|
fn setGridPosition(node: opaque, x: int, y: int) {
|
||||||
gridX = x;
|
gridPositionX = x;
|
||||||
gridY = y;
|
gridPositionY = y;
|
||||||
|
|
||||||
if (realX == null) {
|
node.setNodePositionX(floor( gridPositionX * node.getNodeRectW() / node.getNodeScaleX() ));
|
||||||
realX = gridX * TILE_WIDTH;
|
node.setNodePositionY(floor( gridPositionY * node.getNodeRectH() / node.getNodeScaleY() ));
|
||||||
}
|
|
||||||
|
|
||||||
if (realY == null) {
|
|
||||||
realY = gridY * TILE_HEIGHT;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setRealPos(node: opaque, x: int, y: int) {
|
fn getGridPositionX(node: opaque) {
|
||||||
realX = x;
|
return gridPositionX;
|
||||||
realY = y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getGridPos(node: opaque) {
|
fn getGridPositionY(node: opaque) {
|
||||||
return [gridX, gridY];
|
return gridPositionY;
|
||||||
}
|
|
||||||
|
|
||||||
fn getRealPos(node: opaque) {
|
|
||||||
return [realX, realY];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -92,28 +68,23 @@ fn getRealPos(node: opaque) {
|
|||||||
fn onLoad(node: opaque) {
|
fn onLoad(node: opaque) {
|
||||||
node.loadNodeTexture("sprites:/drone.png");
|
node.loadNodeTexture("sprites:/drone.png");
|
||||||
node.faceDown();
|
node.faceDown();
|
||||||
}
|
|
||||||
|
node.setNodeScaleX(CAMERA_SCALE_X);
|
||||||
fn onInit(node: opaque) {
|
node.setNodeScaleY(CAMERA_SCALE_Y);
|
||||||
parent = node.getParentNode();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onStep(node: opaque) {
|
fn onStep(node: opaque) {
|
||||||
if (++stepCount >= 5) {
|
if (++walkAnimationCounter >= 12) {
|
||||||
node.incrementCurrentNodeFrame();
|
node.incrementCurrentNodeFrame();
|
||||||
stepCount = 0;
|
walkAnimationCounter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
//calc movement
|
//move in realspace
|
||||||
var distX = gridX * TILE_WIDTH - realX;
|
var distX = gridPositionX * TILE_PIXEL_WIDTH - node.getNodePositionX();
|
||||||
var distY = gridY * TILE_HEIGHT - realY;
|
var distY = gridPositionY * TILE_PIXEL_HEIGHT - node.getNodePositionY();
|
||||||
|
|
||||||
motionX = normalize(distX);
|
node.setNodeMotionX( normalize(distX) );
|
||||||
motionY = normalize(distY);
|
node.setNodeMotionY( 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) {
|
||||||
@@ -121,10 +92,23 @@ fn onFree(node: opaque) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
fn onDraw(node: opaque) {
|
||||||
var camera = parent.callNodeFn("getCameraPos");
|
var posX: int = node.getNodeWorldPositionX();
|
||||||
node.drawNode(realX + camera[0] - SPRITE_WIDTH / 4, realY + camera[1] - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
|
var posY: int = node.getNodeWorldPositionY();
|
||||||
}
|
|
||||||
|
|
||||||
|
var scaleX: float = node.getNodeWorldScaleX();
|
||||||
|
var scaleY: float = node.getNodeWorldScaleY();
|
||||||
|
|
||||||
|
//this offset is because the sprite cell for lejana is twice as big as the sprite cell for the floor tiles
|
||||||
|
var originOffsetX: int = node.getNodeRectW() * int scaleX / 4;
|
||||||
|
var originOffsetY: int = node.getNodeRectH() * int scaleY / 2;
|
||||||
|
|
||||||
|
node.drawNode(
|
||||||
|
floor(posX * scaleX) - originOffsetX,
|
||||||
|
floor(posY * scaleY) - originOffsetY,
|
||||||
|
floor(node.getNodeRectW() * scaleX),
|
||||||
|
floor(node.getNodeRectH() * scaleY)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
//gameplay functions
|
//gameplay functions
|
||||||
fn runAI(node: opaque, rng: opaque) {
|
fn runAI(node: opaque, rng: opaque) {
|
||||||
@@ -155,9 +139,9 @@ fn runAI(node: opaque, rng: opaque) {
|
|||||||
node.faceLeft();
|
node.faceLeft();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (parent.callNodeFn("getCollisionAt", gridX + moveX, gridY + moveY)) {
|
if (node.getParentNode().callNodeFn("getWalkableAt", gridPositionX + moveX, gridPositionY + moveY)) {
|
||||||
gridX += moveX;
|
gridPositionX += moveX;
|
||||||
gridY += moveY;
|
gridPositionY += moveY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,43 +1,26 @@
|
|||||||
import standard;
|
import standard;
|
||||||
import node;
|
import node;
|
||||||
|
|
||||||
//constants
|
var gridPositionX: int = 1; //position on the game grid
|
||||||
var SPEED: int const = 4;
|
var gridPositionY: int = 1;
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
var gridX: int = 1; //position on the game grid
|
|
||||||
var gridY: int = 1;
|
|
||||||
|
|
||||||
var realX: int = null; //will change until realX = gridX * SPRITE_WIDTH
|
|
||||||
var realY: int = null;
|
|
||||||
|
|
||||||
var motionX: int = 0; //normalized movement direction
|
|
||||||
var motionY: int = 0;
|
|
||||||
|
|
||||||
var inputX: int = 0; //cache the keyboard input
|
var inputX: int = 0; //cache the keyboard input
|
||||||
var inputY: int = 0;
|
var inputY: int = 0;
|
||||||
|
|
||||||
var direction: int = null; //BUGFIX: animation not looping properly
|
var direction: int = null; //BUGFIX: animation not looping properly
|
||||||
var enableMovementCounter: int = 30 * 3; //BUGFIX: freeze while drones reach their starting spot
|
var enableMovementCounter: int = 60 * 3; //BUGFIX: freeze while drones reach their starting spot
|
||||||
var attackCounter: int = 0;
|
|
||||||
|
|
||||||
//polyfills - animating different cycles on one image
|
//polyfills - animating different cycles on one image
|
||||||
var stepCount: int = 0;
|
var walkAnimationCounter: int = 0;
|
||||||
|
var attackAnimationCounter: int = 0;
|
||||||
|
|
||||||
|
//utils for facing different directions (idling)
|
||||||
fn faceDown(node: opaque) {
|
fn faceDown(node: opaque) {
|
||||||
if (direction == 0) return;
|
if (direction == 0) return;
|
||||||
direction = 0;
|
direction = 0;
|
||||||
node.setNodeRect(0, 0, 32, 32);
|
node.setNodeRect(0, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceUp(node: opaque) {
|
fn faceUp(node: opaque) {
|
||||||
@@ -45,6 +28,7 @@ fn faceUp(node: opaque) {
|
|||||||
direction = 1;
|
direction = 1;
|
||||||
node.setNodeRect(32 * 4, 0, 32, 32);
|
node.setNodeRect(32 * 4, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceRight(node: opaque) {
|
fn faceRight(node: opaque) {
|
||||||
@@ -52,6 +36,7 @@ fn faceRight(node: opaque) {
|
|||||||
direction = 2;
|
direction = 2;
|
||||||
node.setNodeRect(32 * 8, 0, 32, 32);
|
node.setNodeRect(32 * 8, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn faceLeft(node: opaque) {
|
fn faceLeft(node: opaque) {
|
||||||
@@ -59,45 +44,33 @@ fn faceLeft(node: opaque) {
|
|||||||
direction = 3;
|
direction = 3;
|
||||||
node.setNodeRect(32 * 12, 0, 32, 32);
|
node.setNodeRect(32 * 12, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//accessors & mutators
|
//accessors & mutators
|
||||||
fn setGridPos(node: opaque, x: int, y: int) {
|
fn setGridPosition(node: opaque, x: int, y: int) {
|
||||||
gridX = x;
|
gridPositionX = x;
|
||||||
gridY = y;
|
gridPositionY = y;
|
||||||
|
|
||||||
if (realX == null) {
|
node.setNodePositionX(floor( gridPositionX * node.getNodeRectW() / node.getNodeScaleX() ));
|
||||||
realX = gridX * TILE_WIDTH;
|
node.setNodePositionY(floor( gridPositionY * node.getNodeRectH() / node.getNodeScaleY() ));
|
||||||
}
|
|
||||||
|
|
||||||
if (realY == null) {
|
|
||||||
realY = gridY * TILE_HEIGHT;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setRealPos(node: opaque, x: int, y: int) {
|
fn getGridPositionX(node: opaque) {
|
||||||
realX = x;
|
return gridPositionX;
|
||||||
realY = y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getGridPos(node: opaque) {
|
fn getGridPositionY(node: opaque) {
|
||||||
return [gridX, gridY];
|
return gridPositionY;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getRealPos(node: opaque) {
|
|
||||||
return [realX, realY];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//lifecycle functions
|
//lifecycle functions
|
||||||
fn onLoad(node: opaque) {
|
fn onLoad(node: opaque) {
|
||||||
node.loadNodeTexture("sprites:/lejana.png");
|
node.loadNodeTexture("sprites:/lejana.png"); //NOTE: all of this script is mapped to this sprite sheet
|
||||||
node.faceDown();
|
node.faceDown();
|
||||||
}
|
|
||||||
|
|
||||||
fn onInit(node: opaque) {
|
node.setNodeScaleX(CAMERA_SCALE_X);
|
||||||
parent = node.getParentNode();
|
node.setNodeScaleY(CAMERA_SCALE_Y);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onStep(node: opaque) {
|
fn onStep(node: opaque) {
|
||||||
@@ -106,91 +79,82 @@ fn onStep(node: opaque) {
|
|||||||
enableMovementCounter--;
|
enableMovementCounter--;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//process input when aligned to a grid
|
|
||||||
if (realX / TILE_WIDTH == gridX && realY / TILE_HEIGHT == gridY && motionX == 0 && motionY == 0) {
|
|
||||||
//facing
|
|
||||||
if (inputY > 0) {
|
|
||||||
node.faceDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inputY < 0) {
|
//animation - start idling
|
||||||
node.faceUp();
|
if (attackAnimationCounter == 0) {
|
||||||
}
|
|
||||||
|
|
||||||
if (inputX > 0) {
|
|
||||||
node.faceRight();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inputX < 0) {
|
|
||||||
node.faceLeft();
|
|
||||||
}
|
|
||||||
|
|
||||||
//disallow wall phasing
|
|
||||||
if (inputX != 0 && parent.callNodeFn("getCollisionAt", gridX + inputX, gridY) != true) {
|
|
||||||
inputX = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inputY != 0 && parent.callNodeFn("getCollisionAt", gridX, gridY + inputY) != true) {
|
|
||||||
inputY = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
//disallow diagonal movement
|
|
||||||
if (abs(inputX) != 0) {
|
|
||||||
gridX += inputX;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
gridY += inputY;
|
|
||||||
}
|
|
||||||
|
|
||||||
//trigger the world
|
|
||||||
if (inputX != 0 || inputY != 0) {
|
|
||||||
parent.callNodeFn("runAI");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//actually animate
|
|
||||||
if (++stepCount >= 5) {
|
|
||||||
if (motionX == 0 && motionY == 0 && inputX == 0 && inputY == 0) {
|
|
||||||
stepCount = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (attackCounter > 0) {
|
|
||||||
attackCounter--;
|
|
||||||
}
|
|
||||||
|
|
||||||
node.incrementCurrentNodeFrame();
|
|
||||||
}
|
|
||||||
|
|
||||||
//animation - standing still
|
|
||||||
if (attackCounter == 0 && stepCount == 0) {
|
|
||||||
//move to standing state
|
//move to standing state
|
||||||
if (node.getNodeRectY() != 0) {
|
if (node.getNodeRectY() != 0) {
|
||||||
node.setNodeRect(direction * 32 * 4, 0, 32, 32);
|
node.setNodeRect(direction * 32 * 4, 0, 32, 32);
|
||||||
node.setNodeFrames(4);
|
node.setNodeFrames(4);
|
||||||
|
walkAnimationCounter = 12;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//animation - attacking
|
//animation - start attacking
|
||||||
if (attackCounter > 0 && stepCount == 0) {
|
if (--attackAnimationCounter >= 0) {
|
||||||
//move to attacking state
|
//move to attacking state
|
||||||
if (node.getNodeRectY() != 32) {
|
if (node.getNodeRectY() != 32) {
|
||||||
node.setNodeRect(direction * 32 * 4, 32, 32, 32);
|
node.setNodeRect(direction * 32 * 4, 32, 32, 32);
|
||||||
node.setNodeFrames(3);
|
node.setNodeFrames(3);
|
||||||
stepCount = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ((attackAnimationCounter+1) % 4 == 0) { //+1 for a bugfix
|
||||||
|
node.incrementCurrentNodeFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
//skip out
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//calc movement
|
//facing
|
||||||
var distX = gridX * TILE_WIDTH - realX;
|
if (inputY > 0) {
|
||||||
var distY = gridY * TILE_HEIGHT - realY;
|
node.faceDown();
|
||||||
|
}
|
||||||
|
|
||||||
motionX = normalize(distX);
|
else if (inputY < 0) {
|
||||||
motionY = normalize(distY);
|
node.faceUp();
|
||||||
|
}
|
||||||
|
|
||||||
//make movement
|
else if (inputX > 0) {
|
||||||
realX += abs(distX) > SPEED ? SPEED * motionX : distX;
|
node.faceRight();
|
||||||
realY += abs(distY) > SPEED ? SPEED * motionY : distY;
|
}
|
||||||
|
|
||||||
|
else if (inputX < 0) {
|
||||||
|
node.faceLeft();
|
||||||
|
}
|
||||||
|
|
||||||
|
//BUGFIX for smooth animation fo
|
||||||
|
var lesser = 0;
|
||||||
|
if ((inputX != 0 || inputY != 0) && attackAnimationCounter == 0) {
|
||||||
|
lesser = 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
//actually animate
|
||||||
|
if (--walkAnimationCounter <= lesser) {
|
||||||
|
node.incrementCurrentNodeFrame();
|
||||||
|
walkAnimationCounter = 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
var parent = node.getParentNode(); //check for collisions from the parent
|
||||||
|
|
||||||
|
if (parent.callNodeFn("getWalkableAt", gridPositionX + inputX, gridPositionY + inputY) && abs(inputX) != abs(inputY)) {
|
||||||
|
//calc movement
|
||||||
|
gridPositionX += inputX;
|
||||||
|
gridPositionY += inputY;
|
||||||
|
|
||||||
|
parent.callNodeFn("runAI");
|
||||||
|
}
|
||||||
|
|
||||||
|
//move in realspace
|
||||||
|
var distX = gridPositionX * TILE_PIXEL_WIDTH - node.getNodePositionX();
|
||||||
|
var distY = gridPositionY * TILE_PIXEL_HEIGHT - node.getNodePositionY();
|
||||||
|
|
||||||
|
node.setNodeMotionX( normalize(distX) );
|
||||||
|
node.setNodeMotionY( normalize(distY) );
|
||||||
|
|
||||||
|
//reset input
|
||||||
|
inputX = 0;
|
||||||
|
inputY = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
fn onFree(node: opaque) {
|
||||||
@@ -198,8 +162,22 @@ fn onFree(node: opaque) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
fn onDraw(node: opaque) {
|
||||||
var camera = parent.callNodeFn("getCameraPos");
|
var posX: int = node.getNodeWorldPositionX();
|
||||||
node.drawNode(realX + camera[0] - SPRITE_WIDTH / 4, realY + camera[1] - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
|
var posY: int = node.getNodeWorldPositionY();
|
||||||
|
|
||||||
|
var scaleX: float = node.getNodeWorldScaleX();
|
||||||
|
var scaleY: float = node.getNodeWorldScaleY();
|
||||||
|
|
||||||
|
//this offset is because the sprite cell for lejana is twice as big as the sprite cell for the floor tiles
|
||||||
|
var originOffsetX: int = node.getNodeRectW() * int scaleX / 4;
|
||||||
|
var originOffsetY: int = node.getNodeRectH() * int scaleY / 2;
|
||||||
|
|
||||||
|
node.drawNode(
|
||||||
|
floor(posX * scaleX) - originOffsetX,
|
||||||
|
floor(posY * scaleY) - originOffsetY,
|
||||||
|
floor(node.getNodeRectW() * scaleX),
|
||||||
|
floor(node.getNodeRectH() * scaleY)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -210,8 +188,14 @@ fn onKeyDown(node: opaque, event: string) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//enable attack
|
||||||
if (event == "character_attack" && inputX == 0 && inputY == 0) {
|
if (event == "character_attack" && inputX == 0 && inputY == 0) {
|
||||||
attackCounter = 3;
|
attackAnimationCounter = 12;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//if moving, don't take any more input
|
||||||
|
if (node.getNodeMotionX() != 0 || node.getNodeMotionY() != 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -258,7 +242,6 @@ fn onKeyUp(node: opaque, event: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//polyfills - move these to standard
|
//polyfills - move these to standard
|
||||||
fn normalize(x): int {
|
fn normalize(x): int {
|
||||||
if (x > 0) {
|
if (x > 0) {
|
||||||
|
|||||||
@@ -2,45 +2,42 @@ import standard;
|
|||||||
import node;
|
import node;
|
||||||
import random;
|
import random;
|
||||||
|
|
||||||
//consts
|
|
||||||
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 CAMERA_X: int const = 0;
|
|
||||||
var CAMERA_Y: int const = 32;
|
|
||||||
|
|
||||||
|
|
||||||
|
//persistent members
|
||||||
var tilemap: opaque = null;
|
var tilemap: opaque = null;
|
||||||
var player: opaque = null;
|
|
||||||
var stepCounter: opaque = null;
|
var stepCounter: opaque = null;
|
||||||
|
var player: opaque = null;
|
||||||
|
var enemies: [opaque] = null;
|
||||||
|
|
||||||
var enemies: [opaque] = [];
|
var collisionDataCache: [bool] = null;
|
||||||
|
|
||||||
var collisionMap: [bool] = null; //cache this, since it won't change during a level
|
|
||||||
|
|
||||||
var rng: opaque = null;
|
|
||||||
|
|
||||||
|
var rng: opaque = null; //TODO: does this have an opaque tag?
|
||||||
|
|
||||||
//lifecycle functions
|
//lifecycle functions
|
||||||
fn onLoad(node: opaque) {
|
|
||||||
var empty = node.loadChild("scripts:/empty.toy");
|
|
||||||
|
|
||||||
tilemap = empty.loadChild("scripts:/gameplay/tilemap.toy");
|
|
||||||
stepCounter = empty.loadChild("scripts:/gameplay/step-counter.toy");
|
|
||||||
|
|
||||||
player = node.loadChild("scripts:/gameplay/lejana.toy");
|
|
||||||
}
|
|
||||||
|
|
||||||
fn onInit(node: opaque) {
|
fn onInit(node: opaque) {
|
||||||
node.generateLevel(clock().hash(), MAP_WIDTH, MAP_HEIGHT);
|
//load the map stuff
|
||||||
|
tilemap = node.loadChild("scripts:/gameplay/tilemap.toy");
|
||||||
|
node.generateScene(clock().hash(), MAP_GRID_WIDTH, MAP_GRID_HEIGHT);
|
||||||
|
|
||||||
|
//load and initialize the UI (depends on map stuff)
|
||||||
|
stepCounter = node.loadChild("scripts:/gameplay/step-counter.toy");
|
||||||
|
stepCounter.setNodeRect(
|
||||||
|
0,
|
||||||
|
0,
|
||||||
|
floor(TILE_PIXEL_WIDTH * MAP_GRID_WIDTH * tilemap.getNodeWorldScaleX()), //width - stretches along the top of the tilemap
|
||||||
|
stepCounter.getNodeRectH() //default height
|
||||||
|
);
|
||||||
stepCounter.callNodeFn("setMaxSteps", 100);
|
stepCounter.callNodeFn("setMaxSteps", 100);
|
||||||
|
|
||||||
|
//offset the scene node, so it shows the map outside separate from the UI
|
||||||
|
node.setNodePositionY(16);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
fn onFree(node: opaque) {
|
||||||
rng.freeRandomGenerator();
|
//cleanup
|
||||||
|
if (rng != null) {
|
||||||
|
rng.freeRandomGenerator();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//fn onStep(node: opaque) {
|
//fn onStep(node: opaque) {
|
||||||
@@ -48,36 +45,32 @@ fn onFree(node: opaque) {
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
fn onDraw(node: opaque) {
|
||||||
|
//use the onDraw to sort - skip too many steps
|
||||||
node.sortChildrenNode(depthComparator);
|
node.sortChildrenNode(depthComparator);
|
||||||
stepCounter.callNodeFn("customOnDraw", 0, 0, TILE_WIDTH * MAP_WIDTH, 32);
|
|
||||||
}
|
|
||||||
|
|
||||||
//utils - polyfills
|
|
||||||
fn loadChild(parent: opaque, fname: string) {
|
|
||||||
var child: opaque = loadNode(fname);
|
|
||||||
parent.pushNode(child);
|
|
||||||
return child;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//gameplay functions
|
//gameplay functions
|
||||||
fn generateLevel(node: opaque, seed: int, width: int, height: int) {
|
fn generateScene(node: opaque, seed: int, width: int, height: int) {
|
||||||
//reset the array
|
//reset the array
|
||||||
enemies = [];
|
enemies = [];
|
||||||
|
|
||||||
if (rng != null) rng.freeRandomGenerator();
|
if (rng != null) {
|
||||||
|
rng.freeRandomGenerator();
|
||||||
|
}
|
||||||
rng = createRandomGenerator(seed);
|
rng = createRandomGenerator(seed);
|
||||||
|
|
||||||
//place player
|
|
||||||
player.callNodeFn("setGridPos", 1, 1);
|
|
||||||
|
|
||||||
//generate map
|
//generate map
|
||||||
tilemap.callNodeFn("generateFromRng", rng, 16, 16);
|
tilemap.callNodeFn("generateFromRng", rng, width, height);
|
||||||
collisionMap = tilemap.callNodeFn("getCollisionMap");
|
collisionDataCache = tilemap.callNodeFn("getCollisionData");
|
||||||
|
|
||||||
|
//place player here, so drop dropping can avoid the player
|
||||||
|
player = node.loadChild("scripts:/gameplay/lejana.toy");
|
||||||
|
player.callNodeFn("setGridPosition", 1, 1);
|
||||||
|
|
||||||
//generate drones
|
//generate drones
|
||||||
var droneCount: int const = rng.generateRandomNumber() % 2 + 20;
|
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: opaque = node.loadChild("scripts:/gameplay/drone.toy");
|
||||||
d.initNode();
|
d.initNode();
|
||||||
|
|
||||||
enemies.push(d);
|
enemies.push(d);
|
||||||
@@ -86,36 +79,40 @@ 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 / 6 * 2 && y < height / 6 * 2) {
|
while (x == 0 && y == 0 || node.getWalkableAt(x, y) == false || x < width / 6 * 2 && y < height / 6 * 2) {
|
||||||
x = rng.generateRandomNumber() % width;
|
x = rng.generateRandomNumber() % width;
|
||||||
y = rng.generateRandomNumber() % height;
|
y = rng.generateRandomNumber() % height;
|
||||||
}
|
}
|
||||||
|
|
||||||
d.callNodeFn("setGridPos", x, y);
|
d.callNodeFn("setGridPosition", x, y);
|
||||||
d.callNodeFn("setRealPos", (width -1) * TILE_WIDTH, (height - 1) * TILE_HEIGHT);
|
d.setNodePositionX(MAP_GRID_WIDTH * TILE_PIXEL_WIDTH);
|
||||||
|
d.setNodePositionY(MAP_GRID_HEIGHT * TILE_PIXEL_HEIGHT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getCollisionAt(node: opaque, x: int, y: int) {
|
fn getWalkableAt(node: opaque, x: int, y: int) {
|
||||||
if (collisionMap == null || x == null || y == null) {
|
if (collisionDataCache == null || x == null || y == null) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//entities
|
//entities
|
||||||
var pos = player.callNodeFn("getGridPos");
|
if (x == player.callNodeFn("getGridPositionX") && y == player.callNodeFn("getGridPositionY")) {
|
||||||
if (x == pos[0] && y == pos[1]) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i = 0; i < enemies.length(); i++) {
|
for (var i = 0; i < enemies.length(); i++) {
|
||||||
var pos = enemies[i].callNodeFn("getGridPos");
|
if (x == enemies[i].callNodeFn("getGridPositionX") && y == enemies[i].callNodeFn("getGridPositionY")) {
|
||||||
if (x == pos[0] && y == pos[1]) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//outside the grid
|
||||||
|
if (y * MAP_GRID_WIDTH + x < 0 || y * MAP_GRID_WIDTH + x >= collisionDataCache.length()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
//default
|
//default
|
||||||
return collisionMap[y * MAP_WIDTH + x];
|
return collisionDataCache[y * MAP_GRID_WIDTH + x];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runAI(node: opaque) {
|
fn runAI(node: opaque) {
|
||||||
@@ -126,21 +123,28 @@ fn runAI(node: opaque) {
|
|||||||
stepCounter.callNodeFn("alterRemainingSteps", -1);
|
stepCounter.callNodeFn("alterRemainingSteps", -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth
|
//utils - polyfills
|
||||||
var lhsPos = lhs.callNodeFn("getRealPos");
|
fn loadChild(parent: opaque, fname: string) {
|
||||||
var rhsPos = rhs.callNodeFn("getRealPos");
|
//TODO: add this to the API proper
|
||||||
|
var child: opaque = loadNode(fname);
|
||||||
|
parent.pushNode(child);
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
if (lhsPos == null || rhsPos == null) { //BUGFIX: children without that function
|
fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth
|
||||||
|
var lhsPositionY = lhs.getNodeWorldPositionY();
|
||||||
|
var rhsPositionY = rhs.getNodeWorldPositionY();
|
||||||
|
|
||||||
|
if (lhsPositionY == null || rhsPositionY == null) { //BUGFIX: children without that function
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lhsPos[1] == rhsPos[1]) { //BUGFIX: prevent z-fighting
|
if (lhsPositionY == rhsPositionY) { //BUGFIX: prevent z-fighting
|
||||||
return lhsPos[0] <= rhsPos[0];
|
var lhsPositionX = lhs.getNodeWorldPositionX();
|
||||||
|
var rhsPositionX = rhs.getNodeWorldPositionX();
|
||||||
|
|
||||||
|
return lhsPositionX < rhsPositionX;
|
||||||
}
|
}
|
||||||
|
|
||||||
return lhsPos[1] < rhsPos[1];
|
return lhsPositionY < rhsPositionY;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getCameraPos(node: opaque) {
|
|
||||||
return [CAMERA_X, CAMERA_Y];
|
|
||||||
}
|
|
||||||
@@ -1,9 +1,19 @@
|
|||||||
import node;
|
import node;
|
||||||
|
|
||||||
//utils
|
//members
|
||||||
var maxSteps: int = 0;
|
var maxSteps: int = 0;
|
||||||
var remainingSteps: int = 0;
|
var remainingSteps: int = 0;
|
||||||
|
|
||||||
|
//accessors
|
||||||
|
fn getMaxSteps(node: opaque) {
|
||||||
|
return maxSteps;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getRemainingSteps(node: opaque) {
|
||||||
|
return remainingSteps;
|
||||||
|
}
|
||||||
|
|
||||||
|
//mutators
|
||||||
fn setMaxSteps(node: opaque, steps: int) {
|
fn setMaxSteps(node: opaque, steps: int) {
|
||||||
maxSteps = steps;
|
maxSteps = steps;
|
||||||
remainingSteps = steps;
|
remainingSteps = steps;
|
||||||
@@ -13,14 +23,7 @@ fn setRemainingSteps(node: opaque, steps: int) {
|
|||||||
remainingSteps = steps;
|
remainingSteps = steps;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getMaxSteps(node: opaque) {
|
//utility mutators
|
||||||
return maxSteps;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn getRemainingSteps(node: opaque) {
|
|
||||||
return remainingSteps;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn alterRemainingSteps(node: opaque, increment: int) {
|
fn alterRemainingSteps(node: opaque, increment: int) {
|
||||||
remainingSteps += increment;
|
remainingSteps += increment;
|
||||||
if (remainingSteps > maxSteps) {
|
if (remainingSteps > maxSteps) {
|
||||||
@@ -47,21 +50,18 @@ fn onLoad(node: opaque) {
|
|||||||
|
|
||||||
node
|
node
|
||||||
.loadChild("scripts:/gameplay/text.toy")
|
.loadChild("scripts:/gameplay/text.toy")
|
||||||
.setNodeText("fonts:/alphbeta.ttf", 32, "Water", 0, 60, 240, 255);
|
.setNodeText("fonts:/alphbeta.ttf", 32, "Water", 0, 60, 240, 255); //rgba
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
fn onFree(node: opaque) {
|
||||||
node.freeNodeTexture();
|
node.freeNodeTexture();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn customOnDraw(node: opaque, x: int, y: int, w: int, h: int) {
|
fn onDraw(node: opaque) {
|
||||||
if (remainingSteps > 0 && maxSteps > 0) {
|
if (remainingSteps > 0 && maxSteps > 0) {
|
||||||
var tmp = float remainingSteps / maxSteps * w;
|
var tmp = float remainingSteps / maxSteps * node.getNodeRectW();
|
||||||
|
var h = node.getNodeRectH();
|
||||||
|
|
||||||
node.drawNode(x, y, int tmp, h);
|
node.drawNode(node.getNodePositionX(), node.getNodePositionY(), int tmp, h);
|
||||||
|
|
||||||
for (var i: int = 0; i < node.getChildNodeCount(); i++) {
|
|
||||||
node.getChildNode(i).callNodeFn("customOnDraw", x, y);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,17 @@ import node;
|
|||||||
|
|
||||||
//this is a child of the step counter, which simply renders text
|
//this is a child of the step counter, which simply renders text
|
||||||
|
|
||||||
fn customOnDraw(node: opaque, parentX: int, parentY: int) {
|
fn onDraw(node: opaque) {
|
||||||
node.drawNode(parentX, parentY);
|
var posX: int = node.getNodePositionX();
|
||||||
|
var posY: int = node.getNodePositionY();
|
||||||
|
var scaleX: float = node.getNodeScaleX();
|
||||||
|
var scaleY: float = node.getNodeScaleY();
|
||||||
|
|
||||||
|
node.drawNode(floor(posX * scaleX), floor(posY * scaleY));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onFree(node: opaque) {
|
fn onFree(node: opaque) {
|
||||||
node.freeNodeTexture();
|
node.freeNodeTexture();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//TODO: polyfill?
|
||||||
@@ -1,22 +1,8 @@
|
|||||||
import standard;
|
import standard;
|
||||||
import node;
|
import node;
|
||||||
|
|
||||||
//consts
|
//constants mapped to the given file "tileset.png"
|
||||||
var TILE_WIDTH: int const = 32;
|
var tileset: [string : [int]] const = [
|
||||||
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]] = [
|
|
||||||
"pillar": [0, 0, 0],
|
"pillar": [0, 0, 0],
|
||||||
|
|
||||||
"floor-0": [0, 1, 1],
|
"floor-0": [0, 1, 1],
|
||||||
@@ -40,57 +26,61 @@ var tileset: [string : [int]] = [
|
|||||||
"edge-br": [3, 4, 0]
|
"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?
|
//fast accessors
|
||||||
var camX = 0;
|
fn getTilemapData(node: opaque) {
|
||||||
var camY = 0;
|
return tilemap;
|
||||||
var camW = 1080;
|
}
|
||||||
var camH = 720;
|
|
||||||
|
|
||||||
|
fn getCollisionData(node: opaque) {
|
||||||
|
return collisions;
|
||||||
|
}
|
||||||
|
|
||||||
//lifecycle functions
|
//lifecycle functions
|
||||||
fn onLoad(node: opaque) {
|
fn onLoad(node: opaque) {
|
||||||
node.loadNodeTexture("sprites:/tileset.png");
|
node.loadNodeTexture("sprites:/tileset.png");
|
||||||
}
|
|
||||||
|
|
||||||
fn onInit(node: opaque) {
|
node.setNodeScaleX(CAMERA_SCALE_X);
|
||||||
//find root
|
node.setNodeScaleY(CAMERA_SCALE_Y);
|
||||||
parent = node;
|
|
||||||
while (parent.getParentNode() != null) {
|
|
||||||
parent = parent.getParentNode();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn onDraw(node: opaque) {
|
fn onDraw(node: opaque) {
|
||||||
if (tilemap == null) {
|
assert tilemap, "tilemap is null";
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var camera = parent.callNodeFn("getCameraPos");
|
|
||||||
|
|
||||||
//calc the region to render
|
var posX: int const = node.getNodeWorldPositionX();
|
||||||
var lowerX: int = round((camX - camW/2.0) / TILE_WIDTH);
|
var posY: int const = node.getNodeWorldPositionY();
|
||||||
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);
|
|
||||||
|
|
||||||
//bounds check
|
//draw everything at twice the original size
|
||||||
lowerX = max(0, lowerX);
|
var scaleX: float const = node.getNodeWorldScaleX();
|
||||||
upperX = min(upperX < 0 ? abs(upperX) : 0, mapWidth);
|
var scaleY: float const = node.getNodeWorldScaleY();
|
||||||
lowerY = max(0, lowerY);
|
|
||||||
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 = 0; j < tilemapGridHeight; j++) {
|
||||||
for (var i = lowerX; i < upperX; i++) {
|
for (var i = 0; i < tilemapGridWidth; i++) {
|
||||||
node.setNodeRect(tilemap[j * mapWidth * 2 + i * 2] * 16, tilemap[j * mapWidth * 2 + i * 2 + 1] * 16, 16, 16);
|
//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
|
//utils functions for map generation
|
||||||
fn generateFromRng(node: opaque, rng: opaque, width: int, height: int) {
|
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);
|
tilemap = bakeTilemap(rawmap, width, height);
|
||||||
collisions = bakeCollisionMap(rawmap, width, height);
|
collisions = bakeCollisionMap(rawmap, width, height);
|
||||||
|
|
||||||
mapWidth = width;
|
tilemapGridWidth = width;
|
||||||
mapHeight = height;
|
tilemapGridHeight = height;
|
||||||
|
|
||||||
|
//debugging
|
||||||
print tilemap;
|
print tilemap;
|
||||||
print collisions;
|
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;
|
import random;
|
||||||
|
|
||||||
//generate an empty grid
|
//generate a grid filled with only pillar tiles, as a starting point
|
||||||
var result: [[string]] = [];
|
var result: [[string]] = [];
|
||||||
var row: [string] = [];
|
var row: [string] = [];
|
||||||
for (var j: int = 0; j < height; j++) {
|
for (var j: int = 0; j < height; j++) {
|
||||||
row.push("pillar");
|
row.push("pillar");
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var i: int = 0; i < width; i++) {
|
for (var i: int = 0; i < width; i++) {
|
||||||
result.push(row);
|
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 j: int = 1; j < height - 1; j++) {
|
||||||
for (var i: int = 1; i < width - 1; i++) {
|
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 x: int = rng.generateRandomNumber() % 4;
|
||||||
var t: string = "floor-" + string x;
|
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[0][height - 1] = "corner-bl";
|
||||||
result[width - 1][height - 1] = "corner-br";
|
result[width - 1][height - 1] = "corner-br";
|
||||||
|
|
||||||
//debug
|
//debugging
|
||||||
result[4][4] = "pillar";
|
result[4][4] = "pillar";
|
||||||
result[width - 5][4] = "pillar";
|
result[width - 5][4] = "pillar";
|
||||||
result[4][height - 5] = "pillar";
|
result[4][height - 5] = "pillar";
|
||||||
result[width - 5][height - 5] = "pillar";
|
result[width - 5][height - 5] = "pillar";
|
||||||
|
|
||||||
|
//return the raw result of strings
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bakeCollisionMap(tilemap: [[string]], width: int, height: int) {
|
//"baking" converts raw tilemaps into usable blobs of data
|
||||||
//generate an empty grid
|
fn bakeTilemap(tilemap: [[string]] const, width: int, height: int): [int] const {
|
||||||
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
|
|
||||||
var result: [int] = [];
|
var result: [int] = [];
|
||||||
|
|
||||||
//extract the position map
|
//extract the positions from the tileset
|
||||||
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++) {
|
||||||
result.push(tileset[ tilemap[i][j] ][0]);
|
result.push(tileset[ tilemap[i][j] ][0]);
|
||||||
result.push(tileset[ tilemap[i][j] ][1]);
|
result.push(tileset[ tilemap[i][j] ][1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn getCollisionMap(node: opaque) {
|
//this bakes the collision map as a separate object
|
||||||
return collisions;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,19 @@
|
|||||||
|
//Globals go here
|
||||||
|
var TILE_PIXEL_WIDTH: int const = 16;
|
||||||
|
var TILE_PIXEL_HEIGHT: int const = 16;
|
||||||
|
|
||||||
|
var MAP_GRID_WIDTH: int const = 16;
|
||||||
|
var MAP_GRID_HEIGHT: int const = 16;
|
||||||
|
|
||||||
|
var CAMERA_SCALE_X: float const = 2.0;
|
||||||
|
var CAMERA_SCALE_Y: float const = 2.0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//A quirk of the setup is that anything defined in the root of `init.toy` becomes a global object
|
//A quirk of the setup is that anything defined in the root of `init.toy` becomes a global object
|
||||||
//To resolve that, the configuration is inside a block scope
|
//To resolve that, the configuration is inside a block scope
|
||||||
{
|
{
|
||||||
|
import standard;
|
||||||
import engine;
|
import engine;
|
||||||
import input;
|
import input;
|
||||||
|
|
||||||
@@ -32,11 +45,13 @@
|
|||||||
|
|
||||||
|
|
||||||
//this function must always be called, or the engine won't run
|
//this function must always be called, or the engine won't run
|
||||||
initWindow("Airport", 800, 600, false); //TODO: custom FPS setting
|
initWindow(
|
||||||
|
"Airport",
|
||||||
|
floor(TILE_PIXEL_WIDTH * MAP_GRID_WIDTH * CAMERA_SCALE_X),
|
||||||
|
floor(TILE_PIXEL_HEIGHT * MAP_GRID_HEIGHT * CAMERA_SCALE_Y) + 32, //32 is UI space
|
||||||
|
false);
|
||||||
|
//TODO: custom FPS setting
|
||||||
|
|
||||||
//kick off the logic of the scene graph
|
//kick off the logic of the scene graph
|
||||||
loadRootNode("scripts:/airplane.toy");
|
loadRootNode("scripts:/gameplay/scene.toy");
|
||||||
}
|
}
|
||||||
|
|
||||||
//Globals go here
|
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 404 B |
Binary file not shown.
|
Before Width: | Height: | Size: 179 B |
Binary file not shown.
|
Before Width: | Height: | Size: 179 B |
Reference in New Issue
Block a user