Code edit (1 edits merged)
Please save this source code
Code edit (6 edits merged)
Please save this source code
User prompt
Add a currency to this game, that can be used to build and upgrade towers. Only implement the currency infrastructure for now. Show my coins in the top right corner with a label + a gold coin. Start the game with 80 coins.
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Please add a health bar to the enemies
User prompt
Please add a health bar to the enemies
Code edit (8 edits merged)
Please save this source code
User prompt
Add a health bar to the enemies
Code edit (1 edits merged)
Please save this source code
Code edit (5 edits merged)
Please save this source code
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'var notification = LK.top.addChild(new Notification("Path is blocked!"));' Line Number: 39
Code edit (15 edits merged)
Please save this source code
User prompt
Add a notification class that can take any string and show it as a notification overlay for 2 seconds. Call this where code says "//Show blocking notification here."
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 'cell.cellX = Math.round(enemy.currentCellX)')' in or related to this line: 'cell.cellX = Math.round(enemy.currentCellX);' Line Number: 236
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: undefined is not an object (evaluating 'cell.targets')' in or related to this line: 'enemy.currentTarget = cell.targets[cell.targets.length * Math.random() >> 0];' Line Number: 225
Code edit (7 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: undefined is not an object (evaluating 'cell.targets')' in or related to this line: 'enemy.currentTarget = cell.targets[cell.targets * Math.random() >> 0];' Line Number: 225
User prompt
Please fix the bug: 'TypeError: undefined is not an object (evaluating 'cell.targets')' in or related to this line: 'enemy.currentTarget = cell.targets[cell.targets * Math.random() >> 0];' Line Number: 225
User prompt
Please fix the bug: 'TypeError: undefined is not an object (evaluating 'cell.targets')' in or related to this line: 'enemy.currentTarget = cell.targets[cell.targets * Math.random() >> 0];' Line Number: 225
Code edit (7 edits merged)
Please save this source code
/**** * 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 debugArrows = []; // Add a number label to the debug cells var numberLabel = new Text2('0', { size: 30, fill: 0xFFFFFF, weight: 800 }); numberLabel.anchor.set(.5, .5); self.addChild(numberLabel); self.update = function () {}; self.down = function () { if (self.cell.type == 0 || self.cell.type == 1) { self.cell.type = self.cell.type == 1 ? 0 : 1; if (grid.pathFind()) { self.cell.type = self.cell.type == 1 ? 0 : 1; grid.pathFind(); var notification = game.addChild(new Notification("Path is blocked!")); notification.x = 2048 / 2; notification.y = grid.height - 50; } grid.renderDebug(); } }; self.removeArrows = function () { while (debugArrows.length) { self.removeChild(debugArrows.pop()); } }; self.render = function (data) { switch (data.type) { case 0: case 2: { if (data.pathId != pathId) { self.removeArrows(); numberLabel.setText("-"); cellGraphics.tint = 0x880000; return; } numberLabel.visible = true; var tint = Math.floor(data.score / maxScore * 0x88); cellGraphics.tint = 0x88 - tint << 8 | tint; while (debugArrows.length > data.targets.length) { self.removeChild(debugArrows.pop()); } for (var a = 0; a < data.targets.length; a++) { var destination = data.targets[a]; var ox = destination.x - data.x; var oy = destination.y - data.y; var angle = Math.atan2(oy, ox); if (!debugArrows[a]) { debugArrows[a] = LK.getAsset('arrow', { anchorX: -.5, anchorY: 0.5 }); debugArrows[a].alpha = .5; self.addChildAt(debugArrows[a], 1); // debugArrows = } debugArrows[a].rotation = angle; // debugArrow.rotation = angle; } break; } case 1: { self.removeArrows(); cellGraphics.tint = 0xaaaaaa; numberLabel.visible = false; break; } case 3: { self.removeArrows(); cellGraphics.tint = 0x008800; numberLabel.visible = false; break; } } numberLabel.setText(Math.floor(data.score / 1000) / 10); }; }); // Enemy class var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemy', { anchorX: 0.5, anchorY: 0.5 }); self.speed = Math.random() * .2 + .03; self.cellX = 0; self.cellY = 0; self.currentCellX = 0; self.currentCellY = 0; self.currentTarget = undefined; // Add health bar to the enemy var healthBarOutline = self.attachAsset('healthBarOutline', { anchorX: 0.5, anchorY: 0.5 }); var healthBar = self.attachAsset('healthBar', { anchorX: 0.5, anchorY: 0.5 }); healthBarOutline.y = healthBar.y = -enemyGraphics.height / 2 - 10; // Position the health bar above the enemy self.healthBar = healthBar; }); //Class for the grid, handles all logic related to grid movement and interaction var Grid = Container.expand(function (gridWidth, gridHeight) { var self = Container.call(this); self.cells = []; self.spawns = []; self.goals = []; for (var i = 0; i < gridWidth; i++) { self.cells[i] = []; for (var j = 0; j < gridHeight; j++) { self.cells[i][j] = { score: 0, pathId: 0 }; } } for (var i = 0; i < gridWidth; 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 cell = self.cells[i][j]; 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; self.spawns.push(cell); } else if (j === gridHeight - 1) { cellType = 3; self.goals.push(cell); } } cell.type = cellType; cell.debugCell = debugCell; cell.x = i; cell.y = j; /*if (cell.type == 0) { if (Math.random() > .5) { cell.type = 1; } }*/ //Cache neighbours for speed cell.upLeft = self.cells[i - 1] && self.cells[i - 1][j - 1]; cell.up = self.cells[i - 1] && self.cells[i - 1][j]; cell.upRight = self.cells[i - 1] && self.cells[i - 1][j + 1]; cell.left = self.cells[i][j - 1]; cell.right = self.cells[i][j + 1]; cell.downLeft = self.cells[i + 1] && self.cells[i + 1][j - 1]; cell.down = self.cells[i + 1] && self.cells[i + 1][j]; cell.downRight = self.cells[i + 1] && self.cells[i + 1][j + 1]; cell.neighbors = [cell.upLeft, cell.up, cell.upRight, cell.right, cell.downRight, cell.down, cell.downLeft, cell.left]; cell.targets = []; self.addChild(debugCell); debugCell.cell = cell; debugCell.x = i * 75; debugCell.y = j * 75; } } self.getCell = function (x, y) { return self.cells[x] && self.cells[x][y]; }; self.pathFind = function () { var before = new Date().getTime(); var toProcess = self.goals.concat([]); maxScore = 0; pathId += 1; for (var a = 0; a < toProcess.length; a++) { toProcess[a].pathId = pathId; } function processNode(node, targetValue, targetNode) { if (node && node.type != 1) { if (node.pathId < pathId || targetValue < node.score) { node.targets = [targetNode]; } else if (node.pathId == pathId && targetValue == node.score) { node.targets.push(targetNode); } if (node.pathId < pathId || targetValue < node.score) { node.score = targetValue; if (node.pathId != pathId) { toProcess.push(node); } node.pathId = pathId; if (targetValue > maxScore) { maxScore = targetValue; } } } } while (toProcess.length) { var nodes = toProcess; toProcess = []; for (var a = 0; a < nodes.length; a++) { var node = nodes[a]; var targetScore = node.score + 14142; processNode(node.upLeft, targetScore, node); processNode(node.upRight, targetScore, node); processNode(node.downRight, targetScore, node); processNode(node.downLeft, targetScore, node); targetScore = node.score + 10000; processNode(node.up, targetScore, node); processNode(node.right, targetScore, node); processNode(node.down, targetScore, node); processNode(node.left, targetScore, node); } } for (var a = 0; a < self.spawns.length; a++) { if (self.spawns[a].pathId != pathId) { console.warn("Spawn blocked"); return true; } } for (var a = 0; a < enemies.length; a++) { var enemy = enemies[a]; var target = self.getCell(enemy.cellX, enemy.cellY); if (enemy.currentTarget) { if (enemy.currentTarget.pathId != pathId) { if (!target || target.pathId != pathId) { console.warn("Enemy blocked"); return true; } } } else if (!target || target.pathId != pathId) { console.warn("Enemy blocked"); return true; } } console.log("Speed", new Date().getTime() - before); }; 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.updateEnemy = function (enemy) { var cell = grid.getCell(enemy.cellX, enemy.cellY); if (cell.type == 3) { return true; } if (!enemy.currentTarget) { enemy.currentTarget = cell.targets[0]; } if (enemy.currentTarget) { //Allows enemies to change direction right away. if (cell.score < enemy.currentTarget.score) { enemy.currentTarget = cell; } var ox = enemy.currentTarget.x - enemy.currentCellX; var oy = enemy.currentTarget.y - enemy.currentCellY; var dist = Math.sqrt(ox * ox + oy * oy); if (dist < enemy.speed) { enemy.cellX = Math.round(enemy.currentCellX); enemy.cellY = Math.round(enemy.currentCellY); enemy.currentTarget = undefined; return; } var angle = Math.atan2(oy, ox); enemy.currentCellX += Math.cos(angle) * enemy.speed; enemy.currentCellY += Math.sin(angle) * enemy.speed; } enemy.x = enemy.currentCellX * 75; enemy.y = enemy.currentCellY * 75; }; }); // Notification class var Notification = Container.expand(function (message) { var self = Container.call(this); var notificationGraphics = self.attachAsset('notification', { anchorX: 0.5, anchorY: 0.5 }); var notificationText = new Text2(message, { size: 50, fill: 0x000000, weight: 800 }); notificationText.anchor.set(0.5, 0.5); notificationGraphics.width = notificationText.width + 30; self.addChild(notificationText); self.alpha = 1; var fadeOutTime = 120; // 2 seconds at 60FPS self.update = function () { if (fadeOutTime > 0) { fadeOutTime--; self.alpha = Math.min(fadeOutTime / 120 * 2, 1); } else { self.destroy(); } }; }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x333333 //Init game with black background }); /**** * Game Code ****/ var pathId = 1; var maxScore = 0; var enemies = []; var defenses = []; // Currency infrastructure var coins = 80; var coinLabel = null; var coinIcon = null; var grid = new Grid(24, 28); grid.x = 160; grid.y = 150; grid.pathFind(); grid.renderDebug(); game.addChild(grid); // Show coins in top right corner coinLabel = new Text2("Coins: " + coins, { size: 60, fill: 0xFFD700, weight: 800 }); coinLabel.anchor.set(1, 0); // right aligned, top // Gold coin icon coinIcon = LK.getAsset('cell', { anchorX: 1, anchorY: 0 }); coinIcon.width = 50; coinIcon.height = 50; coinIcon.tint = 0xffd700; // gold color // Positioning: leave some margin from the top right coinLabel.x = -60; coinLabel.y = 10; coinIcon.x = -10; coinIcon.y = 10; var coinContainer = new Container(); coinContainer.addChild(coinLabel); coinContainer.addChild(coinIcon); LK.gui.topRight.addChild(coinContainer); var offset = 0; // Call this function whenever coins change function updateCoins() { if (coinLabel) { coinLabel.setText("Coins: " + coins); } } game.update = function () { if (++offset % 100 == 0) { isFirst = false; var target = grid.spawns[Math.random() * grid.spawns.length >> 0]; var enemy = grid.addChild(new Enemy()); enemy.currentCellX = enemy.cellX = target.x; enemy.currentCellY = enemy.cellY = target.y; enemies.push(enemy); } for (var a = enemies.length - 1; a >= 0; a--) { if (grid.updateEnemy(enemies[a])) { grid.removeChild(enemies[a]); enemies.splice(a--, 1); } } };
===================================================================
--- original.js
+++ change.js
@@ -13,9 +13,9 @@
var debugArrows = [];
// Add a number label to the debug cells
var numberLabel = new Text2('0', {
size: 30,
- fill: "#ffffff",
+ fill: 0xFFFFFF,
weight: 800
});
numberLabel.anchor.set(.5, .5);
self.addChild(numberLabel);
@@ -98,9 +98,9 @@
var enemyGraphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
- self.speed = .02; //Math.random() * .2 + .03;
+ self.speed = Math.random() * .2 + .03;
self.cellX = 0;
self.cellY = 0;
self.currentCellX = 0;
self.currentCellY = 0;
@@ -227,16 +227,19 @@
}
}
for (var a = 0; a < enemies.length; a++) {
var enemy = enemies[a];
+ var target = self.getCell(enemy.cellX, enemy.cellY);
if (enemy.currentTarget) {
if (enemy.currentTarget.pathId != pathId) {
- var target = self.getCell(enemy.cellX, enemy.cellY);
if (!target || target.pathId != pathId) {
console.warn("Enemy blocked");
return true;
}
}
+ } else if (!target || target.pathId != pathId) {
+ console.warn("Enemy blocked");
+ return true;
}
}
console.log("Speed", new Date().getTime() - before);
};
@@ -286,9 +289,9 @@
anchorY: 0.5
});
var notificationText = new Text2(message, {
size: 50,
- fill: "#000000",
+ fill: 0x000000,
weight: 800
});
notificationText.anchor.set(0.5, 0.5);
notificationGraphics.width = notificationText.width + 30;
@@ -303,42 +306,64 @@
self.destroy();
}
};
});
-// 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
- });
-});
/****
* Initialize Game
****/
var game = new LK.Game({
- backgroundColor: 0x000000 //Init game with black background
+ backgroundColor: 0x333333 //Init game with black background
});
/****
* Game Code
****/
var pathId = 1;
var maxScore = 0;
-var tower = game.addChild(new Tower());
-tower.x = 2048 / 2;
-tower.y = 2732 / 2;
var enemies = [];
var defenses = [];
+// Currency infrastructure
+var coins = 80;
+var coinLabel = null;
+var coinIcon = null;
var grid = new Grid(24, 28);
grid.x = 160;
grid.y = 150;
grid.pathFind();
grid.renderDebug();
game.addChild(grid);
+// Show coins in top right corner
+coinLabel = new Text2("Coins: " + coins, {
+ size: 60,
+ fill: 0xFFD700,
+ weight: 800
+});
+coinLabel.anchor.set(1, 0); // right aligned, top
+// Gold coin icon
+coinIcon = LK.getAsset('cell', {
+ anchorX: 1,
+ anchorY: 0
+});
+coinIcon.width = 50;
+coinIcon.height = 50;
+coinIcon.tint = 0xffd700; // gold color
+// Positioning: leave some margin from the top right
+coinLabel.x = -60;
+coinLabel.y = 10;
+coinIcon.x = -10;
+coinIcon.y = 10;
+var coinContainer = new Container();
+coinContainer.addChild(coinLabel);
+coinContainer.addChild(coinIcon);
+LK.gui.topRight.addChild(coinContainer);
var offset = 0;
+// Call this function whenever coins change
+function updateCoins() {
+ if (coinLabel) {
+ coinLabel.setText("Coins: " + coins);
+ }
+}
game.update = function () {
if (++offset % 100 == 0) {
isFirst = false;
var target = grid.spawns[Math.random() * grid.spawns.length >> 0];
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