Code edit (16 edits merged)
Please save this source code
Code edit (7 edits merged)
Please save this source code
User prompt
Please fix the bug: 'ReferenceError: dragonFire is not defined' in or related to this line: 'dragonFire.scale.x = 1;' Line Number: 438
Code edit (1 edits merged)
Please save this source code
/**** * Classes ****/ // Enemies below here. var BalrogEnemy = Container.expand(function () { var self = Container.call(this); console.log('Balrog Spawned'); var balrogGraphics = self.attachAsset('balrogSprite', { anchorX: 0.5, anchorY: 0.5 }); balrogGraphics.scale.set(2.5); var healthBar = new HealthBar(); healthBar.y = -balrogGraphics.height + 100; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(100); self.hitpoints = balrogHitpoints; self.maxHitpoints = balrogHitpoints; self.isDead = false; balrogHitpoints *= 2; self._move_migrated = function () { 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 + self.width / 2) { //closestTower.health -= 4; closestTower.takeDamage(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 = self.x - closestTower.x; var ay = self.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; self.x += ax; self.y += ay; } else { self.zigzagCounter = self.zigzagCounter || 0; self.zigzagDirection = self.zigzagDirection || 1; if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } self.x += 4 * self.zigzagDirection; self.y += 2; if (Math.random() < 0.01) { self.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (self.x > 2048) { self.zigzagDirection = -1; } if (self.x < 0) { self.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; self.scale.x += scaleIncrement; self.scale.y += scaleIncrement; self.scale.x = Math.min(self.scale.x, 1); self.scale.y = Math.min(self.scale.y, 1); if (self.zigzagCounter % 10 == 0) { self.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(self.x, self.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } self.isDead = true; }; }); var Bullet = Container.expand(function (upgradeLevel) { var self = Container.call(this); //self.game = null; self.upgradeLevel = upgradeLevel; self.damage = towerDamage[self.upgradeLevel]; self.isDead = false; 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_migrated = function () { if (self.target) { var tx = self.target.x - this.x; var ty = self.target.y - this.y; var dist = Math.sqrt(tx * tx + ty * ty); if (dist > 0) { self.x += self.vx * 10; self.y += self.vy * 10; } if (dist < self.width / 2 + self.target.width / 2) { self.hit(); } else if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) { self.isDead = true; } } }; self.hit = function () { self.target.hitpoints -= self.damage; self.target.healthBar.updateHealth(self.target.hitpoints / self.target.maxHitpoints * 100); if (self.target.hitpoints <= 0) { self.target.die(); } self.isDead = true; }; }); 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); console.log('DeathKnight Spawned'); var dKnightGraphics = self.attachAsset('deathKnightSprite', { anchorX: 0.5, anchorY: 0.5 }); dKnightGraphics.scale.set(2.5); var healthBar = new HealthBar(); healthBar.y = -dKnightGraphics.height - 50; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(100); self.hitpoints = 100; self.maxHitpoints = 100; self.isDead = false; self._move_migrated = function () { 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 + self.width / 2) { //closestTower.health -= 1; closestTower.takeDamage(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 = self.x - closestTower.x; var ay = self.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; self.x += ax; self.y += ay; } else { self.zigzagCounter = self.zigzagCounter || 0; self.zigzagDirection = self.zigzagDirection || 1; if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } self.x += 4 * self.zigzagDirection; self.y += 2; if (Math.random() < 0.01) { self.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (self.x > 2048) { self.zigzagDirection = -1; } if (self.x < 0) { self.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; self.scale.x += scaleIncrement; self.scale.y += scaleIncrement; self.scale.x = Math.min(self.scale.x, 1); self.scale.y = Math.min(self.scale.y, 1); if (self.zigzagCounter % 10 == 0) { self.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(self.x, self.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } self.isDead = true; }; }); var DragonEnemy = Container.expand(function () { var self = Container.call(this); console.log('Dragon Enemy Spawned'); var enemyGraphics1 = self.attachAsset('dragonSprite1', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics1.scale.set(1); var enemyGraphics2 = self.attachAsset('dragonSprite2', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics2.scale.set(1); enemyGraphics2.alpha = 0; var dragonFire = new DragonFire(); dragonFire.x = -100; dragonFire.y = 80; self.addChild(dragonFire); var healthBar = new HealthBar(); healthBar.y = -enemyGraphics1.height + 150; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(100); self.hitpoints = 6000; self.maxHitpoints = 6000; self.isDead = false; self.zigzagTension = 400; self.zigzagDirection = 1; self.flying = true; self._move_migrated = function () { self.zigzagCounter = self.zigzagCounter || 0; //self.zigzagDirection = self.zigzagDirection || 1; if (self.zigzagCounter++ % self.zigzagTension === 0) { if (self.y < 1800) { self.zigzagDirection *= -1; } } self.x += 4 * self.zigzagDirection; if (self.y < 1800) { self.y += 2; } /* if (Math.random() < 0.005) { self.zigzagTension = Math.floor(Math.random() * 380) + 20; }*/ if (self.x > 2048) { if (self.y < 1800) { self.zigzagDirection = -1; } else { if (self.x > 2448) { self.isDead = true; } } } if (self.x < 0) { if (self.y < 1800) { self.zigzagDirection = 1; } else { if (self.x < -400) { self.isDead = true; } } } var scaleIncrement = (1 - 0.1) / 200; self.scale.x += scaleIncrement; self.scale.y += scaleIncrement; self.scale.x = Math.min(self.scale.x, 1); self.scale.y = Math.min(self.scale.y, 1); if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } if (self.zigzagCounter % 5 == 0) { self.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 50); } if (self.zigzagDirection == 1) { //self.removeChild(self.graphics); //self.graphics = enemyGraphics1; //self.addChild(self.graphics); enemyGraphics2.alpha = 0; enemyGraphics1.alpha = 1; dragonFire.scale.x = -1; dragonFire.x = 100; } else { //self.removeChild(self.graphics); //self.graphics = enemyGraphics2; //self.addChild(self.graphics); enemyGraphics1.alpha = 0; enemyGraphics2.alpha = 1; dragonFire.scale.x = 1; dragonFire.x = -100; } // Check for damage to towers var minDist = Infinity; var closestTower = null; for (var i = 0; i < towers.length; i++) { var dfx = self.x + self.zigzagDirection == 1 ? 300 : -300; var dfy = self.y + 100; if (LK.ticks % 10 == 0) { var deb = self.attachAsset('debug', { anchorX: 0.5, anchorY: 0.5 }); self.addChild(deb); deb.x = self.zigzagDirection == 1 ? 300 : -300; deb.y = 300; } //var dx = towers[i].x - self.x + dragonFire.x; //var dy = towers[i].y - self.y + dragonFire.y; var dx = towers[i].x - dfx; var dy = towers[i].y - dfy; var dist = Math.sqrt(dx * dx + dy * dy); if (dist < minDist) { minDist = dist; closestTower = towers[i]; } } //if (closestTower && minDist < closestTower.width / 2 + self.width / 2) { if (closestTower && minDist < 600) { //closestTower.health -= 0.5; closestTower.takeDamage(10); if (closestTower.health <= 0) { var towerIndex = towers.indexOf(closestTower); if (towerIndex > -1) { towers.splice(towerIndex, 1); } closestTower.destroy(); } } }; self.die = function () { playerGold += 10; playerScore += 150; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 8; i++) { var t = new GoldCoin(self.x, self.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } self.isDead = true; }; }); var DragonFire = Container.expand(function () { var self = Container.call(this); var graphics = self.attachAsset('dragonFire', { anchorX: 1, anchorY: 0 }); self.update = function () { this.scale.x += 0.2 - Math.random() * 0.4; }; }); // Goblin, var Enemy = Container.expand(function () { var self = Container.call(this); console.log('Standard Enemy Spawned'); var enemyGraphics = self.attachAsset('enemySprite', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics.scale.set(0.8); var healthBar = new HealthBar(); healthBar.y = -enemyGraphics.height + 40; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(100); self.isDead = false; self.hitpoints = 3; self.maxHitpoints = 3; self.zigzagTension = Math.floor(Math.random() * 40) + 10; self._move_migrated = 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 + self.width / 2) { //closestTower.health -= 0.5; closestTower.takeDamage(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 = self.x - closestTower.x; var ay = self.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; self.x += ax; self.y += ay; } else { self.zigzagCounter = self.zigzagCounter || 0; self.zigzagDirection = self.zigzagDirection || 1; if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } self.x += 4 * self.zigzagDirection; self.y += 2; if (Math.random() < 0.01) { self.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (self.x > 2048) { self.zigzagDirection = -1; } if (self.x < 0) { self.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; self.scale.x += scaleIncrement; self.scale.y += scaleIncrement; self.scale.x = Math.min(self.scale.x, 1); self.scale.y = Math.min(self.scale.y, 1); self.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(self.x, self.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } self.isDead = true; }; }); var FlyingEnemy = Container.expand(function () { var self = Container.call(this); console.log('Flying Enemy Spawned'); 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); var healthBar = new HealthBar(); healthBar.y = -enemyGraphics1.height + 20; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(100); self.hitpoints = 6; self.maxHitpoints = 6; self.isDead = false; self.zigzagTension = 400; self.flying = true; self._move_migrated = function () { self.zigzagCounter = self.zigzagCounter || 0; self.zigzagDirection = self.zigzagDirection || 1; if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } self.x += 4 * self.zigzagDirection; self.y += 2; if (Math.random() < 0.005) { self.zigzagTension = Math.floor(Math.random() * 380) + 20; } if (self.x > 2048) { self.zigzagDirection = -1; } if (self.x < 0) { self.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; self.scale.x += scaleIncrement; self.scale.y += scaleIncrement; self.scale.x = Math.min(self.scale.x, 1); self.scale.y = Math.min(self.scale.y, 1); if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } if (self.zigzagCounter % 5 == 0) { self.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 50); } if (self.zigzagDirection == 1) { self.removeChild(self.graphics); self.graphics = enemyGraphics1; self.addChild(self.graphics); } else { self.removeChild(self.graphics); self.graphics = enemyGraphics2; self.addChild(self.graphics); } }; self.die = function () { playerGold += 10; playerScore += 150; updateScoreLabel(); updateGoldLabel(); for (var i = 0; i < 8; i++) { var t = new GoldCoin(self.x, self.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } self.isDead = true; }; }); 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; self.isDead = false; 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_migrated = 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(); self.isDead = true; } }; }); var HealthBar = Container.expand(function () { var self = Container.call(this); var healthBarGraphics = self.attachAsset('healthBar', { anchorX: 0.5, anchorY: 1 }); /* self.setTint = function (tint) { healthBarGraphics.tint = tint; };*/ //self.setTint(0x00FF00); self.updateHealth = function (health) { self.scale.set(health / 100, 1); }; }); var HealthBarTower = Container.expand(function () { var self = Container.call(this); var healthBarGraphics = self.attachAsset('healthBarTower', { anchorX: 0.5, anchorY: 1 }); /* self.setTint = function (tint) { healthBarGraphics.tint = tint; };*/ //self.setTint(0x00FF00); self.updateHealth = function (health) { self.scale.set(health, 1); }; }); var OrcEnemy = Container.expand(function () { var self = Container.call(this); console.log('Orc Spawned'); var enemyGraphics = self.attachAsset('orcEnemySprite', { anchorX: 0.5, anchorY: 0.5 }); enemyGraphics.scale.set(0.8); var healthBar = new HealthBar(); healthBar.y = -enemyGraphics.height + 100; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(100); self.hitpoints = 6; self.maxHitpoints = 6; self.isDead = false; self.zigzagTension = Math.floor(Math.random() * 20) + 10; self._move_migrated = function () { 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 + self.width / 2) { //closestTower.health -= 0.8; closestTower.takeDamage(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 = self.x - closestTower.x; var ay = self.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; self.x += ax; self.y += ay; } else { self.zigzagCounter = self.zigzagCounter || 0; self.zigzagDirection = self.zigzagDirection || 1; if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } self.x += 4 * self.zigzagDirection; self.y += 2; if (Math.random() < 0.01) { self.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (self.x > 2048) { self.zigzagDirection = -1; } if (self.x < 0) { self.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; self.scale.x += scaleIncrement; self.scale.y += scaleIncrement; self.scale.x = Math.min(self.scale.x, 1); self.scale.y = Math.min(self.scale.y, 1); if (self.zigzagCounter % 5 == 0) { self.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(self.x, self.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } self.isDead = true; }; }); 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; } } } var healthBar = new HealthBarTower(); healthBar.y = towerGraphics.height; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(1); 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.takeDamage = function (dam) { self.health -= dam; self.healthBar.updateHealth(self.health / self.maxHealth); }; self.shootCounter = 0; self.health = 200; self.maxHealth = 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); console.log('Troll Spawned'); var trollGraphics = self.attachAsset('trollSprite', { anchorX: 0.5, anchorY: 0.5 }); trollGraphics.scale.set(2.5); var healthBar = new HealthBar(); healthBar.y = -trollGraphics.height - 40; self.addChild(healthBar); self.healthBar = healthBar; self.healthBar.updateHealth(100); self.hitpoints = 30; self.maxHitpoints = 30; self.isDead = false; self._move_migrated = function () { 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 + self.width / 2) { //closestTower.health -= 0.5; closestTower.takeDamage(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 = self.x - closestTower.x; var ay = self.y - closestTower.y; var len = Math.sqrt(ax * ax + ay * ay); ax = ax / len * avoidanceForce; ay = ay / len * avoidanceForce; self.x += ax; self.y += ay; } else { self.zigzagCounter = self.zigzagCounter || 0; self.zigzagDirection = self.zigzagDirection || 1; if (self.zigzagCounter++ % self.zigzagTension === 0) { self.zigzagDirection *= -1; } self.x += 4 * self.zigzagDirection; self.y += 2; if (Math.random() < 0.01) { self.zigzagTension = Math.floor(Math.random() * 40) + 10; } if (self.x > 2048) { self.zigzagDirection = -1; } if (self.x < 0) { self.zigzagDirection = 1; } var scaleIncrement = (1 - 0.1) / 200; self.scale.x += scaleIncrement; self.scale.y += scaleIncrement; self.scale.x = Math.min(self.scale.x, 1); self.scale.y = Math.min(self.scale.y, 1); if (self.zigzagCounter % 5 == 0) { self.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(self.x, self.y, 720, 2450, i * 2); game.addChild(t); goldCoins.push(t); } self.isDead = true; }; }); 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 = 400; // TODO: Reset to 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 towers = []; var enemies = []; var bullets = []; var goldCoins = []; var enemySpawnCounter = 0; var totalEnemiesSpawned = 0; var selectedTowerType = null; 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; } } } } ; 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 addBullet = function addBullet(bullet) { bullets.push(bullet); game.addChild(bullet); }; function sufficientDistanceToOtherTowers(position) { console.log('sufficientDistanceToOtherTowers'); 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 initialEnemy = new Enemy(); initialEnemy.x = 300 + Math.random() * (1848 - 300); initialEnemy.y = 200; initialEnemy.scale.set(0.1); enemies.push(initialEnemy); game.addChild(initialEnemy); 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; upgradeButton.on('down', function (x, y, obj) { obj.event = obj; upgradeTowers(obj); }); 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); // Main game loop and click handler below here. 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(); newEnemy = new DragonEnemy(); } else { newEnemy = new Enemy(); } } } if (newEnemy) { 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; } //console.log('enemies length:' + enemies.length); for (var i = enemies.length - 1; i >= 0; i--) { if (enemies[i].isDead) { enemies[i].destroy(); enemies.splice(i, 1); } else { enemies[i]._move_migrated(); if (enemies[i].y >= 2332 - enemies[i].height / 2) { /* 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.setScore(playerScore); LK.showGameOver(); } } } //console.log('towers length:' + towers.length); for (var j = 0; j < towers.length; j++) { towers[j].shoot(enemies); } //console.log('bullets length:' + bullets.length); for (var k = bullets.length - 1; k >= 0; k--) { if (bullets[k].isDead) { bullets[k].destroy(); bullets.splice(k, 1); } else { bullets[k]._move_migrated(); } } //console.log('goldCoins length:' + goldCoins.length); for (var l = goldCoins.length - 1; l >= 0; l--) { if (goldCoins[l].isDead) { goldCoins[l].destroy(); goldCoins.splice(l, 1); } else { goldCoins[l]._move_migrated(); } } if (LK.ticks % 10 == 0) { sortDepths(); } }); function sortDepths() { var en = enemies.filter(function (el) { return !el.flying; }); var a = [].concat(towers, en); a.sort(function (a, b) { return a.y - b.y; }); a.forEach(function (e, index) { game.addChildAt(e, 6 + index); //console.log('y and zIndex:' + e.y + ', ' + e.parent.getChildIndex(e)); }); } ; game.on('down', function (x, y, obj) { //console.log('game on down, position:' + obj.event.getLocalPosition(game).x + ',' + obj.event.getLocalPosition(game).y); var position = game.toLocal(obj.global); 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); sortDepths(); /* 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(); } } } });
/****
* Classes
****/
// Enemies below here.
var BalrogEnemy = Container.expand(function () {
var self = Container.call(this);
console.log('Balrog Spawned');
var balrogGraphics = self.attachAsset('balrogSprite', {
anchorX: 0.5,
anchorY: 0.5
});
balrogGraphics.scale.set(2.5);
var healthBar = new HealthBar();
healthBar.y = -balrogGraphics.height + 100;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(100);
self.hitpoints = balrogHitpoints;
self.maxHitpoints = balrogHitpoints;
self.isDead = false;
balrogHitpoints *= 2;
self._move_migrated = function () {
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 + self.width / 2) {
//closestTower.health -= 4;
closestTower.takeDamage(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 = self.x - closestTower.x;
var ay = self.y - closestTower.y;
var len = Math.sqrt(ax * ax + ay * ay);
ax = ax / len * avoidanceForce;
ay = ay / len * avoidanceForce;
self.x += ax;
self.y += ay;
} else {
self.zigzagCounter = self.zigzagCounter || 0;
self.zigzagDirection = self.zigzagDirection || 1;
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
self.x += 4 * self.zigzagDirection;
self.y += 2;
if (Math.random() < 0.01) {
self.zigzagTension = Math.floor(Math.random() * 40) + 10;
}
if (self.x > 2048) {
self.zigzagDirection = -1;
}
if (self.x < 0) {
self.zigzagDirection = 1;
}
var scaleIncrement = (1 - 0.1) / 200;
self.scale.x += scaleIncrement;
self.scale.y += scaleIncrement;
self.scale.x = Math.min(self.scale.x, 1);
self.scale.y = Math.min(self.scale.y, 1);
if (self.zigzagCounter % 10 == 0) {
self.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(self.x, self.y, 720, 2450, i * 2);
game.addChild(t);
goldCoins.push(t);
}
self.isDead = true;
};
});
var Bullet = Container.expand(function (upgradeLevel) {
var self = Container.call(this);
//self.game = null;
self.upgradeLevel = upgradeLevel;
self.damage = towerDamage[self.upgradeLevel];
self.isDead = false;
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_migrated = function () {
if (self.target) {
var tx = self.target.x - this.x;
var ty = self.target.y - this.y;
var dist = Math.sqrt(tx * tx + ty * ty);
if (dist > 0) {
self.x += self.vx * 10;
self.y += self.vy * 10;
}
if (dist < self.width / 2 + self.target.width / 2) {
self.hit();
} else if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
self.isDead = true;
}
}
};
self.hit = function () {
self.target.hitpoints -= self.damage;
self.target.healthBar.updateHealth(self.target.hitpoints / self.target.maxHitpoints * 100);
if (self.target.hitpoints <= 0) {
self.target.die();
}
self.isDead = true;
};
});
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);
console.log('DeathKnight Spawned');
var dKnightGraphics = self.attachAsset('deathKnightSprite', {
anchorX: 0.5,
anchorY: 0.5
});
dKnightGraphics.scale.set(2.5);
var healthBar = new HealthBar();
healthBar.y = -dKnightGraphics.height - 50;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(100);
self.hitpoints = 100;
self.maxHitpoints = 100;
self.isDead = false;
self._move_migrated = function () {
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 + self.width / 2) {
//closestTower.health -= 1;
closestTower.takeDamage(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 = self.x - closestTower.x;
var ay = self.y - closestTower.y;
var len = Math.sqrt(ax * ax + ay * ay);
ax = ax / len * avoidanceForce;
ay = ay / len * avoidanceForce;
self.x += ax;
self.y += ay;
} else {
self.zigzagCounter = self.zigzagCounter || 0;
self.zigzagDirection = self.zigzagDirection || 1;
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
self.x += 4 * self.zigzagDirection;
self.y += 2;
if (Math.random() < 0.01) {
self.zigzagTension = Math.floor(Math.random() * 40) + 10;
}
if (self.x > 2048) {
self.zigzagDirection = -1;
}
if (self.x < 0) {
self.zigzagDirection = 1;
}
var scaleIncrement = (1 - 0.1) / 200;
self.scale.x += scaleIncrement;
self.scale.y += scaleIncrement;
self.scale.x = Math.min(self.scale.x, 1);
self.scale.y = Math.min(self.scale.y, 1);
if (self.zigzagCounter % 10 == 0) {
self.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(self.x, self.y, 720, 2450, i * 2);
game.addChild(t);
goldCoins.push(t);
}
self.isDead = true;
};
});
var DragonEnemy = Container.expand(function () {
var self = Container.call(this);
console.log('Dragon Enemy Spawned');
var enemyGraphics1 = self.attachAsset('dragonSprite1', {
anchorX: 0.5,
anchorY: 0.5
});
enemyGraphics1.scale.set(1);
var enemyGraphics2 = self.attachAsset('dragonSprite2', {
anchorX: 0.5,
anchorY: 0.5
});
enemyGraphics2.scale.set(1);
enemyGraphics2.alpha = 0;
var dragonFire = new DragonFire();
dragonFire.x = -100;
dragonFire.y = 80;
self.addChild(dragonFire);
var healthBar = new HealthBar();
healthBar.y = -enemyGraphics1.height + 150;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(100);
self.hitpoints = 6000;
self.maxHitpoints = 6000;
self.isDead = false;
self.zigzagTension = 400;
self.zigzagDirection = 1;
self.flying = true;
self._move_migrated = function () {
self.zigzagCounter = self.zigzagCounter || 0;
//self.zigzagDirection = self.zigzagDirection || 1;
if (self.zigzagCounter++ % self.zigzagTension === 0) {
if (self.y < 1800) {
self.zigzagDirection *= -1;
}
}
self.x += 4 * self.zigzagDirection;
if (self.y < 1800) {
self.y += 2;
}
/*
if (Math.random() < 0.005) {
self.zigzagTension = Math.floor(Math.random() * 380) + 20;
}*/
if (self.x > 2048) {
if (self.y < 1800) {
self.zigzagDirection = -1;
} else {
if (self.x > 2448) {
self.isDead = true;
}
}
}
if (self.x < 0) {
if (self.y < 1800) {
self.zigzagDirection = 1;
} else {
if (self.x < -400) {
self.isDead = true;
}
}
}
var scaleIncrement = (1 - 0.1) / 200;
self.scale.x += scaleIncrement;
self.scale.y += scaleIncrement;
self.scale.x = Math.min(self.scale.x, 1);
self.scale.y = Math.min(self.scale.y, 1);
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
if (self.zigzagCounter % 5 == 0) {
self.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 50);
}
if (self.zigzagDirection == 1) {
//self.removeChild(self.graphics);
//self.graphics = enemyGraphics1;
//self.addChild(self.graphics);
enemyGraphics2.alpha = 0;
enemyGraphics1.alpha = 1;
dragonFire.scale.x = -1;
dragonFire.x = 100;
} else {
//self.removeChild(self.graphics);
//self.graphics = enemyGraphics2;
//self.addChild(self.graphics);
enemyGraphics1.alpha = 0;
enemyGraphics2.alpha = 1;
dragonFire.scale.x = 1;
dragonFire.x = -100;
}
// Check for damage to towers
var minDist = Infinity;
var closestTower = null;
for (var i = 0; i < towers.length; i++) {
var dfx = self.x + self.zigzagDirection == 1 ? 300 : -300;
var dfy = self.y + 100;
if (LK.ticks % 10 == 0) {
var deb = self.attachAsset('debug', {
anchorX: 0.5,
anchorY: 0.5
});
self.addChild(deb);
deb.x = self.zigzagDirection == 1 ? 300 : -300;
deb.y = 300;
}
//var dx = towers[i].x - self.x + dragonFire.x;
//var dy = towers[i].y - self.y + dragonFire.y;
var dx = towers[i].x - dfx;
var dy = towers[i].y - dfy;
var dist = Math.sqrt(dx * dx + dy * dy);
if (dist < minDist) {
minDist = dist;
closestTower = towers[i];
}
}
//if (closestTower && minDist < closestTower.width / 2 + self.width / 2) {
if (closestTower && minDist < 600) {
//closestTower.health -= 0.5;
closestTower.takeDamage(10);
if (closestTower.health <= 0) {
var towerIndex = towers.indexOf(closestTower);
if (towerIndex > -1) {
towers.splice(towerIndex, 1);
}
closestTower.destroy();
}
}
};
self.die = function () {
playerGold += 10;
playerScore += 150;
updateScoreLabel();
updateGoldLabel();
for (var i = 0; i < 8; i++) {
var t = new GoldCoin(self.x, self.y, 720, 2450, i * 2);
game.addChild(t);
goldCoins.push(t);
}
self.isDead = true;
};
});
var DragonFire = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('dragonFire', {
anchorX: 1,
anchorY: 0
});
self.update = function () {
this.scale.x += 0.2 - Math.random() * 0.4;
};
});
// Goblin,
var Enemy = Container.expand(function () {
var self = Container.call(this);
console.log('Standard Enemy Spawned');
var enemyGraphics = self.attachAsset('enemySprite', {
anchorX: 0.5,
anchorY: 0.5
});
enemyGraphics.scale.set(0.8);
var healthBar = new HealthBar();
healthBar.y = -enemyGraphics.height + 40;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(100);
self.isDead = false;
self.hitpoints = 3;
self.maxHitpoints = 3;
self.zigzagTension = Math.floor(Math.random() * 40) + 10;
self._move_migrated = 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 + self.width / 2) {
//closestTower.health -= 0.5;
closestTower.takeDamage(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 = self.x - closestTower.x;
var ay = self.y - closestTower.y;
var len = Math.sqrt(ax * ax + ay * ay);
ax = ax / len * avoidanceForce;
ay = ay / len * avoidanceForce;
self.x += ax;
self.y += ay;
} else {
self.zigzagCounter = self.zigzagCounter || 0;
self.zigzagDirection = self.zigzagDirection || 1;
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
self.x += 4 * self.zigzagDirection;
self.y += 2;
if (Math.random() < 0.01) {
self.zigzagTension = Math.floor(Math.random() * 40) + 10;
}
if (self.x > 2048) {
self.zigzagDirection = -1;
}
if (self.x < 0) {
self.zigzagDirection = 1;
}
var scaleIncrement = (1 - 0.1) / 200;
self.scale.x += scaleIncrement;
self.scale.y += scaleIncrement;
self.scale.x = Math.min(self.scale.x, 1);
self.scale.y = Math.min(self.scale.y, 1);
self.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(self.x, self.y, 720, 2450, i * 2);
game.addChild(t);
goldCoins.push(t);
}
self.isDead = true;
};
});
var FlyingEnemy = Container.expand(function () {
var self = Container.call(this);
console.log('Flying Enemy Spawned');
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);
var healthBar = new HealthBar();
healthBar.y = -enemyGraphics1.height + 20;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(100);
self.hitpoints = 6;
self.maxHitpoints = 6;
self.isDead = false;
self.zigzagTension = 400;
self.flying = true;
self._move_migrated = function () {
self.zigzagCounter = self.zigzagCounter || 0;
self.zigzagDirection = self.zigzagDirection || 1;
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
self.x += 4 * self.zigzagDirection;
self.y += 2;
if (Math.random() < 0.005) {
self.zigzagTension = Math.floor(Math.random() * 380) + 20;
}
if (self.x > 2048) {
self.zigzagDirection = -1;
}
if (self.x < 0) {
self.zigzagDirection = 1;
}
var scaleIncrement = (1 - 0.1) / 200;
self.scale.x += scaleIncrement;
self.scale.y += scaleIncrement;
self.scale.x = Math.min(self.scale.x, 1);
self.scale.y = Math.min(self.scale.y, 1);
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
if (self.zigzagCounter % 5 == 0) {
self.rotation = (0.5 - Math.random()) * (Math.PI * 2 / 50);
}
if (self.zigzagDirection == 1) {
self.removeChild(self.graphics);
self.graphics = enemyGraphics1;
self.addChild(self.graphics);
} else {
self.removeChild(self.graphics);
self.graphics = enemyGraphics2;
self.addChild(self.graphics);
}
};
self.die = function () {
playerGold += 10;
playerScore += 150;
updateScoreLabel();
updateGoldLabel();
for (var i = 0; i < 8; i++) {
var t = new GoldCoin(self.x, self.y, 720, 2450, i * 2);
game.addChild(t);
goldCoins.push(t);
}
self.isDead = true;
};
});
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;
self.isDead = false;
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_migrated = 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();
self.isDead = true;
}
};
});
var HealthBar = Container.expand(function () {
var self = Container.call(this);
var healthBarGraphics = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 1
});
/*
self.setTint = function (tint) {
healthBarGraphics.tint = tint;
};*/
//self.setTint(0x00FF00);
self.updateHealth = function (health) {
self.scale.set(health / 100, 1);
};
});
var HealthBarTower = Container.expand(function () {
var self = Container.call(this);
var healthBarGraphics = self.attachAsset('healthBarTower', {
anchorX: 0.5,
anchorY: 1
});
/*
self.setTint = function (tint) {
healthBarGraphics.tint = tint;
};*/
//self.setTint(0x00FF00);
self.updateHealth = function (health) {
self.scale.set(health, 1);
};
});
var OrcEnemy = Container.expand(function () {
var self = Container.call(this);
console.log('Orc Spawned');
var enemyGraphics = self.attachAsset('orcEnemySprite', {
anchorX: 0.5,
anchorY: 0.5
});
enemyGraphics.scale.set(0.8);
var healthBar = new HealthBar();
healthBar.y = -enemyGraphics.height + 100;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(100);
self.hitpoints = 6;
self.maxHitpoints = 6;
self.isDead = false;
self.zigzagTension = Math.floor(Math.random() * 20) + 10;
self._move_migrated = function () {
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 + self.width / 2) {
//closestTower.health -= 0.8;
closestTower.takeDamage(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 = self.x - closestTower.x;
var ay = self.y - closestTower.y;
var len = Math.sqrt(ax * ax + ay * ay);
ax = ax / len * avoidanceForce;
ay = ay / len * avoidanceForce;
self.x += ax;
self.y += ay;
} else {
self.zigzagCounter = self.zigzagCounter || 0;
self.zigzagDirection = self.zigzagDirection || 1;
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
self.x += 4 * self.zigzagDirection;
self.y += 2;
if (Math.random() < 0.01) {
self.zigzagTension = Math.floor(Math.random() * 40) + 10;
}
if (self.x > 2048) {
self.zigzagDirection = -1;
}
if (self.x < 0) {
self.zigzagDirection = 1;
}
var scaleIncrement = (1 - 0.1) / 200;
self.scale.x += scaleIncrement;
self.scale.y += scaleIncrement;
self.scale.x = Math.min(self.scale.x, 1);
self.scale.y = Math.min(self.scale.y, 1);
if (self.zigzagCounter % 5 == 0) {
self.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(self.x, self.y, 720, 2450, i * 2);
game.addChild(t);
goldCoins.push(t);
}
self.isDead = true;
};
});
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;
}
}
}
var healthBar = new HealthBarTower();
healthBar.y = towerGraphics.height;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(1);
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.takeDamage = function (dam) {
self.health -= dam;
self.healthBar.updateHealth(self.health / self.maxHealth);
};
self.shootCounter = 0;
self.health = 200;
self.maxHealth = 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);
console.log('Troll Spawned');
var trollGraphics = self.attachAsset('trollSprite', {
anchorX: 0.5,
anchorY: 0.5
});
trollGraphics.scale.set(2.5);
var healthBar = new HealthBar();
healthBar.y = -trollGraphics.height - 40;
self.addChild(healthBar);
self.healthBar = healthBar;
self.healthBar.updateHealth(100);
self.hitpoints = 30;
self.maxHitpoints = 30;
self.isDead = false;
self._move_migrated = function () {
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 + self.width / 2) {
//closestTower.health -= 0.5;
closestTower.takeDamage(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 = self.x - closestTower.x;
var ay = self.y - closestTower.y;
var len = Math.sqrt(ax * ax + ay * ay);
ax = ax / len * avoidanceForce;
ay = ay / len * avoidanceForce;
self.x += ax;
self.y += ay;
} else {
self.zigzagCounter = self.zigzagCounter || 0;
self.zigzagDirection = self.zigzagDirection || 1;
if (self.zigzagCounter++ % self.zigzagTension === 0) {
self.zigzagDirection *= -1;
}
self.x += 4 * self.zigzagDirection;
self.y += 2;
if (Math.random() < 0.01) {
self.zigzagTension = Math.floor(Math.random() * 40) + 10;
}
if (self.x > 2048) {
self.zigzagDirection = -1;
}
if (self.x < 0) {
self.zigzagDirection = 1;
}
var scaleIncrement = (1 - 0.1) / 200;
self.scale.x += scaleIncrement;
self.scale.y += scaleIncrement;
self.scale.x = Math.min(self.scale.x, 1);
self.scale.y = Math.min(self.scale.y, 1);
if (self.zigzagCounter % 5 == 0) {
self.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(self.x, self.y, 720, 2450, i * 2);
game.addChild(t);
goldCoins.push(t);
}
self.isDead = true;
};
});
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 = 400; // TODO: Reset to 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 towers = [];
var enemies = [];
var bullets = [];
var goldCoins = [];
var enemySpawnCounter = 0;
var totalEnemiesSpawned = 0;
var selectedTowerType = null;
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;
}
}
}
}
;
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 addBullet = function addBullet(bullet) {
bullets.push(bullet);
game.addChild(bullet);
};
function sufficientDistanceToOtherTowers(position) {
console.log('sufficientDistanceToOtherTowers');
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 initialEnemy = new Enemy();
initialEnemy.x = 300 + Math.random() * (1848 - 300);
initialEnemy.y = 200;
initialEnemy.scale.set(0.1);
enemies.push(initialEnemy);
game.addChild(initialEnemy);
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;
upgradeButton.on('down', function (x, y, obj) {
obj.event = obj;
upgradeTowers(obj);
});
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);
// Main game loop and click handler below here.
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();
newEnemy = new DragonEnemy();
} else {
newEnemy = new Enemy();
}
}
}
if (newEnemy) {
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;
}
//console.log('enemies length:' + enemies.length);
for (var i = enemies.length - 1; i >= 0; i--) {
if (enemies[i].isDead) {
enemies[i].destroy();
enemies.splice(i, 1);
} else {
enemies[i]._move_migrated();
if (enemies[i].y >= 2332 - enemies[i].height / 2) {
/*
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.setScore(playerScore);
LK.showGameOver();
}
}
}
//console.log('towers length:' + towers.length);
for (var j = 0; j < towers.length; j++) {
towers[j].shoot(enemies);
}
//console.log('bullets length:' + bullets.length);
for (var k = bullets.length - 1; k >= 0; k--) {
if (bullets[k].isDead) {
bullets[k].destroy();
bullets.splice(k, 1);
} else {
bullets[k]._move_migrated();
}
}
//console.log('goldCoins length:' + goldCoins.length);
for (var l = goldCoins.length - 1; l >= 0; l--) {
if (goldCoins[l].isDead) {
goldCoins[l].destroy();
goldCoins.splice(l, 1);
} else {
goldCoins[l]._move_migrated();
}
}
if (LK.ticks % 10 == 0) {
sortDepths();
}
});
function sortDepths() {
var en = enemies.filter(function (el) {
return !el.flying;
});
var a = [].concat(towers, en);
a.sort(function (a, b) {
return a.y - b.y;
});
a.forEach(function (e, index) {
game.addChildAt(e, 6 + index);
//console.log('y and zIndex:' + e.y + ', ' + e.parent.getChildIndex(e));
});
}
;
game.on('down', function (x, y, obj) {
//console.log('game on down, position:' + obj.event.getLocalPosition(game).x + ',' + obj.event.getLocalPosition(game).y);
var position = game.toLocal(obj.global);
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);
sortDepths();
/*
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();
}
}
}
});
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.