Drones working

This commit is contained in:
2023-03-05 18:25:00 +11:00
parent d517949f53
commit 7f7e5a56c9
5 changed files with 339 additions and 26 deletions

View File

@@ -28,6 +28,7 @@
<None Include="assets\scripts\demo\tilemap\tilemap.toy" /> <None Include="assets\scripts\demo\tilemap\tilemap.toy" />
<None Include="assets\scripts\empty.toy" /> <None Include="assets\scripts\empty.toy" />
<None Include="assets\scripts\airplane.toy" /> <None Include="assets\scripts\airplane.toy" />
<None Include="assets\scripts\gameplay\drone.toy" />
<None Include="assets\scripts\gameplay\lejana.toy" /> <None Include="assets\scripts\gameplay\lejana.toy" />
<None Include="assets\scripts\gameplay\scene.toy" /> <None Include="assets\scripts\gameplay\scene.toy" />
<None Include="assets\scripts\gameplay\tilemap.toy" /> <None Include="assets\scripts\gameplay\tilemap.toy" />

View File

@@ -0,0 +1,186 @@
import standard;
import node;
//constants
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
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 stepAI: int = 0;
//polyfills - animating different cycles on one image
var stepCount: int = 0;
fn faceDown(node: opaque) {
if (direction == 0) return;
direction = 0;
node.setNodeRect(0, 0, 32, 32);
node.setNodeFrames(2);
}
fn faceUp(node: opaque) {
if (direction == 1) return;
direction = 1;
node.setNodeRect(32 * 2, 0, 32, 32);
node.setNodeFrames(2);
}
fn faceRight(node: opaque) {
if (direction == 2) return;
direction = 2;
node.setNodeRect(32 * 4, 0, 32, 32);
node.setNodeFrames(2);
}
fn faceLeft(node: opaque) {
if (direction == 3) return;
direction = 3;
node.setNodeRect(32 * 6, 0, 32, 32);
node.setNodeFrames(2);
}
//accessors & mutators
fn setGridPos(node: opaque, x: int, y: int) {
gridX = x;
gridY = y;
if (realX == null) {
realX = gridX * TILE_WIDTH;
}
if (realY == null) {
realY = gridY * TILE_HEIGHT;
}
}
fn setRealPos(node: opaque, x: int, y: int) {
realX = x;
realY = y;
}
fn getGridPos(node: opaque) {
return [gridX, gridY];
}
fn getRealPos(node: opaque) {
return [realX, realY];
}
//lifecycle functions
fn onLoad(node: opaque) {
node.loadTexture("sprites:/drone.png");
node.faceDown();
}
fn onInit(node: opaque) {
parent = node.getParentNode();
}
fn onStep(node: opaque) {
if (++stepCount >= 10) {
node.incrementCurrentNodeFrame();
stepCount = 0;
}
//calc movement
var distX = gridX * TILE_WIDTH - realX;
var distY = gridY * TILE_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) {
node.freeTexture();
}
fn customOnDraw(node: opaque) {
var px = 0;
var py = 0;
if (parent != null) {
px = parent.callNodeFn("getX");
py = parent.callNodeFn("getY");
px = px != null ? px : 0;
py = py != null ? py : 0;
}
node.drawNode(realX + px - SPRITE_WIDTH / 4, realY + py - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT);
}
//gameplay functions
fn runAI(node: opaque, rng: opaque) {
if ((stepAI++) >= 1) { //TODO: Toy bug here, something to do with precedence of postfix++?
stepAI = 0;
import random;
var dir = rng.generateRandomNumber() % 4;
var moveX = 0;
var moveY = 0;
if (dir == 0) {
moveY += 1;
node.faceDown();
}
if (dir == 1) {
moveY -= 1;
node.faceUp();
}
if (dir == 2) {
moveX += 1;
node.faceRight();
}
if (dir == 3) {
moveX -= 1;
node.faceLeft();
}
if (parent.callNodeFn("getCollisionAt", gridX + moveX, gridY + moveY)) {
gridX += moveX;
gridY += moveY;
}
}
}
//polyfills - move these to standard
fn normalize(x): int {
if (x > 0) {
return 1;
}
if (x < 0) {
return -1;
}
return 0;
}

View File

