Hey Ike, kick the drone!

This commit is contained in:
2023-07-16 13:06:16 +10:00
parent 8801d97f5b
commit 7b45e2d5d7
10 changed files with 675 additions and 12 deletions

2
Box

Submodule Box updated: 032ea0d34f...e240b7ac3f

View File

@@ -0,0 +1,182 @@
import standard;
import node;
import random;
//constants
var TILE_PIXEL_WIDTH: int const = 16;
var TILE_PIXEL_HEIGHT: int const = 16;
var DIRECTION_DOWN: int const = 0;
var DIRECTION_UP: int const = 1;
var DIRECTION_RIGHT: int const = 2;
var DIRECTION_LEFT: int const = 3;
//variables
var gridPositionX: int = null;
var gridPositionY: int = null;
var direction: int = null; //BUGFIX: animation not looping properly
var stepAI: int = 0;
var health: int = 2;
var requestFree: bool = false;
//polyfills - animating different cycles on one image
var walkAnimationCounter: int = 0;
//accessors & mutators
fn setGridPosition(node: opaque, x: int, y: int) {
gridPositionX = x;
gridPositionY = y;
node.setNodePositionX(floor( gridPositionX * TILE_PIXEL_WIDTH / node.getNodeScaleX() ));
node.setNodePositionY(floor( gridPositionY * TILE_PIXEL_HEIGHT / node.getNodeScaleY() ));
}
fn getGridPositionX(node: opaque) {
return gridPositionX;
}
fn getGridPositionY(node: opaque) {
return gridPositionY;
}
fn applyDamage(node: opaque, damage: int) {
health -= damage;
}
fn isRequestingFree(node: opaque) {
if (requestFree) {
var parent = node.getParentNode();
var explosion: opaque = parent.loadChild("scripts:/entities/explosion.toy");
explosion.callNodeFn("setGridPosition", gridPositionX, gridPositionY);
return true;
}
else {
return false;
}
}
//lifecycle functions
fn onLoad(node: opaque) {
node.loadNodeTexture("sprites:/drone.png");
node.faceDirection(DIRECTION_DOWN);
}
//fn onUpdate(node: opaque, delta: int) {
// //
//}
fn onStep(node: opaque) {
if (++walkAnimationCounter >= 12) {
node.incrementCurrentNodeFrame();
walkAnimationCounter = 0;
}
//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) );
}
fn onFree(node: opaque) {
node.freeNodeTexture();
}
fn onDraw(node: opaque) {
var posX: int = node.getNodeWorldPositionX();
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 /* + globalCameraX */,
floor(posY * scaleY) - originOffsetY /* + globalCameraY */,
floor(node.getNodeRectW() * scaleX),
floor(node.getNodeRectH() * scaleY)
);
}
//gameplay functions
fn runAI(node: opaque, rng: opaque) {
//If I'm out of health, I need to be removed
if (health <= 0) {
requestFree = true;
return;
}
//every second call
if (++stepAI >= 2) {
stepAI = 0;
var dir = rng.generateRandomNumber() % 4;
var moveX = 0;
var moveY = 0;
if (dir == DIRECTION_DOWN) {
moveY += 1;
node.faceDirection(DIRECTION_DOWN);
}
else if (dir == DIRECTION_UP) {
moveY -= 1;
node.faceDirection(DIRECTION_UP);
}
else if (dir == DIRECTION_RIGHT) {
moveX += 1;
node.faceDirection(DIRECTION_RIGHT);
}
else if (dir == DIRECTION_LEFT) {
moveX -= 1;
node.faceDirection(DIRECTION_LEFT);
}
if (node.getParentNode().callNodeFn("getWalkableAt", gridPositionX + moveX, gridPositionY + moveY)) {
gridPositionX += moveX;
gridPositionY += moveY;
}
}
}
//utils for facing different directions (idling)
fn faceDirection(node: opaque, dir: int) {
if (direction == dir) return;
direction = dir;
node.setNodeRect(32 * 2 * dir, 0, 32, 32);
node.setNodeFrames(2);
walkAnimationCounter = 12;
}
//polyfills - move these to standard
fn normalize(x): int {
if (x > 0) {
return 1;
}
if (x < 0) {
return -1;
}
return 0;
}
fn loadChild(parent: opaque, fname: string) {
//TODO: add this to the API proper
var child: opaque = loadNode(fname);
parent.pushNode(child);
return child;
}

