User prompt
Please fix the bug: 'Uncaught TypeError: Cannot read properties of undefined (reading 'push')' in or related to this line: 'britishAircraft.push(aircraft);' Line Number: 428
Code edit (1 edits merged)
Please save this source code
User prompt
Gallipoli: Ottoman vs British War
Initial prompt
The game will be set during World War I, specifically at the Gallipoli front, allowing players to make two different choices: they can choose to play as either the Ottoman side or the British side. I want the atmosphere to reflect a war setting, which you can research online if necessary. In short, the environment will have a somewhat foggy ambiance. As it is a war game, there will be soldiers, and of course, they will be armed. I need a well-coded warfare mechanic. The capabilities of each side will be as follows: the Ottoman soldiers will have cannons that can fire, and they will also possess various weapons. Occasionally, an Ottoman soldier may randomly fix bayonets. Additionally, there will be sound effects for the Ottoman soldiers; sometimes they will shout "Allah Allah!" On the other hand, the British soldiers will have the ability to deploy from aircraft, and they can disembark many troops from ships, as well as have naval vessels. The British soldiers will also be equipped with weapons and gear similar to those of the Ottoman soldiers. In summary, while the British will have aerial, land, and naval support, the Ottomans will only have ground support. At the end of the game, I want you to create a menu depending on which side wins. For example, if the Ottomans win, it should say "Congratulations! The Ottoman soldiers have won." If the British win, it should state "Congratulations! The British soldiers have won." Finally, once the game is completely over, a message in Turkish saying "ÇANAKKALE GEÇİLMEZ" (Gallipoli cannot be crossed) will appear, along with the Turkish flag, signaling the end of the game. I want to emphasize again that the game will have a war atmosphere. It would be great if it were in 3D, and of course, the player should be able to eliminate the opposing side wherever they choose to attack.
/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var BritishAircraft = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('britishAircraft', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 150;
self.lastDrop = 0;
self.speed = 2;
self.direction = 1;
self.update = function () {
self.x += self.speed * self.direction;
if (self.x > 1900 || self.x < 148) {
self.direction *= -1;
}
// Drop soldiers
if (LK.ticks - self.lastDrop > 200 && britishUnits.length < 20) {
var newSoldier = new BritishSoldier();
newSoldier.x = self.x;
newSoldier.y = self.y + 100;
britishUnits.push(newSoldier);
game.addChild(newSoldier);
self.lastDrop = LK.ticks;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = britishAircraft.indexOf(self);
if (index > -1) britishAircraft.splice(index, 1);
}
};
return self;
});
var BritishShip = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('britishShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 300;
self.lastDeploy = 0;
self.update = function () {
// Deploy soldiers
if (LK.ticks - self.lastDeploy > 180 && britishUnits.length < 15) {
var newSoldier = new BritishSoldier();
newSoldier.x = self.x + (Math.random() - 0.5) * 100;
newSoldier.y = self.y + 80;
britishUnits.push(newSoldier);
game.addChild(newSoldier);
self.lastDeploy = LK.ticks;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = britishShips.indexOf(self);
if (index > -1) britishShips.splice(index, 1);
}
};
return self;
});
var BritishSoldier = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('britishSoldier', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.lastShot = 0;
self.update = function () {
// Shoot at ottoman soldiers
if (LK.ticks - self.lastShot > 60) {
for (var i = 0; i < ottomanUnits.length; i++) {
var enemy = ottomanUnits[i];
var dx = enemy.x - self.x;
var dy = enemy.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 250) {
var newBullet = new Bullet();
newBullet.x = self.x;
newBullet.y = self.y;
newBullet.targetX = enemy.x;
newBullet.targetY = enemy.y;
bullets.push(newBullet);
game.addChild(newBullet);
LK.getSound('gunshot').play();
self.lastShot = LK.ticks;
break;
}
}
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = britishUnits.indexOf(self);
if (index > -1) britishUnits.splice(index, 1);
}
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.speed = 12;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
for (var i = 0; i < ottomanUnits.length; i++) {
var unit = ottomanUnits[i];
var unitDx = unit.x - self.x;
var unitDy = unit.y - self.y;
var unitDistance = Math.sqrt(unitDx * unitDx + unitDy * unitDy);
if (unitDistance < 40) {
unit.takeDamage(25);
break;
}
}
for (var j = 0; j < ottomanCannons.length; j++) {
var cannon = ottomanCannons[j];
var cannonDx = cannon.x - self.x;
var cannonDy = cannon.y - self.y;
var cannonDistance = Math.sqrt(cannonDx * cannonDx + cannonDy * cannonDy);
if (cannonDistance < 60) {
cannon.takeDamage(30);
break;
}
}
self.destroy();
var index = bullets.indexOf(self);
if (index > -1) bullets.splice(index, 1);
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
var Cannonball = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('cannonball', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.speed = 8;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
// Explode
for (var i = 0; i < britishUnits.length; i++) {
var unit = britishUnits[i];
var unitDx = unit.x - self.x;
var unitDy = unit.y - self.y;
var unitDistance = Math.sqrt(unitDx * unitDx + unitDy * unitDy);
if (unitDistance < 80) {
unit.takeDamage(50);
}
}
self.destroy();
var index = cannonballs.indexOf(self);
if (index > -1) cannonballs.splice(index, 1);
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
var OttomanCannon = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('ottomanCannon', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 200;
self.lastFire = 0;
self.update = function () {
if (LK.ticks - self.lastFire > 120) {
for (var i = 0; i < britishUnits.length; i++) {
var enemy = britishUnits[i];
var dx = enemy.x - self.x;
var dy = enemy.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 400) {
var newCannonball = new Cannonball();
newCannonball.x = self.x;
newCannonball.y = self.y;
newCannonball.targetX = enemy.x;
newCannonball.targetY = enemy.y;
cannonballs.push(newCannonball);
game.addChild(newCannonball);
LK.getSound('cannonFire').play();
self.lastFire = LK.ticks;
break;
}
}
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = ottomanCannons.indexOf(self);
if (index > -1) ottomanCannons.splice(index, 1);
}
};
return self;
});
var OttomanSoldier = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('ottomanSoldier', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.hasBayonet = false;
self.lastBattleCry = 0;
self.update = function () {
// Random bayonet fixing
if (LK.ticks % 180 == 0 && Math.random() < 0.3) {
self.hasBayonet = true;
graphics.tint = 0xFF6B6B;
}
// Battle cry
if (LK.ticks - self.lastBattleCry > 300 && Math.random() < 0.1) {
LK.getSound('battleCry').play();
self.lastBattleCry = LK.ticks;
}
// Move towards enemies
for (var i = 0; i < britishUnits.length; i++) {
var enemy = britishUnits[i];
var dx = enemy.x - self.x;
var dy = enemy.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 300) {
self.x += dx / distance * 1;
self.y += dy / distance * 1;
if (distance < 50) {
enemy.takeDamage(self.hasBayonet ? 30 : 15);
}
}
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = ottomanUnits.indexOf(self);
if (index > -1) ottomanUnits.splice(index, 1);
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x8FBC8F
});
/****
* Game Code
****/
var selectedFaction = null;
var gameStarted = false;
var ottomanUnits = [];
var ottomanCannons = [];
var britishUnits = [];
var britishShips = [];
var britishAircraft = [];
var bullets = [];
var cannonballs = [];
var fogElements = [];
var gameWon = false;
// Faction selection UI
var factionText = new Text2('Choose Your Faction', {
size: 80,
fill: 0xFFFFFF
});
factionText.anchor.set(0.5, 0.5);
factionText.x = 1024;
factionText.y = 500;
game.addChild(factionText);
var ottomanButton = LK.getAsset('ottomanSoldier', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2,
x: 700,
y: 800
});
game.addChild(ottomanButton);
var ottomanText = new Text2('Ottoman Empire', {
size: 60,
fill: 0x8B4513
});
ottomanText.anchor.set(0.5, 0.5);
ottomanText.x = 700;
ottomanText.y = 950;
game.addChild(ottomanText);
var britishButton = LK.getAsset('britishSoldier', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2,
x: 1348,
y: 800
});
game.addChild(britishButton);
var britishText = new Text2('British Empire', {
size: 60,
fill: 0x4169E1
});
britishText.anchor.set(0.5, 0.5);
britishText.x = 1348;
britishText.y = 950;
game.addChild(britishText);
// Create fog elements
for (var f = 0; f < 20; f++) {
var fog = LK.getAsset('fog', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3,
x: Math.random() * 2048,
y: Math.random() * 2732
});
fogElements.push(fog);
game.addChild(fog);
}
function startGame(faction) {
selectedFaction = faction;
gameStarted = true;
// Remove faction selection UI
factionText.destroy();
ottomanButton.destroy();
ottomanText.destroy();
britishButton.destroy();
britishText.destroy();
// Start music
LK.playMusic('warMusic');
if (faction === 'ottoman') {
// Create Ottoman forces
for (var i = 0; i < 8; i++) {
var soldier = new OttomanSoldier();
soldier.x = Math.random() * 1500 + 274;
soldier.y = Math.random() * 1000 + 1366;
ottomanUnits.push(soldier);
game.addChild(soldier);
}
for (var j = 0; j < 4; j++) {
var cannon = new OttomanCannon();
cannon.x = Math.random() * 1200 + 424;
cannon.y = Math.random() * 800 + 1500;
ottomanCannons.push(cannon);
game.addChild(cannon);
}
// Create British attackers
for (var k = 0; k < 2; k++) {
var ship = new BritishShip();
ship.x = Math.random() * 1500 + 274;
ship.y = Math.random() * 200 + 100;
britishShips.push(ship);
game.addChild(ship);
}
var aircraft = new BritishAircraft();
aircraft.x = 148;
aircraft.y = 400;
britishAircraft.push(aircraft);
game.addChild(aircraft);
} else {
// Create British forces
for (var l = 0; l < 6; l++) {
var britishSoldier = new BritishSoldier();
britishSoldier.x = Math.random() * 1500 + 274;
britishSoldier.y = Math.random() * 600 + 300;
britishUnits.push(britishSoldier);
game.addChild(britishSoldier);
}
for (var m = 0; m < 3; m++) {
var britishShip = new BritishShip();
britishShip.x = Math.random() * 1500 + 274;
britishShip.y = Math.random() * 200 + 100;
britishShips.push(britishShip);
game.addChild(britishShip);
}
var aircraft2 = new BritishAircraft();
aircraft2.x = 148;
aircraft2.y = 400;
britishAircraft.push(aircraft2);
game.addChild(aircraft2);
// Create Ottoman defenders
for (var n = 0; n < 10; n++) {
var ottomanSoldier = new OttomanSoldier();
ottomanSoldier.x = Math.random() * 1500 + 274;
ottomanSoldier.y = Math.random() * 1000 + 1366;
ottomanUnits.push(ottomanSoldier);
game.addChild(ottomanSoldier);
}
for (var o = 0; o < 5; o++) {
var ottomanCannon = new OttomanCannon();
ottomanCannon.x = Math.random() * 1200 + 424;
ottomanCannon.y = Math.random() * 800 + 1500;
ottomanCannons.push(ottomanCannon);
game.addChild(ottomanCannon);
}
}
}
function checkVictory() {
if (gameWon) return;
if (selectedFaction === 'ottoman') {
if (britishUnits.length === 0 && britishShips.length === 0 && britishAircraft.length === 0) {
gameWon = true;
showVictory('Ottoman Victory!');
} else if (ottomanUnits.length === 0 && ottomanCannons.length === 0) {
gameWon = true;
showVictory('British Victory!');
}
} else {
if (ottomanUnits.length === 0 && ottomanCannons.length === 0) {
gameWon = true;
showVictory('British Victory!');
} else if (britishUnits.length === 0 && britishShips.length === 0 && britishAircraft.length === 0) {
gameWon = true;
showVictory('Ottoman Victory!');
}
}
}
function showVictory(message) {
var victoryText = new Text2(message, {
size: 100,
fill: 0xFFD700
});
victoryText.anchor.set(0.5, 0.5);
victoryText.x = 1024;
victoryText.y = 1000;
game.addChild(victoryText);
LK.setTimeout(function () {
victoryText.destroy();
showTribute();
}, 2000);
}
function showTribute() {
var tributeText = new Text2('ÇANAKKALE GEÇİLMEZ', {
size: 80,
fill: 0xFFFFFF
});
tributeText.anchor.set(0.5, 0.5);
tributeText.x = 1024;
tributeText.y = 1200;
game.addChild(tributeText);
var flag = LK.getAsset('turkishFlag', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1450
});
game.addChild(flag);
LK.setTimeout(function () {
LK.showYouWin();
}, 3000);
}
game.down = function (x, y, obj) {
if (!gameStarted) {
if (x > 550 && x < 850 && y > 650 && y < 1100) {
startGame('ottoman');
} else if (x > 1198 && x < 1498 && y > 650 && y < 1100) {
startGame('british');
}
}
};
game.update = function () {
if (!gameStarted) return;
// Animate fog
for (var f = 0; f < fogElements.length; f++) {
var fog = fogElements[f];
fog.x += Math.sin(LK.ticks * 0.01 + f) * 0.5;
fog.y += Math.cos(LK.ticks * 0.01 + f) * 0.3;
if (fog.x < -150) fog.x = 2198;
if (fog.x > 2198) fog.x = -150;
if (fog.y < -100) fog.y = 2832;
if (fog.y > 2832) fog.y = -100;
}
checkVictory();
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
/****
* Classes
****/
var BritishAircraft = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('britishAircraft', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 150;
self.lastDrop = 0;
self.speed = 2;
self.direction = 1;
self.update = function () {
self.x += self.speed * self.direction;
if (self.x > 1900 || self.x < 148) {
self.direction *= -1;
}
// Drop soldiers
if (LK.ticks - self.lastDrop > 200 && britishUnits.length < 20) {
var newSoldier = new BritishSoldier();
newSoldier.x = self.x;
newSoldier.y = self.y + 100;
britishUnits.push(newSoldier);
game.addChild(newSoldier);
self.lastDrop = LK.ticks;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = britishAircraft.indexOf(self);
if (index > -1) britishAircraft.splice(index, 1);
}
};
return self;
});
var BritishShip = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('britishShip', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 300;
self.lastDeploy = 0;
self.update = function () {
// Deploy soldiers
if (LK.ticks - self.lastDeploy > 180 && britishUnits.length < 15) {
var newSoldier = new BritishSoldier();
newSoldier.x = self.x + (Math.random() - 0.5) * 100;
newSoldier.y = self.y + 80;
britishUnits.push(newSoldier);
game.addChild(newSoldier);
self.lastDeploy = LK.ticks;
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = britishShips.indexOf(self);
if (index > -1) britishShips.splice(index, 1);
}
};
return self;
});
var BritishSoldier = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('britishSoldier', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.lastShot = 0;
self.update = function () {
// Shoot at ottoman soldiers
if (LK.ticks - self.lastShot > 60) {
for (var i = 0; i < ottomanUnits.length; i++) {
var enemy = ottomanUnits[i];
var dx = enemy.x - self.x;
var dy = enemy.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 250) {
var newBullet = new Bullet();
newBullet.x = self.x;
newBullet.y = self.y;
newBullet.targetX = enemy.x;
newBullet.targetY = enemy.y;
bullets.push(newBullet);
game.addChild(newBullet);
LK.getSound('gunshot').play();
self.lastShot = LK.ticks;
break;
}
}
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = britishUnits.indexOf(self);
if (index > -1) britishUnits.splice(index, 1);
}
};
return self;
});
var Bullet = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('bullet', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.speed = 12;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
for (var i = 0; i < ottomanUnits.length; i++) {
var unit = ottomanUnits[i];
var unitDx = unit.x - self.x;
var unitDy = unit.y - self.y;
var unitDistance = Math.sqrt(unitDx * unitDx + unitDy * unitDy);
if (unitDistance < 40) {
unit.takeDamage(25);
break;
}
}
for (var j = 0; j < ottomanCannons.length; j++) {
var cannon = ottomanCannons[j];
var cannonDx = cannon.x - self.x;
var cannonDy = cannon.y - self.y;
var cannonDistance = Math.sqrt(cannonDx * cannonDx + cannonDy * cannonDy);
if (cannonDistance < 60) {
cannon.takeDamage(30);
break;
}
}
self.destroy();
var index = bullets.indexOf(self);
if (index > -1) bullets.splice(index, 1);
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
var Cannonball = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('cannonball', {
anchorX: 0.5,
anchorY: 0.5
});
self.targetX = 0;
self.targetY = 0;
self.speed = 8;
self.update = function () {
var dx = self.targetX - self.x;
var dy = self.targetY - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < self.speed) {
// Explode
for (var i = 0; i < britishUnits.length; i++) {
var unit = britishUnits[i];
var unitDx = unit.x - self.x;
var unitDy = unit.y - self.y;
var unitDistance = Math.sqrt(unitDx * unitDx + unitDy * unitDy);
if (unitDistance < 80) {
unit.takeDamage(50);
}
}
self.destroy();
var index = cannonballs.indexOf(self);
if (index > -1) cannonballs.splice(index, 1);
} else {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
var OttomanCannon = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('ottomanCannon', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 200;
self.lastFire = 0;
self.update = function () {
if (LK.ticks - self.lastFire > 120) {
for (var i = 0; i < britishUnits.length; i++) {
var enemy = britishUnits[i];
var dx = enemy.x - self.x;
var dy = enemy.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 400) {
var newCannonball = new Cannonball();
newCannonball.x = self.x;
newCannonball.y = self.y;
newCannonball.targetX = enemy.x;
newCannonball.targetY = enemy.y;
cannonballs.push(newCannonball);
game.addChild(newCannonball);
LK.getSound('cannonFire').play();
self.lastFire = LK.ticks;
break;
}
}
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = ottomanCannons.indexOf(self);
if (index > -1) ottomanCannons.splice(index, 1);
}
};
return self;
});
var OttomanSoldier = Container.expand(function () {
var self = Container.call(this);
var graphics = self.attachAsset('ottomanSoldier', {
anchorX: 0.5,
anchorY: 0.5
});
self.health = 100;
self.hasBayonet = false;
self.lastBattleCry = 0;
self.update = function () {
// Random bayonet fixing
if (LK.ticks % 180 == 0 && Math.random() < 0.3) {
self.hasBayonet = true;
graphics.tint = 0xFF6B6B;
}
// Battle cry
if (LK.ticks - self.lastBattleCry > 300 && Math.random() < 0.1) {
LK.getSound('battleCry').play();
self.lastBattleCry = LK.ticks;
}
// Move towards enemies
for (var i = 0; i < britishUnits.length; i++) {
var enemy = britishUnits[i];
var dx = enemy.x - self.x;
var dy = enemy.y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 300) {
self.x += dx / distance * 1;
self.y += dy / distance * 1;
if (distance < 50) {
enemy.takeDamage(self.hasBayonet ? 30 : 15);
}
}
}
};
self.takeDamage = function (damage) {
self.health -= damage;
LK.effects.flashObject(self, 0xFF0000, 200);
if (self.health <= 0) {
self.destroy();
var index = ottomanUnits.indexOf(self);
if (index > -1) ottomanUnits.splice(index, 1);
}
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x8FBC8F
});
/****
* Game Code
****/
var selectedFaction = null;
var gameStarted = false;
var ottomanUnits = [];
var ottomanCannons = [];
var britishUnits = [];
var britishShips = [];
var britishAircraft = [];
var bullets = [];
var cannonballs = [];
var fogElements = [];
var gameWon = false;
// Faction selection UI
var factionText = new Text2('Choose Your Faction', {
size: 80,
fill: 0xFFFFFF
});
factionText.anchor.set(0.5, 0.5);
factionText.x = 1024;
factionText.y = 500;
game.addChild(factionText);
var ottomanButton = LK.getAsset('ottomanSoldier', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2,
x: 700,
y: 800
});
game.addChild(ottomanButton);
var ottomanText = new Text2('Ottoman Empire', {
size: 60,
fill: 0x8B4513
});
ottomanText.anchor.set(0.5, 0.5);
ottomanText.x = 700;
ottomanText.y = 950;
game.addChild(ottomanText);
var britishButton = LK.getAsset('britishSoldier', {
anchorX: 0.5,
anchorY: 0.5,
scaleX: 2,
scaleY: 2,
x: 1348,
y: 800
});
game.addChild(britishButton);
var britishText = new Text2('British Empire', {
size: 60,
fill: 0x4169E1
});
britishText.anchor.set(0.5, 0.5);
britishText.x = 1348;
britishText.y = 950;
game.addChild(britishText);
// Create fog elements
for (var f = 0; f < 20; f++) {
var fog = LK.getAsset('fog', {
anchorX: 0.5,
anchorY: 0.5,
alpha: 0.3,
x: Math.random() * 2048,
y: Math.random() * 2732
});
fogElements.push(fog);
game.addChild(fog);
}
function startGame(faction) {
selectedFaction = faction;
gameStarted = true;
// Remove faction selection UI
factionText.destroy();
ottomanButton.destroy();
ottomanText.destroy();
britishButton.destroy();
britishText.destroy();
// Start music
LK.playMusic('warMusic');
if (faction === 'ottoman') {
// Create Ottoman forces
for (var i = 0; i < 8; i++) {
var soldier = new OttomanSoldier();
soldier.x = Math.random() * 1500 + 274;
soldier.y = Math.random() * 1000 + 1366;
ottomanUnits.push(soldier);
game.addChild(soldier);
}
for (var j = 0; j < 4; j++) {
var cannon = new OttomanCannon();
cannon.x = Math.random() * 1200 + 424;
cannon.y = Math.random() * 800 + 1500;
ottomanCannons.push(cannon);
game.addChild(cannon);
}
// Create British attackers
for (var k = 0; k < 2; k++) {
var ship = new BritishShip();
ship.x = Math.random() * 1500 + 274;
ship.y = Math.random() * 200 + 100;
britishShips.push(ship);
game.addChild(ship);
}
var aircraft = new BritishAircraft();
aircraft.x = 148;
aircraft.y = 400;
britishAircraft.push(aircraft);
game.addChild(aircraft);
} else {
// Create British forces
for (var l = 0; l < 6; l++) {
var britishSoldier = new BritishSoldier();
britishSoldier.x = Math.random() * 1500 + 274;
britishSoldier.y = Math.random() * 600 + 300;
britishUnits.push(britishSoldier);
game.addChild(britishSoldier);
}
for (var m = 0; m < 3; m++) {
var britishShip = new BritishShip();
britishShip.x = Math.random() * 1500 + 274;
britishShip.y = Math.random() * 200 + 100;
britishShips.push(britishShip);
game.addChild(britishShip);
}
var aircraft2 = new BritishAircraft();
aircraft2.x = 148;
aircraft2.y = 400;
britishAircraft.push(aircraft2);
game.addChild(aircraft2);
// Create Ottoman defenders
for (var n = 0; n < 10; n++) {
var ottomanSoldier = new OttomanSoldier();
ottomanSoldier.x = Math.random() * 1500 + 274;
ottomanSoldier.y = Math.random() * 1000 + 1366;
ottomanUnits.push(ottomanSoldier);
game.addChild(ottomanSoldier);
}
for (var o = 0; o < 5; o++) {
var ottomanCannon = new OttomanCannon();
ottomanCannon.x = Math.random() * 1200 + 424;
ottomanCannon.y = Math.random() * 800 + 1500;
ottomanCannons.push(ottomanCannon);
game.addChild(ottomanCannon);
}
}
}
function checkVictory() {
if (gameWon) return;
if (selectedFaction === 'ottoman') {
if (britishUnits.length === 0 && britishShips.length === 0 && britishAircraft.length === 0) {
gameWon = true;
showVictory('Ottoman Victory!');
} else if (ottomanUnits.length === 0 && ottomanCannons.length === 0) {
gameWon = true;
showVictory('British Victory!');
}
} else {
if (ottomanUnits.length === 0 && ottomanCannons.length === 0) {
gameWon = true;
showVictory('British Victory!');
} else if (britishUnits.length === 0 && britishShips.length === 0 && britishAircraft.length === 0) {
gameWon = true;
showVictory('Ottoman Victory!');
}
}
}
function showVictory(message) {
var victoryText = new Text2(message, {
size: 100,
fill: 0xFFD700
});
victoryText.anchor.set(0.5, 0.5);
victoryText.x = 1024;
victoryText.y = 1000;
game.addChild(victoryText);
LK.setTimeout(function () {
victoryText.destroy();
showTribute();
}, 2000);
}
function showTribute() {
var tributeText = new Text2('ÇANAKKALE GEÇİLMEZ', {
size: 80,
fill: 0xFFFFFF
});
tributeText.anchor.set(0.5, 0.5);
tributeText.x = 1024;
tributeText.y = 1200;
game.addChild(tributeText);
var flag = LK.getAsset('turkishFlag', {
anchorX: 0.5,
anchorY: 0.5,
x: 1024,
y: 1450
});
game.addChild(flag);
LK.setTimeout(function () {
LK.showYouWin();
}, 3000);
}
game.down = function (x, y, obj) {
if (!gameStarted) {
if (x > 550 && x < 850 && y > 650 && y < 1100) {
startGame('ottoman');
} else if (x > 1198 && x < 1498 && y > 650 && y < 1100) {
startGame('british');
}
}
};
game.update = function () {
if (!gameStarted) return;
// Animate fog
for (var f = 0; f < fogElements.length; f++) {
var fog = fogElements[f];
fog.x += Math.sin(LK.ticks * 0.01 + f) * 0.5;
fog.y += Math.cos(LK.ticks * 0.01 + f) * 0.3;
if (fog.x < -150) fog.x = 2198;
if (fog.x > 2198) fog.x = -150;
if (fog.y < -100) fog.y = 2832;
if (fog.y > 2832) fog.y = -100;
}
checkVictory();
};