User prompt
Sabit bir yönde ilerlemesin, düşük sayıda olan birimler önlerinde ki diğer birliklerin kenarından geçebilsinler.
User prompt
Hızlı olan birimler, önlerine çıkan birimlerin etrafını dolaşsın ve önüne geçsin.
User prompt
Daha az askerler daha hızlı, daha çok askerler daha yavaş hareket etsin.
User prompt
Birim sayısı, birimlerin üst kısmında ilerlesin. birim sayısı satır satır ilerlemesin.
User prompt
birim sayısı, birimler ile hareket etsin. atlıyor gibi görünmesin. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'length')' in or related to this line: 'currentPath = null;' Line Number: 936
User prompt
Please fix the bug: 'Cannot read properties of undefined (reading 'length')' in or related to this line: 'for (var i = 0; i < movingUnitGroups.length; i++) {' Line Number: 938
User prompt
Hareket eden birimlerin üst kısmında, kaç adet birim var ise, kule birim sayısının boyutunda yazsın.
User prompt
Birimlerin hareket hızını %50 oranında yavaşlat.
User prompt
Fulloyun arkaplan resmi oluştur.
User prompt
Kulede ki birim sayısı, Kule resminin üst satırına denk gelsin. Ayrıca boyutunu %50 küçült.
User prompt
sorunu düzelt.
User prompt
Her düşmana ve diğer kule birimine özel resim dosyası oluştur.
User prompt
Hiç bir yapay zeka 100 olarak birim olmasın, 100'e yaklaşınca harekete geçsin
User prompt
Reset yapınca baştan başlasın, game over olunca aynı seviyeden başlasın.
User prompt
Please fix the bug: 'Error: Error: Invalid color format. Expected 0xRRGGBB format, received: undefined' in or related to this line: 'tween(towerGraphics, {' Line Number: 140 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Error: Error: Invalid color format. Expected 0xRRGGBB format, received: undefined' in or related to this line: 'tween(towerGraphics, {' Line Number: 138 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'tween(tower.children[i], {' Line Number: 354 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'tween(tower.children[i], {' Line Number: 353 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Please fix the bug: 'Script error.' in or related to this line: 'tween(tower.children[i], {' Line Number: 353 ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Sahip olduğumuz kulenin üzerine tıklayınca; Kule Türleri ve özelleştirme seçebildiğimiz bir açılır pencere ekle. **Özel Kule Tipleri**: - **Savunma Kulesi**: Daha dayanıklı, saldırılara karşı dirençli. - **Hızlı Üretim Kulesi**: Birim üretimi daha hızlı ama savunması zayıf. - **Güçlü Saldırı Kulesi**: Gönderdiği birimler daha güçlü ama üretimi yavaş. - **Ekonomi Kulesi**: Kaynak üretimi yapan, savaşmayan özel kule. Seçilen özel kule tipi, kaybedilene kadar bir daha değiştirilmesin. ↪💡 Consider importing and using the following plugins: @upit/tween.v1
User prompt
Game over olunca, kaldığı seviye yeniden başlasın.
User prompt
panel içinde ki Sıralama; Birim sayısına göre anlık olarak alt alta hizalansın.
User prompt
Panelde, ''Rakip'' yerine, rakibin ismi yazsın. Ayrıca ''0'' olan rakibin üstü çizilsin. ayrıca birim sayısına göre sıralansın.
User prompt
Ekranın sol altına, oyuncu ve rakiplerin toplam birim sayıları yazan bir panel oluştur.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var MovingUnitGroup = Container.expand(function () {
var self = Container.call(this);
self.owner = 1;
self.units = [];
self.lastUpdateTime = 0;
// Create count text with same size as tower unit count
self.countText = new Text2('0', {
size: 30,
fill: 0xFFFFFF
});
self.countText.anchor.set(0.5, 0.5);
self.addChild(self.countText);
self.updateGroup = function (nearbyUnits) {
self.units = nearbyUnits;
self.countText.setText(nearbyUnits.length);
// Calculate center position of the group
if (nearbyUnits.length > 0) {
var totalX = 0;
var totalY = 0;
for (var i = 0; i < nearbyUnits.length; i++) {
totalX += nearbyUnits[i].x;
totalY += nearbyUnits[i].y;
}
self.x = totalX / nearbyUnits.length;
self.y = totalY / nearbyUnits.length - 25; // Position above units
self.visible = true;
} else {
self.visible = false;
}
self.lastUpdateTime = LK.ticks;
};
// Add continuous update method to track unit movement
self.update = function () {
if (self.units && self.units.length > 0) {
var totalX = 0;
var totalY = 0;
var validUnits = 0;
// Calculate new center position based on current unit positions
for (var i = 0; i < self.units.length; i++) {
if (self.units[i] && self.units[i].parent) {
totalX += self.units[i].x;
totalY += self.units[i].y;
validUnits++;
}
}
// Update position if we have valid units
if (validUnits > 0) {
self.x = totalX / validUnits;
self.y = totalY / validUnits - 25; // Position above units
self.visible = true;
} else {
self.visible = false;
}
}
};
self.isStale = function () {
return LK.ticks - self.lastUpdateTime > 60; // 1 second at 60fps
};
return self;
});
var PathLine = Container.expand(function () {
var self = Container.call(this);
self.points = [];
self.graphics = [];
self.setPath = function (points) {
// Clear old graphics
for (var i = 0; i < self.graphics.length; i++) {
self.graphics[i].destroy();
}
self.graphics = [];
self.points = points;
// Draw new path
for (var i = 0; i < points.length - 1; i++) {
var dist = Math.sqrt(Math.pow(points[i + 1].x - points[i].x, 2) + Math.pow(points[i + 1].y - points[i].y, 2));
var segments = Math.floor(dist / 20);
for (var j = 0; j < segments; j++) {
var t = j / segments;
var dot;
if (memoryManager.pathDotPool.length > 0) {
dot = memoryManager.pathDotPool.pop();
dot.visible = true;
dot.alpha = 0.5;
self.addChild(dot);
} else {
dot = self.attachAsset('path', {
anchorX: 0.5,
anchorY: 0.5
});
}
dot.x = points[i].x + (points[i + 1].x - points[i].x) * t;
dot.y = points[i].y + (points[i + 1].y - points[i].y) * t;
dot.alpha = 0.5;
self.graphics.push(dot);
}
}
};
self.clear = function () {
for (var i = 0; i < self.graphics.length; i++) {
var dot = self.graphics[i];
if (dot && dot.parent) {
dot.parent.removeChild(dot);
dot.visible = false;
memoryManager.pathDotPool.push(dot);
}
}
self.graphics = [];
self.points = [];
};
return self;
});
var Tower = Container.expand(function () {
var self = Container.call(this);
self.owner = 0; // 0 = neutral, 1 = player, 2 = enemy
self.unitCount = 0;
self.maxUnits = 100;
self.spawnRate = 30; // ticks between spawns
self.lastSpawn = 0;
// Initialize with neutral tower, will be updated by setOwner
var towerGraphics = self.attachAsset('neutralTower', {
anchorX: 0.5,
anchorY: 0.5
});
self.countText = new Text2('0', {
size: 30,
fill: 0xFFFFFF
});
self.countText.anchor.set(0.5, 1);
self.countText.y = -85; // Position above tower graphic
self.addChild(self.countText);
// Enemy name text (positioned below tower)
self.nameText = new Text2('', {
size: 40,
fill: 0xFFFFFF
});
self.nameText.anchor.set(0.5, 0);
self.nameText.y = 90; // Position below the tower
self.addChild(self.nameText);
self.setOwner = function (newOwner) {
var oldOwner = self.owner;
self.owner = newOwner;
var targetAsset;
if (newOwner === 0) {
targetAsset = 'neutralTower';
} else if (newOwner === 1) {
targetAsset = 'playerTower';
} else if (newOwner === 2) {
targetAsset = 'enemyTower1';
} else if (newOwner === 3) {
targetAsset = 'enemyTower2';
} else if (newOwner === 4) {
targetAsset = 'enemyTower3';
} else {
targetAsset = 'enemyTower1'; // default enemy red
}
// Replace tower graphics with new asset
if (oldOwner !== newOwner) {
self.removeChild(towerGraphics);
towerGraphics = self.attachAsset(targetAsset, {
anchorX: 0.5,
anchorY: 0.5
});
// Ensure text stays on top of the new graphics
self.removeChild(self.countText);
self.addChild(self.countText);
self.removeChild(self.nameText);
self.addChild(self.nameText);
}
// Update name display
if (newOwner >= 2) {
// Enemy tower - show name
var enemyName = assignEnemyName(newOwner);
self.nameText.setText(enemyName);
self.nameText.alpha = 1;
} else if (newOwner === 1) {
// Player tower - show "Player"
self.nameText.setText('Player');
self.nameText.alpha = 1;
} else {
// Neutral tower - hide name
self.nameText.setText('');
self.nameText.alpha = 0;
}
// Graphics already updated above with new asset
};
self.addUnits = function (count) {
self.unitCount = Math.min(self.unitCount + count, self.maxUnits);
self.countText.setText(Math.floor(self.unitCount));
};
self.removeUnits = function (count) {
self.unitCount = Math.max(0, self.unitCount - count);
self.countText.setText(Math.floor(self.unitCount));
return count;
};
self.update = function () {
if (self.owner > 0) {
// Calculate dynamic spawn rate based on unit count
// More units = faster production (lower spawn rate number)
// Base rate is 30, reduced by unit count factor
var dynamicSpawnRate = Math.max(10, self.spawnRate - Math.floor(self.unitCount / 10));
if (LK.ticks - self.lastSpawn > dynamicSpawnRate) {
self.addUnits(1);
self.lastSpawn = LK.ticks;
}
}
};
return self;
});
var Unit = Container.expand(function () {
var self = Container.call(this);
self.owner = 1;
self.speed = 3.0; // Will be dynamically calculated during update
self.targetTower = null;
self.pathIndex = 0;
self.path = [];
// Initialize with player unit, will be updated by setOwner
var unitGraphics = self.attachAsset('playerUnit', {
anchorX: 0.5,
anchorY: 0.5
});
self.setOwner = function (owner) {
self.owner = owner;
var targetAsset;
if (owner === 1) {
targetAsset = 'playerUnit';
} else if (owner === 2) {
targetAsset = 'enemyUnit1';
} else if (owner === 3) {
targetAsset = 'enemyUnit2';
} else if (owner === 4) {
targetAsset = 'enemyUnit3';
} else {
targetAsset = 'enemyUnit1'; // default enemy red
}
// Replace unit graphics with new asset
self.removeChild(unitGraphics);
unitGraphics = self.attachAsset(targetAsset, {
anchorX: 0.5,
anchorY: 0.5
});
};
self.setPath = function (path, target) {
// Add random offset to create scattered movement
var scatterRange = 40; // Maximum scatter distance
var offsetX = (Math.random() - 0.5) * scatterRange;
var offsetY = (Math.random() - 0.5) * scatterRange;
// Create scattered path by adding random offsets to each point
self.path = [];
for (var i = 0; i < path.length; i++) {
// Apply consistent offset throughout the path, but reduce it near the end
var offsetFactor = Math.max(0.2, 1 - i / path.length * 0.8);
self.path.push({
x: path[i].x + offsetX * offsetFactor,
y: path[i].y + offsetY * offsetFactor
});
}
self.targetTower = target;
self.pathIndex = 0;
if (self.path.length > 0) {
self.x = self.path[0].x;
self.y = self.path[0].y;
}
};
self.lastWasIntersecting = false;
self.avoidanceOffset = {
x: 0,
y: 0
}; // Track avoidance movement
self.avoidanceTimer = 0; // Timer for avoidance behavior
self.update = function () {
if (!self.targetTower || self.pathIndex >= self.path.length) {
return;
}
// Calculate dynamic speed based on nearby units of same owner
var nearbyUnits = 1; // Count self
for (var i = 0; i < units.length; i++) {
var otherUnit = units[i];
if (otherUnit !== self && otherUnit.owner === self.owner) {
var dx = self.x - otherUnit.x;
var dy = self.y - otherUnit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
// Same grouping distance as display
nearbyUnits++;
}
}
}
// Calculate speed: fewer units = faster, more units = slower
// Base speed of 3.0, reduced by group size
// 1 unit: 3.0 speed, 10 units: 1.5 speed, 20+ units: 0.75 speed
var dynamicSpeed = Math.max(0.75, 3.0 - (nearbyUnits - 1) * 0.15);
self.speed = dynamicSpeed;
// Edge-based movement system for smaller groups to avoid larger groups
var needsAvoidance = false;
var avoidanceDirection = {
x: 0,
y: 0
};
var largerGroupAhead = null;
// Check for larger groups ahead that need to be avoided
for (var i = 0; i < units.length; i++) {
var otherUnit = units[i];
if (otherUnit !== self && otherUnit.owner === self.owner) {
var dx = otherUnit.x - self.x;
var dy = otherUnit.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Check if unit is ahead and close
if (distance < 120 && distance > 0) {
// Calculate if other unit is in front (same general direction)
var target = self.path[self.pathIndex];
if (target) {
var toTargetX = target.x - self.x;
var toTargetY = target.y - self.y;
var toOtherX = otherUnit.x - self.x;
var toOtherY = otherUnit.y - self.y;
// Normalize vectors
var targetLength = Math.sqrt(toTargetX * toTargetX + toTargetY * toTargetY);
var otherLength = Math.sqrt(toOtherX * toOtherX + toOtherY * toOtherY);
if (targetLength > 0 && otherLength > 0) {
toTargetX /= targetLength;
toTargetY /= targetLength;
toOtherX /= otherLength;
toOtherY /= otherLength;
// Check if other unit is in same direction (dot product > 0.3 for wider detection)
var dotProduct = toTargetX * toOtherX + toTargetY * toOtherY;
if (dotProduct > 0.3) {
// Count nearby units for both this unit and the other unit
var otherNearbyUnits = 1;
for (var j = 0; j < units.length; j++) {
var checkUnit = units[j];
if (checkUnit !== otherUnit && checkUnit.owner === otherUnit.owner) {
var checkDx = otherUnit.x - checkUnit.x;
var checkDy = otherUnit.y - checkUnit.y;
var checkDistance = Math.sqrt(checkDx * checkDx + checkDy * checkDy);
if (checkDistance < 100) {
otherNearbyUnits++;
}
}
}
// If this unit is in a smaller group, it should go around the larger group
if (nearbyUnits < otherNearbyUnits) {
needsAvoidance = true;
largerGroupAhead = otherUnit;
// Calculate edge-based avoidance direction
// Try to go around the edge of the larger group
var perpX = -toTargetY; // Perpendicular to movement direction
var perpY = toTargetX;
// Choose the side with less obstruction or better path to target
var leftSideX = self.x + perpX * 80;
var leftSideY = self.y + perpY * 80;
var rightSideX = self.x - perpX * 80;
var rightSideY = self.y - perpY * 80;
// Check which side has less units blocking
var leftBlockCount = 0;
var rightBlockCount = 0;
for (var k = 0; k < units.length; k++) {
var blockUnit = units[k];
if (blockUnit !== self && blockUnit.owner === self.owner) {
var leftDist = Math.sqrt(Math.pow(blockUnit.x - leftSideX, 2) + Math.pow(blockUnit.y - leftSideY, 2));
var rightDist = Math.sqrt(Math.pow(blockUnit.x - rightSideX, 2) + Math.pow(blockUnit.y - rightSideY, 2));
if (leftDist < 60) leftBlockCount++;
if (rightDist < 60) rightBlockCount++;
}
}
// Choose the side with fewer blocking units
var sideChoice = leftBlockCount <= rightBlockCount ? 1 : -1;
avoidanceDirection.x = perpX * sideChoice;
avoidanceDirection.y = perpY * sideChoice;
break;
}
}
}
}
}
}
}
// Apply edge-based avoidance behavior
if (needsAvoidance && largerGroupAhead) {
self.avoidanceTimer = 90; // Avoid for 1.5 seconds (longer to go around)
self.avoidanceOffset.x = avoidanceDirection.x * 60; // Larger offset for edge movement
self.avoidanceOffset.y = avoidanceDirection.y * 60;
} else if (self.avoidanceTimer > 0) {
// Continue avoidance movement but gradually return to path
self.avoidanceTimer--;
if (self.avoidanceTimer <= 30) {
// Start reducing avoidance offset in the last 0.5 seconds
var returnFactor = self.avoidanceTimer / 30.0;
self.avoidanceOffset.x *= returnFactor;
self.avoidanceOffset.y *= returnFactor;
if (self.avoidanceTimer <= 0) {
self.avoidanceOffset.x = 0;
self.avoidanceOffset.y = 0;
}
}
}
// Check for combat with enemy units
var currentIntersecting = false;
for (var i = 0; i < units.length; i++) {
var otherUnit = units[i];
if (otherUnit !== self && otherUnit.owner !== self.owner) {
// Check if units are close enough to fight (within 30 pixels)
var combatDx = self.x - otherUnit.x;
var combatDy = self.y - otherUnit.y;
var combatDist = Math.sqrt(combatDx * combatDx + combatDy * combatDy);
if (combatDist < 30) {
currentIntersecting = true;
// Combat occurs - both units destroy each other
if (!self.lastWasIntersecting) {
// Create combat sparkle effects at the battle location
var battleX = (self.x + otherUnit.x) / 2;
var battleY = (self.y + otherUnit.y) / 2;
// Create multiple sparkle particles
for (var sparkleIndex = 0; sparkleIndex < 8; sparkleIndex++) {
var sparkle = memoryManager.getSparkle();
game.addChild(sparkle);
sparkle.x = battleX + (Math.random() - 0.5) * 40;
sparkle.y = battleY + (Math.random() - 0.5) * 40;
sparkle.alpha = 1;
sparkle.scaleX = 0.5 + Math.random() * 0.5;
sparkle.scaleY = sparkle.scaleX;
sparkle.tint = 0xffff00 + Math.floor(Math.random() * 0x444444);
// Animate sparkles flying outward and fading
var randomAngle = Math.random() * Math.PI * 2;
var flyDistance = 50 + Math.random() * 30;
var targetX = sparkle.x + Math.cos(randomAngle) * flyDistance;
var targetY = sparkle.y + Math.sin(randomAngle) * flyDistance;
tween(sparkle, {
x: targetX,
y: targetY,
alpha: 0,
scaleX: 0,
scaleY: 0
}, {
duration: 300 + Math.random() * 200,
easing: tween.easeOut,
onFinish: function onFinish() {
if (sparkle && sparkle.parent) {
memoryManager.recycleSparkle(sparkle);
}
}
});
}
// Flash both units red before destroying
LK.effects.flashObject(self, 0xff0000, 200);
LK.effects.flashObject(otherUnit, 0xff0000, 200);
LK.getSound('combat').play();
// Destroy both units after flash
LK.setTimeout(function () {
if (self && self.parent) {
memoryManager.recycleUnit(self);
var selfIndex = units.indexOf(self);
if (selfIndex !== -1) {
units.splice(selfIndex, 1);
}
}
if (otherUnit && otherUnit.parent) {
memoryManager.recycleUnit(otherUnit);
var otherIndex = units.indexOf(otherUnit);
if (otherIndex !== -1) {
units.splice(otherIndex, 1);
}
}
}, 200);
return;
}
}
}
}
self.lastWasIntersecting = currentIntersecting;
var target = self.path[self.pathIndex];
var dx = target.x - self.x;
var dy = target.y - self.y;
var dist = Math.sqrt(dx * dx + dy * dy);
// Apply avoidance offset to target position
var effectiveTargetX = target.x + self.avoidanceOffset.x;
var effectiveTargetY = target.y + self.avoidanceOffset.y;
dx = effectiveTargetX - self.x;
dy = effectiveTargetY - self.y;
dist = Math.sqrt(dx * dx + dy * dy);
if (dist < self.speed) {
self.pathIndex++;
if (self.pathIndex >= self.path.length && self.targetTower) {
// Reached target tower
if (self.targetTower.owner === self.owner) {
self.targetTower.addUnits(1);
} else {
self.targetTower.unitCount--;
if (self.targetTower.unitCount < 0) {
self.targetTower.unitCount = 1;
self.targetTower.setOwner(self.owner);
LK.getSound('capture').play();
}
self.targetTower.countText.setText(Math.floor(Math.abs(self.targetTower.unitCount)));
}
memoryManager.recycleUnit(self);
units.splice(units.indexOf(self), 1);
}
} else {
// Calculate movement direction with avoidance
var moveX = dx / dist * self.speed;
var moveY = dy / dist * self.speed;
// Add small random deviation (5% of movement speed)
var deviation = self.speed * 0.05;
moveX += (Math.random() - 0.5) * deviation;
moveY += (Math.random() - 0.5) * deviation;
self.x += moveX;
self.y += moveY;
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
// Add background image to cover the full game area
var backgroundImage = game.attachAsset('gameBackground', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
// Center horizontally (2048/2)
y: 1366 // Center vertically (2732/2)
});
// Legacy shape assets for other elements
// Unit Assets - Different for each faction
// Tower Assets - Different types for each faction
var towers = [];
var units = [];
var movingUnitGroups = [];
var currentPath = null;
var selectedTower = null;
var isDragging = false;
var currentLevel = storage.currentLevel || 0;
var aiStartTime = null; // Track when AI should start (null = not started yet)
// Enemy names for different factions
var enemyNames = ["Karabekir", "Enver", "Talat", "Cemal", "İsmet", "Fevzi", "Kazım", "Refet", "Ali Fuat", "Rauf", "Bekir Sami", "Adnan", "Kâzım Özalp", "Salih", "Nureddin"];
var assignedEnemyNames = {}; // Track names assigned to each enemy faction
// Level configurations - 10 levels with progressive difficulty (scaled up 100%)
var levels = [{
// Level 1 - Tutorial
towers: [{
x: 300,
y: 1366,
owner: 1,
units: 30
}, {
x: 1024,
y: 800,
owner: 0,
units: 10
}, {
x: 1748,
y: 1366,
owner: 2,
units: 20
}, {
x: 1748,
y: 400,
owner: 3,
units: 15
}]
}, {
// Level 2 - Basic Strategy
towers: [{
x: 300,
y: 400,
owner: 1,
units: 35
}, {
x: 1024,
y: 1366,
owner: 0,
units: 15
}, {
x: 1748,
y: 400,
owner: 2,
units: 25
}, {
x: 1748,
y: 2332,
owner: 3,
units: 25
}, {
x: 300,
y: 2332,
owner: 4,
units: 20
}]
}, {
// Level 3 - Multi-Front
towers: [{
x: 300,
y: 1366,
owner: 1,
units: 45
}, {
x: 700,
y: 800,
owner: 0,
units: 15
}, {
x: 1348,
y: 800,
owner: 0,
units: 15
}, {
x: 700,
y: 1932,
owner: 0,
units: 15
}, {
x: 1348,
y: 1932,
owner: 0,
units: 15
}, {
x: 1748,
y: 400,
owner: 2,
units: 30
}, {
x: 1748,
y: 1366,
owner: 3,
units: 30
}, {
x: 1748,
y: 2332,
owner: 4,
units: 30
}]
}, {
// Level 4 - Defensive Challenge
towers: [{
x: 300,
y: 1366,
owner: 1,
units: 50
}, {
x: 600,
y: 600,
owner: 0,
units: 20
}, {
x: 1024,
y: 400,
owner: 2,
units: 40
}, {
x: 1448,
y: 600,
owner: 0,
units: 20
}, {
x: 1748,
y: 1366,
owner: 2,
units: 40
}, {
x: 1024,
y: 2100,
owner: 2,
units: 40
}]
}, {
// Level 5 - Resource Management
towers: [{
x: 200,
y: 800,
owner: 1,
units: 55
}, {
x: 200,
y: 1932,
owner: 1,
units: 55
}, {
x: 700,
y: 1366,
owner: 0,
units: 25
}, {
x: 1348,
y: 1366,
owner: 0,
units: 25
}, {
x: 1748,
y: 600,
owner: 2,
units: 45
}, {
x: 1748,
y: 1366,
owner: 2,
units: 45
}, {
x: 1748,
y: 2132,
owner: 2,
units: 45
}]
}, {
// Level 6 - Territory Control
towers: [{
x: 300,
y: 400,
owner: 1,
units: 60
}, {
x: 300,
y: 2332,
owner: 1,
units: 60
}, {
x: 600,
y: 800,
owner: 0,
units: 30
}, {
x: 1024,
y: 1366,
owner: 0,
units: 30
}, {
x: 1448,
y: 1932,
owner: 0,
units: 30
}, {
x: 1748,
y: 800,
owner: 2,
units: 50
}, {
x: 1748,
y: 1500,
owner: 2,
units: 50
}, {
x: 1748,
y: 2200,
owner: 2,
units: 50
}]
}, {
// Level 7 - Advanced Tactics
towers: [{
x: 200,
y: 1366,
owner: 1,
units: 65
}, {
x: 500,
y: 600,
owner: 0,
units: 35
}, {
x: 500,
y: 1366,
owner: 0,
units: 35
}, {
x: 500,
y: 2132,
owner: 0,
units: 35
}, {
x: 1024,
y: 400,
owner: 0,
units: 35
}, {
x: 1024,
y: 2332,
owner: 0,
units: 35
}, {
x: 1548,
y: 600,
owner: 0,
units: 35
}, {
x: 1548,
y: 1366,
owner: 0,
units: 35
}, {
x: 1548,
y: 2132,
owner: 0,
units: 35
}, {
x: 1848,
y: 1366,
owner: 2,
units: 55
}]
}, {
// Level 8 - Siege Warfare
towers: [{
x: 300,
y: 600,
owner: 1,
units: 70
}, {
x: 300,
y: 1366,
owner: 1,
units: 70
}, {
x: 300,
y: 2132,
owner: 1,
units: 70
}, {
x: 800,
y: 800,
owner: 0,
units: 40
}, {
x: 800,
y: 1932,
owner: 0,
units: 40
}, {
x: 1248,
y: 800,
owner: 0,
units: 40
}, {
x: 1248,
y: 1932,
owner: 0,
units: 40
}, {
x: 1650,
y: 400,
owner: 2,
units: 60
}, {
x: 1650,
y: 1366,
owner: 2,
units: 60
}, {
x: 1650,
y: 2332,
owner: 2,
units: 60
}]
}, {
// Level 9 - Final Challenge
towers: [{
x: 200,
y: 800,
owner: 1,
units: 75
}, {
x: 200,
y: 1932,
owner: 1,
units: 75
}, {
x: 600,
y: 400,
owner: 0,
units: 45
}, {
x: 600,
y: 1366,
owner: 0,
units: 45
}, {
x: 600,
y: 2332,
owner: 0,
units: 45
}, {
x: 1024,
y: 600,
owner: 0,
units: 45
}, {
x: 1024,
y: 2132,
owner: 0,
units: 45
}, {
x: 1448,
y: 400,
owner: 0,
units: 45
}, {
x: 1448,
y: 1366,
owner: 0,
units: 45
}, {
x: 1448,
y: 2332,
owner: 0,
units: 45
}, {
x: 1748,
y: 600,
owner: 2,
units: 65
}, {
x: 1748,
y: 1366,
owner: 2,
units: 65
}, {
x: 1748,
y: 2132,
owner: 2,
units: 65
}]
}, {
// Level 10 - Master's Trial
towers: [{
x: 150,
y: 1366,
owner: 1,
units: 80
}, {
x: 400,
y: 600,
owner: 0,
units: 50
}, {
x: 400,
y: 1100,
owner: 0,
units: 50
}, {
x: 400,
y: 1632,
owner: 0,
units: 50
}, {
x: 400,
y: 2132,
owner: 0,
units: 50
}, {
x: 800,
y: 400,
owner: 0,
units: 50
}, {
x: 800,
y: 800,
owner: 0,
units: 50
}, {
x: 800,
y: 1932,
owner: 0,
units: 50
}, {
x: 800,
y: 2332,
owner: 0,
units: 50
}, {
x: 1248,
y: 400,
owner: 0,
units: 50
}, {
x: 1248,
y: 800,
owner: 0,
units: 50
}, {
x: 1248,
y: 1932,
owner: 0,
units: 50
}, {
x: 1248,
y: 2332,
owner: 0,
units: 50
}, {
x: 1648,
y: 600,
owner: 0,
units: 50
}, {
x: 1648,
y: 1100,
owner: 0,
units: 50
}, {
x: 1648,
y: 1632,
owner: 0,
units: 50
}, {
x: 1648,
y: 2132,
owner: 0,
units: 50
}, {
x: 1898,
y: 1366,
owner: 2,
units: 70
}]
}];
// UI Elements
var levelText = new Text2('Level ' + (currentLevel + 1), {
size: 80,
fill: 0xFFFFFF
});
levelText.anchor.set(0.5, 0);
LK.gui.top.addChild(levelText);
function assignEnemyName(owner) {
if (!assignedEnemyNames[owner] && owner >= 2) {
// Get available names (not already assigned)
var availableNames = enemyNames.filter(function (name) {
return !Object.values(assignedEnemyNames).includes(name);
});
// If no available names, reuse from the pool
if (availableNames.length === 0) {
availableNames = enemyNames;
}
// Assign random name
var randomIndex = Math.floor(Math.random() * availableNames.length);
assignedEnemyNames[owner] = availableNames[randomIndex];
}
return assignedEnemyNames[owner] || "";
}
function loadLevel(levelIndex) {
// Initialize movingUnitGroups array if undefined
if (!movingUnitGroups) {
movingUnitGroups = [];
}
// Clear existing game objects
for (var i = 0; i < towers.length; i++) {
towers[i].destroy();
}
for (var i = 0; i < units.length; i++) {
units[i].destroy();
}
towers = [];
units = [];
if (currentPath) {
currentPath.destroy();
currentPath = null;
}
// Clean up moving unit groups
for (var i = 0; i < movingUnitGroups.length; i++) {
if (movingUnitGroups[i].parent) {
movingUnitGroups[i].destroy();
}
}
movingUnitGroups = [];
// Reset game state variables
selectedTower = null;
isDragging = false;
// Reset enemy name assignments
assignedEnemyNames = {};
// Load new level
var levelData = levels[levelIndex % levels.length];
for (var i = 0; i < levelData.towers.length; i++) {
var towerData = levelData.towers[i];
var tower = new Tower();
tower.x = towerData.x;
tower.y = towerData.y;
tower.setOwner(towerData.owner);
tower.addUnits(towerData.units);
towers.push(tower);
game.addChild(tower);
}
levelText.setText('Level ' + (levelIndex + 1));
// Set AI to start after 5 seconds (5000ms)
aiStartTime = LK.ticks + 5 * 60; // 5 seconds at 60 FPS
}
function createPath(start, end) {
var points = [];
var steps = 20;
for (var i = 0; i <= steps; i++) {
points.push({
x: start.x + (end.x - start.x) * (i / steps),
y: start.y + (end.y - start.y) * (i / steps)
});
}
return points;
}
function sendUnits(fromTower, toTower, count) {
var path = createPath(fromTower, toTower);
var unitsToSend = Math.min(count, fromTower.unitCount);
for (var i = 0; i < unitsToSend; i++) {
var unit = memoryManager.getUnit();
unit.setOwner(fromTower.owner);
unit.setPath(path, toTower);
units.push(unit);
game.addChild(unit);
// Add random spawn delay and position offset for scattered deployment
var randomDelay = i * (30 + Math.random() * 40); // Random delay between 30-70ms per unit
var spawnOffsetX = (Math.random() - 0.5) * 60; // Random spawn position offset
var spawnOffsetY = (Math.random() - 0.5) * 60;
// Position unit at spawn location with offset
unit.x = fromTower.x + spawnOffsetX;
unit.y = fromTower.y + spawnOffsetY;
// Stagger unit spawning with random timing
tween(unit, {
x: path[0].x,
y: path[0].y
}, {
duration: randomDelay,
onFinish: function onFinish() {
LK.getSound('deploy').play();
}
});
}
fromTower.removeUnits(unitsToSend);
}
function checkWinCondition() {
var playerTowers = 0;
var enemyTowers = 0;
for (var i = 0; i < towers.length; i++) {
if (towers[i].owner === 1) playerTowers++;
if (towers[i].owner >= 2) enemyTowers++;
}
if (enemyTowers === 0) {
// Win condition - player defeated all enemy factions
currentLevel++;
storage.currentLevel = currentLevel;
LK.showYouWin();
} else if (playerTowers === 0) {
// Lose condition - restart current level
LK.showGameOver();
}
}
// Multi-faction AI with sophisticated strategies
function runAI() {
// Don't run AI until 5 seconds have passed
if (aiStartTime && LK.ticks < aiStartTime) {
return;
}
// Analyze game state for strategic decisions
var gameState = analyzeGameState();
for (var i = 0; i < towers.length; i++) {
var tower = towers[i];
if (tower.owner >= 2) {
// Force action when AI towers approach 100 units (at 80+ units)
if (tower.unitCount >= 80) {
// Find any nearby target to attack immediately
var emergencyTarget = findNearestEnemy(tower);
if (emergencyTarget) {
// Send most units to prevent reaching 100
var unitsToSend = Math.floor(tower.unitCount * 0.7);
if (unitsToSend > 0) {
sendUnits(tower, emergencyTarget, unitsToSend);
}
}
} else {
// Normal strategic behavior for towers under 80 units
var strategy = getOptimalStrategy(tower, gameState);
executeStrategy(tower, strategy, gameState);
}
}
}
}
// Analyze current game state for AI decision making
function analyzeGameState() {
var state = {
playerStrength: 0,
enemyStrength: 0,
neutralTowers: 0,
playerTowers: [],
enemyTowers: [],
neutralTowersByDistance: [],
threats: [],
opportunities: []
};
// Calculate faction strengths and positions
for (var i = 0; i < towers.length; i++) {
var tower = towers[i];
if (tower.owner === 1) {
state.playerStrength += tower.unitCount;
state.playerTowers.push(tower);
} else if (tower.owner >= 2) {
state.enemyStrength += tower.unitCount;
state.enemyTowers.push(tower);
} else {
state.neutralTowers++;
}
}
// Find strategic opportunities and threats for each enemy tower
for (var i = 0; i < state.enemyTowers.length; i++) {
var enemyTower = state.enemyTowers[i];
var towerThreats = [];
var towerOpportunities = [];
// Analyze threats from player and other enemy factions
for (var j = 0; j < towers.length; j++) {
var otherTower = towers[j];
if (otherTower.owner !== enemyTower.owner) {
var distance = getDistance(enemyTower, otherTower);
var threatLevel = calculateThreatLevel(enemyTower, otherTower, distance);
if (threatLevel > 0) {
towerThreats.push({
tower: otherTower,
distance: distance,
threatLevel: threatLevel
});
}
// Check for opportunities (weak targets)
if (otherTower.unitCount < enemyTower.unitCount * 0.8) {
towerOpportunities.push({
tower: otherTower,
distance: distance,
advantage: enemyTower.unitCount - otherTower.unitCount
});
}
}
}
state.threats[enemyTower.owner] = towerThreats.sort(function (a, b) {
return b.threatLevel - a.threatLevel;
});
state.opportunities[enemyTower.owner] = towerOpportunities.sort(function (a, b) {
return b.advantage - a.advantage;
});
}
return state;
}
// Calculate threat level between two towers
function calculateThreatLevel(myTower, enemyTower, distance) {
if (distance > 1000) return 0; // Too far to be immediate threat
var unitRatio = enemyTower.unitCount / Math.max(myTower.unitCount, 1);
var distanceFactor = Math.max(0, 1 - distance / 1000);
return unitRatio * distanceFactor * 100;
}
// Get distance between two towers
function getDistance(tower1, tower2) {
var dx = tower1.x - tower2.x;
var dy = tower1.y - tower2.y;
return Math.sqrt(dx * dx + dy * dy);
}
// Determine optimal strategy for a tower based on game state
function getOptimalStrategy(tower, gameState) {
var threats = gameState.threats[tower.owner] || [];
var opportunities = gameState.opportunities[tower.owner] || [];
var isPlayerWinning = gameState.playerStrength > gameState.enemyStrength * 1.2;
var isPlayerLosing = gameState.enemyStrength > gameState.playerStrength * 1.2;
// Emergency defense if under immediate threat
if (threats.length > 0 && threats[0].threatLevel > 50) {
return {
type: 'EMERGENCY_DEFENSE',
target: threats[0].tower,
urgency: threats[0].threatLevel
};
}
// Different faction personalities with adaptive behavior
if (tower.owner === 2) {
// Faction 2: Adaptive Aggressor - changes tactics based on game state
if (isPlayerLosing) {
return {
type: 'PRESS_ADVANTAGE',
target: findWeakestPlayerTower(gameState.playerTowers, tower),
minUnits: 8
};
} else if (opportunities.length > 0 && tower.unitCount > 15) {
return {
type: 'OPPORTUNISTIC_STRIKE',
target: opportunities[0].tower,
minUnits: 12
};
} else {
return {
type: 'AGGRESSIVE_EXPAND',
target: findNearestEnemy(tower),
minUnits: 10
};
}
} else if (tower.owner === 3) {
// Faction 3: Strategic Defender - focuses on timing and coordination
if (tower.unitCount > 30 && isPlayerWinning) {
return {
type: 'COORDINATED_COUNTER',
target: findStrongestPlayerTower(gameState.playerTowers, tower),
minUnits: 25
};
} else if (opportunities.length > 0 && tower.unitCount > opportunities[0].tower.unitCount * 1.5) {
return {
type: 'CALCULATED_STRIKE',
target: opportunities[0].tower,
minUnits: 20
};
} else {
return {
type: 'DEFENSIVE_BUILD',
target: null,
minUnits: 35
};
}
} else if (tower.owner === 4) {
// Faction 4: Economic Opportunist - focuses on efficient expansion
var nearbyNeutrals = findNearbyNeutralTowers(tower, 600);
if (nearbyNeutrals.length > 0 && tower.unitCount > 15) {
return {
type: 'ECONOMIC_EXPANSION',
target: nearbyNeutrals[0],
minUnits: 12
};
} else if (opportunities.length > 0 && getDistance(tower, opportunities[0].tower) < 500) {
return {
type: 'EFFICIENT_CAPTURE',
target: opportunities[0].tower,
minUnits: opportunities[0].tower.unitCount + 5
};
} else {
return {
type: 'RESOURCE_BUILD',
target: null,
minUnits: 25
};
}
} else {
// Default faction: Balanced approach
if (opportunities.length > 0 && tower.unitCount > 18) {
return {
type: 'BALANCED_ATTACK',
target: opportunities[0].tower,
minUnits: 15
};
} else {
return {
type: 'DEFENSIVE_BUILD',
target: null,
minUnits: 20
};
}
}
}
// Execute the determined strategy
function executeStrategy(tower, strategy, gameState) {
if (!strategy.target && strategy.type.indexOf('BUILD') === -1) return;
if (tower.unitCount < strategy.minUnits) return;
var unitsToSend;
switch (strategy.type) {
case 'EMERGENCY_DEFENSE':
unitsToSend = Math.floor(tower.unitCount * 0.8); // Send most units in emergency
break;
case 'PRESS_ADVANTAGE':
unitsToSend = Math.floor(tower.unitCount * 0.7); // Aggressive when winning
break;
case 'COORDINATED_COUNTER':
unitsToSend = Math.floor(tower.unitCount * 0.6); // Calculated counter-attack
break;
case 'OPPORTUNISTIC_STRIKE':
case 'CALCULATED_STRIKE':
case 'EFFICIENT_CAPTURE':
unitsToSend = Math.min(Math.floor(tower.unitCount * 0.6), strategy.target.unitCount + 10);
break;
case 'ECONOMIC_EXPANSION':
unitsToSend = Math.floor(tower.unitCount * 0.4); // Conservative expansion
break;
case 'AGGRESSIVE_EXPAND':
case 'BALANCED_ATTACK':
unitsToSend = Math.floor(tower.unitCount * 0.5); // Standard attack
break;
default:
return;
// No action for build strategies
}
if (strategy.target && unitsToSend > 0) {
sendUnits(tower, strategy.target, unitsToSend);
}
}
// Helper functions for target selection
function findWeakestPlayerTower(playerTowers, fromTower) {
var weakest = null;
var minUnits = Infinity;
for (var i = 0; i < playerTowers.length; i++) {
if (playerTowers[i].unitCount < minUnits) {
minUnits = playerTowers[i].unitCount;
weakest = playerTowers[i];
}
}
return weakest;
}
function findStrongestPlayerTower(playerTowers, fromTower) {
var strongest = null;
var maxUnits = 0;
for (var i = 0; i < playerTowers.length; i++) {
if (playerTowers[i].unitCount > maxUnits) {
maxUnits = playerTowers[i].unitCount;
strongest = playerTowers[i];
}
}
return strongest;
}
function findNearestEnemy(fromTower) {
var nearest = null;
var minDist = Infinity;
for (var i = 0; i < towers.length; i++) {
if (towers[i].owner !== fromTower.owner) {
var dist = getDistance(fromTower, towers[i]);
if (dist < minDist) {
minDist = dist;
nearest = towers[i];
}
}
}
return nearest;
}
function findNearbyNeutralTowers(fromTower, maxDistance) {
var nearby = [];
for (var i = 0; i < towers.length; i++) {
if (towers[i].owner === 0) {
var dist = getDistance(fromTower, towers[i]);
if (dist <= maxDistance) {
nearby.push(towers[i]);
}
}
}
return nearby.sort(function (a, b) {
return getDistance(fromTower, a) - getDistance(fromTower, b);
});
}
game.down = function (x, y, obj) {
// Find if we clicked on a player tower (scaled up detection area)
for (var i = 0; i < towers.length; i++) {
var tower = towers[i];
if (tower.owner === 1 && Math.abs(tower.x - x) < 75 && Math.abs(tower.y - y) < 75) {
selectedTower = tower;
isDragging = true;
if (!currentPath) {
currentPath = new PathLine();
game.addChild(currentPath);
}
currentPath.setPath([{
x: tower.x,
y: tower.y
}]);
break;
}
}
};
game.move = function (x, y, obj) {
if (isDragging && selectedTower && currentPath) {
currentPath.setPath([{
x: selectedTower.x,
y: selectedTower.y
}, {
x: x,
y: y
}]);
}
};
game.up = function (x, y, obj) {
if (isDragging && selectedTower) {
// Find target tower (scaled up detection area)
var targetTower = null;
for (var i = 0; i < towers.length; i++) {
var tower = towers[i];
if (tower !== selectedTower && Math.abs(tower.x - x) < 75 && Math.abs(tower.y - y) < 75) {
targetTower = tower;
break;
}
}
if (targetTower && selectedTower.unitCount > 0) {
sendUnits(selectedTower, targetTower, Math.floor(selectedTower.unitCount * 0.5));
}
if (currentPath) {
currentPath.clear();
}
}
isDragging = false;
selectedTower = null;
};
game.update = function () {
// Update all units
for (var i = units.length - 1; i >= 0; i--) {
if (!units[i] || !units[i].parent) {
units.splice(i, 1);
}
}
// Run AI every 2 seconds
if (LK.ticks % 120 === 0) {
runAI();
}
// Run memory cleanup
memoryManager.performCleanup();
// Check win/lose conditions
if (LK.ticks % 60 === 0) {
checkWinCondition();
}
// Update unit count display every 30 ticks (twice per second)
if (LK.ticks % 30 === 0) {
updateUnitCountDisplay();
updateMovingUnitGroups();
}
};
// Create Reset button
var resetButton = new Text2('Reset', {
size: 60,
fill: 0xFFFFFF
});
resetButton.anchor.set(1, 1); // Anchor to bottom right
LK.gui.bottomRight.addChild(resetButton);
// Reset button functionality
resetButton.down = function (x, y, obj) {
// Clear existing game objects
for (var i = 0; i < towers.length; i++) {
towers[i].destroy();
}
for (var i = 0; i < units.length; i++) {
units[i].destroy();
}
towers = [];
units = [];
if (currentPath) {
currentPath.destroy();
currentPath = null;
}
// Clean up moving unit groups
for (var i = 0; i < movingUnitGroups.length; i++) {
if (movingUnitGroups[i].parent) {
movingUnitGroups[i].destroy();
}
}
movingUnitGroups = [];
// Reset game state variables
selectedTower = null;
isDragging = false;
aiStartTime = null;
// Reset to level 0 and update storage
currentLevel = 0;
storage.currentLevel = currentLevel;
// Load level 0 (restart from beginning)
loadLevel(currentLevel);
};
// Initialize first level
loadLevel(currentLevel);
// Memory Management and Performance Optimization System
var memoryManager = {
// Object pools for reusing objects instead of creating new ones
unitPool: [],
sparklePool: [],
pathDotPool: [],
// Memory cleanup settings
cleanupInterval: 300,
// Clean up every 5 seconds (300 ticks at 60fps)
lastCleanup: 0,
// Get unit from pool or create new one
getUnit: function getUnit() {
if (this.unitPool.length > 0) {
var unit = this.unitPool.pop();
// Reset unit properties
unit.alpha = 1;
unit.visible = true;
unit.owner = 1;
unit.speed = 3.0; // Will be dynamically calculated during update
unit.targetTower = null;
unit.pathIndex = 0;
unit.path = [];
unit.lastWasIntersecting = false;
// Reset unit graphics to player unit (will be updated by setOwner)
unit.setOwner(1);
return unit;
}
return new Unit();
},
// Return unit to pool instead of destroying
recycleUnit: function recycleUnit(unit) {
if (unit && unit.parent) {
unit.parent.removeChild(unit);
unit.visible = false;
this.unitPool.push(unit);
}
},
// Get sparkle from pool or create new one
getSparkle: function getSparkle() {
if (this.sparklePool.length > 0) {
var sparkle = this.sparklePool.pop();
sparkle.alpha = 1;
sparkle.visible = true;
sparkle.scaleX = 0.5 + Math.random() * 0.5;
sparkle.scaleY = sparkle.scaleX;
return sparkle;
}
return game.attachAsset('sparkle', {
anchorX: 0.5,
anchorY: 0.5
});
},
// Return sparkle to pool
recycleSparkle: function recycleSparkle(sparkle) {
if (sparkle && sparkle.parent) {
sparkle.parent.removeChild(sparkle);
sparkle.visible = false;
this.sparklePool.push(sparkle);
}
},
// Periodic memory cleanup
performCleanup: function performCleanup() {
if (LK.ticks - this.lastCleanup < this.cleanupInterval) {
return;
}
this.lastCleanup = LK.ticks;
// Clean up destroyed units from arrays
for (var i = units.length - 1; i >= 0; i--) {
if (!units[i] || !units[i].parent) {
units.splice(i, 1);
}
}
// Limit pool sizes to prevent excessive memory usage
if (this.unitPool.length > 50) {
// Actually destroy excess units
var excess = this.unitPool.splice(50);
for (var i = 0; i < excess.length; i++) {
if (excess[i].destroy) {
excess[i].destroy();
}
}
}
if (this.sparklePool.length > 100) {
var excess = this.sparklePool.splice(100);
for (var i = 0; i < excess.length; i++) {
if (excess[i].destroy) {
excess[i].destroy();
}
}
}
if (this.pathDotPool.length > 200) {
var excess = this.pathDotPool.splice(200);
for (var i = 0; i < excess.length; i++) {
if (excess[i].destroy) {
excess[i].destroy();
}
}
}
// Force garbage collection hint by nullifying references
var temp = [];
temp = null;
}
};
// Moving unit group management
var movingUnitGroups = [];
// Create unit count panel in bottom left
var unitCountPanel = new Container();
LK.gui.bottomLeft.addChild(unitCountPanel);
// Panel background (optional visual enhancement)
var panelBg = LK.getAsset('tower', {
width: 300,
height: 200,
anchorX: 0,
anchorY: 1,
alpha: 0.3
});
unitCountPanel.addChild(panelBg);
// Player unit count text
var playerCountText = new Text2('Oyuncu: 0', {
size: 40,
fill: 0x4a90e2 // Player blue color
});
playerCountText.anchor.set(0, 1);
playerCountText.x = 10;
playerCountText.y = -10;
unitCountPanel.addChild(playerCountText);
// Enemy faction count texts
var enemyCountTexts = [];
var enemyColors = [0xe74c3c, 0x9b59b6, 0xf39c12]; // Red, purple, orange
for (var i = 0; i < 3; i++) {
var enemyText = new Text2('', {
size: 40,
fill: enemyColors[i]
});
enemyText.anchor.set(0, 1);
enemyText.x = 10;
enemyText.y = -50 - i * 40;
unitCountPanel.addChild(enemyText);
enemyCountTexts.push(enemyText);
}
// Function to update moving unit groups
function updateMovingUnitGroups() {
// Clear existing groups
for (var i = 0; i < movingUnitGroups.length; i++) {
if (movingUnitGroups[i].parent) {
movingUnitGroups[i].parent.removeChild(movingUnitGroups[i]);
}
}
movingUnitGroups = [];
// Group units by owner and proximity
var processedUnits = [];
for (var i = 0; i < units.length; i++) {
var unit = units[i];
if (processedUnits.indexOf(unit) !== -1) continue;
// Find nearby units of same owner
var group = [unit];
processedUnits.push(unit);
for (var j = i + 1; j < units.length; j++) {
var otherUnit = units[j];
if (processedUnits.indexOf(otherUnit) !== -1) continue;
if (otherUnit.owner !== unit.owner) continue;
// Check if within grouping distance (100 pixels)
var dx = unit.x - otherUnit.x;
var dy = unit.y - otherUnit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 100) {
group.push(otherUnit);
processedUnits.push(otherUnit);
}
}
// Create group display if more than 1 unit
if (group.length > 1) {
var groupDisplay = new MovingUnitGroup();
groupDisplay.owner = unit.owner;
groupDisplay.updateGroup(group);
game.addChild(groupDisplay);
movingUnitGroups.push(groupDisplay);
}
}
}
// Function to update unit count display
function updateUnitCountDisplay() {
var playerUnits = 0;
var enemyFactions = []; // Array to store enemy faction data
// Count units in towers
for (var i = 0; i < towers.length; i++) {
var tower = towers[i];
if (tower.owner === 1) {
playerUnits += tower.unitCount;
} else if (tower.owner >= 2 && tower.owner <= 4) {
// Find or create enemy faction entry
var factionIndex = enemyFactions.findIndex(function (faction) {
return faction.owner === tower.owner;
});
if (factionIndex === -1) {
enemyFactions.push({
owner: tower.owner,
units: tower.unitCount,
name: assignedEnemyNames[tower.owner] || 'Rakip ' + (tower.owner - 1)
});
} else {
enemyFactions[factionIndex].units += tower.unitCount;
}
}
}
// Count units in transit
for (var i = 0; i < units.length; i++) {
var unit = units[i];
if (unit.owner === 1) {
playerUnits++;
} else if (unit.owner >= 2 && unit.owner <= 4) {
var factionIndex = enemyFactions.findIndex(function (faction) {
return faction.owner === unit.owner;
});
if (factionIndex !== -1) {
enemyFactions[factionIndex].units++;
}
}
}
// Sort enemy factions by unit count (descending)
enemyFactions.sort(function (a, b) {
return b.units - a.units;
});
// Update display texts
playerCountText.setText('Oyuncu: ' + playerUnits);
// Clear all enemy texts first
for (var i = 0; i < enemyCountTexts.length; i++) {
enemyCountTexts[i].setText('');
}
// Update enemy faction displays with dynamic positioning
for (var i = 0; i < Math.min(enemyFactions.length, enemyCountTexts.length); i++) {
var faction = enemyFactions[i];
var displayText = faction.name + ': ' + faction.units;
// Add strikethrough for zero units
if (faction.units === 0) {
displayText = '~~' + displayText + '~~';
}
enemyCountTexts[i].setText(displayText);
// Update position for dynamic sorting (reposition based on current index)
enemyCountTexts[i].y = -50 - i * 40;
}
}
// Play background music
LK.playMusic('battle');
; ===================================================================
--- original.js
+++ change.js
@@ -297,24 +297,24 @@
// Base speed of 3.0, reduced by group size
// 1 unit: 3.0 speed, 10 units: 1.5 speed, 20+ units: 0.75 speed
var dynamicSpeed = Math.max(0.75, 3.0 - (nearbyUnits - 1) * 0.15);
self.speed = dynamicSpeed;
- // Pathfinding and overtaking logic for faster units
+ // Edge-based movement system for smaller groups to avoid larger groups
var needsAvoidance = false;
var avoidanceDirection = {
x: 0,
y: 0
};
- var slowUnitAhead = null;
- // Check for slower units ahead that need to be avoided
+ var largerGroupAhead = null;
+ // Check for larger groups ahead that need to be avoided
for (var i = 0; i < units.length; i++) {
var otherUnit = units[i];
if (otherUnit !== self && otherUnit.owner === self.owner) {
var dx = otherUnit.x - self.x;
var dy = otherUnit.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
// Check if unit is ahead and close
- if (distance < 80 && distance > 0) {
+ if (distance < 120 && distance > 0) {
// Calculate if other unit is in front (same general direction)
var target = self.path[self.pathIndex];
if (target) {
var toTargetX = target.x - self.x;
@@ -328,12 +328,12 @@
toTargetX /= targetLength;
toTargetY /= targetLength;
toOtherX /= otherLength;
toOtherY /= otherLength;
- // Check if other unit is in same direction (dot product > 0.5)
+ // Check if other unit is in same direction (dot product > 0.3 for wider detection)
var dotProduct = toTargetX * toOtherX + toTargetY * toOtherY;
- if (dotProduct > 0.5) {
- // Check if this unit is faster (should overtake)
+ if (dotProduct > 0.3) {
+ // Count nearby units for both this unit and the other unit
var otherNearbyUnits = 1;
for (var j = 0; j < units.length; j++) {
var checkUnit = units[j];
if (checkUnit !== otherUnit && checkUnit.owner === otherUnit.owner) {
@@ -344,18 +344,35 @@
otherNearbyUnits++;
}
}
}
- var otherSpeed = Math.max(0.75, 3.0 - (otherNearbyUnits - 1) * 0.15);
- // If this unit is faster, initiate overtaking
- if (self.speed > otherSpeed + 0.2) {
+ // If this unit is in a smaller group, it should go around the larger group
+ if (nearbyUnits < otherNearbyUnits) {
needsAvoidance = true;
- slowUnitAhead = otherUnit;
- // Calculate perpendicular avoidance direction
+ largerGroupAhead = otherUnit;
+ // Calculate edge-based avoidance direction
+ // Try to go around the edge of the larger group
var perpX = -toTargetY; // Perpendicular to movement direction
var perpY = toTargetX;
- // Choose side with more space or randomly
- var sideChoice = Math.random() > 0.5 ? 1 : -1;
+ // Choose the side with less obstruction or better path to target
+ var leftSideX = self.x + perpX * 80;
+ var leftSideY = self.y + perpY * 80;
+ var rightSideX = self.x - perpX * 80;
+ var rightSideY = self.y - perpY * 80;
+ // Check which side has less units blocking
+ var leftBlockCount = 0;
+ var rightBlockCount = 0;
+ for (var k = 0; k < units.length; k++) {
+ var blockUnit = units[k];
+ if (blockUnit !== self && blockUnit.owner === self.owner) {
+ var leftDist = Math.sqrt(Math.pow(blockUnit.x - leftSideX, 2) + Math.pow(blockUnit.y - leftSideY, 2));
+ var rightDist = Math.sqrt(Math.pow(blockUnit.x - rightSideX, 2) + Math.pow(blockUnit.y - rightSideY, 2));
+ if (leftDist < 60) leftBlockCount++;
+ if (rightDist < 60) rightBlockCount++;
+ }
+ }
+ // Choose the side with fewer blocking units
+ var sideChoice = leftBlockCount <= rightBlockCount ? 1 : -1;
avoidanceDirection.x = perpX * sideChoice;
avoidanceDirection.y = perpY * sideChoice;
break;
}
@@ -364,21 +381,22 @@
}
}
}
}
- // Apply avoidance behavior
- if (needsAvoidance && slowUnitAhead) {
- self.avoidanceTimer = 60; // Avoid for 1 second
- self.avoidanceOffset.x = avoidanceDirection.x * 40; // 40 pixel offset
- self.avoidanceOffset.y = avoidanceDirection.y * 40;
+ // Apply edge-based avoidance behavior
+ if (needsAvoidance && largerGroupAhead) {
+ self.avoidanceTimer = 90; // Avoid for 1.5 seconds (longer to go around)
+ self.avoidanceOffset.x = avoidanceDirection.x * 60; // Larger offset for edge movement
+ self.avoidanceOffset.y = avoidanceDirection.y * 60;
} else if (self.avoidanceTimer > 0) {
- // Continue avoidance movement
+ // Continue avoidance movement but gradually return to path
self.avoidanceTimer--;
- if (self.avoidanceTimer <= 0) {
- // Gradually reduce avoidance offset
- self.avoidanceOffset.x *= 0.8;
- self.avoidanceOffset.y *= 0.8;
- if (Math.abs(self.avoidanceOffset.x) < 1 && Math.abs(self.avoidanceOffset.y) < 1) {
+ if (self.avoidanceTimer <= 30) {
+ // Start reducing avoidance offset in the last 0.5 seconds
+ var returnFactor = self.avoidanceTimer / 30.0;
+ self.avoidanceOffset.x *= returnFactor;
+ self.avoidanceOffset.y *= returnFactor;
+ if (self.avoidanceTimer <= 0) {
self.avoidanceOffset.x = 0;
self.avoidanceOffset.y = 0;
}
}
Saldırı kulesi, Gerçekçi, kuş bakışı görünüm, yazısız.. In-Game asset. High contrast. No shadows, 3d olsun.
Saldırı kulesi, Gerçekçi, kuş bakışı görünüm, yazısız.. In-Game asset. High contrast. No shadows, 3d olsun.
Gece çölü arkaplan resmi, Gerçekçi, kuş bakışı görünüm, yazısız. susuz. In-Game asset. High contrast. No shadows, 3d olsun..
Kılıç ve kalkanlı çağı saldırı askeri, Gerçekçi, kuş bakışı görünüm, yazısız.. In-Game asset. High contrast. No shadows, renkli 3d olsun.