@@ -17,8 +17,8 @@ var parent: opaque = null; //cache the parent for quick access
var gridX: int = 1; //position on the game grid var gridX: int = 1; //position on the game grid
var gridY: int = 1; var gridY: int = 1;
var realX: int = 0; //will change until realX = gridX * SPRITE_WIDTH var realX: int = null; //will change until realX = gridX * SPRITE_WIDTH
var realY: int = 0; var realY: int = null;
var motionX: int = 0; //normalized movement direction var motionX: int = 0; //normalized movement direction
var motionY: int = 0; var motionY: int = 0;
@@ -27,6 +27,7 @@ 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 = 60 * 4; //BUGFIX: freeze while drones reach their starting spot
//polyfills - animating different cycles on one image //polyfills - animating different cycles on one image
@@ -46,13 +47,6 @@ fn faceUp(node: opaque) {
node.setNodeFrames(4); node.setNodeFrames(4);
} }
fn faceLeft(node: opaque) {
if (direction == 3) return;
direction = 3;
node.setNodeRect(32 * 12, 0, 32, 32);
node.setNodeFrames(4);
}
fn faceRight(node: opaque) { fn faceRight(node: opaque) {
if (direction == 2) return; if (direction == 2) return;
direction = 2; direction = 2;
@@ -60,15 +54,39 @@ fn faceRight(node: opaque) {
node.setNodeFrames(4); node.setNodeFrames(4);
} }
//polyfills - move these to standard fn faceLeft(node: opaque) {
fn normalize(x): int { if (direction == 3) return;
if (x > 0) { direction = 3;
return 1; node.setNodeRect(32 * 12, 0, 32, 32);
node.setNodeFrames(4);
}
//accessors & mutators
fn setGridPos(node: opaque, x: int, y: int) {
gridX = x;
gridY = y;
if (realX == null) {
realX = gridX * TILE_WIDTH;
} }
if (x < 0) {
return -1; if (realY == null) {
realY = gridY * TILE_HEIGHT;
} }
return 0; }
fn setRealPos(node: opaque, x: int, y: int) {
realX = x;
realY = y;
}
fn getGridPos(node: opaque) {
return [gridX, gridY];
}
fn getRealPos(node: opaque) {
return [realX, realY];
} }
@@ -83,6 +101,12 @@ fn onInit(node: opaque) {
} }
fn onStep(node: opaque) { fn onStep(node: opaque) {
//initial freeze
if (enableMovementCounter > 0) {
enableMovementCounter--;
return;
}
//process input when aligned to a grid //process input when aligned to a grid
if (realX / TILE_WIDTH == gridX && realY / TILE_HEIGHT == gridY && motionX == 0 && motionY == 0) { if (realX / TILE_WIDTH == gridX && realY / TILE_HEIGHT == gridY && motionX == 0 && motionY == 0) {
//disallow wall phasing //disallow wall phasing
@@ -118,6 +142,11 @@ fn onStep(node: opaque) {
if (inputX < 0) { if (inputX < 0) {
node.faceLeft(); node.faceLeft();
} }
//trigger the world
if (inputX != 0 || inputY != 0) {
parent.callNodeFn("runAI");
}
} }
//animation //animation
@@ -147,7 +176,7 @@ fn onFree(node: opaque) {
node.freeTexture(); node.freeTexture();
} }
fn onDraw(node: opaque) { fn customOnDraw(node: opaque) {
var px = 0; var px = 0;
var py = 0; var py = 0;
@@ -162,6 +191,7 @@ fn onDraw(node: opaque) {
node.drawNode(realX + px - SPRITE_WIDTH / 4, realY + py - SPRITE_HEIGHT / 2, SPRITE_WIDTH, SPRITE_HEIGHT); node.drawNode(realX + px - SPRITE_WIDTH / 4, realY + py - SPRITE_HEIGHT / 2, 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") {
@@ -205,4 +235,16 @@ fn onKeyUp(node: opaque, event: string) {
inputX = 0; inputX = 0;
return; return;
} }
}
//polyfills - move these to standard
fn normalize(x): int {
if (x > 0) {
return 1;
}
if (x < 0) {
return -1;
}
return 0;
} }

View File

@@ -1,11 +1,22 @@
import standard; import standard;
import node; import node;
//consts
var TILE_WIDTH: int const = 32;
var TILE_HEIGHT: int const = 32;
var tilemap: opaque = null; var tilemap: opaque = null;
var player: opaque = null; var player: opaque = null;
var enemies: [opaque] = [];
var entities: [opaque] = null; //full list of entities
var collisionMap: [[bool]] = null; //cache this, since it won't change during a level var collisionMap: [[bool]] = null; //cache this, since it won't change during a level
var rng: opaque = null;
//debugging tools
var stepCounter: int = 0; var stepCounter: int = 0;
var drawCounter: int = 0; var drawCounter: int = 0;
@@ -16,8 +27,7 @@ fn onLoad(node: opaque) {
} }
fn onInit(node: opaque) { fn onInit(node: opaque) {
tilemap.callNodeFn("generateFromSeed", clock().hash(), 16, 16); node.generateLevel(clock().hash(), 16, 16);
collisionMap = tilemap.callNodeFn("getCollisionMap");
} }
fn onStep(node: opaque) { fn onStep(node: opaque) {
@@ -26,10 +36,18 @@ fn onStep(node: opaque) {
stepCounter = 0; stepCounter = 0;
drawCounter = 0; drawCounter = 0;
} }
entities = entities.sort(depthComparator);
} }
fn onDraw(node: opaque) { fn onDraw(node: opaque) {
drawCounter++; drawCounter++;
//call each child's custom sort fn
tilemap.callNodeFn("customOnDraw");
for (var i = 0; i < entities.length(); i++) {
entities[i].callNodeFn("customOnDraw");
}
} }
//utils - polyfills //utils - polyfills
@@ -39,11 +57,78 @@ fn loadChild(parent: opaque, fname: string) {
return child; return child;
} }
//connective functions //gameplay functions
fn generateLevel(node: opaque, seed: int, width: int, height: int) {
//reset the array
enemies = [];
entities = [player]; //assume the player exists already
import random;
if (rng != null) rng.freeRandomGenerator();
rng = createRandomGenerator(seed);
//place player
player.callNodeFn("setGridPos", 1, 1);
//generate map
tilemap.callNodeFn("generateFromRng", rng, 16, 16);
collisionMap = tilemap.callNodeFn("getCollisionMap");
//generate drones
var droneCount: int const = rng.generateRandomNumber() % 2 + 2;
for (var i = 0; i < droneCount; i++) {
var d = node.loadChild("scripts:/gameplay/drone.toy");
d.initNode();
enemies.push(d);
entities.push(d);
var x = 0;
var y = 0;
//while generated spot is a collision, or too close to the player
while ((x == 0 && y == 0) || node.getCollisionAt(x, y) == false || (x < (width / 2) && y < (height / 2))) { //TODO: toy bug - no short-circuiting?
x = rng.generateRandomNumber() % width;
y = rng.generateRandomNumber() % height;
}
d.callNodeFn("setGridPos", x, y);
d.callNodeFn("setRealPos", (width -1) * TILE_WIDTH, (height - 1) * TILE_HEIGHT);
}
}
fn getCollisionAt(node: opaque, x: int, y: int) { fn getCollisionAt(node: opaque, x: int, y: int) {
if (collisionMap == null) { if (collisionMap == null || x == null || y == null) {
return false; return false;
} }
//entities
var pos = player.callNodeFn("getGridPos");
if (x == pos[0] && y == pos[1]) {
return false;
}
for (var i = 0; i < enemies.length(); i++) {
var pos = enemies[i].callNodeFn("getGridPos");
if (x == pos[0] && y == pos[1]) {
return false;
}
}
//default
return collisionMap[x][y]; return collisionMap[x][y];
} }
fn runAI(node: opaque) {
for (var i = 0; i < enemies.length(); i++) {
enemies[i].callNodeFn("runAI", rng);
}
}
fn depthComparator(lhs: opaque, rhs: opaque) { //for sorting by depth
var lhsPos = lhs.callNodeFn("getRealPos");
var rhsPos = rhs.callNodeFn("getRealPos");
return lhsPos[1] < rhsPos[1];
}

