User prompt
fes que si la ai arriba a 1000 gasti per forza coloci una torra o millori
User prompt
fes que si te torras danyadas posi una torra de curacio aprop
User prompt
posa una barra de vida a las torras
User prompt
posa a cada torra un assets difarent
User prompt
crea torras que curan a altras torras i torras que fan dany daria
User prompt
fes que cada cop que mato una torra emb donan diners
User prompt
Please fix the bug: 'TypeError: tower.canUpgrade is not a function' in or related to this line: 'if (tower.canUpgrade()) {' Line Number: 1623
User prompt
fes que el cami que seguiexen las units pasi per totas las torras
User prompt
crea varias clases de units
User prompt
crea varias classes de torres
User prompt
fes que las units ataquin a la base
User prompt
fes visibla las monedas que tinc
User prompt
fes que las torras es puguin millorar
User prompt
posa una barra de vida a las units
User prompt
fes que els meus units no ataquin
User prompt
treu la vida a las torres
User prompt
fes que las defensas no es puguin atacar
User prompt
fes que el enemic defensi la base
User prompt
fes casellas per posar torres que posara el enemic
User prompt
fes un cami per las units ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
crea un joc de dafensa la base pero tu ets el que ataca
User prompt
Base Assault
Initial prompt
crea un joc de dafensa la base pero tu ets el que ataca
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var AttackUnit = Container.expand(function () {
var self = Container.call(this);
var unitGraphics = self.attachAsset('attackUnit', {
anchorX: 0.5,
anchorY: 0.5
});
// Health bar background
var healthBarBg = self.attachAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0.5
});
healthBarBg.x = 0;
healthBarBg.y = -40;
// Health bar foreground
var healthBar = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 0.5
});
healthBar.x = 0;
healthBar.y = -40;
self.updateHealthBar = function () {
var healthPercent = self.health / self.maxHealth;
healthBar.scaleX = healthPercent;
// Change color based on health
if (healthPercent > 0.6) {
healthBar.tint = 0x00ff00; // Green
} else if (healthPercent > 0.3) {
healthBar.tint = 0xffff00; // Yellow
} else {
healthBar.tint = 0xff0000; // Red
}
};
self.health = 100;
self.maxHealth = 100;
self.speed = 2;
self.damage = 25;
self.range = 150;
self.shootCooldown = 0;
self.target = null;
self.lastX = 0;
self.lastY = 0;
self.currentWaypoint = 0;
self.isMovingToWaypoint = false;
self.pathWaypoints = [{
x: 400,
y: self.y
}, {
x: 700,
y: 1200
}, {
x: 1000,
y: 800
}, {
x: 1300,
y: 1400
}, {
x: 1600,
y: 1000
}];
self.followPath = function () {
if (self.isMovingToWaypoint) return;
if (self.currentWaypoint >= self.pathWaypoints.length) {
// Reached end of path, move directly to base
self.x += self.speed;
return;
}
var waypoint = self.pathWaypoints[self.currentWaypoint];
var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
var duration = distance / self.speed * 16.67; // Convert speed to ms (60fps = 16.67ms per frame)
self.isMovingToWaypoint = true;
tween(self, {
x: waypoint.x,
y: waypoint.y
}, {
duration: duration,
easing: tween.linear,
onFinish: function onFinish() {
self.currentWaypoint++;
self.isMovingToWaypoint = false;
}
});
};
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
// Find target (prioritize base, then towers)
if (!self.target || !self.target.parent) {
self.target = null;
var closestTarget = null;
var closestDistance = Infinity;
// Check distance to enemy base first
if (enemyBase && enemyBase.parent) {
var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
if (baseDistance <= self.range) {
closestTarget = enemyBase;
closestDistance = baseDistance;
}
}
// Check towers if no base target or if tower is closer
for (var i = 0; i < enemyTowers.length; i++) {
var tower = enemyTowers[i];
if (tower && tower.parent) {
var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
if (towerDistance <= self.range && towerDistance < closestDistance) {
closestDistance = towerDistance;
closestTarget = tower;
}
}
}
self.target = closestTarget;
}
// Attack target if in range
if (self.target && self.shootCooldown <= 0) {
var bullet = new UnitBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
unitBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 60; // 1 second cooldown at 60fps
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
// Move towards enemy base using waypoints only if no target in range
if (!self.target) {
self.followPath();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
self.updateHealthBar();
if (self.health <= 0) {
return true; // Unit is dead
}
return false;
};
return self;
});
var BasicTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('enemyTower', {
anchorX: 0.5,
anchorY: 0.5
});
towerGraphics.tint = 0x666666; // Gray color for basic tower
self.towerType = 'basic';
self.health = 100;
self.maxHealth = 100;
self.range = 150;
self.damage = 20;
self.shootCooldown = 0;
self.target = null;
self.cost = 75;
self.update = function () {
// Find target
if (!self.target || !self.target.parent) {
self.target = null;
var closestUnit = null;
var closestDistance = Infinity;
for (var i = 0; i < playerUnits.length; i++) {
var unit = playerUnits[i];
var distance = Math.sqrt(Math.pow(unit.x - self.x, 2) + Math.pow(unit.y - self.y, 2));
if (distance <= self.range && distance < closestDistance) {
closestDistance = distance;
closestUnit = unit;
}
}
self.target = closestUnit;
}
// Shoot at target
if (self.target && self.shootCooldown <= 0) {
var bullet = new TowerBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
towerBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 90; // 1.5 second cooldown
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xff0000, 200);
if (self.health <= 0) {
return true; // Tower is destroyed
}
return false;
};
return self;
});
var BasicUnit = Container.expand(function () {
var self = Container.call(this);
var unitGraphics = self.attachAsset('attackUnit', {
anchorX: 0.5,
anchorY: 0.5
});
unitGraphics.tint = 0x87CEEB; // Sky blue color for basic unit
unitGraphics.scaleX = 0.9;
unitGraphics.scaleY = 0.9;
// Health bar background
var healthBarBg = self.attachAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0.5
});
healthBarBg.x = 0;
healthBarBg.y = -35;
healthBarBg.scaleX = 0.8;
// Health bar foreground
var healthBar = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 0.5
});
healthBar.x = 0;
healthBar.y = -35;
healthBar.scaleX = 0.8;
self.updateHealthBar = function () {
var healthPercent = self.health / self.maxHealth;
healthBar.scaleX = healthPercent * 0.8;
if (healthPercent > 0.6) {
healthBar.tint = 0x00ff00;
} else if (healthPercent > 0.3) {
healthBar.tint = 0xffff00;
} else {
healthBar.tint = 0xff0000;
}
};
self.unitType = 'basic';
self.health = 80;
self.maxHealth = 80;
self.speed = 2;
self.damage = 20;
self.range = 140;
self.cost = 40;
self.shootCooldown = 0;
self.target = null;
self.lastX = 0;
self.lastY = 0;
self.currentWaypoint = 0;
self.isMovingToWaypoint = false;
self.pathWaypoints = [{
x: 400,
y: self.y
}, {
x: 700,
y: 1200
}, {
x: 1000,
y: 800
}, {
x: 1300,
y: 1400
}, {
x: 1600,
y: 1000
}];
self.followPath = function () {
if (self.isMovingToWaypoint) return;
if (self.currentWaypoint >= self.pathWaypoints.length) {
self.x += self.speed;
return;
}
var waypoint = self.pathWaypoints[self.currentWaypoint];
var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
var duration = distance / self.speed * 16.67;
self.isMovingToWaypoint = true;
tween(self, {
x: waypoint.x,
y: waypoint.y
}, {
duration: duration,
easing: tween.linear,
onFinish: function onFinish() {
self.currentWaypoint++;
self.isMovingToWaypoint = false;
}
});
};
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
if (!self.target || !self.target.parent) {
self.target = null;
var closestTarget = null;
var closestDistance = Infinity;
if (enemyBase && enemyBase.parent) {
var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
if (baseDistance <= self.range) {
closestTarget = enemyBase;
closestDistance = baseDistance;
}
}
for (var i = 0; i < enemyTowers.length; i++) {
var tower = enemyTowers[i];
if (tower && tower.parent) {
var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
if (towerDistance <= self.range && towerDistance < closestDistance) {
closestDistance = towerDistance;
closestTarget = tower;
}
}
}
self.target = closestTarget;
}
if (self.target && self.shootCooldown <= 0) {
var bullet = new UnitBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
unitBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 75;
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
if (!self.target) {
self.followPath();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
self.updateHealthBar();
if (self.health <= 0) {
return true;
}
return false;
};
return self;
});
var EnemyBase = Container.expand(function () {
var self = Container.call(this);
var baseGraphics = self.attachAsset('enemyBase', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 500;
self.maxHealth = 500;
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xff0000, 300);
if (self.health <= 0) {
return true; // Base destroyed - player wins!
}
return false;
};
return self;
});
// Game arrays to track entities
var EnemyTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('enemyTower', {
anchorX: 0.5,
anchorY: 0.5
});
// Upgrade indicator (initially hidden)
var upgradeIndicator = self.attachAsset('upgradeIndicator', {
anchorX: 0.5,
anchorY: 0.5
});
upgradeIndicator.x = 30;
upgradeIndicator.y = -30;
upgradeIndicator.visible = false;
self.level = 1;
self.maxLevel = 3;
self.health = 150;
self.maxHealth = 150;
self.baseRange = 200;
self.baseDamage = 30;
self.range = self.baseRange;
self.damage = self.baseDamage;
self.shootCooldown = 0;
self.target = null;
self.updateStats = function () {
var multiplier = 1 + (self.level - 1) * 0.5; // 50% increase per level
self.range = Math.floor(self.baseRange * multiplier);
self.damage = Math.floor(self.baseDamage * multiplier);
self.maxHealth = Math.floor(150 * multiplier);
self.health = Math.min(self.health, self.maxHealth);
// Update visual appearance based on level
if (self.level === 2) {
towerGraphics.tint = 0xc0392b; // Darker red
upgradeIndicator.visible = true;
upgradeIndicator.tint = 0xffd700; // Gold
} else if (self.level === 3) {
towerGraphics.tint = 0x8b0000; // Dark red
upgradeIndicator.visible = true;
upgradeIndicator.tint = 0xff4500; // Orange red
}
};
self.canUpgrade = function () {
return self.level < self.maxLevel;
};
self.getUpgradeCost = function () {
return 75 * self.level; // Cost increases with level
};
self.upgrade = function () {
if (self.canUpgrade()) {
self.level++;
self.updateStats();
LK.effects.flashObject(self, 0xffd700, 500);
return true;
}
return false;
};
self.update = function () {
// Increase range and damage if base is low on health
var baseHealthPercent = enemyBase ? enemyBase.health / enemyBase.maxHealth : 1;
var bonusRange = baseHealthPercent < 0.5 ? 100 : 0;
var bonusDamage = baseHealthPercent < 0.3 ? 20 : 0;
var currentRange = self.range + bonusRange;
var currentDamage = self.damage + bonusDamage;
// Find target
if (!self.target || !self.target.parent) {
self.target = null;
var closestUnit = null;
var closestDistance = Infinity;
// Prioritize units closer to base
for (var i = 0; i < playerUnits.length; i++) {
var unit = playerUnits[i];
var distance = Math.sqrt(Math.pow(unit.x - self.x, 2) + Math.pow(unit.y - self.y, 2));
var distanceToBase = Math.sqrt(Math.pow(unit.x - enemyBase.x, 2) + Math.pow(unit.y - enemyBase.y, 2));
if (distance <= currentRange) {
// Prioritize units closer to base
var priority = distance - distanceToBase * 0.3;
if (priority < closestDistance) {
closestDistance = priority;
closestUnit = unit;
}
}
}
self.target = closestUnit;
}
// Shoot at target
if (self.target && self.shootCooldown <= 0) {
var bullet = new TowerBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = currentDamage;
towerBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
// Shoot faster when base is in danger
var shootCooldown = baseHealthPercent < 0.5 ? 60 : 90;
self.shootCooldown = shootCooldown;
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xff0000, 200);
if (self.health <= 0) {
return true; // Tower is destroyed
}
return false;
};
return self;
});
var FastTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('enemyTower', {
anchorX: 0.5,
anchorY: 0.5
});
towerGraphics.tint = 0x00ff00; // Green color for fast tower
towerGraphics.scaleX = 0.8;
towerGraphics.scaleY = 0.8;
self.towerType = 'fast';
self.health = 80;
self.maxHealth = 80;
self.range = 180;
self.damage = 15;
self.shootCooldown = 0;
self.target = null;
self.cost = 100;
self.update = function () {
// Find target
if (!self.target || !self.target.parent) {
self.target = null;
var closestUnit = null;
var closestDistance = Infinity;
for (var i = 0; i < playerUnits.length; i++) {
var unit = playerUnits[i];
var distance = Math.sqrt(Math.pow(unit.x - self.x, 2) + Math.pow(unit.y - self.y, 2));
if (distance <= self.range && distance < closestDistance) {
closestDistance = distance;
closestUnit = unit;
}
}
self.target = closestUnit;
}
// Shoot at target with fast rate
if (self.target && self.shootCooldown <= 0) {
var bullet = new TowerBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
towerBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 30; // 0.5 second cooldown (very fast)
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xff0000, 200);
if (self.health <= 0) {
return true; // Tower is destroyed
}
return false;
};
return self;
});
var FastUnit = Container.expand(function () {
var self = Container.call(this);
var unitGraphics = self.attachAsset('attackUnit', {
anchorX: 0.5,
anchorY: 0.5
});
unitGraphics.tint = 0x32CD32; // Lime green color for fast unit
unitGraphics.scaleX = 0.8;
unitGraphics.scaleY = 0.8;
// Health bar background
var healthBarBg = self.attachAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0.5
});
healthBarBg.x = 0;
healthBarBg.y = -32;
healthBarBg.scaleX = 0.7;
// Health bar foreground
var healthBar = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 0.5
});
healthBar.x = 0;
healthBar.y = -32;
healthBar.scaleX = 0.7;
self.updateHealthBar = function () {
var healthPercent = self.health / self.maxHealth;
healthBar.scaleX = healthPercent * 0.7;
if (healthPercent > 0.6) {
healthBar.tint = 0x00ff00;
} else if (healthPercent > 0.3) {
healthBar.tint = 0xffff00;
} else {
healthBar.tint = 0xff0000;
}
};
self.unitType = 'fast';
self.health = 60;
self.maxHealth = 60;
self.speed = 4;
self.damage = 15;
self.range = 120;
self.cost = 60;
self.shootCooldown = 0;
self.target = null;
self.lastX = 0;
self.lastY = 0;
self.currentWaypoint = 0;
self.isMovingToWaypoint = false;
self.pathWaypoints = [{
x: 400,
y: self.y
}, {
x: 700,
y: 1200
}, {
x: 1000,
y: 800
}, {
x: 1300,
y: 1400
}, {
x: 1600,
y: 1000
}];
self.followPath = function () {
if (self.isMovingToWaypoint) return;
if (self.currentWaypoint >= self.pathWaypoints.length) {
self.x += self.speed;
return;
}
var waypoint = self.pathWaypoints[self.currentWaypoint];
var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
var duration = distance / self.speed * 16.67;
self.isMovingToWaypoint = true;
tween(self, {
x: waypoint.x,
y: waypoint.y
}, {
duration: duration,
easing: tween.linear,
onFinish: function onFinish() {
self.currentWaypoint++;
self.isMovingToWaypoint = false;
}
});
};
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
if (!self.target || !self.target.parent) {
self.target = null;
var closestTarget = null;
var closestDistance = Infinity;
if (enemyBase && enemyBase.parent) {
var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
if (baseDistance <= self.range) {
closestTarget = enemyBase;
closestDistance = baseDistance;
}
}
for (var i = 0; i < enemyTowers.length; i++) {
var tower = enemyTowers[i];
if (tower && tower.parent) {
var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
if (towerDistance <= self.range && towerDistance < closestDistance) {
closestDistance = towerDistance;
closestTarget = tower;
}
}
}
self.target = closestTarget;
}
if (self.target && self.shootCooldown <= 0) {
var bullet = new UnitBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
unitBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 45; // Faster shooting
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
if (!self.target) {
self.followPath();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
self.updateHealthBar();
if (self.health <= 0) {
return true;
}
return false;
};
return self;
});
var HeavyTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('enemyTower', {
anchorX: 0.5,
anchorY: 0.5
});
towerGraphics.tint = 0x8b4513; // Brown color for heavy tower
towerGraphics.scaleX = 1.3;
towerGraphics.scaleY = 1.3;
self.towerType = 'heavy';
self.health = 200;
self.maxHealth = 200;
self.range = 160;
self.damage = 50;
self.shootCooldown = 0;
self.target = null;
self.cost = 150;
self.update = function () {
// Find target
if (!self.target || !self.target.parent) {
self.target = null;
var closestUnit = null;
var closestDistance = Infinity;
for (var i = 0; i < playerUnits.length; i++) {
var unit = playerUnits[i];
var distance = Math.sqrt(Math.pow(unit.x - self.x, 2) + Math.pow(unit.y - self.y, 2));
if (distance <= self.range && distance < closestDistance) {
closestDistance = distance;
closestUnit = unit;
}
}
self.target = closestUnit;
}
// Shoot at target with heavy damage
if (self.target && self.shootCooldown <= 0) {
var bullet = new TowerBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
towerBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 120; // 2 second cooldown (slow)
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xff0000, 200);
if (self.health <= 0) {
return true; // Tower is destroyed
}
return false;
};
return self;
});
var HeavyUnit = Container.expand(function () {
var self = Container.call(this);
var unitGraphics = self.attachAsset('attackUnit', {
anchorX: 0.5,
anchorY: 0.5
});
unitGraphics.tint = 0x8B4513; // Brown color for heavy unit
unitGraphics.scaleX = 1.2;
unitGraphics.scaleY = 1.2;
// Health bar background
var healthBarBg = self.attachAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0.5
});
healthBarBg.x = 0;
healthBarBg.y = -45;
healthBarBg.scaleX = 1.0;
// Health bar foreground
var healthBar = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 0.5
});
healthBar.x = 0;
healthBar.y = -45;
healthBar.scaleX = 1.0;
self.updateHealthBar = function () {
var healthPercent = self.health / self.maxHealth;
healthBar.scaleX = healthPercent * 1.0;
if (healthPercent > 0.6) {
healthBar.tint = 0x00ff00;
} else if (healthPercent > 0.3) {
healthBar.tint = 0xffff00;
} else {
healthBar.tint = 0xff0000;
}
};
self.unitType = 'heavy';
self.health = 150;
self.maxHealth = 150;
self.speed = 1;
self.damage = 40;
self.range = 160;
self.cost = 90;
self.shootCooldown = 0;
self.target = null;
self.lastX = 0;
self.lastY = 0;
self.currentWaypoint = 0;
self.isMovingToWaypoint = false;
self.pathWaypoints = [{
x: 400,
y: self.y
}, {
x: 700,
y: 1200
}, {
x: 1000,
y: 800
}, {
x: 1300,
y: 1400
}, {
x: 1600,
y: 1000
}];
self.followPath = function () {
if (self.isMovingToWaypoint) return;
if (self.currentWaypoint >= self.pathWaypoints.length) {
self.x += self.speed;
return;
}
var waypoint = self.pathWaypoints[self.currentWaypoint];
var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
var duration = distance / self.speed * 16.67;
self.isMovingToWaypoint = true;
tween(self, {
x: waypoint.x,
y: waypoint.y
}, {
duration: duration,
easing: tween.linear,
onFinish: function onFinish() {
self.currentWaypoint++;
self.isMovingToWaypoint = false;
}
});
};
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
if (!self.target || !self.target.parent) {
self.target = null;
var closestTarget = null;
var closestDistance = Infinity;
if (enemyBase && enemyBase.parent) {
var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
if (baseDistance <= self.range) {
closestTarget = enemyBase;
closestDistance = baseDistance;
}
}
for (var i = 0; i < enemyTowers.length; i++) {
var tower = enemyTowers[i];
if (tower && tower.parent) {
var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
if (towerDistance <= self.range && towerDistance < closestDistance) {
closestDistance = towerDistance;
closestTarget = tower;
}
}
}
self.target = closestTarget;
}
if (self.target && self.shootCooldown <= 0) {
var bullet = new UnitBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
unitBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 100; // Slower shooting
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
if (!self.target) {
self.followPath();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
self.updateHealthBar();
if (self.health <= 0) {
return true;
}
return false;
};
return self;
});
var SniperTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('enemyTower', {
anchorX: 0.5,
anchorY: 0.5
});
towerGraphics.tint = 0x4169e1; // Royal blue color for sniper tower
towerGraphics.scaleY = 1.2; // Make it taller
self.towerType = 'sniper';
self.health = 120;
self.maxHealth = 120;
self.range = 300; // Very long range
self.damage = 40;
self.shootCooldown = 0;
self.target = null;
self.cost = 125;
self.update = function () {
// Find target (prioritize units furthest away)
if (!self.target || !self.target.parent) {
self.target = null;
var bestUnit = null;
var longestDistance = 0;
for (var i = 0; i < playerUnits.length; i++) {
var unit = playerUnits[i];
var distance = Math.sqrt(Math.pow(unit.x - self.x, 2) + Math.pow(unit.y - self.y, 2));
if (distance <= self.range && distance > longestDistance) {
longestDistance = distance;
bestUnit = unit;
}
}
self.target = bestUnit;
}
// Shoot at target with precision
if (self.target && self.shootCooldown <= 0) {
var bullet = new TowerBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
bullet.speed = 10; // Faster bullet
towerBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 150; // 2.5 second cooldown
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xff0000, 200);
if (self.health <= 0) {
return true; // Tower is destroyed
}
return false;
};
return self;
});
var SniperUnit = Container.expand(function () {
var self = Container.call(this);
var unitGraphics = self.attachAsset('attackUnit', {
anchorX: 0.5,
anchorY: 0.5
});
unitGraphics.tint = 0x4B0082; // Indigo color for sniper unit
unitGraphics.scaleX = 1.0;
unitGraphics.scaleY = 1.3; // Taller appearance
// Health bar background
var healthBarBg = self.attachAsset('healthBarBg', {
anchorX: 0.5,
anchorY: 0.5
});
healthBarBg.x = 0;
healthBarBg.y = -42;
healthBarBg.scaleX = 0.9;
// Health bar foreground
var healthBar = self.attachAsset('healthBar', {
anchorX: 0.5,
anchorY: 0.5
});
healthBar.x = 0;
healthBar.y = -42;
healthBar.scaleX = 0.9;
self.updateHealthBar = function () {
var healthPercent = self.health / self.maxHealth;
healthBar.scaleX = healthPercent * 0.9;
if (healthPercent > 0.6) {
healthBar.tint = 0x00ff00;
} else if (healthPercent > 0.3) {
healthBar.tint = 0xffff00;
} else {
healthBar.tint = 0xff0000;
}
};
self.unitType = 'sniper';
self.health = 70;
self.maxHealth = 70;
self.speed = 1.5;
self.damage = 50;
self.range = 250; // Very long range
self.cost = 80;
self.shootCooldown = 0;
self.target = null;
self.lastX = 0;
self.lastY = 0;
self.currentWaypoint = 0;
self.isMovingToWaypoint = false;
self.pathWaypoints = [{
x: 400,
y: self.y
}, {
x: 700,
y: 1200
}, {
x: 1000,
y: 800
}, {
x: 1300,
y: 1400
}, {
x: 1600,
y: 1000
}];
self.followPath = function () {
if (self.isMovingToWaypoint) return;
if (self.currentWaypoint >= self.pathWaypoints.length) {
self.x += self.speed;
return;
}
var waypoint = self.pathWaypoints[self.currentWaypoint];
var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
var duration = distance / self.speed * 16.67;
self.isMovingToWaypoint = true;
tween(self, {
x: waypoint.x,
y: waypoint.y
}, {
duration: duration,
easing: tween.linear,
onFinish: function onFinish() {
self.currentWaypoint++;
self.isMovingToWaypoint = false;
}
});
};
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
if (!self.target || !self.target.parent) {
self.target = null;
var closestTarget = null;
var closestDistance = Infinity;
// Prioritize towers over base due to long range
for (var i = 0; i < enemyTowers.length; i++) {
var tower = enemyTowers[i];
if (tower && tower.parent) {
var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
if (towerDistance <= self.range && towerDistance < closestDistance) {
closestDistance = towerDistance;
closestTarget = tower;
}
}
}
// Check base if no towers in range
if (!closestTarget && enemyBase && enemyBase.parent) {
var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
if (baseDistance <= self.range) {
closestTarget = enemyBase;
closestDistance = baseDistance;
}
}
self.target = closestTarget;
}
if (self.target && self.shootCooldown <= 0) {
var bullet = new UnitBullet();
bullet.x = self.x;
bullet.y = self.y;
bullet.target = self.target;
bullet.damage = self.damage;
bullet.speed = 12; // Faster bullet
unitBullets.push(bullet);
game.addChild(bullet);
LK.getSound('shoot').play();
self.shootCooldown = 120; // Slow shooting rate
}
if (self.shootCooldown > 0) {
self.shootCooldown--;
}
if (!self.target) {
self.followPath();
}
};
self.takeDamage = function (damage) {
self.health -= damage;
self.updateHealthBar();
if (self.health <= 0) {
return true;
}
return false;
};
return self;
});
var TowerBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('towerBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 6;
self.target = null;
self.damage = 30;
self.update = function () {
if (!self.target || !self.target.parent) {
// Target destroyed, remove bullet
return;
}
// Move towards target
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 10) {
// Hit target
if (self.target.takeDamage) {
self.target.takeDamage(self.damage);
LK.getSound('hit').play();
}
return; // Bullet will be removed
}
// Normalize and move
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
};
return self;
});
var TowerSlot = Container.expand(function () {
var self = Container.call(this);
var slotGraphics = self.attachAsset('towerSlot', {
anchorX: 0.5,
anchorY: 0.5
});
slotGraphics.alpha = 0.3;
self.occupied = false;
self.tower = null;
self.placeTower = function () {
if (!self.occupied) {
var tower = new EnemyTower();
tower.x = self.x;
tower.y = self.y;
tower.slot = self;
tower.updateStats(); // Initialize stats properly
enemyTowers.push(tower);
game.addChild(tower);
self.occupied = true;
self.tower = tower;
slotGraphics.alpha = 0.1;
return tower;
}
return null;
};
self.removeTower = function () {
if (self.occupied && self.tower) {
self.occupied = false;
self.tower = null;
slotGraphics.alpha = 0.3;
}
};
return self;
});
var UnitBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('unitBullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.speed = 8;
self.target = null;
self.damage = 25;
self.update = function () {
if (!self.target || !self.target.parent) {
// Target destroyed, remove bullet
return;
}
// Move towards target
var dx = self.target.x - self.x;
var dy = self.target.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 10) {
// Hit target
if (self.target.takeDamage) {
self.target.takeDamage(self.damage);
LK.getSound('hit').play();
}
return; // Bullet will be removed
}
// Normalize and move
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2d5016
});
/****
* Game Code
****/
// Game arrays to track entities
// Initialize game assets
var playerUnits = [];
var enemyTowers = [];
var unitBullets = [];
var towerBullets = [];
var towerSlots = [];
var enemyBase;
// Game resources
var coins = 150;
var unitCost = 50;
// UI Elements
var coinsText = new Text2('Coins: ' + coins, {
size: 80,
fill: 0xFFD700
});
coinsText.anchor.set(1, 0);
coinsText.x = -20; // Small margin from right edge
coinsText.y = 20; // Small margin from top
LK.gui.topRight.addChild(coinsText);
var coinIcon = LK.getAsset('coinIcon', {
anchorX: 0.5,
anchorY: 0.5
});
coinIcon.x = -coinsText.width - 60; // Position to left of text
coinIcon.y = 45; // Align with text
LK.gui.topRight.addChild(coinIcon);
var healthText = new Text2('Base Health: 500', {
size: 50,
fill: 0xFFFFFF
});
healthText.anchor.set(0.5, 0);
LK.gui.top.addChild(healthText);
// Unit spawn buttons
var basicButton = new Text2('Basic (40)', {
size: 40,
fill: 0x87CEEB
});
basicButton.anchor.set(0.5, 1);
basicButton.x = -300;
basicButton.interactive = true;
basicButton.down = function () {
if (coins >= 40) {
spawnUnit('basic');
}
};
LK.gui.bottom.addChild(basicButton);
var fastButton = new Text2('Fast (60)', {
size: 40,
fill: 0x32CD32
});
fastButton.anchor.set(0.5, 1);
fastButton.x = -100;
fastButton.interactive = true;
fastButton.down = function () {
if (coins >= 60) {
spawnUnit('fast');
}
};
LK.gui.bottom.addChild(fastButton);
var heavyButton = new Text2('Heavy (90)', {
size: 40,
fill: 0x8B4513
});
heavyButton.anchor.set(0.5, 1);
heavyButton.x = 100;
heavyButton.interactive = true;
heavyButton.down = function () {
if (coins >= 90) {
spawnUnit('heavy');
}
};
LK.gui.bottom.addChild(heavyButton);
var sniperButton = new Text2('Sniper (80)', {
size: 40,
fill: 0x4B0082
});
sniperButton.anchor.set(0.5, 1);
sniperButton.x = 300;
sniperButton.interactive = true;
sniperButton.down = function () {
if (coins >= 80) {
spawnUnit('sniper');
}
};
LK.gui.bottom.addChild(sniperButton);
// Create enemy base
enemyBase = game.addChild(new EnemyBase());
enemyBase.x = 1800;
enemyBase.y = 2732 / 2;
// Create tower slots in a grid
var gridStartX = 800;
var gridStartY = 600;
var gridSpacingX = 200;
var gridSpacingY = 200;
var gridRows = 8;
var gridCols = 4;
for (var row = 0; row < gridRows; row++) {
for (var col = 0; col < gridCols; col++) {
var slot = new TowerSlot();
slot.x = gridStartX + col * gridSpacingX;
slot.y = gridStartY + row * gridSpacingY;
towerSlots.push(slot);
game.addChild(slot);
}
}
// Initially place some towers in random slots
var initialTowerCount = 4;
var placedTowers = 0;
while (placedTowers < initialTowerCount && placedTowers < towerSlots.length) {
var randomSlotIndex = Math.floor(Math.random() * towerSlots.length);
var slot = towerSlots[randomSlotIndex];
if (slot.placeTower()) {
placedTowers++;
}
}
function spawnUnit(unitType) {
if (!unitType) unitType = 'basic';
var unit = null;
var cost = 40;
switch (unitType) {
case 'basic':
unit = new BasicUnit();
cost = 40;
break;
case 'fast':
unit = new FastUnit();
cost = 60;
break;
case 'heavy':
unit = new HeavyUnit();
cost = 90;
break;
case 'sniper':
unit = new SniperUnit();
cost = 80;
break;
default:
unit = new BasicUnit();
cost = 40;
break;
}
if (coins >= cost && unit) {
coins -= cost;
coinsText.setText('Coins: ' + coins);
coinIcon.x = -coinsText.width - 60;
unit.x = 100;
unit.y = 500 + playerUnits.length % 5 * 150;
// Set custom waypoints based on spawn position to create varied paths
unit.pathWaypoints = [{
x: 400,
y: unit.y
}, {
x: 700,
y: unit.y < 1366 ? 1200 : 800
}, {
x: 1000,
y: unit.y < 1366 ? 800 : 1400
}, {
x: 1300,
y: unit.y < 1366 ? 1400 : 1000
}, {
x: 1600,
y: 1366
}];
playerUnits.push(unit);
game.addChild(unit);
unit.updateHealthBar();
LK.getSound('unitSpawn').play();
}
}
// Coin generation timer
var coinTimer = 0;
// Enemy AI system
var enemyAI = {
coins: 200,
towerCost: 100,
lastTowerPlacement: 0,
placementCooldown: 300,
// 5 seconds at 60fps
update: function update() {
// Generate coins for enemy AI
if (LK.ticks % 180 === 0) {
// Every 3 seconds
this.coins += 50;
}
// Check if should place new towers
if (this.coins >= this.towerCost && LK.ticks - this.lastTowerPlacement > this.placementCooldown) {
this.considerTowerPlacement();
}
// Upgrade existing towers
this.considerTowerUpgrades();
// Repair damaged towers
this.repairTowers();
},
considerTowerPlacement: function considerTowerPlacement() {
// Find empty slots closest to player units
var bestSlot = null;
var minDistance = Infinity;
for (var i = 0; i < towerSlots.length; i++) {
var slot = towerSlots[i];
if (!slot.occupied) {
// Calculate average distance to all player units
var totalDistance = 0;
var unitCount = 0;
for (var j = 0; j < playerUnits.length; j++) {
var unit = playerUnits[j];
var distance = Math.sqrt(Math.pow(unit.x - slot.x, 2) + Math.pow(unit.y - slot.y, 2));
totalDistance += distance;
unitCount++;
}
if (unitCount > 0) {
var avgDistance = totalDistance / unitCount;
if (avgDistance < minDistance && avgDistance < 400) {
// Only place if units are nearby
minDistance = avgDistance;
bestSlot = slot;
}
}
}
}
// Place tower at best location
if (bestSlot) {
// Choose random tower type based on available coins
var towerTypes = [];
if (this.coins >= 75) towerTypes.push('basic');
if (this.coins >= 100) towerTypes.push('fast');
if (this.coins >= 125) towerTypes.push('sniper');
if (this.coins >= 150) towerTypes.push('heavy');
if (towerTypes.length > 0) {
var randomType = towerTypes[Math.floor(Math.random() * towerTypes.length)];
var tower = null;
var cost = 75;
switch (randomType) {
case 'basic':
tower = new BasicTower();
cost = 75;
break;
case 'fast':
tower = new FastTower();
cost = 100;
break;
case 'sniper':
tower = new SniperTower();
cost = 125;
break;
case 'heavy':
tower = new HeavyTower();
cost = 150;
break;
}
if (tower) {
tower.x = bestSlot.x;
tower.y = bestSlot.y;
tower.slot = bestSlot;
enemyTowers.push(tower);
game.addChild(tower);
bestSlot.occupied = true;
bestSlot.tower = tower;
bestSlot.attachAsset('towerSlot', {}).alpha = 0.1;
this.coins -= cost;
this.lastTowerPlacement = LK.ticks;
// Visual feedback
LK.effects.flashObject(bestSlot, 0xff0000, 500);
}
}
}
},
considerTowerUpgrades: function considerTowerUpgrades() {
// Find towers that can be upgraded and are in combat zones
for (var i = 0; i < enemyTowers.length; i++) {
var tower = enemyTowers[i];
if (tower.canUpgrade()) {
var upgradeCost = tower.getUpgradeCost();
if (this.coins >= upgradeCost) {
// Check if tower is in an active combat zone (has nearby player units)
var nearbyUnits = 0;
for (var j = 0; j < playerUnits.length; j++) {
var unit = playerUnits[j];
var distance = Math.sqrt(Math.pow(unit.x - tower.x, 2) + Math.pow(unit.y - tower.y, 2));
if (distance <= tower.range + 100) {
nearbyUnits++;
}
}
// Upgrade towers that are actively engaged or if we have excess coins
if (nearbyUnits > 0 || this.coins > upgradeCost * 2) {
if (tower.upgrade()) {
this.coins -= upgradeCost;
break; // Only upgrade one tower per update
}
}
}
}
}
},
repairTowers: function repairTowers() {
// Find damaged towers and repair them
for (var i = 0; i < enemyTowers.length; i++) {
var tower = enemyTowers[i];
if (tower.health < tower.maxHealth * 0.5 && this.coins >= 25) {
tower.health = Math.min(tower.health + 25, tower.maxHealth);
this.coins -= 25;
LK.effects.flashObject(tower, 0x00ff00, 300);
break; // Only repair one tower per update
}
}
}
};
// Touch controls for spawning units
game.down = function (x, y, obj) {
if (coins >= 40) {
spawnUnit('basic');
}
};
game.update = function () {
// Update enemy AI
enemyAI.update();
// Generate coins over time
coinTimer++;
if (coinTimer >= 120) {
// Every 2 seconds
coins += 25;
coinsText.setText('Coins: ' + coins);
coinIcon.x = -coinsText.width - 60; // Update icon position
coinTimer = 0;
}
// Update unit bullets
for (var i = unitBullets.length - 1; i >= 0; i--) {
var bullet = unitBullets[i];
if (!bullet.target || !bullet.target.parent) {
bullet.destroy();
unitBullets.splice(i, 1);
continue;
}
var dx = bullet.target.x - bullet.x;
var dy = bullet.target.y - bullet.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 15) {
// Hit target
var isDead = bullet.target.takeDamage(bullet.damage);
if (isDead) {
// Remove from appropriate array
if (bullet.target === enemyBase) {
// Player wins!
LK.effects.flashScreen(0x00ff00, 2000);
LK.showYouWin();
return;
} else {
// Remove tower
for (var j = 0; j < enemyTowers.length; j++) {
if (enemyTowers[j] === bullet.target) {
// Clean up slot if tower has one
if (enemyTowers[j].slot) {
enemyTowers[j].slot.removeTower();
}
enemyTowers[j].destroy();
enemyTowers.splice(j, 1);
coins += 75; // Bonus for destroying tower
coinsText.setText('Coins: ' + coins);
coinIcon.x = -coinsText.width - 60; // Update icon position
break;
}
}
}
}
bullet.destroy();
unitBullets.splice(i, 1);
}
}
// Update tower bullets
for (var i = towerBullets.length - 1; i >= 0; i--) {
var bullet = towerBullets[i];
if (!bullet.target || !bullet.target.parent) {
bullet.destroy();
towerBullets.splice(i, 1);
continue;
}
var dx = bullet.target.x - bullet.x;
var dy = bullet.target.y - bullet.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 15) {
// Hit target
var isDead = bullet.target.takeDamage(bullet.damage);
if (isDead) {
// Remove unit
for (var j = 0; j < playerUnits.length; j++) {
if (playerUnits[j] === bullet.target) {
playerUnits[j].destroy();
playerUnits.splice(j, 1);
break;
}
}
}
bullet.destroy();
towerBullets.splice(i, 1);
}
}
// Check if player has no units and insufficient coins (lose condition)
if (playerUnits.length === 0 && coins < unitCost) {
// Check if there are still towers or base with health
if (enemyTowers.length > 0 || enemyBase && enemyBase.health > 0) {
LK.effects.flashScreen(0xff0000, 2000);
LK.showGameOver();
return;
}
}
// Update base health display
if (enemyBase) {
healthText.setText('Base Health: ' + enemyBase.health);
}
}; ===================================================================
--- original.js
+++ change.js
@@ -203,8 +203,149 @@
return false;
};
return self;
});
+var BasicUnit = Container.expand(function () {
+ var self = Container.call(this);
+ var unitGraphics = self.attachAsset('attackUnit', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ unitGraphics.tint = 0x87CEEB; // Sky blue color for basic unit
+ unitGraphics.scaleX = 0.9;
+ unitGraphics.scaleY = 0.9;
+ // Health bar background
+ var healthBarBg = self.attachAsset('healthBarBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBarBg.x = 0;
+ healthBarBg.y = -35;
+ healthBarBg.scaleX = 0.8;
+ // Health bar foreground
+ var healthBar = self.attachAsset('healthBar', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBar.x = 0;
+ healthBar.y = -35;
+ healthBar.scaleX = 0.8;
+ self.updateHealthBar = function () {
+ var healthPercent = self.health / self.maxHealth;
+ healthBar.scaleX = healthPercent * 0.8;
+ if (healthPercent > 0.6) {
+ healthBar.tint = 0x00ff00;
+ } else if (healthPercent > 0.3) {
+ healthBar.tint = 0xffff00;
+ } else {
+ healthBar.tint = 0xff0000;
+ }
+ };
+ self.unitType = 'basic';
+ self.health = 80;
+ self.maxHealth = 80;
+ self.speed = 2;
+ self.damage = 20;
+ self.range = 140;
+ self.cost = 40;
+ self.shootCooldown = 0;
+ self.target = null;
+ self.lastX = 0;
+ self.lastY = 0;
+ self.currentWaypoint = 0;
+ self.isMovingToWaypoint = false;
+ self.pathWaypoints = [{
+ x: 400,
+ y: self.y
+ }, {
+ x: 700,
+ y: 1200
+ }, {
+ x: 1000,
+ y: 800
+ }, {
+ x: 1300,
+ y: 1400
+ }, {
+ x: 1600,
+ y: 1000
+ }];
+ self.followPath = function () {
+ if (self.isMovingToWaypoint) return;
+ if (self.currentWaypoint >= self.pathWaypoints.length) {
+ self.x += self.speed;
+ return;
+ }
+ var waypoint = self.pathWaypoints[self.currentWaypoint];
+ var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
+ var duration = distance / self.speed * 16.67;
+ self.isMovingToWaypoint = true;
+ tween(self, {
+ x: waypoint.x,
+ y: waypoint.y
+ }, {
+ duration: duration,
+ easing: tween.linear,
+ onFinish: function onFinish() {
+ self.currentWaypoint++;
+ self.isMovingToWaypoint = false;
+ }
+ });
+ };
+ self.update = function () {
+ self.lastX = self.x;
+ self.lastY = self.y;
+ if (!self.target || !self.target.parent) {
+ self.target = null;
+ var closestTarget = null;
+ var closestDistance = Infinity;
+ if (enemyBase && enemyBase.parent) {
+ var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
+ if (baseDistance <= self.range) {
+ closestTarget = enemyBase;
+ closestDistance = baseDistance;
+ }
+ }
+ for (var i = 0; i < enemyTowers.length; i++) {
+ var tower = enemyTowers[i];
+ if (tower && tower.parent) {
+ var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
+ if (towerDistance <= self.range && towerDistance < closestDistance) {
+ closestDistance = towerDistance;
+ closestTarget = tower;
+ }
+ }
+ }
+ self.target = closestTarget;
+ }
+ if (self.target && self.shootCooldown <= 0) {
+ var bullet = new UnitBullet();
+ bullet.x = self.x;
+ bullet.y = self.y;
+ bullet.target = self.target;
+ bullet.damage = self.damage;
+ unitBullets.push(bullet);
+ game.addChild(bullet);
+ LK.getSound('shoot').play();
+ self.shootCooldown = 75;
+ }
+ if (self.shootCooldown > 0) {
+ self.shootCooldown--;
+ }
+ if (!self.target) {
+ self.followPath();
+ }
+ };
+ self.takeDamage = function (damage) {
+ self.health -= damage;
+ self.updateHealthBar();
+ if (self.health <= 0) {
+ return true;
+ }
+ return false;
+ };
+ return self;
+});
var EnemyBase = Container.expand(function () {
var self = Container.call(this);
var baseGraphics = self.attachAsset('enemyBase', {
anchorX: 0.5,
@@ -393,8 +534,149 @@
return false;
};
return self;
});
+var FastUnit = Container.expand(function () {
+ var self = Container.call(this);
+ var unitGraphics = self.attachAsset('attackUnit', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ unitGraphics.tint = 0x32CD32; // Lime green color for fast unit
+ unitGraphics.scaleX = 0.8;
+ unitGraphics.scaleY = 0.8;
+ // Health bar background
+ var healthBarBg = self.attachAsset('healthBarBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBarBg.x = 0;
+ healthBarBg.y = -32;
+ healthBarBg.scaleX = 0.7;
+ // Health bar foreground
+ var healthBar = self.attachAsset('healthBar', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBar.x = 0;
+ healthBar.y = -32;
+ healthBar.scaleX = 0.7;
+ self.updateHealthBar = function () {
+ var healthPercent = self.health / self.maxHealth;
+ healthBar.scaleX = healthPercent * 0.7;
+ if (healthPercent > 0.6) {
+ healthBar.tint = 0x00ff00;
+ } else if (healthPercent > 0.3) {
+ healthBar.tint = 0xffff00;
+ } else {
+ healthBar.tint = 0xff0000;
+ }
+ };
+ self.unitType = 'fast';
+ self.health = 60;
+ self.maxHealth = 60;
+ self.speed = 4;
+ self.damage = 15;
+ self.range = 120;
+ self.cost = 60;
+ self.shootCooldown = 0;
+ self.target = null;
+ self.lastX = 0;
+ self.lastY = 0;
+ self.currentWaypoint = 0;
+ self.isMovingToWaypoint = false;
+ self.pathWaypoints = [{
+ x: 400,
+ y: self.y
+ }, {
+ x: 700,
+ y: 1200
+ }, {
+ x: 1000,
+ y: 800
+ }, {
+ x: 1300,
+ y: 1400
+ }, {
+ x: 1600,
+ y: 1000
+ }];
+ self.followPath = function () {
+ if (self.isMovingToWaypoint) return;
+ if (self.currentWaypoint >= self.pathWaypoints.length) {
+ self.x += self.speed;
+ return;
+ }
+ var waypoint = self.pathWaypoints[self.currentWaypoint];
+ var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
+ var duration = distance / self.speed * 16.67;
+ self.isMovingToWaypoint = true;
+ tween(self, {
+ x: waypoint.x,
+ y: waypoint.y
+ }, {
+ duration: duration,
+ easing: tween.linear,
+ onFinish: function onFinish() {
+ self.currentWaypoint++;
+ self.isMovingToWaypoint = false;
+ }
+ });
+ };
+ self.update = function () {
+ self.lastX = self.x;
+ self.lastY = self.y;
+ if (!self.target || !self.target.parent) {
+ self.target = null;
+ var closestTarget = null;
+ var closestDistance = Infinity;
+ if (enemyBase && enemyBase.parent) {
+ var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
+ if (baseDistance <= self.range) {
+ closestTarget = enemyBase;
+ closestDistance = baseDistance;
+ }
+ }
+ for (var i = 0; i < enemyTowers.length; i++) {
+ var tower = enemyTowers[i];
+ if (tower && tower.parent) {
+ var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
+ if (towerDistance <= self.range && towerDistance < closestDistance) {
+ closestDistance = towerDistance;
+ closestTarget = tower;
+ }
+ }
+ }
+ self.target = closestTarget;
+ }
+ if (self.target && self.shootCooldown <= 0) {
+ var bullet = new UnitBullet();
+ bullet.x = self.x;
+ bullet.y = self.y;
+ bullet.target = self.target;
+ bullet.damage = self.damage;
+ unitBullets.push(bullet);
+ game.addChild(bullet);
+ LK.getSound('shoot').play();
+ self.shootCooldown = 45; // Faster shooting
+ }
+ if (self.shootCooldown > 0) {
+ self.shootCooldown--;
+ }
+ if (!self.target) {
+ self.followPath();
+ }
+ };
+ self.takeDamage = function (damage) {
+ self.health -= damage;
+ self.updateHealthBar();
+ if (self.health <= 0) {
+ return true;
+ }
+ return false;
+ };
+ return self;
+});
var HeavyTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('enemyTower', {
anchorX: 0.5,
@@ -452,8 +734,149 @@
return false;
};
return self;
});
+var HeavyUnit = Container.expand(function () {
+ var self = Container.call(this);
+ var unitGraphics = self.attachAsset('attackUnit', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ unitGraphics.tint = 0x8B4513; // Brown color for heavy unit
+ unitGraphics.scaleX = 1.2;
+ unitGraphics.scaleY = 1.2;
+ // Health bar background
+ var healthBarBg = self.attachAsset('healthBarBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBarBg.x = 0;
+ healthBarBg.y = -45;
+ healthBarBg.scaleX = 1.0;
+ // Health bar foreground
+ var healthBar = self.attachAsset('healthBar', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBar.x = 0;
+ healthBar.y = -45;
+ healthBar.scaleX = 1.0;
+ self.updateHealthBar = function () {
+ var healthPercent = self.health / self.maxHealth;
+ healthBar.scaleX = healthPercent * 1.0;
+ if (healthPercent > 0.6) {
+ healthBar.tint = 0x00ff00;
+ } else if (healthPercent > 0.3) {
+ healthBar.tint = 0xffff00;
+ } else {
+ healthBar.tint = 0xff0000;
+ }
+ };
+ self.unitType = 'heavy';
+ self.health = 150;
+ self.maxHealth = 150;
+ self.speed = 1;
+ self.damage = 40;
+ self.range = 160;
+ self.cost = 90;
+ self.shootCooldown = 0;
+ self.target = null;
+ self.lastX = 0;
+ self.lastY = 0;
+ self.currentWaypoint = 0;
+ self.isMovingToWaypoint = false;
+ self.pathWaypoints = [{
+ x: 400,
+ y: self.y
+ }, {
+ x: 700,
+ y: 1200
+ }, {
+ x: 1000,
+ y: 800
+ }, {
+ x: 1300,
+ y: 1400
+ }, {
+ x: 1600,
+ y: 1000
+ }];
+ self.followPath = function () {
+ if (self.isMovingToWaypoint) return;
+ if (self.currentWaypoint >= self.pathWaypoints.length) {
+ self.x += self.speed;
+ return;
+ }
+ var waypoint = self.pathWaypoints[self.currentWaypoint];
+ var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
+ var duration = distance / self.speed * 16.67;
+ self.isMovingToWaypoint = true;
+ tween(self, {
+ x: waypoint.x,
+ y: waypoint.y
+ }, {
+ duration: duration,
+ easing: tween.linear,
+ onFinish: function onFinish() {
+ self.currentWaypoint++;
+ self.isMovingToWaypoint = false;
+ }
+ });
+ };
+ self.update = function () {
+ self.lastX = self.x;
+ self.lastY = self.y;
+ if (!self.target || !self.target.parent) {
+ self.target = null;
+ var closestTarget = null;
+ var closestDistance = Infinity;
+ if (enemyBase && enemyBase.parent) {
+ var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
+ if (baseDistance <= self.range) {
+ closestTarget = enemyBase;
+ closestDistance = baseDistance;
+ }
+ }
+ for (var i = 0; i < enemyTowers.length; i++) {
+ var tower = enemyTowers[i];
+ if (tower && tower.parent) {
+ var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
+ if (towerDistance <= self.range && towerDistance < closestDistance) {
+ closestDistance = towerDistance;
+ closestTarget = tower;
+ }
+ }
+ }
+ self.target = closestTarget;
+ }
+ if (self.target && self.shootCooldown <= 0) {
+ var bullet = new UnitBullet();
+ bullet.x = self.x;
+ bullet.y = self.y;
+ bullet.target = self.target;
+ bullet.damage = self.damage;
+ unitBullets.push(bullet);
+ game.addChild(bullet);
+ LK.getSound('shoot').play();
+ self.shootCooldown = 100; // Slower shooting
+ }
+ if (self.shootCooldown > 0) {
+ self.shootCooldown--;
+ }
+ if (!self.target) {
+ self.followPath();
+ }
+ };
+ self.takeDamage = function (damage) {
+ self.health -= damage;
+ self.updateHealthBar();
+ if (self.health <= 0) {
+ return true;
+ }
+ return false;
+ };
+ return self;
+});
var SniperTower = Container.expand(function () {
var self = Container.call(this);
var towerGraphics = self.attachAsset('enemyTower', {
anchorX: 0.5,
@@ -511,8 +934,152 @@
return false;
};
return self;
});
+var SniperUnit = Container.expand(function () {
+ var self = Container.call(this);
+ var unitGraphics = self.attachAsset('attackUnit', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ unitGraphics.tint = 0x4B0082; // Indigo color for sniper unit
+ unitGraphics.scaleX = 1.0;
+ unitGraphics.scaleY = 1.3; // Taller appearance
+ // Health bar background
+ var healthBarBg = self.attachAsset('healthBarBg', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBarBg.x = 0;
+ healthBarBg.y = -42;
+ healthBarBg.scaleX = 0.9;
+ // Health bar foreground
+ var healthBar = self.attachAsset('healthBar', {
+ anchorX: 0.5,
+ anchorY: 0.5
+ });
+ healthBar.x = 0;
+ healthBar.y = -42;
+ healthBar.scaleX = 0.9;
+ self.updateHealthBar = function () {
+ var healthPercent = self.health / self.maxHealth;
+ healthBar.scaleX = healthPercent * 0.9;
+ if (healthPercent > 0.6) {
+ healthBar.tint = 0x00ff00;
+ } else if (healthPercent > 0.3) {
+ healthBar.tint = 0xffff00;
+ } else {
+ healthBar.tint = 0xff0000;
+ }
+ };
+ self.unitType = 'sniper';
+ self.health = 70;
+ self.maxHealth = 70;
+ self.speed = 1.5;
+ self.damage = 50;
+ self.range = 250; // Very long range
+ self.cost = 80;
+ self.shootCooldown = 0;
+ self.target = null;
+ self.lastX = 0;
+ self.lastY = 0;
+ self.currentWaypoint = 0;
+ self.isMovingToWaypoint = false;
+ self.pathWaypoints = [{
+ x: 400,
+ y: self.y
+ }, {
+ x: 700,
+ y: 1200
+ }, {
+ x: 1000,
+ y: 800
+ }, {
+ x: 1300,
+ y: 1400
+ }, {
+ x: 1600,
+ y: 1000
+ }];
+ self.followPath = function () {
+ if (self.isMovingToWaypoint) return;
+ if (self.currentWaypoint >= self.pathWaypoints.length) {
+ self.x += self.speed;
+ return;
+ }
+ var waypoint = self.pathWaypoints[self.currentWaypoint];
+ var distance = Math.sqrt(Math.pow(waypoint.x - self.x, 2) + Math.pow(waypoint.y - self.y, 2));
+ var duration = distance / self.speed * 16.67;
+ self.isMovingToWaypoint = true;
+ tween(self, {
+ x: waypoint.x,
+ y: waypoint.y
+ }, {
+ duration: duration,
+ easing: tween.linear,
+ onFinish: function onFinish() {
+ self.currentWaypoint++;
+ self.isMovingToWaypoint = false;
+ }
+ });
+ };
+ self.update = function () {
+ self.lastX = self.x;
+ self.lastY = self.y;
+ if (!self.target || !self.target.parent) {
+ self.target = null;
+ var closestTarget = null;
+ var closestDistance = Infinity;
+ // Prioritize towers over base due to long range
+ for (var i = 0; i < enemyTowers.length; i++) {
+ var tower = enemyTowers[i];
+ if (tower && tower.parent) {
+ var towerDistance = Math.sqrt(Math.pow(tower.x - self.x, 2) + Math.pow(tower.y - self.y, 2));
+ if (towerDistance <= self.range && towerDistance < closestDistance) {
+ closestDistance = towerDistance;
+ closestTarget = tower;
+ }
+ }
+ }
+ // Check base if no towers in range
+ if (!closestTarget && enemyBase && enemyBase.parent) {
+ var baseDistance = Math.sqrt(Math.pow(enemyBase.x - self.x, 2) + Math.pow(enemyBase.y - self.y, 2));
+ if (baseDistance <= self.range) {
+ closestTarget = enemyBase;
+ closestDistance = baseDistance;
+ }
+ }
+ self.target = closestTarget;
+ }
+ if (self.target && self.shootCooldown <= 0) {
+ var bullet = new UnitBullet();
+ bullet.x = self.x;
+ bullet.y = self.y;
+ bullet.target = self.target;
+ bullet.damage = self.damage;
+ bullet.speed = 12; // Faster bullet
+ unitBullets.push(bullet);
+ game.addChild(bullet);
+ LK.getSound('shoot').play();
+ self.shootCooldown = 120; // Slow shooting rate
+ }
+ if (self.shootCooldown > 0) {
+ self.shootCooldown--;
+ }
+ if (!self.target) {
+ self.followPath();
+ }
+ };
+ self.takeDamage = function (damage) {
+ self.health -= damage;
+ self.updateHealthBar();
+ if (self.health <= 0) {
+ return true;
+ }
+ return false;
+ };
+ return self;
+});
var TowerBullet = Container.expand(function () {
var self = Container.call(this);
var bulletGraphics = self.attachAsset('towerBullet', {
anchorX: 0.5,
@@ -653,20 +1220,61 @@
fill: 0xFFFFFF
});
healthText.anchor.set(0.5, 0);
LK.gui.top.addChild(healthText);
-var spawnButton = new Text2('SPAWN UNIT (50 coins)', {
- size: 50,
- fill: 0x00FF00
+// Unit spawn buttons
+var basicButton = new Text2('Basic (40)', {
+ size: 40,
+ fill: 0x87CEEB
});
-spawnButton.anchor.set(0.5, 1);
-spawnButton.interactive = true;
-spawnButton.down = function () {
- if (coins >= unitCost) {
- spawnUnit();
+basicButton.anchor.set(0.5, 1);
+basicButton.x = -300;
+basicButton.interactive = true;
+basicButton.down = function () {
+ if (coins >= 40) {
+ spawnUnit('basic');
}
};
-LK.gui.bottom.addChild(spawnButton);
+LK.gui.bottom.addChild(basicButton);
+var fastButton = new Text2('Fast (60)', {
+ size: 40,
+ fill: 0x32CD32
+});
+fastButton.anchor.set(0.5, 1);
+fastButton.x = -100;
+fastButton.interactive = true;
+fastButton.down = function () {
+ if (coins >= 60) {
+ spawnUnit('fast');
+ }
+};
+LK.gui.bottom.addChild(fastButton);
+var heavyButton = new Text2('Heavy (90)', {
+ size: 40,
+ fill: 0x8B4513
+});
+heavyButton.anchor.set(0.5, 1);
+heavyButton.x = 100;
+heavyButton.interactive = true;
+heavyButton.down = function () {
+ if (coins >= 90) {
+ spawnUnit('heavy');
+ }
+};
+LK.gui.bottom.addChild(heavyButton);
+var sniperButton = new Text2('Sniper (80)', {
+ size: 40,
+ fill: 0x4B0082
+});
+sniperButton.anchor.set(0.5, 1);
+sniperButton.x = 300;
+sniperButton.interactive = true;
+sniperButton.down = function () {
+ if (coins >= 80) {
+ spawnUnit('sniper');
+ }
+};
+LK.gui.bottom.addChild(sniperButton);
// Create enemy base
enemyBase = game.addChild(new EnemyBase());
enemyBase.x = 1800;
enemyBase.y = 2732 / 2;
@@ -695,14 +1303,38 @@
if (slot.placeTower()) {
placedTowers++;
}
}
-function spawnUnit() {
- if (coins >= unitCost) {
- coins -= unitCost;
+function spawnUnit(unitType) {
+ if (!unitType) unitType = 'basic';
+ var unit = null;
+ var cost = 40;
+ switch (unitType) {
+ case 'basic':
+ unit = new BasicUnit();
+ cost = 40;
+ break;
+ case 'fast':
+ unit = new FastUnit();
+ cost = 60;
+ break;
+ case 'heavy':
+ unit = new HeavyUnit();
+ cost = 90;
+ break;
+ case 'sniper':
+ unit = new SniperUnit();
+ cost = 80;
+ break;
+ default:
+ unit = new BasicUnit();
+ cost = 40;
+ break;
+ }
+ if (coins >= cost && unit) {
+ coins -= cost;
coinsText.setText('Coins: ' + coins);
- coinIcon.x = -coinsText.width - 60; // Update icon position
- var unit = new AttackUnit();
+ coinIcon.x = -coinsText.width - 60;
unit.x = 100;
unit.y = 500 + playerUnits.length % 5 * 150;
// Set custom waypoints based on spawn position to create varied paths
unit.pathWaypoints = [{
@@ -722,9 +1354,9 @@
y: 1366
}];
playerUnits.push(unit);
game.addChild(unit);
- unit.updateHealthBar(); // Initialize health bar display
+ unit.updateHealthBar();
LK.getSound('unitSpawn').play();
}
}
// Coin generation timer
@@ -865,10 +1497,10 @@
}
};
// Touch controls for spawning units
game.down = function (x, y, obj) {
- if (coins >= unitCost) {
- spawnUnit();
+ if (coins >= 40) {
+ spawnUnit('basic');
}
};
game.update = function () {
// Update enemy AI
coinIcon. In-Game asset. 2d. High contrast. No shadows
enemyBase. In-Game asset. 2d. High contrast. No shadows
enemyTower. In-Game asset. 2d. High contrast. No shadows
towerBullet. In-Game asset. 2d. High contrast. No shadows
towerSlot. In-Game asset. 2d. High contrast. No shadows
upgradeIndicator. In-Game asset. 2d. High contrast. No shadows
heavyTowerAsset. In-Game asset. 2d. High contrast. No shadows
fastTowerAsset. In-Game asset. 2d. High contrast. No shadows
healTowerAsset. In-Game asset. 2d. High contrast. No shadows
sniperTowerAsset. In-Game asset. 2d. High contrast. No shadows
areaTowerAsset. In-Game asset. 2d. High contrast. No shadows
meleeUnitAsset. In-Game asset. 2d. High contrast. No shadows
laserTowerAsset. In-Game asset. 2d. High contrast. No shadows
poisonTowerAsset. In-Game asset. 2d. High contrast. No shadows
slowTowerAsset. In-Game asset. 2d. High contrast. No shadows
resurrectionTowerAsset. In-Game asset. 2d. High contrast. No shadows