Code edit (7 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < towers.length; i++) {' Line Number: 504
Code edit (4 edits merged)
Please save this source code
User prompt
Please fix the bug: 'TypeError: Cannot read properties of undefined (reading 'towers')' in or related to this line: 'for (var i = 0; i < self.game.towers.length; i++) {' Line Number: 320
Code edit (1 edits merged)
Please save this source code
Code edit (20 edits merged)
Please save this source code
User prompt
change the flying enemy graphics asset whenever it changes zigzagdirection
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
Code edit (2 edits merged)
Please save this source code
User prompt
Fix Bug: 'TypeError: Cannot read properties of undefined (reading '0')' in this line: 'self.x = self.path[duration - self.delayCounter][0];' Line Number: 58
User prompt
Fix Bug: 'ReferenceError: delayCounter is not defined' in this line: 'self.x = self.path[duration - delayCounter][0];' Line Number: 58
Code edit (4 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught TypeError: Cannot set properties of undefined (setting '0')' in this line: 'return start * (1 - t) + end * t;' Line Number: 9
Code edit (5 edits merged)
Please save this source code
User prompt
Fix Bug: 'Uncaught ReferenceError: number_of_steps is not defined' in this line: 'var stepping_constant = 1 / number_of_steps;' Line Number: 13
User prompt
Fix Bug: 'Uncaught ReferenceError: createQuadraticBezierCurve is not defined' in this line: 'var temp = createQuadraticBezierCurve(0, 0, 50, 50, 25, 25, 10);' Line Number: 24
Code edit (1 edits merged)
Please save this source code
User prompt
GoldCoin should follow a curved path to endpoint, not a straight one
Code edit (4 edits merged)
Please save this source code
User prompt
write a function that creates a random bezier curve between two points
Code edit (1 edits merged)
Please save this source code
Code edit (1 edits merged)
Please save this source code
User prompt
Fix Bug: 'ReferenceError: zigzagDirection is not defined' in this line: 'this.rotation = zigzagDirection * 2;' Line Number: 320
User prompt
Fix Bug: 'ReferenceError: zigzagDirection is not defined' in this line: 'this.rotation = zigzagDirection * 2;' Line Number: 320
/**** * Classes ****/ var BalrogEnemy = Container.expand(function () { var self = Container.call(this); var balrogGraphics = self.attachAsset('balrogSprite', { anchorX: 0.5, anchorY: 0.5 }); balrogGraphics.scale.set(2.5); self.hitpoints = balrogHitpoint; balrogHitpoints *= 2; self.move = function (towers) { var minDist = Infinity; var closestTower = null; for (var i = 0; i < towers.length; i++) { var dx = towers[i].x - self.x; var dy = towers[i].y - self.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist < minDist) { minDist = dist; closestTower = towers[i]; } } if (closestTower && minDist < closestTower.width / 2 + this.width / 2) { closestTower.health -= 4; if (closestTower.health <= 0) { var towerIndex = towers.indexOf(closestTower); if (towerIndex > -1) { towers.splice(towerIndex, 1); } closestTower.destroy(); } } else if (closestTower && minDist < 100) { var avoidanceForce = 8; var ax = this.x - closestTower.x; var ay = this.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; this.x += ax; this.y += ay; } else { this.zigzagCounter = this.zigzagCounter || 0; this.zigzagDirection = this.zigzagDirection || 1; if (this.zigzagCounter++ % self.zigzagTension === 0) { this.zigzagDirection *= -1; } this.x += 4 * this.zigzagDirection; this.y += 2; if (Math.random() < 0.01) { this.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (this.x > 2048) { this.zigzagDirection = -1; } if (this.x < 0) { this.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; this.scale.x += scaleIncrement; this.scale.y += scaleIncrement; this.scale.x = Math.min(this.scale.x, 1); this.scale.y = Math.min(this.scale.y, 1); if (this.zigzagCounter % 10 == 0) { this.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 100); } } }; self.die = function () { playerGold += 500; playerScore += 10000; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 100; i++) { var t = new GoldCoin(this.x, this.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } var index = enemies.indexOf(this); if (index > -1) { enemies.splice(index, 1); } this.destroy(); }; }); var Bullet = Container.expand(function (upgradeLevel) { var self = Container.call(this); self.game = null; self.upgradeLevel = upgradeLevel; self.damage = towerDamage[self.upgradeLevel]; var bulletGraphics = self.attachAsset('bullet', { anchorX: 0.5, anchorY: 0.5 }); bulletGraphics.scale.set(0.2); var bulletGraphics1 = self.attachAsset('bulletSprite1', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics2 = self.attachAsset('bulletSprite2', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics3 = self.attachAsset('bulletSprite3', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics4 = self.attachAsset('bulletSprite4', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics5 = self.attachAsset('bulletSprite5', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics6 = self.attachAsset('bulletSprite6', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics7 = self.attachAsset('bulletSprite7', { anchorX: 0.5, anchorY: 0.5 }); var bulletGraphics8 = self.attachAsset('bulletSprite8', { anchorX: 0.5, anchorY: 0.5 }); bulletGraphics1.scale.set(.2); bulletGraphics2.scale.set(.2); bulletGraphics3.scale.set(.2); bulletGraphics4.scale.set(.2); bulletGraphics5.scale.set(.2); bulletGraphics6.scale.set(.2); bulletGraphics7.scale.set(.2); bulletGraphics8.scale.set(.2); bulletGraphics1.alpha = 0; bulletGraphics2.alpha = 0; bulletGraphics3.alpha = 0; bulletGraphics4.alpha = 0; bulletGraphics5.alpha = 0; bulletGraphics6.alpha = 0; bulletGraphics7.alpha = 0; bulletGraphics8.alpha = 0; var a = [bulletGraphics1, bulletGraphics2, bulletGraphics3, bulletGraphics4, bulletGraphics5, bulletGraphics6, bulletGraphics7, bulletGraphics8]; if (self.upgradeLevel > 0) { bulletGraphics.alpha = 0; for (var i = 0; i < 8; i++) { if (i + 1 == self.upgradeLevel) { a[i].alpha = 1; } else { a[i].setAlpha = 0; } } } self.vx = 0; self.vy = 0; self.move = function () { if (this.target) { var tx = this.target.x - this.x; var ty = this.target.y - this.y; var dist = Math.sqrt(tx * tx + ty * ty); if (dist > 0) { this.x += this.vx * 10; this.y += this.vy * 10; } if (dist < this.width / 2 + this.target.width / 2) { this.hit(); } else if (this.x < 0 || this.x > 2048 || this.y < 0 || this.y > 2732) { this.destroy(); } } }; self.hit = function () { this.target.hitpoints -= this.damage; if (this.target.hitpoints <= 0 && !this.target.dead) { this.target.dead = true; this.target.die(); } this.destroy(); }; }); var CityWall = Container.expand(function () { var self = Container.call(this); var wallGraphics = self.attachAsset('cityWallSprite', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(wallGraphics); }); var DeathKnightEnemy = Container.expand(function () { var self = Container.call(this); var dKnightGraphics = self.attachAsset('deathKnightSprite', { anchorX: 0.5, anchorY: 0.5 }); dKnightGraphics.scale.set(2.5); self.hitpoints = 100; self.move = function (towers) { var minDist = Infinity; var closestTower = null; for (var i = 0; i < towers.length; i++) { var dx = towers[i].x - self.x; var dy = towers[i].y - self.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist < minDist) { minDist = dist; closestTower = towers[i]; } } if (closestTower && minDist < closestTower.width / 2 + this.width / 2) { closestTower.health -= 1; if (closestTower.health <= 0) { var towerIndex = towers.indexOf(closestTower); if (towerIndex > -1) { towers.splice(towerIndex, 1); } closestTower.destroy(); } } else if (closestTower && minDist < 100) { var avoidanceForce = 8; var ax = this.x - closestTower.x; var ay = this.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; this.x += ax; this.y += ay; } else { this.zigzagCounter = this.zigzagCounter || 0; this.zigzagDirection = this.zigzagDirection || 1; if (this.zigzagCounter++ % self.zigzagTension === 0) { this.zigzagDirection *= -1; } this.x += 4 * this.zigzagDirection; this.y += 2; if (Math.random() < 0.01) { this.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (this.x > 2048) { this.zigzagDirection = -1; } if (this.x < 0) { this.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; this.scale.x += scaleIncrement; this.scale.y += scaleIncrement; this.scale.x = Math.min(this.scale.x, 1); this.scale.y = Math.min(this.scale.y, 1); if (this.zigzagCounter % 10 == 0) { this.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 100); } } }; self.die = function () { playerGold += 50; playerScore += 2222; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 40; i++) { var t = new GoldCoin(this.x, this.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } var index = enemies.indexOf(this); if (index > -1) { enemies.splice(index, 1); } this.destroy(); }; }); var Enemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('enemySprite', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics.scale.set(0.8); self.hitpoints = 3; self.zigzagTension = Math.floor(Math.random() * 40) + 10; self.move = function () { //console.log('Enemy move'); var minDist = Infinity; var closestTower = null; for (var i = 0; i < towers.length; i++) { var dx = towers[i].x - self.x; var dy = towers[i].y - self.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist < minDist) { minDist = dist; closestTower = towers[i]; } } if (closestTower && minDist < closestTower.width / 2 + this.width / 2) { closestTower.health -= 0.5; if (closestTower.health <= 0) { var towerIndex = towers.indexOf(closestTower); if (towerIndex > -1) { towers.splice(towerIndex, 1); } closestTower.destroy(); } } else if (closestTower && minDist < 100) { var avoidanceForce = 8; var ax = this.x - closestTower.x; var ay = this.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; this.x += ax; this.y += ay; } else { this.zigzagCounter = this.zigzagCounter || 0; this.zigzagDirection = this.zigzagDirection || 1; if (this.zigzagCounter++ % self.zigzagTension === 0) { this.zigzagDirection *= -1; } this.x += 4 * this.zigzagDirection; this.y += 2; if (Math.random() < 0.01) { this.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (this.x > 2048) { this.zigzagDirection = -1; } if (this.x < 0) { this.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; this.scale.x += scaleIncrement; this.scale.y += scaleIncrement; this.scale.x = Math.min(this.scale.x, 1); this.scale.y = Math.min(this.scale.y, 1); this.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 50); } }; self.die = function () { playerGold += 5; playerScore += 100; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 5; i++) { var t = new GoldCoin(this.x, this.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } var index = enemies.indexOf(this); if (index > -1) { enemies.splice(index, 1); } this.destroy(); }; }); var FlyingEnemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics1 = self.attachAsset('flyingEnemySprite', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics1.scale.set(1); var enemyGraphics2 = self.attachAsset('flyingEnemySprite2', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics2.scale.set(1); self.hitpoints = 6; self.zigzagTension = 400; self.move = function (towers) { this.zigzagCounter = this.zigzagCounter || 0; this.zigzagDirection = this.zigzagDirection || 1; if (this.zigzagCounter++ % this.zigzagTension === 0) { this.zigzagDirection *= -1; } this.x += 4 * this.zigzagDirection; this.y += 2; if (Math.random() < 0.005) { this.zigzagTension = Math.floor(Math.random() * 380) + 20; } if (this.x > 2048) { this.zigzagDirection = -1; } if (this.x < 0) { this.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; this.scale.x += scaleIncrement; this.scale.y += scaleIncrement; this.scale.x = Math.min(this.scale.x, 1); this.scale.y = Math.min(this.scale.y, 1); if (this.zigzagCounter++ % self.zigzagTension === 0) { this.zigzagDirection *= -1; } if (this.zigzagCounter % 5 == 0) { this.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 50); } if (this.zigzagDirection == 1) { this.removeChild(this.graphics); this.graphics = enemyGraphics1; this.addChild(this.graphics); } else { this.removeChild(this.graphics); this.graphics = enemyGraphics2; this.addChild(this.graphics); } }; self.die = function () { playerGold += 10; playerScore += 150; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 8; i++) { var t = new GoldCoin(this.x, this.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } var index = enemies.indexOf(this); if (index > -1) { enemies.splice(index, 1); } this.destroy(); }; }); var GoldCoin = Container.expand(function (startX, startY, endX, endY, delay) { var self = Container.call(this); GoldCoin.delayIncrement = 0; self.game = this; var coinGraphics = self.attachAsset('goldCoinSprite', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(coinGraphics); self.x = startX; self.y = startY; var distance = Math.sqrt(Math.pow(endX - startX, 2) + Math.pow(endY - startY, 2)); self.delayCounter = delay * 2; var duration = 60 + delay; var vx = (endX - startX) / duration; var vy = (endY - startY) / duration; self.alpha = 0; self.path = createQuadraticBezierArcPoints(startX, startY, endX, startY - 100, endX, endY, 60); self.move = function () { if (self.delayCounter > 0) { self.delayCounter--; } else { self.alpha = 1; if (self.path[duration]) { self.x = self.path[duration - self.delayCounter][0]; self.y = self.path[duration - self.delayCounter][1]; } } if (--duration <= 0) { self.destroy(); } }; }); var OrcEnemy = Container.expand(function () { var self = Container.call(this); var enemyGraphics = self.attachAsset('orcEnemySprite', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics.scale.set(0.8); self.hitpoints = 6; self.zigzagTension = Math.floor(Math.random() * 20) + 10; self.move = function (towers) { var minDist = Infinity; var closestTower = null; for (var i = 0; i < towers.length; i++) { var dx = towers[i].x - self.x; var dy = towers[i].y - self.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist < minDist) { minDist = dist; closestTower = towers[i]; } } if (closestTower && minDist < closestTower.width / 2 + this.width / 2) { closestTower.health -= 0.8; if (closestTower.health <= 0) { var towerIndex = towers.indexOf(closestTower); if (towerIndex > -1) { towers.splice(towerIndex, 1); } closestTower.destroy(); } } else if (closestTower && minDist < 100) { var avoidanceForce = 8; var ax = this.x - closestTower.x; var ay = this.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; this.x += ax; this.y += ay; } else { this.zigzagCounter = this.zigzagCounter || 0; this.zigzagDirection = this.zigzagDirection || 1; if (this.zigzagCounter++ % self.zigzagTension === 0) { this.zigzagDirection *= -1; } this.x += 4 * this.zigzagDirection; this.y += 2; if (Math.random() < 0.01) { this.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (this.x > 2048) { this.zigzagDirection = -1; } if (this.x < 0) { this.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; this.scale.x += scaleIncrement; this.scale.y += scaleIncrement; this.scale.x = Math.min(this.scale.x, 1); this.scale.y = Math.min(this.scale.y, 1); if (this.zigzagCounter % 5 == 0) { this.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 100); } } }; self.die = function () { playerGold += 10; playerScore += 150; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 8; i++) { var t = new GoldCoin(this.x, this.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } var index = enemies.indexOf(this); if (index > -1) { enemies.splice(index, 1); } this.destroy(); }; }); var Tower = Container.expand(function () { var self = Container.call(this); self.upgradeLevel = upgradeLevel; var towerGraphics = self.attachAsset('towerSprite', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics1 = self.attachAsset('towerSprite1', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics2 = self.attachAsset('towerSprite2', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics3 = self.attachAsset('towerSprite3', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics4 = self.attachAsset('towerSprite4', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics5 = self.attachAsset('towerSprite5', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics6 = self.attachAsset('towerSprite6', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics7 = self.attachAsset('towerSprite7', { anchorX: 0.5, anchorY: 0.5 }); var towerGraphics8 = self.attachAsset('towerSprite8', { anchorX: 0.5, anchorY: 0.5 }); towerGraphics.scale.set(2); towerGraphics1.scale.set(2); towerGraphics2.scale.set(2); towerGraphics3.scale.set(2); towerGraphics4.scale.set(2); towerGraphics5.scale.set(2); towerGraphics6.scale.set(2); towerGraphics7.scale.set(2); towerGraphics8.scale.set(2); towerGraphics1.alpha = 0; towerGraphics2.alpha = 0; towerGraphics3.alpha = 0; towerGraphics4.alpha = 0; towerGraphics5.alpha = 0; towerGraphics6.alpha = 0; towerGraphics7.alpha = 0; towerGraphics8.alpha = 0; var a = [towerGraphics1, towerGraphics2, towerGraphics3, towerGraphics4, towerGraphics5, towerGraphics6, towerGraphics7, towerGraphics8]; if (upgradeLevel > 0) { towerGraphics.alpha = 0; for (var i = 0; i < 8; i++) { if (i + 1 == upgradeLevel) { a[i].alpha = 1; } else { a[i].alpha = 0; } } } self.shoot = function (enemies) { if (self.shootCounter++ % towerShotDelay[self.upgradeLevel] === 0) { for (var i = 0; i < enemies.length; i++) { var enemy = enemies[i]; var dx = enemy.x - self.x; var dy = enemy.y - self.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance <= 300) { console.log('shot'); var bullet = new Bullet(self.upgradeLevel); bullet.x = self.x; bullet.y = self.y; bullet.target = enemy; var tx = bullet.target.x - bullet.x; var ty = bullet.target.y - bullet.y; var rad = Math.atan2(ty, tx); bullet.rotation = rad + Math.PI / 2; bullet.vx = Math.cos(rad); bullet.vy = Math.sin(rad); addBullet(bullet); break; } } } }; self.upgrade = function () {}; self.shootCounter = 0; self.health = 200; }); var TreasureChest = Container.expand(function () { var self = Container.call(this); var chestGraphics = self.attachAsset('treasureChestSprite', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(chestGraphics); }); var TrollEnemy = Container.expand(function () { var self = Container.call(this); var trollGraphics = self.attachAsset('trollSprite', { anchorX: 0.5, anchorY: 0.5 }); trollGraphics.scale.set(2.5); self.hitpoints = 30; self.move = function (towers) { var minDist = Infinity; var closestTower = null; for (var i = 0; i < towers.length; i++) { var dx = towers[i].x - self.x; var dy = towers[i].y - self.y; var dist = Math.sqrt(dx * dx + dy * dy); if (dist < minDist) { minDist = dist; closestTower = towers[i]; } } if (closestTower && minDist < closestTower.width / 2 + this.width / 2) { closestTower.health -= 0.5; if (closestTower.health <= 0) { var towerIndex = towers.indexOf(closestTower); if (towerIndex > -1) { towers.splice(towerIndex, 1); } closestTower.destroy(); } } else if (closestTower && minDist < 100) { var avoidanceForce = 8; var ax = this.x - closestTower.x; var ay = this.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; this.x += ax; this.y += ay; } else { this.zigzagCounter = this.zigzagCounter || 0; this.zigzagDirection = this.zigzagDirection || 1; if (this.zigzagCounter++ % self.zigzagTension === 0) { this.zigzagDirection *= -1; } this.x += 4 * this.zigzagDirection; this.y += 2; if (Math.random() < 0.01) { this.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (this.x > 2048) { this.zigzagDirection = -1; } if (this.x < 0) { this.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; this.scale.x += scaleIncrement; this.scale.y += scaleIncrement; this.scale.x = Math.min(this.scale.x, 1); this.scale.y = Math.min(this.scale.y, 1); if (this.zigzagCounter % 5 == 0) { this.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 100); } } }; self.die = function () { playerGold += 20; playerScore += 300; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 20; i++) { var t = new GoldCoin(this.x, this.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } var index = enemies.indexOf(this); if (index > -1) { enemies.splice(index, 1); } this.destroy(); }; }); var UpgradeButton = Container.expand(function () { var self = Container.call(this); var upgradeButtonGraphics = self.attachAsset('buttonSprite', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(upgradeButtonGraphics); }); /**** * Initialize Game ****/ var game = new LK.Game({ backgroundColor: 0x000000 }); /**** * Game Code ****/ var playerGold = 30; var playerScore = 0; var enemySpawnDelay = 120; var upgradeLevel = 0; var upgradeCost = 100; var towerDamage = [1, 1.5, 2, 2.5, 3, 3.5, 4, 5, 8]; var towerShotDelay = [50, 45, 45, 40, 35, 30, 25, 20, 15]; var balrogHitpoints = 500; var lerp = function lerp(start, end, t) { return start * (1 - t) + end * t; }; var createQuadraticBezierArcPoints = function createQuadraticBezierArcPoints(p0x, p0y, p1x, p1y, p2x, p2y, numberOfSteps) { var points_along_axis = new Array(numberOfSteps).fill(null).map(function () { return [0, 0]; }); var stepping_constant = 1 / numberOfSteps; for (var i = 0; i < numberOfSteps; i++) { var i_p0_p1x = lerp(p0x, p1x, i * stepping_constant); var i_p0_p1y = lerp(p0y, p1y, i * stepping_constant); var i_p1_p2x = lerp(p1x, p2x, i * stepping_constant); var i_p1_p2y = lerp(p1y, p2y, i * stepping_constant); if (false) { points_along_axis.push(lerp(i_p0_p1x, i_p1_p2x, i * stepping_constant), lerp(i_p0_p1y, i_p1_p2y, i * stepping_constant)); } points_along_axis[i][0] = lerp(i_p0_p1x, i_p1_p2x, i * stepping_constant); points_along_axis[i][1] = lerp(i_p0_p1y, i_p1_p2y, i * stepping_constant); } if (false) { console.log('path: ' + points_along_axis); console.log('path[2]: ' + points_along_axis[2]); } return points_along_axis.reverse(); }; var temp = createQuadraticBezierArcPoints(0, 0, 50, 50, 0, 50, 10); var updateGoldLabel = function updateGoldLabel() { goldLabel.setText('Gold: ' + playerGold); }; var updateScoreLabel = function updateScoreLabel() { scoreLabel.setText('Score: ' + playerScore); }; var updateCostLabel = function updateCostLabel() { costLabel.setText('Cost: ' + upgradeCost); }; var playerGold = 0; var addBullet = function addBullet(bullet) { bullets.push(bullet); game.addChild(bullet); }; var sufficientDistanceToOtherTowers = function sufficientDistanceToOtherTowers(position) { for (var i = 0; i < towers.length; i++) { var tower = towers[i]; var dx = tower.x - position.x; var dy = tower.y - position.y; var distance = Math.sqrt(dx * dx + dy * dy); if (distance < 120) { return false; } } return true; }; var background = game.attachAsset('background', {}); background.width = 2048; background.height = 2732; background.y = 0; game.addChildAt(background, 0); var towers = []; var enemies = []; var bullets = []; var goldCoins = []; var enemySpawnCounter = 0; var totalEnemiesSpawned = 0; var selectedTowerType = null; var initialEnemy = new Enemy(); initialEnemy.__game = game; initialEnemy.x = 300 + Math.random() * (1848 - 300); initialEnemy.y = 200; initialEnemy.scale.set(0.1); enemies.push(initialEnemy); game.addChild(initialEnemy); LK.on('tick', function () { //console.log('tick:' + LK.ticks); enemySpawnCounter = enemySpawnCounter || 0; totalEnemiesSpawned = totalEnemiesSpawned || 0; if (++enemySpawnCounter >= enemySpawnDelay) { var newEnemy; if (totalEnemiesSpawned > 200 && totalEnemiesSpawned % 199 == 0) { newEnemy = new BalrogEnemy(); } else if (totalEnemiesSpawned > 3 && totalEnemiesSpawned % 50 == 0) { newEnemy = new DeathKnightEnemy(); } else if (totalEnemiesSpawned > 3 && totalEnemiesSpawned % 10 == 0) { newEnemy = new TrollEnemy(); } else { if (Math.random() < 0.2 + upgradeLevel * 0.06) { newEnemy = new OrcEnemy(); } else { if (Math.random() < 0.5) { newEnemy = new FlyingEnemy(); } else { newEnemy = new Enemy(); } } } if (newEnemy) { newEnemy.__game = game; newEnemy.x = 300 + Math.random() * (1848 - 300); newEnemy.y = 200; newEnemy.scale.set(0.1); enemies.push(newEnemy); game.addChild(newEnemy); } totalEnemiesSpawned++; if (totalEnemiesSpawned > 0 && totalEnemiesSpawned % 20 == 0) { enemySpawnDelay -= 4; if (enemySpawnDelay < 20) { enemySpawnDelay = 20; } } enemySpawnCounter = 0; } for (var i = 0; i < enemies.length; i++) { enemies[i].move(); if (enemies[i].y >= 2332 - enemies[i].height / 2) { if (false) { scoreLabel.size = 130; scoreLabel.fill = '#ffffff'; scoreLabel.x = 1024 - scoreLabel.width / 2; scoreLabel.y = 60; } var finalScoreLabel = new Text2('*** Your Final Score: ' + playerScore + ' ***', { size: 120, fill: '#ffffff', align: 'left', dropShadow: true }); finalScoreLabel.x = 1024 - finalScoreLabel.width / 2; finalScoreLabel.y = 260; game.addChild(finalScoreLabel); LK.showGameOver(); } } for (var j = 0; j < towers.length; j++) { towers[j].shoot(enemies); } for (var k = bullets.length - 1; k >= 0; k--) { bullets[k].move(); if (!bullets[k].parent) { bullets.splice(k, 1); } } for (var l = goldCoins.length - 1; l >= 0; l--) { goldCoins[l].move(); if (!goldCoins[l].parent) { goldCoins.splice(l, 1); } } }); game.on('down', function (obj) { var position = obj.event.getLocalPosition(game); if (position.y >= 600 && position.y <= 2112) { if (playerGold >= 10) { if (sufficientDistanceToOtherTowers(position)) { playerGold -= 10; var newTower = game.addChild(new Tower()); newTower.x = position.x; newTower.y = position.y; towers.push(newTower); towers.sort(function (a, b) { return a.y - b.y; }); towers.forEach(function (tower, index) { game.addChildAt(tower, 6 + index); console.log('y and zIndex:' + tower.y + ', ' + tower.parent.getChildIndex(tower)); }); updateGoldLabel(); } } } }); var cityWall = game.addChild(new CityWall()); cityWall.y = 2632; cityWall.x = 332; cityWall.scale.y = 0.5; cityWall.scale.x = 0.5; var cityWall2 = game.addChild(new CityWall()); cityWall2.y = 2632; cityWall2.x = 1012; cityWall2.scale.y = 0.5; cityWall2.scale.x = 0.5; var cityWall3 = game.addChild(new CityWall()); cityWall3.y = 2632; cityWall3.x = 1691; cityWall3.scale.y = 0.5; cityWall3.scale.x = 0.5; var bottomBlackBox = game.attachAsset('blackBox', { anchorY: 1 }); bottomBlackBox.width = 2048; bottomBlackBox.height = 400; bottomBlackBox.y = 2732; game.addChild(bottomBlackBox); var goldLabel = new Text2('Gold: 30', { size: 100, fill: '#ffd700', align: 'left', dropShadow: true }); goldLabel.x = 120; goldLabel.y = 2632 - bottomBlackBox.height + (bottomBlackBox.height - goldLabel.height) / 2; game.addChild(goldLabel); var scoreLabel = new Text2('Score: 0', { size: 100, fill: '#ccbbff', align: 'left', dropShadow: true }); scoreLabel.x = 120; scoreLabel.y = 2772 - bottomBlackBox.height + (bottomBlackBox.height - scoreLabel.height) / 2; game.addChild(scoreLabel); var instructionLabel = new Text2("Tap to build towers (cost 10 gold)\nDon't let enemies reach the City Wall!", { size: 58, fill: '#eeeeee', align: 'left', dropShadow: true }); instructionLabel.x = 920; instructionLabel.y = 2632 - bottomBlackBox.height + (bottomBlackBox.height - instructionLabel.height) / 2; game.addChild(instructionLabel); var treasureChest = game.addChild(new TreasureChest()); treasureChest.scale.x = 1.3; treasureChest.scale.y = 1.3; treasureChest.x = 720; treasureChest.y = goldLabel.y + 65; var upgradeButton = game.addChild(new UpgradeButton()); upgradeButton.x = 1500; upgradeButton.y = 2590; var upgradeLabel = new Text2("Upgrade Towers:", { size: 58, fill: '#dddddd', align: 'left', dropShadow: true }); upgradeLabel.x = 920; upgradeLabel.y = 2552; game.addChild(upgradeLabel); var costLabel = new Text2("Cost: 100", { size: 58, fill: '#dddd00', align: 'left', dropShadow: true }); costLabel.x = 1600; costLabel.y = 2552; game.addChild(costLabel); var upgradeTowers = function upgradeTowers() { if (playerGold >= upgradeCost) { playerGold -= upgradeCost; upgradeLevel += 1; upgradeCost = Math.floor(upgradeCost * 1.5); updateGoldLabel(); updateCostLabel(); if (upgradeLevel == 8) { upgradeLabel.setText('All upgraded - Go for the High Score!'); if (upgradeButton) { upgradeButton.destroy(); upgradeButton = null; } if (costLabel) { costLabel.destroy(); costLabel = null; } } } }; upgradeButton.on('down', upgradeTowers);
===================================================================
--- original.js
+++ change.js
@@ -806,9 +806,9 @@
background.width = 2048;
background.height = 2732;
background.y = 0;
game.addChildAt(background, 0);
-var towers = towers || [];
+var towers = [];
var enemies = [];
var bullets = [];
var goldCoins = [];
var enemySpawnCounter = 0;
a medieval style stone tower. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
an evil goblin. pixelart. top down view. In-Game asset. 2d. Blank background. High contrast. No shadows.
a silver sling bullet. pixelart. top down view. In-Game asset. 2d. Blank background. High contrast. No shadows.
brown wooden board. game gui style. 2048x400 pixels. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A medieval stone wall seen from the front. pixelart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A large scary troll. front top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A treasure chest. Medieval style. cartoony. Open and full of gold coins. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A metalic square button with the symbol of a tower from chess. Pixelart. Gamegui style. Medieval. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a medieval style stone tower. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a medieval style stone tower. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a medieval style stone tower. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a medieval style stone tower. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a medieval style stone tower. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a medieval style stone tower. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a colossal menacing black knight in heavy armor. pixelart game sprite. front view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a crossbow bolt made of bronze. straight. pixelart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a straight crossbow bolt made of gold. top down view. pixelart. bolt only, crossbow not included. vertical display, from bottom to top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a straight crossbow bolt made of jade. top down view. pixelart. bolt only, crossbow not included. vertical display, from bottom to top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a straight crossbow bolt made of cobalt. top down view. pixelart. bolt only, crossbow not included. vertical display, from bottom to top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a straight magic missile. top down view. pixelart. bolt only, crossbow not included. vertical display, from bottom to top. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
a yellow fire arrow, pointing up. top down view. pixelart. vertical. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A red hunting arrow, pointing straight up. Pixelart. Vertical view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A purple posion dart. Feathers pointing down in the picture. Pixelart.vertical. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
an orc warrior with a large head and red eyes. pixelart. top down view. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A fierce balrog. Pixelart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A smooth round gold coin. Pixelart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
An evil snotling flying on a dragonfly. front view. enemy character in a game. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
A flying red dragon breathing fire. side view. enemy character in a game. pixelart.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.