View File

@@ -48,7 +48,7 @@ fn onLoad(node: opaque) {
node.loadTexture("sprites:/tileset.png"); node.loadTexture("sprites:/tileset.png");
} }
fn onDraw(node: opaque) { fn customOnDraw(node: opaque) {
if (tilemap == null) { if (tilemap == null) {
return; return;
} }
@@ -89,8 +89,8 @@ fn onDraw(node: opaque) {
//utils functions for map generation //utils functions for map generation
fn generateFromSeed(node: opaque, seed: int, width: int, height: int) { fn generateFromRng(node: opaque, rng: opaque, width: int, height: int) {
rawmap = generateRawTilemap(seed, width, height); rawmap = generateRawTilemap(rng, width, height);
tilemap = bakeTilemap(rawmap, width, height); tilemap = bakeTilemap(rawmap, width, height);
collisions = bakeCollisionMap(rawmap, width, height); collisions = bakeCollisionMap(rawmap, width, height);
@@ -99,9 +99,8 @@ fn generateFromSeed(node: opaque, seed: int, width: int, height: int) {
print collisions; print collisions;
} }
fn generateRawTilemap(seed: int, width: int, height: int) { fn generateRawTilemap(rng: opaque, width: int, height: int) {
import random; import random;
var rng: opaque = createRandomGenerator(seed);
//generate an empty grid //generate an empty grid
var result: [[string]] = []; var result: [[string]] = [];