View File

@@ -0,0 +1,79 @@
import standard;
import node;
//constants
var TILE_PIXEL_WIDTH: int const = 16;
var TILE_PIXEL_HEIGHT: int const = 16;
//variables
var gridPositionX: int = null;
var gridPositionY: int = null;
//polyfills - animating different cycles on one image
var walkAnimationCounter: int = 0;
var ticksToDeath: int = 4 * 8 - 1;
//accessors & mutators
fn setGridPosition(node: opaque, x: int, y: int) {
gridPositionX = x;
gridPositionY = y;
node.setNodePositionX(floor( gridPositionX * TILE_PIXEL_WIDTH / node.getNodeScaleX() ));
node.setNodePositionY(floor( gridPositionY * TILE_PIXEL_HEIGHT / node.getNodeScaleY() ));
}
fn getGridPositionX(node: opaque) {
return gridPositionX;
}
fn getGridPositionY(node: opaque) {
return gridPositionY;
}
fn isRequestingFree(node: opaque) {
return ticksToDeath <= 0;
}
//lifecycle functions
fn onLoad(node: opaque) {
node.loadNodeTexture("sprites:/explosion.png");
node.setNodeRect(0, 0, 32, 32);
node.setNodeFrames(8);
}
fn onStep(node: opaque) {
if (++walkAnimationCounter >= 4) {
node.incrementCurrentNodeFrame();
walkAnimationCounter = 0;
}
ticksToDeath--;
}
fn onFree(node: opaque) {
node.freeNodeTexture();
}
fn onDraw(node: opaque) {
var posX: int = node.getNodeWorldPositionX();
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 /* + globalCameraX */,
floor(posY * scaleY) - originOffsetY /* + globalCameraY */,
floor(node.getNodeRectW() * scaleX),
floor(node.getNodeRectH() * scaleY)
);
}

View File

