/****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var ContractCard = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('contractCard', {
anchorX: 0.5,
anchorY: 0.5
});
var titleText = new Text2('', {
size: 22,
fill: '#ffffff'
});
titleText.anchor.set(0.5, 0.1);
titleText.y = 20;
self.addChild(titleText);
var requireText = new Text2('', {
size: 16,
fill: '#cccccc'
});
requireText.anchor.set(0.5, 0.5);
requireText.y = 100;
self.addChild(requireText);
var rewardText = new Text2('', {
size: 18,
fill: '#FFD700'
});
rewardText.anchor.set(0.5, 0.5);
rewardText.y = 150;
self.addChild(rewardText);
var completeButton = new Text2('Complete', {
size: 14,
fill: '#000000'
});
completeButton.anchor.set(0.5, 0.5);
completeButton.y = 200;
self.addChild(completeButton);
self.contractId = 0;
self.requirements = {};
self.reward = 0;
self.completed = false;
self.setContract = function (id, reqs, reward) {
self.contractId = id;
self.requirements = reqs;
self.reward = reward;
self.completed = false;
var reqStr = '';
for (var key in reqs) {
reqStr += key + ': ' + reqs[key] + '\n';
}
titleText.setText('Contract ' + id);
requireText.setText(reqStr);
rewardText.setText('Reward: ' + reward + ' IP');
bg.tint = 0xFFFFFF;
};
self.markCompleted = function () {
self.completed = true;
bg.tint = 0xA0D995;
};
self.down = function (x, y, obj) {
if (!self.completed) {
self.emit('tryComplete');
}
};
return self;
});
var MarketGoodUI = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('marketButton', {
anchorX: 0.5,
anchorY: 0.5
});
var nameText = new Text2('', {
size: 14,
fill: '#ffffff'
});
nameText.anchor.set(0.5, 0.2);
nameText.y = -15;
self.addChild(nameText);
var priceText = new Text2('', {
size: 12,
fill: '#FFD700'
});
priceText.anchor.set(0.5, 0.5);
priceText.y = 5;
self.addChild(priceText);
var stockText = new Text2('', {
size: 11,
fill: '#cccccc'
});
stockText.anchor.set(0.5, 0.8);
stockText.y = 15;
self.addChild(stockText);
self.goodName = '';
self.price = 0;
self.stock = 0;
self.setGood = function (name, price, stock) {
self.goodName = name;
self.price = price;
self.stock = stock;
nameText.setText(name);
priceText.setText(price + '$');
stockText.setText('Stock: ' + stock);
};
self.setStock = function (stock) {
self.stock = stock;
stockText.setText('Stock: ' + stock);
};
self.down = function (x, y, obj) {
self.emit('buyGood');
};
return self;
});
var ProductionFacility = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('productionChain', {
anchorX: 0.5,
anchorY: 0.5
});
var nameText = new Text2('', {
size: 18,
fill: '#ffffff'
});
nameText.anchor.set(0.5, 0.5);
nameText.y = -20;
self.addChild(nameText);
var workerText = new Text2('0W', {
size: 16,
fill: '#cccccc'
});
workerText.anchor.set(0.5, 0.5);
workerText.y = 10;
self.addChild(workerText);
self.facilityName = '';
self.inputResource = '';
self.outputGood = '';
self.workersAssigned = 0;
self.isActive = false;
self.setFacility = function (name, input, output) {
self.facilityName = name;
self.inputResource = input;
self.outputGood = output;
nameText.setText(name);
};
self.setWorkers = function (count) {
self.workersAssigned = count;
if (count > 0) {
self.isActive = true;
bg.tint = 0x4CAF50;
} else {
self.isActive = false;
bg.tint = 0xFFFFFF;
}
workerText.setText(count + 'W');
};
self.down = function (x, y, obj) {
self.emit('selected');
};
return self;
});
var ResourceDisplay = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('rawMaterial', {
anchorX: 0.5,
anchorY: 0.5
});
var label = new Text2('0', {
size: 24,
fill: '#ffffff'
});
label.anchor.set(0.5, 0.5);
self.addChild(label);
self.setValue = function (value) {
label.setText(String(value));
};
self.width = bg.width;
self.height = bg.height;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
// Game state
var currentTurn = 1;
var maxTurns = 8;
var totalPrestige = 0;
var money = 50;
var availableWorkers = 5;
var totalWorkers = 5;
var inventory = {
wood: 0,
coal: 0,
wool: 0,
corn: 0,
planks: 0,
steel: 0,
fabric: 0,
schnapps: 0
};
var productionFacilities = [];
var contracts = [];
var marketGoods = {};
var selectedFacility = null;
// Market prices
var marketPrices = {
planks: 20,
steel: 25,
fabric: 15,
schnapps: 30
};
var marketStock = {
planks: 10,
steel: 8,
fabric: 12,
schnapps: 6
};
// UI Elements
var turnsText = new Text2('Turn: 1/8', {
size: 40,
fill: '#ffffff'
});
turnsText.anchor.set(0.5, 0);
turnsText.x = 2048 / 2;
turnsText.y = 20;
game.addChild(turnsText);
var prestigeText = new Text2('IP: 0', {
size: 36,
fill: '#FFD700'
});
prestigeText.anchor.set(0, 0);
prestigeText.x = 50;
prestigeText.y = 30;
game.addChild(prestigeText);
var moneyText = new Text2('Money: 50', {
size: 32,
fill: '#90EE90'
});
moneyText.anchor.set(0, 0);
moneyText.x = 50;
moneyText.y = 80;
game.addChild(moneyText);
var workersText = new Text2('Workers: 5/5', {
size: 32,
fill: '#87CEEB'
});
workersText.anchor.set(0, 0);
workersText.x = 50;
moneyText.y = 130;
game.addChild(workersText);
// Inventory display
var inventoryContainer = new Container();
inventoryContainer.x = 50;
inventoryContainer.y = 200;
game.addChild(inventoryContainer);
var inventoryLabel = new Text2('Raw Materials', {
size: 24,
fill: '#ffffff'
});
inventoryLabel.y = 0;
inventoryContainer.addChild(inventoryLabel);
var resourceDisplays = {};
var resourceNames = ['wood', 'coal', 'wool', 'corn'];
for (var i = 0; i < resourceNames.length; i++) {
var display = new ResourceDisplay();
display.x = i * 80;
display.y = 40;
inventoryContainer.addChild(display);
resourceDisplays[resourceNames[i]] = display;
}
// Processed goods display
var goodsContainer = new Container();
goodsContainer.x = 50;
goodsContainer.y = 330;
game.addChild(goodsContainer);
var goodsLabel = new Text2('Processed Goods', {
size: 24,
fill: '#ffffff'
});
goodsLabel.y = 0;
goodsContainer.addChild(goodsLabel);
var goodDisplays = {};
var goodNames = ['planks', 'steel', 'fabric', 'schnapps'];
for (var i = 0; i < goodNames.length; i++) {
var display = new ResourceDisplay();
display.x = i * 80;
display.y = 40;
goodsContainer.addChild(display);
goodDisplays[goodNames[i]] = display;
}
// Production facilities
var facilitiesContainer = new Container();
facilitiesContainer.x = 600;
facilitiesContainer.y = 200;
game.addChild(facilitiesContainer);
var facilitiesLabel = new Text2('Production Facilities', {
size: 24,
fill: '#ffffff'
});
facilitiesLabel.y = 0;
facilitiesContainer.addChild(facilitiesLabel);
var facilities = [{
name: 'Lumber Mill',
input: 'wood',
output: 'planks'
}, {
name: 'Foundry',
input: 'coal',
output: 'steel'
}, {
name: 'Textile Mill',
input: 'wool',
output: 'fabric'
}, {
name: 'Distillery',
input: 'corn',
output: 'schnapps'
}];
for (var i = 0; i < facilities.length; i++) {
var facility = new ProductionFacility();
facility.setFacility(facilities[i].name, facilities[i].input, facilities[i].output);
facility.x = i % 2 * 150;
facility.y = Math.floor(i / 2) * 120 + 40;
facilitiesContainer.addChild(facility);
productionFacilities.push({
ui: facility,
name: facilities[i].name,
input: facilities[i].input,
output: facilities[i].output,
workers: 0
});
}
// Contracts display
var contractsContainer = new Container();
contractsContainer.x = 1200;
contractsContainer.y = 200;
game.addChild(contractsContainer);
var contractsLabel = new Text2('Contracts', {
size: 24,
fill: '#ffffff'
});
contractsLabel.y = 0;
contractsContainer.addChild(contractsLabel);
function generateContract(id) {
var requirements = {};
var requiredGoods = [];
var numGoods = Math.floor(Math.random() * 2) + 2;
for (var i = 0; i < numGoods; i++) {
var good = goodNames[Math.floor(Math.random() * goodNames.length)];
if (!requirements[good]) {
requiredGoods.push(good);
requirements[good] = Math.floor(Math.random() * 3) + 1;
}
}
var reward = 30 + numGoods * 10;
return {
id: id,
requirements: requirements,
reward: reward,
completed: false
};
}
function initializeContracts() {
contracts = [];
for (var i = 0; i < 2; i++) {
contracts.push(generateContract(i + 1));
}
contractsContainer.removeChildren(1);
for (var i = 0; i < contracts.length; i++) {
var card = new ContractCard();
card.setContract(contracts[i].id, contracts[i].requirements, contracts[i].reward);
card.x = i * 220;
card.y = 40;
contractsContainer.addChild(card);
contracts[i].ui = card;
}
}
initializeContracts();
// Market display
var marketContainer = new Container();
marketContainer.x = 1200;
marketContainer.y = 650;
game.addChild(marketContainer);
var marketLabel = new Text2('Global Market', {
size: 24,
fill: '#ffffff'
});
marketLabel.y = 0;
marketContainer.addChild(marketLabel);
for (var i = 0; i < goodNames.length; i++) {
var marketUI = new MarketGoodUI();
marketUI.setGood(goodNames[i], marketPrices[goodNames[i]], marketStock[goodNames[i]]);
marketUI.x = i * 110;
marketUI.y = 40;
marketContainer.addChild(marketUI);
marketGoods[goodNames[i]] = {
ui: marketUI,
stock: marketStock[goodNames[i]]
};
}
// End turn button
var endTurnButton = new Container();
endTurnButton.x = 2048 / 2;
endTurnButton.y = 2600;
game.addChild(endTurnButton);
var buttonBg = endTurnButton.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2('End Turn', {
size: 24,
fill: '#000000'
});
buttonText.anchor.set(0.5, 0.5);
endTurnButton.addChild(buttonText);
endTurnButton.down = function (x, y, obj) {
processProduction();
endTurn();
};
function updateUI() {
turnsText.setText('Turn: ' + currentTurn + '/' + maxTurns);
prestigeText.setText('IP: ' + totalPrestige);
moneyText.setText('Money: ' + money);
workersText.setText('Workers: ' + availableWorkers + '/' + totalWorkers);
for (var i = 0; i < resourceNames.length; i++) {
resourceDisplays[resourceNames[i]].setValue(inventory[resourceNames[i]]);
}
for (var i = 0; i < goodNames.length; i++) {
goodDisplays[goodNames[i]].setValue(inventory[goodNames[i]]);
}
}
function processProduction() {
for (var i = 0; i < productionFacilities.length; i++) {
var facility = productionFacilities[i];
if (facility.workers > 0 && inventory[facility.input] > 0) {
var produced = facility.workers;
inventory[facility.input] -= produced;
inventory[facility.output] += produced;
}
}
}
function restockMarket() {
for (var good in marketStock) {
marketStock[good] = Math.floor(Math.random() * 10) + 5;
marketGoods[good].stock = marketStock[good];
marketGoods[good].ui.setStock(marketStock[good]);
}
}
function endTurn() {
currentTurn++;
availableWorkers = totalWorkers;
for (var i = 0; i < productionFacilities.length; i++) {
productionFacilities[i].workers = 0;
productionFacilities[i].ui.setWorkers(0);
}
for (var i = 0; i < resourceNames.length; i++) {
inventory[resourceNames[i]] += 1;
}
restockMarket();
updateUI();
if (currentTurn > maxTurns) {
endGame();
}
}
function tryCompleteContract(contractIndex) {
var contract = contracts[contractIndex];
if (contract.completed) return;
var canComplete = true;
for (var good in contract.requirements) {
if (!inventory[good] || inventory[good] < contract.requirements[good]) {
canComplete = false;
break;
}
}
if (canComplete) {
for (var good in contract.requirements) {
inventory[good] -= contract.requirements[good];
}
totalPrestige += contract.reward;
contract.completed = true;
contract.ui.markCompleted();
totalWorkers = Math.min(totalWorkers + 1, 7);
availableWorkers = Math.min(availableWorkers + 1, 7);
LK.getSound('complete').play();
updateUI();
}
}
function buyGood(goodName) {
if (money >= marketPrices[goodName] && marketStock[goodName] > 0) {
money -= marketPrices[goodName];
inventory[goodName] += 1;
marketStock[goodName] -= 1;
marketGoods[goodName].ui.setStock(marketStock[goodName]);
LK.getSound('trade').play();
updateUI();
}
}
function sellGood(goodName) {
if (inventory[goodName] > 0) {
money += marketPrices[goodName];
inventory[goodName] -= 1;
marketStock[goodName] += 1;
marketGoods[goodName].ui.setStock(marketStock[goodName]);
LK.getSound('trade').play();
updateUI();
}
}
function endGame() {
var message = '';
if (totalPrestige < 150) {
message = 'Final Score: ' + totalPrestige + ' IP\n\nYour company struggled. Better luck next time!';
} else if (totalPrestige < 250) {
message = 'Final Score: ' + totalPrestige + ' IP\n\nYou showed competence in managing your alliance.';
} else {
message = 'Final Score: ' + totalPrestige + ' IP\n\nYou dominated the industrial landscape!';
}
LK.showGameOver();
}
// Event handlers for facility selection
for (var i = 0; i < productionFacilities.length; i++) {
(function (index) {
productionFacilities[index].ui.on('selected', function () {
if (availableWorkers > 0) {
var facility = productionFacilities[index];
if (facility.workers < 2) {
facility.workers += 1;
availableWorkers -= 1;
facility.ui.setWorkers(facility.workers);
updateUI();
}
}
});
})(i);
}
// Event handlers for contracts
for (var i = 0; i < contracts.length; i++) {
(function (index) {
contracts[index].ui.on('tryComplete', function () {
tryCompleteContract(index);
});
})(i);
}
// Event handlers for market
for (var i = 0; i < goodNames.length; i++) {
(function (goodName) {
marketGoods[goodName].ui.on('buyGood', function () {
buyGood(goodName);
});
})(goodNames[i]);
}
game.update = function () {
// Main game loop - update displays if needed
};
updateUI(); /****
* Plugins
****/
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/****
* Classes
****/
var ContractCard = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('contractCard', {
anchorX: 0.5,
anchorY: 0.5
});
var titleText = new Text2('', {
size: 22,
fill: '#ffffff'
});
titleText.anchor.set(0.5, 0.1);
titleText.y = 20;
self.addChild(titleText);
var requireText = new Text2('', {
size: 16,
fill: '#cccccc'
});
requireText.anchor.set(0.5, 0.5);
requireText.y = 100;
self.addChild(requireText);
var rewardText = new Text2('', {
size: 18,
fill: '#FFD700'
});
rewardText.anchor.set(0.5, 0.5);
rewardText.y = 150;
self.addChild(rewardText);
var completeButton = new Text2('Complete', {
size: 14,
fill: '#000000'
});
completeButton.anchor.set(0.5, 0.5);
completeButton.y = 200;
self.addChild(completeButton);
self.contractId = 0;
self.requirements = {};
self.reward = 0;
self.completed = false;
self.setContract = function (id, reqs, reward) {
self.contractId = id;
self.requirements = reqs;
self.reward = reward;
self.completed = false;
var reqStr = '';
for (var key in reqs) {
reqStr += key + ': ' + reqs[key] + '\n';
}
titleText.setText('Contract ' + id);
requireText.setText(reqStr);
rewardText.setText('Reward: ' + reward + ' IP');
bg.tint = 0xFFFFFF;
};
self.markCompleted = function () {
self.completed = true;
bg.tint = 0xA0D995;
};
self.down = function (x, y, obj) {
if (!self.completed) {
self.emit('tryComplete');
}
};
return self;
});
var MarketGoodUI = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('marketButton', {
anchorX: 0.5,
anchorY: 0.5
});
var nameText = new Text2('', {
size: 14,
fill: '#ffffff'
});
nameText.anchor.set(0.5, 0.2);
nameText.y = -15;
self.addChild(nameText);
var priceText = new Text2('', {
size: 12,
fill: '#FFD700'
});
priceText.anchor.set(0.5, 0.5);
priceText.y = 5;
self.addChild(priceText);
var stockText = new Text2('', {
size: 11,
fill: '#cccccc'
});
stockText.anchor.set(0.5, 0.8);
stockText.y = 15;
self.addChild(stockText);
self.goodName = '';
self.price = 0;
self.stock = 0;
self.setGood = function (name, price, stock) {
self.goodName = name;
self.price = price;
self.stock = stock;
nameText.setText(name);
priceText.setText(price + '$');
stockText.setText('Stock: ' + stock);
};
self.setStock = function (stock) {
self.stock = stock;
stockText.setText('Stock: ' + stock);
};
self.down = function (x, y, obj) {
self.emit('buyGood');
};
return self;
});
var ProductionFacility = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('productionChain', {
anchorX: 0.5,
anchorY: 0.5
});
var nameText = new Text2('', {
size: 18,
fill: '#ffffff'
});
nameText.anchor.set(0.5, 0.5);
nameText.y = -20;
self.addChild(nameText);
var workerText = new Text2('0W', {
size: 16,
fill: '#cccccc'
});
workerText.anchor.set(0.5, 0.5);
workerText.y = 10;
self.addChild(workerText);
self.facilityName = '';
self.inputResource = '';
self.outputGood = '';
self.workersAssigned = 0;
self.isActive = false;
self.setFacility = function (name, input, output) {
self.facilityName = name;
self.inputResource = input;
self.outputGood = output;
nameText.setText(name);
};
self.setWorkers = function (count) {
self.workersAssigned = count;
if (count > 0) {
self.isActive = true;
bg.tint = 0x4CAF50;
} else {
self.isActive = false;
bg.tint = 0xFFFFFF;
}
workerText.setText(count + 'W');
};
self.down = function (x, y, obj) {
self.emit('selected');
};
return self;
});
var ResourceDisplay = Container.expand(function () {
var self = Container.call(this);
var bg = self.attachAsset('rawMaterial', {
anchorX: 0.5,
anchorY: 0.5
});
var label = new Text2('0', {
size: 24,
fill: '#ffffff'
});
label.anchor.set(0.5, 0.5);
self.addChild(label);
self.setValue = function (value) {
label.setText(String(value));
};
self.width = bg.width;
self.height = bg.height;
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x1a1a1a
});
/****
* Game Code
****/
// Game state
var currentTurn = 1;
var maxTurns = 8;
var totalPrestige = 0;
var money = 50;
var availableWorkers = 5;
var totalWorkers = 5;
var inventory = {
wood: 0,
coal: 0,
wool: 0,
corn: 0,
planks: 0,
steel: 0,
fabric: 0,
schnapps: 0
};
var productionFacilities = [];
var contracts = [];
var marketGoods = {};
var selectedFacility = null;
// Market prices
var marketPrices = {
planks: 20,
steel: 25,
fabric: 15,
schnapps: 30
};
var marketStock = {
planks: 10,
steel: 8,
fabric: 12,
schnapps: 6
};
// UI Elements
var turnsText = new Text2('Turn: 1/8', {
size: 40,
fill: '#ffffff'
});
turnsText.anchor.set(0.5, 0);
turnsText.x = 2048 / 2;
turnsText.y = 20;
game.addChild(turnsText);
var prestigeText = new Text2('IP: 0', {
size: 36,
fill: '#FFD700'
});
prestigeText.anchor.set(0, 0);
prestigeText.x = 50;
prestigeText.y = 30;
game.addChild(prestigeText);
var moneyText = new Text2('Money: 50', {
size: 32,
fill: '#90EE90'
});
moneyText.anchor.set(0, 0);
moneyText.x = 50;
moneyText.y = 80;
game.addChild(moneyText);
var workersText = new Text2('Workers: 5/5', {
size: 32,
fill: '#87CEEB'
});
workersText.anchor.set(0, 0);
workersText.x = 50;
moneyText.y = 130;
game.addChild(workersText);
// Inventory display
var inventoryContainer = new Container();
inventoryContainer.x = 50;
inventoryContainer.y = 200;
game.addChild(inventoryContainer);
var inventoryLabel = new Text2('Raw Materials', {
size: 24,
fill: '#ffffff'
});
inventoryLabel.y = 0;
inventoryContainer.addChild(inventoryLabel);
var resourceDisplays = {};
var resourceNames = ['wood', 'coal', 'wool', 'corn'];
for (var i = 0; i < resourceNames.length; i++) {
var display = new ResourceDisplay();
display.x = i * 80;
display.y = 40;
inventoryContainer.addChild(display);
resourceDisplays[resourceNames[i]] = display;
}
// Processed goods display
var goodsContainer = new Container();
goodsContainer.x = 50;
goodsContainer.y = 330;
game.addChild(goodsContainer);
var goodsLabel = new Text2('Processed Goods', {
size: 24,
fill: '#ffffff'
});
goodsLabel.y = 0;
goodsContainer.addChild(goodsLabel);
var goodDisplays = {};
var goodNames = ['planks', 'steel', 'fabric', 'schnapps'];
for (var i = 0; i < goodNames.length; i++) {
var display = new ResourceDisplay();
display.x = i * 80;
display.y = 40;
goodsContainer.addChild(display);
goodDisplays[goodNames[i]] = display;
}
// Production facilities
var facilitiesContainer = new Container();
facilitiesContainer.x = 600;
facilitiesContainer.y = 200;
game.addChild(facilitiesContainer);
var facilitiesLabel = new Text2('Production Facilities', {
size: 24,
fill: '#ffffff'
});
facilitiesLabel.y = 0;
facilitiesContainer.addChild(facilitiesLabel);
var facilities = [{
name: 'Lumber Mill',
input: 'wood',
output: 'planks'
}, {
name: 'Foundry',
input: 'coal',
output: 'steel'
}, {
name: 'Textile Mill',
input: 'wool',
output: 'fabric'
}, {
name: 'Distillery',
input: 'corn',
output: 'schnapps'
}];
for (var i = 0; i < facilities.length; i++) {
var facility = new ProductionFacility();
facility.setFacility(facilities[i].name, facilities[i].input, facilities[i].output);
facility.x = i % 2 * 150;
facility.y = Math.floor(i / 2) * 120 + 40;
facilitiesContainer.addChild(facility);
productionFacilities.push({
ui: facility,
name: facilities[i].name,
input: facilities[i].input,
output: facilities[i].output,
workers: 0
});
}
// Contracts display
var contractsContainer = new Container();
contractsContainer.x = 1200;
contractsContainer.y = 200;
game.addChild(contractsContainer);
var contractsLabel = new Text2('Contracts', {
size: 24,
fill: '#ffffff'
});
contractsLabel.y = 0;
contractsContainer.addChild(contractsLabel);
function generateContract(id) {
var requirements = {};
var requiredGoods = [];
var numGoods = Math.floor(Math.random() * 2) + 2;
for (var i = 0; i < numGoods; i++) {
var good = goodNames[Math.floor(Math.random() * goodNames.length)];
if (!requirements[good]) {
requiredGoods.push(good);
requirements[good] = Math.floor(Math.random() * 3) + 1;
}
}
var reward = 30 + numGoods * 10;
return {
id: id,
requirements: requirements,
reward: reward,
completed: false
};
}
function initializeContracts() {
contracts = [];
for (var i = 0; i < 2; i++) {
contracts.push(generateContract(i + 1));
}
contractsContainer.removeChildren(1);
for (var i = 0; i < contracts.length; i++) {
var card = new ContractCard();
card.setContract(contracts[i].id, contracts[i].requirements, contracts[i].reward);
card.x = i * 220;
card.y = 40;
contractsContainer.addChild(card);
contracts[i].ui = card;
}
}
initializeContracts();
// Market display
var marketContainer = new Container();
marketContainer.x = 1200;
marketContainer.y = 650;
game.addChild(marketContainer);
var marketLabel = new Text2('Global Market', {
size: 24,
fill: '#ffffff'
});
marketLabel.y = 0;
marketContainer.addChild(marketLabel);
for (var i = 0; i < goodNames.length; i++) {
var marketUI = new MarketGoodUI();
marketUI.setGood(goodNames[i], marketPrices[goodNames[i]], marketStock[goodNames[i]]);
marketUI.x = i * 110;
marketUI.y = 40;
marketContainer.addChild(marketUI);
marketGoods[goodNames[i]] = {
ui: marketUI,
stock: marketStock[goodNames[i]]
};
}
// End turn button
var endTurnButton = new Container();
endTurnButton.x = 2048 / 2;
endTurnButton.y = 2600;
game.addChild(endTurnButton);
var buttonBg = endTurnButton.attachAsset('button', {
anchorX: 0.5,
anchorY: 0.5
});
var buttonText = new Text2('End Turn', {
size: 24,
fill: '#000000'
});
buttonText.anchor.set(0.5, 0.5);
endTurnButton.addChild(buttonText);
endTurnButton.down = function (x, y, obj) {
processProduction();
endTurn();
};
function updateUI() {
turnsText.setText('Turn: ' + currentTurn + '/' + maxTurns);
prestigeText.setText('IP: ' + totalPrestige);
moneyText.setText('Money: ' + money);
workersText.setText('Workers: ' + availableWorkers + '/' + totalWorkers);
for (var i = 0; i < resourceNames.length; i++) {
resourceDisplays[resourceNames[i]].setValue(inventory[resourceNames[i]]);
}
for (var i = 0; i < goodNames.length; i++) {
goodDisplays[goodNames[i]].setValue(inventory[goodNames[i]]);
}
}
function processProduction() {
for (var i = 0; i < productionFacilities.length; i++) {
var facility = productionFacilities[i];
if (facility.workers > 0 && inventory[facility.input] > 0) {
var produced = facility.workers;
inventory[facility.input] -= produced;
inventory[facility.output] += produced;
}
}
}
function restockMarket() {
for (var good in marketStock) {
marketStock[good] = Math.floor(Math.random() * 10) + 5;
marketGoods[good].stock = marketStock[good];
marketGoods[good].ui.setStock(marketStock[good]);
}
}
function endTurn() {
currentTurn++;
availableWorkers = totalWorkers;
for (var i = 0; i < productionFacilities.length; i++) {
productionFacilities[i].workers = 0;
productionFacilities[i].ui.setWorkers(0);
}
for (var i = 0; i < resourceNames.length; i++) {
inventory[resourceNames[i]] += 1;
}
restockMarket();
updateUI();
if (currentTurn > maxTurns) {
endGame();
}
}
function tryCompleteContract(contractIndex) {
var contract = contracts[contractIndex];
if (contract.completed) return;
var canComplete = true;
for (var good in contract.requirements) {
if (!inventory[good] || inventory[good] < contract.requirements[good]) {
canComplete = false;
break;
}
}
if (canComplete) {
for (var good in contract.requirements) {
inventory[good] -= contract.requirements[good];
}
totalPrestige += contract.reward;
contract.completed = true;
contract.ui.markCompleted();
totalWorkers = Math.min(totalWorkers + 1, 7);
availableWorkers = Math.min(availableWorkers + 1, 7);
LK.getSound('complete').play();
updateUI();
}
}
function buyGood(goodName) {
if (money >= marketPrices[goodName] && marketStock[goodName] > 0) {
money -= marketPrices[goodName];
inventory[goodName] += 1;
marketStock[goodName] -= 1;
marketGoods[goodName].ui.setStock(marketStock[goodName]);
LK.getSound('trade').play();
updateUI();
}
}
function sellGood(goodName) {
if (inventory[goodName] > 0) {
money += marketPrices[goodName];
inventory[goodName] -= 1;
marketStock[goodName] += 1;
marketGoods[goodName].ui.setStock(marketStock[goodName]);
LK.getSound('trade').play();
updateUI();
}
}
function endGame() {
var message = '';
if (totalPrestige < 150) {
message = 'Final Score: ' + totalPrestige + ' IP\n\nYour company struggled. Better luck next time!';
} else if (totalPrestige < 250) {
message = 'Final Score: ' + totalPrestige + ' IP\n\nYou showed competence in managing your alliance.';
} else {
message = 'Final Score: ' + totalPrestige + ' IP\n\nYou dominated the industrial landscape!';
}
LK.showGameOver();
}
// Event handlers for facility selection
for (var i = 0; i < productionFacilities.length; i++) {
(function (index) {
productionFacilities[index].ui.on('selected', function () {
if (availableWorkers > 0) {
var facility = productionFacilities[index];
if (facility.workers < 2) {
facility.workers += 1;
availableWorkers -= 1;
facility.ui.setWorkers(facility.workers);
updateUI();
}
}
});
})(i);
}
// Event handlers for contracts
for (var i = 0; i < contracts.length; i++) {
(function (index) {
contracts[index].ui.on('tryComplete', function () {
tryCompleteContract(index);
});
})(i);
}
// Event handlers for market
for (var i = 0; i < goodNames.length; i++) {
(function (goodName) {
marketGoods[goodName].ui.on('buyGood', function () {
buyGood(goodName);
});
})(goodNames[i]);
}
game.update = function () {
// Main game loop - update displays if needed
};
updateUI();