/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Unit = Container.expand(function () {
var self = Container.call(this);
self.type = 'infantry';
self.health = 100;
self.maxHealth = 100;
self.damage = 10;
self.cost = 50;
self.isEnemy = false;
self.targetX = 0;
self.targetY = 0;
self.speed = 1;
self.takeDamage = function (amount) {
self.health -= amount;
if (self.health <= 0) {
self.health = 0;
return true; // Unit died
}
return false;
};
self.moveToward = function (x, y) {
var dx = x - self.x;
var dy = y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
var Infantry = Unit.expand(function () {
var self = Unit.call(this);
var graphics = self.attachAsset('soldier', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'infantry';
self.health = 100;
self.maxHealth = 100;
self.damage = 15;
self.cost = 50;
self.speed = 1.5;
return self;
});
var EnemyUnit = Unit.expand(function () {
var self = Unit.call(this);
self.isEnemy = true;
return self;
});
var EnemyInfantry = EnemyUnit.expand(function () {
var self = EnemyUnit.call(this);
var graphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'infantry';
self.health = 100;
self.maxHealth = 100;
self.damage = 12;
self.speed = 1.3;
return self;
});
var EnemyCavalry = EnemyUnit.expand(function () {
var self = EnemyUnit.call(this);
var graphics = self.attachAsset('enemyCavalry', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'cavalry';
self.health = 140;
self.maxHealth = 140;
self.damage = 22;
self.speed = 2.8;
return self;
});
var EnemyArtillery = EnemyUnit.expand(function () {
var self = EnemyUnit.call(this);
var graphics = self.attachAsset('enemyArtillery', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'artillery';
self.health = 75;
self.maxHealth = 75;
self.damage = 35;
self.speed = 0.7;
return self;
});
var Cavalry = Unit.expand(function () {
var self = Unit.call(this);
var graphics = self.attachAsset('cavalry', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'cavalry';
self.health = 150;
self.maxHealth = 150;
self.damage = 25;
self.cost = 100;
self.speed = 3;
return self;
});
var Artillery = Unit.expand(function () {
var self = Unit.call(this);
var graphics = self.attachAsset('artillery', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'artillery';
self.health = 80;
self.maxHealth = 80;
self.damage = 40;
self.cost = 200;
self.speed = 0.8;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game state variables
var money = storage.money || 1000;
var playerUnits = [];
var enemyUnits = [];
var gameState = 'menu'; // menu, battle, event
var battleTimer = 0;
var eventTimer = 0;
var waveNumber = 1;
var lastEventTime = 0;
// Unit costs (affected by events)
var unitCosts = {
infantry: 50,
cavalry: 100,
artillery: 200
};
// Law system
var activeLaws = [];
var maxActiveLaws = 3; // Maximum number of active laws at once
var lawTypes = [{
name: 'Military Conscription',
description: 'Reduce infantry cost by 30%',
cost: 500,
effect: function effect() {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.7);
},
active: false
}, {
name: 'Arms Manufacturing',
description: 'Reduce artillery cost by 25%',
cost: 800,
effect: function effect() {
unitCosts.artillery = Math.floor(unitCosts.artillery * 0.75);
},
active: false
}, {
name: 'Trade Agreements',
description: 'Increase passive income by 100%',
cost: 1000,
effect: function effect() {
// Effect handled in passive income section
},
active: false
}, {
name: 'War Economy',
description: 'All units cost 20% more but deal 50% more damage',
cost: 1200,
effect: function effect() {
unitCosts.infantry = Math.floor(unitCosts.infantry * 1.2);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 1.2);
unitCosts.artillery = Math.floor(unitCosts.artillery * 1.2);
// Damage boost handled in combat
},
active: false
}];
// AI Generated Laws Templates
var aiLawTemplates = [{
namePrefix: 'Economic Stimulus',
descriptions: ['Boost trade revenue', 'Increase tax efficiency', 'Stimulate market growth'],
effects: [function () {/* Income boost handled in passive income */}, function () {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.9);
}, function () {
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 0.85);
}]
}, {
namePrefix: 'Military Doctrine',
descriptions: ['Enhanced training programs', 'Advanced tactics', 'Superior logistics'],
effects: [function () {
unitCosts.artillery = Math.floor(unitCosts.artillery * 0.8);
}, function () {/* Damage boost handled in combat */}, function () {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.8);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 0.8);
}]
}, {
namePrefix: 'Defense Initiative',
descriptions: ['Fortification programs', 'Homeland security', 'Strategic reserves'],
effects: [function () {/* Health boost handled in unit creation */}, function () {
unitCosts.artillery = Math.floor(unitCosts.artillery * 1.1);
}, function () {/* Speed boost handled in combat */}]
}, {
namePrefix: 'Industrial Reform',
descriptions: ['Factory modernization', 'Resource optimization', 'Production efficiency'],
effects: [function () {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.75);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 0.75);
}, function () {/* Income boost handled in passive income */}, function () {
unitCosts.artillery = Math.floor(unitCosts.artillery * 0.7);
}]
}];
// Event system
var activeEvents = [];
var eventTypes = [{
name: 'Plague',
description: 'A plague reduces all unit health by 20%',
effect: function effect() {
for (var i = 0; i < playerUnits.length; i++) {
playerUnits[i].health = Math.max(1, Math.floor(playerUnits[i].health * 0.8));
}
}
}, {
name: 'Revolution',
description: 'Economic upheaval reduces income by 30%',
effect: function effect() {
money = Math.floor(money * 0.7);
// Revolution also disables all active laws
for (var i = 0; i < lawTypes.length; i++) {
lawTypes[i].active = false;
}
activeLaws = [];
resetUnitCosts();
}
}, {
name: 'Military Reform',
description: 'Government increases unit costs by 50%',
effect: function effect() {
unitCosts.infantry = Math.floor(unitCosts.infantry * 1.5);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 1.5);
unitCosts.artillery = Math.floor(unitCosts.artillery * 1.5);
}
}, {
name: 'Government Coup',
description: 'New government allows creation of beneficial laws',
effect: function effect() {
// Reset all laws to available
for (var i = 0; i < lawTypes.length; i++) {
lawTypes[i].active = false;
}
activeLaws = [];
resetUnitCosts();
}
}];
// UI Elements
var moneyText = new Text2(money.toString(), {
size: 60,
fill: 0xFFFFFF
});
moneyText.anchor.set(0, 0);
LK.gui.top.addChild(moneyText);
moneyText.x = 200;
moneyText.y = 20;
var waveText = new Text2('Wave: ' + waveNumber, {
size: 50,
fill: 0xFFFFFF
});
waveText.anchor.set(0.5, 0);
LK.gui.top.addChild(waveText);
var eventText = new Text2('', {
size: 40,
fill: 0xFF6B6B
});
eventText.anchor.set(0.5, 0.5);
LK.gui.center.addChild(eventText);
// Buy buttons
var infantryButton = new Text2('Infantry ($' + unitCosts.infantry + ')', {
size: 45,
fill: 0x4A90E2
});
infantryButton.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(infantryButton);
infantryButton.x = 50;
infantryButton.y = -200;
var cavalryButton = new Text2('Cavalry ($' + unitCosts.cavalry + ')', {
size: 45,
fill: 0x7ED321
});
cavalryButton.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(cavalryButton);
cavalryButton.x = 50;
cavalryButton.y = -150;
var artilleryButton = new Text2('Artillery ($' + unitCosts.artillery + ')', {
size: 45,
fill: 0xD0021B
});
artilleryButton.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(artilleryButton);
artilleryButton.x = 50;
artilleryButton.y = -100;
var startBattleButton = new Text2('Start Battle', {
size: 50,
fill: 0xF39C12
});
startBattleButton.anchor.set(0.5, 1);
LK.gui.bottom.addChild(startBattleButton);
startBattleButton.y = -50;
// Laws UI
var lawsTitle = new Text2('LAWS', {
size: 40,
fill: 0xE74C3C
});
lawsTitle.anchor.set(1, 0);
LK.gui.topRight.addChild(lawsTitle);
lawsTitle.x = -50;
lawsTitle.y = 20;
var lawButtons = [];
for (var i = 0; i < lawTypes.length; i++) {
var lawButton = new Text2(lawTypes[i].name + ' ($' + lawTypes[i].cost + ')', {
size: 35,
fill: 0x95A5A6
});
lawButton.anchor.set(1, 0);
LK.gui.topRight.addChild(lawButton);
lawButton.x = -50;
lawButton.y = 80 + i * 60;
lawButton.lawIndex = i;
lawButtons.push(lawButton);
}
// Functions
function updateMoney() {
moneyText.setText('$' + money);
storage.money = money;
}
function updateButtonTexts() {
infantryButton.setText('Infantry ($' + unitCosts.infantry + ')');
cavalryButton.setText('Cavalry ($' + unitCosts.cavalry + ')');
artilleryButton.setText('Artillery ($' + unitCosts.artillery + ')');
}
function resetUnitCosts() {
unitCosts.infantry = 50;
unitCosts.cavalry = 100;
unitCosts.artillery = 200;
}
function applyAllActiveLaws() {
resetUnitCosts();
for (var i = 0; i < activeLaws.length; i++) {
activeLaws[i].effect();
}
updateButtonTexts();
}
function enactLaw(lawIndex) {
var law = lawTypes[lawIndex];
if (!law.active && money >= law.cost) {
money -= law.cost;
// If we're at max capacity, replace the oldest law with a new AI-generated law
if (activeLaws.length >= maxActiveLaws) {
var oldestLaw = activeLaws[0];
oldestLaw.active = false;
activeLaws.splice(0, 1);
// Generate new AI law to replace the old spot
var newAILaw = generateAILaw();
lawTypes[lawTypes.indexOf(oldestLaw)] = newAILaw;
eventText.setText('Law Replaced: ' + oldestLaw.name + ' → ' + newAILaw.name);
} else {
eventText.setText('Law Enacted: ' + law.name);
}
law.active = true;
activeLaws.push(law);
// AI replaces currently active laws with new ones
if (Math.random() < 0.4) {
// 40% chance
for (var i = 0; i < activeLaws.length; i++) {
if (activeLaws[i] !== law && Math.random() < 0.3) {
// 30% chance per active law (excluding the one just enacted)
var oldLaw = activeLaws[i];
oldLaw.active = false;
var replacementLaw = generateAILaw();
lawTypes[lawTypes.indexOf(oldLaw)] = replacementLaw;
replacementLaw.active = true;
activeLaws[i] = replacementLaw;
eventText.setText(eventText.text + ' | AI Replaced: ' + oldLaw.name + ' → ' + replacementLaw.name);
}
}
}
applyAllActiveLaws();
updateMoney();
updateLawButtons();
LK.getSound('event').play();
LK.setTimeout(function () {
eventText.setText('');
}, 3000);
}
}
function generateAILaw() {
var template = aiLawTemplates[Math.floor(Math.random() * aiLawTemplates.length)];
var effectIndex = Math.floor(Math.random() * template.effects.length);
var lawNumber = Math.floor(Math.random() * 999) + 1;
var newLaw = {
name: template.namePrefix + ' #' + lawNumber,
description: template.descriptions[effectIndex],
cost: Math.floor(Math.random() * 800) + 400,
// 400-1200 cost range
effect: template.effects[effectIndex],
active: false,
isAIGenerated: true,
damageBoost: Math.random() < 0.3 ? 1.3 + Math.random() * 0.4 : 1,
// 30% chance for damage boost
incomeBoost: Math.random() < 0.25 ? 1.5 + Math.random() * 0.5 : 1,
// 25% chance for income boost
healthBoost: Math.random() < 0.2 ? 1.2 + Math.random() * 0.3 : 1,
// 20% chance for health boost
speedBoost: Math.random() < 0.15 ? 1.4 + Math.random() * 0.6 : 1 // 15% chance for speed boost
};
return newLaw;
}
function updateLawButtons() {
for (var i = 0; i < lawButtons.length; i++) {
var law = lawTypes[i];
var button = lawButtons[i];
if (law.active) {
button.fill = 0x27AE60;
button.setText(law.name + ' (ACTIVE)');
} else {
button.fill = money >= law.cost ? 0x3498DB : 0x95A5A6;
button.setText(law.name + ' ($' + law.cost + ')');
}
}
}
function buyUnit(type) {
var cost = unitCosts[type];
if (money >= cost) {
money -= cost;
updateMoney();
var unit;
if (type === 'infantry') {
unit = new Infantry();
} else if (type === 'cavalry') {
unit = new Cavalry();
} else if (type === 'artillery') {
unit = new Artillery();
}
// Apply AI-generated law effects to new units
for (var i = 0; i < activeLaws.length; i++) {
if (activeLaws[i].isAIGenerated) {
if (activeLaws[i].healthBoost > 1) {
unit.health = Math.floor(unit.health * activeLaws[i].healthBoost);
unit.maxHealth = Math.floor(unit.maxHealth * activeLaws[i].healthBoost);
}
if (activeLaws[i].speedBoost > 1) {
unit.speed = unit.speed * activeLaws[i].speedBoost;
}
}
}
unit.x = 200 + Math.random() * 400;
unit.y = 2000 + Math.random() * 200;
playerUnits.push(unit);
game.addChild(unit);
LK.getSound('purchase').play();
}
}
function spawnEnemyWave() {
var enemyCount = waveNumber + 2;
for (var i = 0; i < enemyCount; i++) {
var enemy;
var rand = Math.random();
if (rand < 0.6) {
enemy = new EnemyInfantry();
} else if (rand < 0.8) {
enemy = new EnemyCavalry();
} else {
enemy = new EnemyArtillery();
}
enemy.x = 800 + Math.random() * 800;
enemy.y = 500 + Math.random() * 200;
enemyUnits.push(enemy);
game.addChild(enemy);
}
}
function startBattle() {
if (playerUnits.length === 0) return;
gameState = 'battle';
battleTimer = 0;
spawnEnemyWave();
LK.getSound('battle').play();
}
function triggerRandomEvent() {
if (Math.random() < 0.3 && LK.ticks - lastEventTime > 1800) {
// 30 seconds
var eventIndex = Math.floor(Math.random() * eventTypes.length);
var event = eventTypes[eventIndex];
eventText.setText(event.name + ': ' + event.description);
event.effect();
updateButtonTexts();
LK.getSound('event').play();
lastEventTime = LK.ticks;
// Clear event text after 3 seconds
LK.setTimeout(function () {
eventText.setText('');
}, 3000);
}
}
function findNearestEnemy(unit) {
var nearest = null;
var nearestDistance = Infinity;
var targets = unit.isEnemy ? playerUnits : enemyUnits;
for (var i = 0; i < targets.length; i++) {
var target = targets[i];
var dx = target.x - unit.x;
var dy = target.y - unit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = target;
}
}
return nearest;
}
function updateBattle() {
// Move and attack player units
for (var i = playerUnits.length - 1; i >= 0; i--) {
var unit = playerUnits[i];
var target = findNearestEnemy(unit);
if (target) {
var dx = target.x - unit.x;
var dy = target.y - unit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 100) {
unit.moveToward(target.x, target.y);
} else if (battleTimer % 60 === 0) {
// Attack once per second
var damage = unit.damage;
// Check if War Economy law is active
for (var j = 0; j < activeLaws.length; j++) {
if (activeLaws[j].name === 'War Economy') {
damage = Math.floor(damage * 1.5);
break;
}
// Apply AI-generated law damage boosts
if (activeLaws[j].isAIGenerated && activeLaws[j].damageBoost > 1) {
damage = Math.floor(damage * activeLaws[j].damageBoost);
}
}
var killed = target.takeDamage(damage);
if (killed) {
var targetIndex = enemyUnits.indexOf(target);
if (targetIndex !== -1) {
enemyUnits[targetIndex].destroy();
enemyUnits.splice(targetIndex, 1);
money += 25; // Reward for killing enemy
updateMoney();
}
}
}
}
}
// Move and attack enemy units
for (var i = enemyUnits.length - 1; i >= 0; i--) {
var unit = enemyUnits[i];
var target = findNearestEnemy(unit);
if (target) {
var dx = target.x - unit.x;
var dy = target.y - unit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 100) {
unit.moveToward(target.x, target.y);
} else if (battleTimer % 60 === 0) {
// Attack once per second
var killed = target.takeDamage(unit.damage);
if (killed) {
var targetIndex = playerUnits.indexOf(target);
if (targetIndex !== -1) {
playerUnits[targetIndex].destroy();
playerUnits.splice(targetIndex, 1);
}
}
}
}
}
// Check battle end conditions
if (enemyUnits.length === 0) {
// Player won
gameState = 'menu';
waveNumber++;
waveText.setText('Wave: ' + waveNumber);
money += 100 * waveNumber; // Wave completion bonus
updateMoney();
} else if (playerUnits.length === 0) {
// Player lost
LK.showGameOver();
}
}
// Event handlers
infantryButton.down = function () {
buyUnit('infantry');
};
cavalryButton.down = function () {
buyUnit('cavalry');
};
artilleryButton.down = function () {
buyUnit('artillery');
};
startBattleButton.down = function () {
if (gameState === 'menu') {
startBattle();
}
};
// Law button handlers
for (var i = 0; i < lawButtons.length; i++) {
lawButtons[i].down = function (index) {
return function () {
enactLaw(index);
};
}(i);
}
// Main game update loop
game.update = function () {
if (gameState === 'battle') {
battleTimer++;
updateBattle();
}
// Passive income every 3 seconds
if (LK.ticks % 180 === 0) {
var income = 50;
// Check if Trade Agreements law is active
for (var i = 0; i < activeLaws.length; i++) {
if (activeLaws[i].name === 'Trade Agreements') {
income *= 2;
break;
}
// Apply AI-generated law income boosts
if (activeLaws[i].isAIGenerated && activeLaws[i].incomeBoost > 1) {
income = Math.floor(income * activeLaws[i].incomeBoost);
}
}
money += income;
updateMoney();
}
// Trigger random events
triggerRandomEvent();
// Update law button states
updateLawButtons();
// Check for bankruptcy
if (money < 0) {
LK.showGameOver();
}
}; /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var Unit = Container.expand(function () {
var self = Container.call(this);
self.type = 'infantry';
self.health = 100;
self.maxHealth = 100;
self.damage = 10;
self.cost = 50;
self.isEnemy = false;
self.targetX = 0;
self.targetY = 0;
self.speed = 1;
self.takeDamage = function (amount) {
self.health -= amount;
if (self.health <= 0) {
self.health = 0;
return true; // Unit died
}
return false;
};
self.moveToward = function (x, y) {
var dx = x - self.x;
var dy = y - self.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 5) {
self.x += dx / distance * self.speed;
self.y += dy / distance * self.speed;
}
};
return self;
});
var Infantry = Unit.expand(function () {
var self = Unit.call(this);
var graphics = self.attachAsset('soldier', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'infantry';
self.health = 100;
self.maxHealth = 100;
self.damage = 15;
self.cost = 50;
self.speed = 1.5;
return self;
});
var EnemyUnit = Unit.expand(function () {
var self = Unit.call(this);
self.isEnemy = true;
return self;
});
var EnemyInfantry = EnemyUnit.expand(function () {
var self = EnemyUnit.call(this);
var graphics = self.attachAsset('enemy', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'infantry';
self.health = 100;
self.maxHealth = 100;
self.damage = 12;
self.speed = 1.3;
return self;
});
var EnemyCavalry = EnemyUnit.expand(function () {
var self = EnemyUnit.call(this);
var graphics = self.attachAsset('enemyCavalry', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'cavalry';
self.health = 140;
self.maxHealth = 140;
self.damage = 22;
self.speed = 2.8;
return self;
});
var EnemyArtillery = EnemyUnit.expand(function () {
var self = EnemyUnit.call(this);
var graphics = self.attachAsset('enemyArtillery', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'artillery';
self.health = 75;
self.maxHealth = 75;
self.damage = 35;
self.speed = 0.7;
return self;
});
var Cavalry = Unit.expand(function () {
var self = Unit.call(this);
var graphics = self.attachAsset('cavalry', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'cavalry';
self.health = 150;
self.maxHealth = 150;
self.damage = 25;
self.cost = 100;
self.speed = 3;
return self;
});
var Artillery = Unit.expand(function () {
var self = Unit.call(this);
var graphics = self.attachAsset('artillery', {
anchorX: 0.5,
anchorY: 0.5
});
self.type = 'artillery';
self.health = 80;
self.maxHealth = 80;
self.damage = 40;
self.cost = 200;
self.speed = 0.8;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x2c3e50
});
/****
* Game Code
****/
// Game state variables
var money = storage.money || 1000;
var playerUnits = [];
var enemyUnits = [];
var gameState = 'menu'; // menu, battle, event
var battleTimer = 0;
var eventTimer = 0;
var waveNumber = 1;
var lastEventTime = 0;
// Unit costs (affected by events)
var unitCosts = {
infantry: 50,
cavalry: 100,
artillery: 200
};
// Law system
var activeLaws = [];
var maxActiveLaws = 3; // Maximum number of active laws at once
var lawTypes = [{
name: 'Military Conscription',
description: 'Reduce infantry cost by 30%',
cost: 500,
effect: function effect() {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.7);
},
active: false
}, {
name: 'Arms Manufacturing',
description: 'Reduce artillery cost by 25%',
cost: 800,
effect: function effect() {
unitCosts.artillery = Math.floor(unitCosts.artillery * 0.75);
},
active: false
}, {
name: 'Trade Agreements',
description: 'Increase passive income by 100%',
cost: 1000,
effect: function effect() {
// Effect handled in passive income section
},
active: false
}, {
name: 'War Economy',
description: 'All units cost 20% more but deal 50% more damage',
cost: 1200,
effect: function effect() {
unitCosts.infantry = Math.floor(unitCosts.infantry * 1.2);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 1.2);
unitCosts.artillery = Math.floor(unitCosts.artillery * 1.2);
// Damage boost handled in combat
},
active: false
}];
// AI Generated Laws Templates
var aiLawTemplates = [{
namePrefix: 'Economic Stimulus',
descriptions: ['Boost trade revenue', 'Increase tax efficiency', 'Stimulate market growth'],
effects: [function () {/* Income boost handled in passive income */}, function () {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.9);
}, function () {
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 0.85);
}]
}, {
namePrefix: 'Military Doctrine',
descriptions: ['Enhanced training programs', 'Advanced tactics', 'Superior logistics'],
effects: [function () {
unitCosts.artillery = Math.floor(unitCosts.artillery * 0.8);
}, function () {/* Damage boost handled in combat */}, function () {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.8);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 0.8);
}]
}, {
namePrefix: 'Defense Initiative',
descriptions: ['Fortification programs', 'Homeland security', 'Strategic reserves'],
effects: [function () {/* Health boost handled in unit creation */}, function () {
unitCosts.artillery = Math.floor(unitCosts.artillery * 1.1);
}, function () {/* Speed boost handled in combat */}]
}, {
namePrefix: 'Industrial Reform',
descriptions: ['Factory modernization', 'Resource optimization', 'Production efficiency'],
effects: [function () {
unitCosts.infantry = Math.floor(unitCosts.infantry * 0.75);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 0.75);
}, function () {/* Income boost handled in passive income */}, function () {
unitCosts.artillery = Math.floor(unitCosts.artillery * 0.7);
}]
}];
// Event system
var activeEvents = [];
var eventTypes = [{
name: 'Plague',
description: 'A plague reduces all unit health by 20%',
effect: function effect() {
for (var i = 0; i < playerUnits.length; i++) {
playerUnits[i].health = Math.max(1, Math.floor(playerUnits[i].health * 0.8));
}
}
}, {
name: 'Revolution',
description: 'Economic upheaval reduces income by 30%',
effect: function effect() {
money = Math.floor(money * 0.7);
// Revolution also disables all active laws
for (var i = 0; i < lawTypes.length; i++) {
lawTypes[i].active = false;
}
activeLaws = [];
resetUnitCosts();
}
}, {
name: 'Military Reform',
description: 'Government increases unit costs by 50%',
effect: function effect() {
unitCosts.infantry = Math.floor(unitCosts.infantry * 1.5);
unitCosts.cavalry = Math.floor(unitCosts.cavalry * 1.5);
unitCosts.artillery = Math.floor(unitCosts.artillery * 1.5);
}
}, {
name: 'Government Coup',
description: 'New government allows creation of beneficial laws',
effect: function effect() {
// Reset all laws to available
for (var i = 0; i < lawTypes.length; i++) {
lawTypes[i].active = false;
}
activeLaws = [];
resetUnitCosts();
}
}];
// UI Elements
var moneyText = new Text2(money.toString(), {
size: 60,
fill: 0xFFFFFF
});
moneyText.anchor.set(0, 0);
LK.gui.top.addChild(moneyText);
moneyText.x = 200;
moneyText.y = 20;
var waveText = new Text2('Wave: ' + waveNumber, {
size: 50,
fill: 0xFFFFFF
});
waveText.anchor.set(0.5, 0);
LK.gui.top.addChild(waveText);
var eventText = new Text2('', {
size: 40,
fill: 0xFF6B6B
});
eventText.anchor.set(0.5, 0.5);
LK.gui.center.addChild(eventText);
// Buy buttons
var infantryButton = new Text2('Infantry ($' + unitCosts.infantry + ')', {
size: 45,
fill: 0x4A90E2
});
infantryButton.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(infantryButton);
infantryButton.x = 50;
infantryButton.y = -200;
var cavalryButton = new Text2('Cavalry ($' + unitCosts.cavalry + ')', {
size: 45,
fill: 0x7ED321
});
cavalryButton.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(cavalryButton);
cavalryButton.x = 50;
cavalryButton.y = -150;
var artilleryButton = new Text2('Artillery ($' + unitCosts.artillery + ')', {
size: 45,
fill: 0xD0021B
});
artilleryButton.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(artilleryButton);
artilleryButton.x = 50;
artilleryButton.y = -100;
var startBattleButton = new Text2('Start Battle', {
size: 50,
fill: 0xF39C12
});
startBattleButton.anchor.set(0.5, 1);
LK.gui.bottom.addChild(startBattleButton);
startBattleButton.y = -50;
// Laws UI
var lawsTitle = new Text2('LAWS', {
size: 40,
fill: 0xE74C3C
});
lawsTitle.anchor.set(1, 0);
LK.gui.topRight.addChild(lawsTitle);
lawsTitle.x = -50;
lawsTitle.y = 20;
var lawButtons = [];
for (var i = 0; i < lawTypes.length; i++) {
var lawButton = new Text2(lawTypes[i].name + ' ($' + lawTypes[i].cost + ')', {
size: 35,
fill: 0x95A5A6
});
lawButton.anchor.set(1, 0);
LK.gui.topRight.addChild(lawButton);
lawButton.x = -50;
lawButton.y = 80 + i * 60;
lawButton.lawIndex = i;
lawButtons.push(lawButton);
}
// Functions
function updateMoney() {
moneyText.setText('$' + money);
storage.money = money;
}
function updateButtonTexts() {
infantryButton.setText('Infantry ($' + unitCosts.infantry + ')');
cavalryButton.setText('Cavalry ($' + unitCosts.cavalry + ')');
artilleryButton.setText('Artillery ($' + unitCosts.artillery + ')');
}
function resetUnitCosts() {
unitCosts.infantry = 50;
unitCosts.cavalry = 100;
unitCosts.artillery = 200;
}
function applyAllActiveLaws() {
resetUnitCosts();
for (var i = 0; i < activeLaws.length; i++) {
activeLaws[i].effect();
}
updateButtonTexts();
}
function enactLaw(lawIndex) {
var law = lawTypes[lawIndex];
if (!law.active && money >= law.cost) {
money -= law.cost;
// If we're at max capacity, replace the oldest law with a new AI-generated law
if (activeLaws.length >= maxActiveLaws) {
var oldestLaw = activeLaws[0];
oldestLaw.active = false;
activeLaws.splice(0, 1);
// Generate new AI law to replace the old spot
var newAILaw = generateAILaw();
lawTypes[lawTypes.indexOf(oldestLaw)] = newAILaw;
eventText.setText('Law Replaced: ' + oldestLaw.name + ' → ' + newAILaw.name);
} else {
eventText.setText('Law Enacted: ' + law.name);
}
law.active = true;
activeLaws.push(law);
// AI replaces currently active laws with new ones
if (Math.random() < 0.4) {
// 40% chance
for (var i = 0; i < activeLaws.length; i++) {
if (activeLaws[i] !== law && Math.random() < 0.3) {
// 30% chance per active law (excluding the one just enacted)
var oldLaw = activeLaws[i];
oldLaw.active = false;
var replacementLaw = generateAILaw();
lawTypes[lawTypes.indexOf(oldLaw)] = replacementLaw;
replacementLaw.active = true;
activeLaws[i] = replacementLaw;
eventText.setText(eventText.text + ' | AI Replaced: ' + oldLaw.name + ' → ' + replacementLaw.name);
}
}
}
applyAllActiveLaws();
updateMoney();
updateLawButtons();
LK.getSound('event').play();
LK.setTimeout(function () {
eventText.setText('');
}, 3000);
}
}
function generateAILaw() {
var template = aiLawTemplates[Math.floor(Math.random() * aiLawTemplates.length)];
var effectIndex = Math.floor(Math.random() * template.effects.length);
var lawNumber = Math.floor(Math.random() * 999) + 1;
var newLaw = {
name: template.namePrefix + ' #' + lawNumber,
description: template.descriptions[effectIndex],
cost: Math.floor(Math.random() * 800) + 400,
// 400-1200 cost range
effect: template.effects[effectIndex],
active: false,
isAIGenerated: true,
damageBoost: Math.random() < 0.3 ? 1.3 + Math.random() * 0.4 : 1,
// 30% chance for damage boost
incomeBoost: Math.random() < 0.25 ? 1.5 + Math.random() * 0.5 : 1,
// 25% chance for income boost
healthBoost: Math.random() < 0.2 ? 1.2 + Math.random() * 0.3 : 1,
// 20% chance for health boost
speedBoost: Math.random() < 0.15 ? 1.4 + Math.random() * 0.6 : 1 // 15% chance for speed boost
};
return newLaw;
}
function updateLawButtons() {
for (var i = 0; i < lawButtons.length; i++) {
var law = lawTypes[i];
var button = lawButtons[i];
if (law.active) {
button.fill = 0x27AE60;
button.setText(law.name + ' (ACTIVE)');
} else {
button.fill = money >= law.cost ? 0x3498DB : 0x95A5A6;
button.setText(law.name + ' ($' + law.cost + ')');
}
}
}
function buyUnit(type) {
var cost = unitCosts[type];
if (money >= cost) {
money -= cost;
updateMoney();
var unit;
if (type === 'infantry') {
unit = new Infantry();
} else if (type === 'cavalry') {
unit = new Cavalry();
} else if (type === 'artillery') {
unit = new Artillery();
}
// Apply AI-generated law effects to new units
for (var i = 0; i < activeLaws.length; i++) {
if (activeLaws[i].isAIGenerated) {
if (activeLaws[i].healthBoost > 1) {
unit.health = Math.floor(unit.health * activeLaws[i].healthBoost);
unit.maxHealth = Math.floor(unit.maxHealth * activeLaws[i].healthBoost);
}
if (activeLaws[i].speedBoost > 1) {
unit.speed = unit.speed * activeLaws[i].speedBoost;
}
}
}
unit.x = 200 + Math.random() * 400;
unit.y = 2000 + Math.random() * 200;
playerUnits.push(unit);
game.addChild(unit);
LK.getSound('purchase').play();
}
}
function spawnEnemyWave() {
var enemyCount = waveNumber + 2;
for (var i = 0; i < enemyCount; i++) {
var enemy;
var rand = Math.random();
if (rand < 0.6) {
enemy = new EnemyInfantry();
} else if (rand < 0.8) {
enemy = new EnemyCavalry();
} else {
enemy = new EnemyArtillery();
}
enemy.x = 800 + Math.random() * 800;
enemy.y = 500 + Math.random() * 200;
enemyUnits.push(enemy);
game.addChild(enemy);
}
}
function startBattle() {
if (playerUnits.length === 0) return;
gameState = 'battle';
battleTimer = 0;
spawnEnemyWave();
LK.getSound('battle').play();
}
function triggerRandomEvent() {
if (Math.random() < 0.3 && LK.ticks - lastEventTime > 1800) {
// 30 seconds
var eventIndex = Math.floor(Math.random() * eventTypes.length);
var event = eventTypes[eventIndex];
eventText.setText(event.name + ': ' + event.description);
event.effect();
updateButtonTexts();
LK.getSound('event').play();
lastEventTime = LK.ticks;
// Clear event text after 3 seconds
LK.setTimeout(function () {
eventText.setText('');
}, 3000);
}
}
function findNearestEnemy(unit) {
var nearest = null;
var nearestDistance = Infinity;
var targets = unit.isEnemy ? playerUnits : enemyUnits;
for (var i = 0; i < targets.length; i++) {
var target = targets[i];
var dx = target.x - unit.x;
var dy = target.y - unit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance < nearestDistance) {
nearestDistance = distance;
nearest = target;
}
}
return nearest;
}
function updateBattle() {
// Move and attack player units
for (var i = playerUnits.length - 1; i >= 0; i--) {
var unit = playerUnits[i];
var target = findNearestEnemy(unit);
if (target) {
var dx = target.x - unit.x;
var dy = target.y - unit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 100) {
unit.moveToward(target.x, target.y);
} else if (battleTimer % 60 === 0) {
// Attack once per second
var damage = unit.damage;
// Check if War Economy law is active
for (var j = 0; j < activeLaws.length; j++) {
if (activeLaws[j].name === 'War Economy') {
damage = Math.floor(damage * 1.5);
break;
}
// Apply AI-generated law damage boosts
if (activeLaws[j].isAIGenerated && activeLaws[j].damageBoost > 1) {
damage = Math.floor(damage * activeLaws[j].damageBoost);
}
}
var killed = target.takeDamage(damage);
if (killed) {
var targetIndex = enemyUnits.indexOf(target);
if (targetIndex !== -1) {
enemyUnits[targetIndex].destroy();
enemyUnits.splice(targetIndex, 1);
money += 25; // Reward for killing enemy
updateMoney();
}
}
}
}
}
// Move and attack enemy units
for (var i = enemyUnits.length - 1; i >= 0; i--) {
var unit = enemyUnits[i];
var target = findNearestEnemy(unit);
if (target) {
var dx = target.x - unit.x;
var dy = target.y - unit.y;
var distance = Math.sqrt(dx * dx + dy * dy);
if (distance > 100) {
unit.moveToward(target.x, target.y);
} else if (battleTimer % 60 === 0) {
// Attack once per second
var killed = target.takeDamage(unit.damage);
if (killed) {
var targetIndex = playerUnits.indexOf(target);
if (targetIndex !== -1) {
playerUnits[targetIndex].destroy();
playerUnits.splice(targetIndex, 1);
}
}
}
}
}
// Check battle end conditions
if (enemyUnits.length === 0) {
// Player won
gameState = 'menu';
waveNumber++;
waveText.setText('Wave: ' + waveNumber);
money += 100 * waveNumber; // Wave completion bonus
updateMoney();
} else if (playerUnits.length === 0) {
// Player lost
LK.showGameOver();
}
}
// Event handlers
infantryButton.down = function () {
buyUnit('infantry');
};
cavalryButton.down = function () {
buyUnit('cavalry');
};
artilleryButton.down = function () {
buyUnit('artillery');
};
startBattleButton.down = function () {
if (gameState === 'menu') {
startBattle();
}
};
// Law button handlers
for (var i = 0; i < lawButtons.length; i++) {
lawButtons[i].down = function (index) {
return function () {
enactLaw(index);
};
}(i);
}
// Main game update loop
game.update = function () {
if (gameState === 'battle') {
battleTimer++;
updateBattle();
}
// Passive income every 3 seconds
if (LK.ticks % 180 === 0) {
var income = 50;
// Check if Trade Agreements law is active
for (var i = 0; i < activeLaws.length; i++) {
if (activeLaws[i].name === 'Trade Agreements') {
income *= 2;
break;
}
// Apply AI-generated law income boosts
if (activeLaws[i].isAIGenerated && activeLaws[i].incomeBoost > 1) {
income = Math.floor(income * activeLaws[i].incomeBoost);
}
}
money += income;
updateMoney();
}
// Trigger random events
triggerRandomEvent();
// Update law button states
updateLawButtons();
// Check for bankruptcy
if (money < 0) {
LK.showGameOver();
}
};