@@ -0,0 +1,276 @@
import standard;
import node;
//constants
var TILE_PIXEL_WIDTH: int const = 16;
var TILE_PIXEL_HEIGHT: int const = 16;
var DIRECTION_DOWN: int const = 0;
var DIRECTION_UP: int const = 1;
var DIRECTION_RIGHT: int const = 2;
var DIRECTION_LEFT: int const = 3;
//position on the game grid
var gridPositionX: int = 1;
var gridPositionY: int = 1;
//cache the keyboard input
var inputX: int = 0;
var inputY: int = 0;
var direction: int = null; //BUGFIX: animation not looping properly
var attackPositionX: int = null;
var attackPositionY: int = null;
//polyfills - animating different cycles on one image
var walkAnimationCounter: int = 0;
var attackAnimationCounter: int = 0;
//accessors & mutators
fn setGridPosition(node: opaque, x: int, y: int) {
gridPositionX = x;
gridPositionY = y;
node.setNodePositionX(floor( gridPositionX * TILE_PIXEL_WIDTH / node.getNodeScaleX() ));
node.setNodePositionY(floor( gridPositionY * TILE_PIXEL_HEIGHT / node.getNodeScaleY() ));
}
fn getGridPositionX(node: opaque) {
return gridPositionX;
}
fn getGridPositionY(node: opaque) {
return gridPositionY;
}
fn setAttackPosition(node: opaque, x: int, y: int) {
attackPositionX = x;
attackPositionY = y;
}
fn getAttackPositionX(node: opaque) {
return attackPositionX;
}
fn getAttackPositionY(node: opaque) {
return attackPositionY;
}
//lifecycle functions
fn onLoad(node: opaque) {
node.loadNodeTexture("sprites:/parvati.png"); //NOTE: all of this script is mapped to this sprite sheet
node.faceDirection(DIRECTION_DOWN);
}
//fn onUpdate(node: opaque, delta: int) {
// //
//}
fn onStep(node: opaque) {
//animation - start idling
if (attackAnimationCounter == 0) {
//move to standing state, from any other animation state
if (node.getNodeRectY() != 0) {
node.setNodeRect(direction * 32 * 4, 0, 32, 32);
node.setNodeFrames(4);
walkAnimationCounter = 12;
}
}
//animation - start attacking
if (attackAnimationCounter-- > 0) {
//move to attacking state, from any other animation state
if (node.getNodeRectY() != 32) {
node.setNodeRect(direction * 32 * 4, 32, 32, 32);
node.setNodeFrames(3);
}
if ((attackAnimationCounter+1) % 4 == 0) { //+1 for a bugfix
node.incrementCurrentNodeFrame();
}
//skip out
return;
}
//make sure you're facing the right direction
if (inputY > 0) {
node.faceDirection(DIRECTION_DOWN);
}
else if (inputY < 0) {
node.faceDirection(DIRECTION_UP);
}
else if (inputX > 0) {
node.faceDirection(DIRECTION_RIGHT);
}
else if (inputX < 0) {
node.faceDirection(DIRECTION_LEFT);
}
//BUGFIX: for smooth animations?
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 (abs(inputX) != abs(inputY) && parent.callNodeFn("getWalkableAt", gridPositionX + inputX, gridPositionY + inputY)) {
//calc movement
gridPositionX += inputX;
gridPositionY += inputY;
parent.callNodeFn("runAI");
}
//if you attacked
else if (attackPositionX != null || attackPositionY != null) {
parent.callNodeFn("runAI");
attackPositionX = null;
attackPositionY = null;
}
//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) {
node.freeNodeTexture();
}
fn onDraw(node: opaque) {
var posX: int = node.getNodeWorldPositionX();
var posY: int = node.getNodeWorldPositionY();
var scaleX: float = node.getNodeWorldScaleX();
var scaleY: float = node.getNodeWorldScaleY();
//this offset is because the sprite cell for parvati.png is twice as big as the sprite cell for the floor tiles
var originOffsetX: int = player.getNodeRectW() / 4;
var originOffsetY: int = player.getNodeRectH() / 2;
node.drawNode(
floor(posX * scaleX) - originOffsetX /* + globalCameraX */,
floor(posY * scaleY) - originOffsetY /* + globalCameraY */,
floor(node.getNodeRectW() * scaleX),
floor(node.getNodeRectH() * scaleY)
);
}
//event functions
fn onKeyDown(node: opaque, event: string) {
//enable attack
if (event == "character_attack" && inputX == 0 && inputY == 0) {
attackAnimationCounter = 12;
if (direction == DIRECTION_DOWN) {
node.setAttackPosition(gridPositionX, gridPositionY + 1);
}
else if (direction == DIRECTION_UP) {
node.setAttackPosition(gridPositionX, gridPositionY - 1);
}
else if (direction == DIRECTION_RIGHT) {
node.setAttackPosition(gridPositionX + 1, gridPositionY);
}
else if (direction == DIRECTION_LEFT) {
node.setAttackPosition(gridPositionX - 1, gridPositionY);
}
return;
}
//if moving, don't take any more input
if (node.getNodeMotionX() != 0 || node.getNodeMotionY() != 0) {
return;
}
if (event == "character_up") {
inputY -= 1;
return;
}
if (event == "character_down") {
inputY += 1;
return;
}
if (event == "character_left") {
inputX -= 1;
return;
}
if (event == "character_right") {
inputX += 1;
return;
}
}
fn onKeyUp(node: opaque, event: string) {
if (event == "character_up" && inputY < 0) {
inputY = 0;
return;
}
if (event == "character_down" && inputY > 0) {
inputY = 0;
return;
}
if (event == "character_left" && inputX < 0) {
inputX = 0;
return;
}
if (event == "character_right" && inputX > 0) {
inputX = 0;
return;
}
}
//utils for facing different directions (idling)
fn faceDirection(node: opaque, dir: int) {
if (direction == dir) return;
direction = dir;
node.setNodeRect(32 * 4 * dir, 0, 32, 32);
node.setNodeFrames(4);
walkAnimationCounter = 12;
}
//polyfills - move these to standard
fn normalize(x): int {
if (x > 0) {
return 1;
}
if (x < 0) {
return -1;
}
return 0;
}

