Code edit (3 edits merged)
Please save this source code
User prompt
do //Processes other 7 node directions here.
Code edit (6 edits merged)
Please save this source code
User prompt
I want to change cell.neighbors to be 8 variables instead consisting of upLeft, up, upRight, right, downRight, down, downLeft, left
Code edit (10 edits merged)
Please save this source code
User prompt
Make cell.neighbors = []; be the cells 8 neighbors, with fixed offsets based on the direction of the neighbour
User prompt
In Grid I want cell.neighbors = []; to be filled with its neighbor cells
Code edit (1 edits merged)
Please save this source code
Code edit (8 edits merged)
Please save this source code
User prompt
Add a method to Grid that does a flood fill a* from staring from goals ending in spawn, giving each tile a score
Code edit (1 edits merged)
Please save this source code
Code edit (3 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: undefined is not an object (evaluating 'data.type')' in or related to this line: 'switch (data.type) {' Line Number: 38
Code edit (3 edits merged)
Please save this source code
User prompt
the cellType check still use hardcoded variables, fix this
User prompt
Update the grid class to use gridWidth, gridHeight instead of hardcoded values
Code edit (1 edits merged)
Please save this source code
User prompt
When initializing the grid, set all tiles that exist on the edge of the grid to to be of the type wall
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
To debug cell, add a debug arrow attached to the class below numberLabel
Code edit (3 edits merged)
Please save this source code
User prompt
anchor the numberLabel centered in debugcell
User prompt
Add a number label to the debug cells
User prompt
I need to debug some A* path finding for this I need a debug cell type which shows a number + an arrow pointing in the direction of the A* grid.
/**** * Classes ****/ // DebugCell class var DebugCell = Container.expand(function () { var self = Container.call(this); var cellGraphics = self.attachAsset('cell', { anchorX: 0.5, anchorY: 0.5 }); cellGraphics.tint = Math.random() * 0xffffff; // Add a debug arrow to the debug cells var debugArrow = self.attachAsset('arrow', { anchorX: 0.5, anchorY: 0.5 }); debugArrow.rotation = Math.random() * Math.PI * 2; self.addChild(debugArrow); debugArrow.blendMode = 2; // Add a number label to the debug cells var numberLabel = new Text2('0', { size: 30, fill: "#ffffff", weight: 800 }); numberLabel.anchor.set(.5, .5); self.addChild(numberLabel); self.update = function () {}; self.render = function (data) { switch (data.type) { case 1: { cellGraphics.tint = 0xaaaaaa; debugArrow.visible = numberLabel.visible = false; break; } case 2: { cellGraphics.tint = 0x000088; debugArrow.visible = numberLabel.visible = false; break; } case 3: { cellGraphics.tint = 0x008800; debugArrow.visible = numberLabel.visible = false; break; } } }; }); // Defense class var Defense = Container.expand(function () { var self = Container.call(this); var defenseGraphics = self.attachAsset('defense', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Defense logic goes here }; }); // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Enemy logic goes here }; }); // Grid class /* Types: 0: Ground 1: Wall 2: Spawn 3: Target */ var Grid = Container.expand(function (gridWidth, gridHeight) { var self = Container.call(this); self.cells = []; for (var i = 0; i < gridWidth; i++) { self.cells[i] = []; for (var j = 0; j < gridHeight; j++) { var debugCell = new DebugCell(); // Set the type to 1 (wall) if the cell is on the edge of the grid, otherwise set it to 0 (ground) var cellType = i === 0 || i === gridWidth - 1 || j === 0 || j === gridHeight - 1 ? 1 : 0; if (i > 11 - 3 && i <= 11 + 3) { if (j === 0) { cellType = 2; } else if (j === gridHeight - 1) { cellType = 3; } } self.cells[i][j] = { type: cellType, debugCell: debugCell }; self.addChild(debugCell); debugCell.x = i * 75; debugCell.y = j * 75; } } self.renderDebug = function () { for (var i = 0; i < gridWidth; i++) { for (var j = 0; j < gridHeight; j++) { var debugCell = self.cells[i][j].debugCell; debugCell.render(self.cells[i][j]); } } }; self.update = function () { // Grid logic goes here // Flood fill a* from goals ending in spawn, giving each tile a score var openList = []; var closedList = []; var startNode = self.cells[0][0]; // Assuming the start node is at (0,0) var goalNode = self.cells[gridWidth - 1][gridHeight - 1]; // Assuming the goal node is at (gridWidth - 1, gridHeight - 1) openList.push(startNode); while (openList.length > 0) { // Sort the open list by score in ascending order openList.sort(function (a, b) { return a.score - b.score; }); var currentNode = openList.shift(); closedList.push(currentNode); if (currentNode === goalNode) { // Reached the goal break; } // Check all 4 adjacent cells for (var dx = -1; dx <= 1; dx++) { for (var dy = -1; dy <= 1; dy++) { if (dx === 0 && dy === 0) { // Skip the current cell continue; } var x = currentNode.x + dx; var y = currentNode.y + dy; if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight) { var adjacentNode = self.cells[x][y]; if (closedList.includes(adjacentNode)) { // Skip if the adjacent cell is in the closed list continue; } var score = currentNode.score + 1; // Assuming the cost to move to an adjacent cell is 1 if (!openList.includes(adjacentNode)) { // Add the adjacent cell to the open list openList.push(adjacentNode); } else if (score < adjacentNode.score) { // Update the score if a better path is found adjacentNode.score = score; } } } } } }; }); // Assets will be automatically created and loaded by the LK engine // Tower class var Tower = Container.expand(function () { var self = Container.call(this); var towerGraphics = self.attachAsset('tower', { anchorX: 0.5, anchorY: 0.5 }); self.update = function () { // Tower logic goes here }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 //Init game with black background }); /**** * Game Code ****/ /* Types: 0: Ground 1: Wall 2: Spawn 3: Target */ // Grid class // Initialize game variables var tower = game.addChild(new Tower()); tower.x = 2048 / 2; tower.y = 2732 / 2; var enemies = []; var defenses = []; var grid = new Grid(24, 28); grid.x = 150; grid.y = 150; grid.renderDebug(); game.addChild(grid); game.update = function () { // Game logic goes here // Spawn enemies, update defenses, check for collisions, etc. };
===================================================================
--- original.js
+++ change.js
@@ -29,9 +29,9 @@
self.render = function (data) {
switch (data.type) {
case 1:
{
- cellGraphics.tint = 0x444444;
+ cellGraphics.tint = 0xaaaaaa;
debugArrow.visible = numberLabel.visible = false;
break;
}
case 2:
@@ -87,10 +87,14 @@
for (var j = 0; j < gridHeight; j++) {
var debugCell = new DebugCell();
// Set the type to 1 (wall) if the cell is on the edge of the grid, otherwise set it to 0 (ground)
var cellType = i === 0 || i === gridWidth - 1 || j === 0 || j === gridHeight - 1 ? 1 : 0;
- if (j === 0 && i > 11 - 2) {
- cellType = 2;
+ if (i > 11 - 3 && i <= 11 + 3) {
+ if (j === 0) {
+ cellType = 2;
+ } else if (j === gridHeight - 1) {
+ cellType = 3;
+ }
}
self.cells[i][j] = {
type: cellType,
debugCell: debugCell
@@ -109,8 +113,52 @@
}
};
self.update = function () {
// Grid logic goes here
+ // Flood fill a* from goals ending in spawn, giving each tile a score
+ var openList = [];
+ var closedList = [];
+ var startNode = self.cells[0][0]; // Assuming the start node is at (0,0)
+ var goalNode = self.cells[gridWidth - 1][gridHeight - 1]; // Assuming the goal node is at (gridWidth - 1, gridHeight - 1)
+ openList.push(startNode);
+ while (openList.length > 0) {
+ // Sort the open list by score in ascending order
+ openList.sort(function (a, b) {
+ return a.score - b.score;
+ });
+ var currentNode = openList.shift();
+ closedList.push(currentNode);
+ if (currentNode === goalNode) {
+ // Reached the goal
+ break;
+ }
+ // Check all 4 adjacent cells
+ for (var dx = -1; dx <= 1; dx++) {
+ for (var dy = -1; dy <= 1; dy++) {
+ if (dx === 0 && dy === 0) {
+ // Skip the current cell
+ continue;
+ }
+ var x = currentNode.x + dx;
+ var y = currentNode.y + dy;
+ if (x >= 0 && x < gridWidth && y >= 0 && y < gridHeight) {
+ var adjacentNode = self.cells[x][y];
+ if (closedList.includes(adjacentNode)) {
+ // Skip if the adjacent cell is in the closed list
+ continue;
+ }
+ var score = currentNode.score + 1; // Assuming the cost to move to an adjacent cell is 1
+ if (!openList.includes(adjacentNode)) {
+ // Add the adjacent cell to the open list
+ openList.push(adjacentNode);
+ } else if (score < adjacentNode.score) {
+ // Update the score if a better path is found
+ adjacentNode.score = score;
+ }
+ }
+ }
+ }
+ }
};
});
// Assets will be automatically created and loaded by the LK engine
// Tower class
White circle with two eyes, seen from above.. In-Game asset. 2d. High contrast. No shadows
White simple circular enemy seen from above, black outline. Black eyes, with a single shield in-font of it. Black and white only. Blue background.
White circle with black outline. Blue background.. In-Game asset. 2d. High contrast. No shadows