View File

@@ -1,14 +1,20 @@
import standard; import standard;
import random; import random;
import runner;
import node; import node;
//persistent members of the scene node //persistent members of the scene node
var rng: opaque = null; var rng: opaque = null;
//map data (interleaved) //map data
var tilemap: [int] = null; var tilemap: [int] = null;
var metadata: [[[string: any]]] = null;
var mapGridWidth: int = null;
var mapGridHeight: int = null;
//entities
var player: opaque = null;
//lifetime functions //lifetime functions
fn onInit(node: opaque) { fn onInit(node: opaque) {
@@ -18,13 +24,27 @@ fn onInit(node: opaque) {
} }
rng = createRandomGenerator(clock().hash()); rng = createRandomGenerator(clock().hash());
//generate the level, filling out the data
generateLevel(rng); //generate the level, filling out the entity data
node.generateLevel(rng);
//generate the child node to render the map //generate the child node to render the map
var child: opaque = node.loadChild("scripts:/tilemap/renderer.toy"); var renderer: opaque = node.loadChild("scripts:/tilemap/renderer.toy");
renderer.callNodeFn("setTilemap", tilemap);
}
child.callNodeFn("setTilemap", tilemap); fn onStep(node: opaque) {
for (var i: int = 0; i < node.getChildNodeCount(); i++) {
var child = node.getChildNode(i);
if (child != null) {
if (child.callNodeFn("isRequestingFree") == true) {
node.freeChildNode(i);
}
}
}
node.sortChildrenNode(depthComparator);
} }
fn onFree(node: opaque) { fn onFree(node: opaque) {
@@ -35,9 +55,7 @@ fn onFree(node: opaque) {
//scene loading //scene loading
fn generateLevel(rng: opaque) { fn generateLevel(node: opaque, rng: opaque) {
import runner;
//load and run the generator script //load and run the generator script
var generatorScript: opaque = loadScript("scripts:/tilemap/generator.toy"); var generatorScript: opaque = loadScript("scripts:/tilemap/generator.toy");
generatorScript.runScript(); generatorScript.runScript();
@@ -47,14 +65,114 @@ fn generateLevel(rng: opaque) {
//get the resulting blobs of data for processing //get the resulting blobs of data for processing
tilemap = generatorScript.getScriptVar("tilemap"); tilemap = generatorScript.getScriptVar("tilemap");
metadata = generatorScript.getScriptVar("metadata");
mapGridWidth = generatorScript.getScriptVar("MAP_GRID_WIDTH"); //cache these for lookups
mapGridHeight = generatorScript.getScriptVar("MAP_GRID_HEIGHT");
//free //free the generator
generatorScript.freeScript(); generatorScript.freeScript();
//spawn && position the player
player = node.loadChild("scripts:/entities/player.toy");
var w: int const = metadata.length();
var h: int const = metadata[0].length();
var room = metadata[rng.generateRandomNumber() % w][rng.generateRandomNumber() % h];
player.callNodeFn("setGridPosition", room["doorX"], room["doorY"]);
//spawn && position the enemies
for (var i: int = 0; i < 20; /* EMPTY */) {
//get random position within a room random room
var room = metadata[rng.generateRandomNumber() % w][rng.generateRandomNumber() % h];
var x = rng.generateRandomNumber() % room["w"];
var y = rng.generateRandomNumber() % room["h"];
if (!node.getWalkableAt(room["x"] + x, room["y"] + y)) {
continue;
}
var drone: opaque = node.loadChild("scripts:/entities/drone.toy");
drone.callNodeFn("setGridPosition", room["x"] + x, room["y"] + y);
//increment here
i++;
}
} }
//to allow the game to progress
fn runAI(node: opaque) {
//player attacking the drones
var attackPositionX: int = player.callNodeFn("getAttackPositionX");
var attackPositionY: int = player.callNodeFn("getAttackPositionY");
//if the player is attacking...
if (attackPositionX != null && attackPositionY != null) {
//for each child node
for (var i = 0; i < node.getChildNodeCount(); i++) {
var child = node.getChildNode(i);
//find the target
if (child.callNodeFn("getGridPositionX") == attackPositionX && child.callNodeFn("getGridPositionY") == attackPositionY) {
child.callNodeFn("applyDamage", 1); //just 1 damage for now
}
}
}
//run normal routines
for (var i: int = 0; i < node.getChildNodeCount(); i++) {
node.getChildNode(i).callNodeFn("runAI", rng);
}
}
//for collisions
fn getWalkableAt(node: opaque, x: int, y: int) {
if (tilemap == null || x == null || y == null) {
return false;
}
//entities
if (x == player.callNodeFn("getGridPositionX") && y == player.callNodeFn("getGridPositionY")) {
return false;
}
for (var i = 0; i < node.getChildNodeCount(); i++) {
var child = node.getChildNode(i);
if (x == child.callNodeFn("getGridPositionX") && y == child.callNodeFn("getGridPositionY")) {
return false;
}
}
//default
return tilemap[y * mapGridWidth * 3 + x * 3 + 2] == 1; // 1 = walkable
}
//for sorting by depth
fn depthComparator(lhs: opaque, rhs: opaque) {
var lhsPositionY = lhs.getNodeWorldPositionY();
var rhsPositionY = rhs.getNodeWorldPositionY();
if (lhsPositionY == null || rhsPositionY == null) { //BUGFIX: children without that function
return true;
}
if (lhsPositionY == rhsPositionY) { //BUGFIX: prevent z-fighting
var lhsPositionX = lhs.getNodeWorldPositionX();
var rhsPositionX = rhs.getNodeWorldPositionX();
return lhsPositionX < rhsPositionX;
}
return lhsPositionY < rhsPositionY;
}
//polyfills //polyfills

View File

@@ -46,10 +46,15 @@ var CELL_HEIGHT: int const = 16;
var CELL_COUNT_X: int const = 3; var CELL_COUNT_X: int const = 3;
var CELL_COUNT_Y: int const = 3; var CELL_COUNT_Y: int const = 3;
var MAP_GRID_WIDTH: int const = CELL_WIDTH * CELL_COUNT_X;
var MAP_GRID_HEIGHT: int const = CELL_HEIGHT * CELL_COUNT_Y;
//raw interleaved tile data //raw interleaved tile data
var tilemap: [int] = null; var tilemap: [int] = null;
//room metadata for placing entities
var metadata: [[[string: any]]] = null;
//public functions //public functions
fn generateTilemapData(rng: opaque) { fn generateTilemapData(rng: opaque) {
@@ -87,7 +92,7 @@ fn generateTilemapData(rng: opaque) {
} }
//generate corridor metadata //generate corridor metadata
var corridorData: any = generateCorridorData(rng); var corridorData = generateCorridorData(rng);
//etch each tile string into the tilemap //etch each tile string into the tilemap
for (var j: int = 0; j < CELL_COUNT_Y; j++) { for (var j: int = 0; j < CELL_COUNT_Y; j++) {
@@ -100,6 +105,9 @@ fn generateTilemapData(rng: opaque) {
etchCorridors(roomData, corridorData, rng); etchCorridors(roomData, corridorData, rng);
//TODO: etch the walls with a filter, based on the room's themes //TODO: etch the walls with a filter, based on the room's themes
//save the metadata for later retrieval
metadata = roomData;
} }
fn generateRoomMetadata(rng: opaque, left: int, top: int, width: int, height: int) { fn generateRoomMetadata(rng: opaque, left: int, top: int, width: int, height: int) {

View File

@@ -3,7 +3,7 @@ import standard;
import node; import node;
//lifecycle functions //lifecycle functions
fn onLoad(node: opaque) { fn onInit(node: opaque) {
// node.setNodeScaleX(CAMERA_SCALE_X); // node.setNodeScaleX(CAMERA_SCALE_X);
// node.setNodeScaleY(CAMERA_SCALE_Y); // node.setNodeScaleY(CAMERA_SCALE_Y);
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

Before

Width:  |  Height:  |  Size: 3.9 KiB

After

Width:  |  Height:  |  Size: 3.9 KiB