/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	coins: 100,
	robots: 0,
	partsPerClick: 1,
	autoPartsPerSecond: 0,
	partCost: 10,
	clickUpgradeCost: 50,
	autoUpgradeCost: 100,
	baseProduceTime: 5000,
	lastTimestamp: undefined,
	completedOrders: 0,
	orderLevel: 1
});
/**** 
* Classes
****/ 
var OrderCard = Container.expand(function (requiredRobots, reward) {
	var self = Container.call(this);
	self.requiredRobots = requiredRobots;
	self.reward = reward;
	var cardBase = self.attachAsset('order_card', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var titleText = new Text2('Robot Order', {
		size: 40,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0.5);
	titleText.y = -70;
	self.addChild(titleText);
	var requirementText = new Text2('Need: ' + requiredRobots + ' robots', {
		size: 30,
		fill: 0x333333
	});
	requirementText.anchor.set(0.5, 0.5);
	requirementText.y = -20;
	self.addChild(requirementText);
	var rewardText = new Text2('Reward: ' + reward + ' coins', {
		size: 30,
		fill: 0x333333
	});
	rewardText.anchor.set(0.5, 0.5);
	rewardText.y = 20;
	self.addChild(rewardText);
	var statusText = new Text2('In Progress', {
		size: 35,
		fill: 0xE74C3C
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = 70;
	self.addChild(statusText);
	self.checkCompletion = function () {
		if (storage.robots >= self.requiredRobots) {
			return true;
		}
		return false;
	};
	self.complete = function () {
		storage.robots -= self.requiredRobots;
		storage.coins += self.reward;
		storage.completedOrders += 1;
		// Increase order level every 3 completed orders
		if (storage.completedOrders % 3 === 0) {
			storage.orderLevel += 1;
		}
		robotsText.setText('Robots: ' + storage.robots);
		coinsText.setText('Coins: ' + storage.coins);
		ordersCompletedText.setText('Orders completed: ' + storage.completedOrders);
		statusText.setText('Completed!');
		tween(statusText, {
			fill: 0x27AE60
		}, {
			duration: 300
		});
		LK.getSound('order_complete').play();
		// Replace this order with a new one after a short delay
		LK.setTimeout(function () {
			createNewOrder();
		}, 2000);
	};
	self.down = function (x, y, obj) {
		if (self.checkCompletion()) {
			self.complete();
		}
	};
	return self;
});
var ProductionStation = Container.expand(function () {
	var self = Container.call(this);
	var stationBase = self.attachAsset('production_station', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var progressBar = self.attachAsset('robot_arm', {
		anchorX: 0,
		anchorY: 0.5,
		x: -120,
		y: 80,
		scaleX: 4,
		scaleY: 0.5
	});
	var progressText = new Text2('0%', {
		size: 40,
		fill: 0xFFFFFF
	});
	progressText.anchor.set(0.5, 0.5);
	progressText.x = 0;
	progressText.y = 80;
	self.addChild(progressText);
	self.isProducing = false;
	self.progress = 0;
	self.produceTime = storage.baseProduceTime;
	self.updateProgress = function (delta) {
		if (self.isProducing) {
			self.progress += delta;
			var percent = Math.min(100, Math.floor(self.progress / self.produceTime * 100));
			progressText.setText(percent + '%');
			progressBar.scaleX = 4 * (percent / 100);
			if (self.progress >= self.produceTime) {
				self.completeProduction();
			}
		}
	};
	self.startProduction = function () {
		if (!self.isProducing) {
			self.isProducing = true;
			self.progress = 0;
			progressText.setText('0%');
			progressBar.scaleX = 0;
		}
	};
	self.completeProduction = function () {
		self.isProducing = false;
		self.progress = 0;
		progressText.setText('Ready');
		progressBar.scaleX = 4;
		storage.robots += 1;
		LK.getSound('build_robot').play();
		robotsText.setText('Robots: ' + storage.robots);
		// See if we can complete any pending orders
		checkOrders();
	};
	self.down = function (x, y, obj) {
		if (!self.isProducing && storage.coins >= storage.partCost * 5) {
			storage.coins -= storage.partCost * 5;
			coinsText.setText('Coins: ' + storage.coins);
			self.startProduction();
		}
	};
	progressText.setText('Ready');
	return self;
});
var Robot = Container.expand(function () {
	var self = Container.call(this);
	var base = self.attachAsset('robot_base', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.8,
		scaleY: 0.8
	});
	var head = self.attachAsset('robot_head', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: -80
	});
	var leftArm = self.attachAsset('robot_arm', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: -100,
		y: -50,
		rotation: -0.2
	});
	var rightArm = self.attachAsset('robot_arm', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: 100,
		y: -50,
		rotation: 0.2
	});
	var leftLeg = self.attachAsset('robot_leg', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: -60,
		y: 80
	});
	var rightLeg = self.attachAsset('robot_leg', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: 60,
		y: 80
	});
	self.animateIdle = function () {
		// Head bobbing animation
		tween(head, {
			y: -75
		}, {
			duration: 1500,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(head, {
					y: -80
				}, {
					duration: 1500,
					easing: tween.easeInOut,
					onFinish: self.animateIdle
				});
			}
		});
		// Arm swaying animations
		tween(leftArm, {
			rotation: -0.3
		}, {
			duration: 2000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(leftArm, {
					rotation: -0.2
				}, {
					duration: 2000,
					easing: tween.easeInOut
				});
			}
		});
		tween(rightArm, {
			rotation: 0.3
		}, {
			duration: 2000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(rightArm, {
					rotation: 0.2
				}, {
					duration: 2000,
					easing: tween.easeInOut
				});
			}
		});
	};
	self.startAnimations = function () {
		self.animateIdle();
	};
	return self;
});
var UpgradeButton = Container.expand(function (upgradeType, upgradeText) {
	var self = Container.call(this);
	self.upgradeType = upgradeType;
	var buttonBase = self.attachAsset('upgrade_button', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var label = new Text2(upgradeText, {
		size: 40,
		fill: 0xFFFFFF
	});
	label.anchor.set(0.5, 0.5);
	self.addChild(label);
	var costText = new Text2('', {
		size: 30,
		fill: 0xFFFFFF
	});
	costText.anchor.set(0.5, 0.5);
	costText.y = 50;
	self.addChild(costText);
	self.updateCost = function () {
		if (self.upgradeType === 'click') {
			costText.setText('Cost: ' + storage.clickUpgradeCost);
		} else if (self.upgradeType === 'auto') {
			costText.setText('Cost: ' + storage.autoUpgradeCost);
		} else if (self.upgradeType === 'speed') {
			costText.setText('Cost: ' + Math.floor(storage.baseProduceTime / 2));
		}
	};
	self.down = function (x, y, obj) {
		if (self.upgradeType === 'click') {
			if (storage.coins >= storage.clickUpgradeCost) {
				storage.coins -= storage.clickUpgradeCost;
				storage.partsPerClick += 1;
				storage.clickUpgradeCost = Math.floor(storage.clickUpgradeCost * 1.5);
				LK.getSound('upgrade_purchased').play();
				partsPerClickText.setText('Parts per click: ' + storage.partsPerClick);
				self.updateCost();
				coinsText.setText('Coins: ' + storage.coins);
			}
		} else if (self.upgradeType === 'auto') {
			if (storage.coins >= storage.autoUpgradeCost) {
				storage.coins -= storage.autoUpgradeCost;
				storage.autoPartsPerSecond += 1;
				storage.autoUpgradeCost = Math.floor(storage.autoUpgradeCost * 1.8);
				LK.getSound('upgrade_purchased').play();
				autoPartsText.setText('Auto parts: ' + storage.autoPartsPerSecond + '/sec');
				self.updateCost();
				coinsText.setText('Coins: ' + storage.coins);
			}
		} else if (self.upgradeType === 'speed') {
			var speedCost = Math.floor(storage.baseProduceTime / 2);
			if (storage.coins >= speedCost && storage.baseProduceTime > 1000) {
				storage.coins -= speedCost;
				storage.baseProduceTime = Math.max(1000, storage.baseProduceTime - 500);
				productionStation.produceTime = storage.baseProduceTime;
				LK.getSound('upgrade_purchased').play();
				self.updateCost();
				coinsText.setText('Coins: ' + storage.coins);
			}
		}
	};
	self.updateCost();
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x34495e
});
/**** 
* Game Code
****/ 
LK.playMusic('factory_ambience');
// Create factory floor
var factoryFloor = LK.getAsset('factory_floor', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 1500 / 2 + 400
});
game.addChild(factoryFloor);
// Create parts production area
var partsArea = new Container();
partsArea.x = 400;
partsArea.y = 600;
game.addChild(partsArea);
var partsIcon = LK.getAsset('resource_icon', {
	anchorX: 0.5,
	anchorY: 0.5,
	scaleX: 3,
	scaleY: 3,
	x: 0,
	y: 0
});
partsArea.addChild(partsIcon);
// Create UI elements
var coinsText = new Text2('Coins: ' + storage.coins, {
	size: 60,
	fill: 0xF1C40F
});
coinsText.anchor.set(0, 0);
LK.gui.topRight.addChild(coinsText);
coinsText.x = -coinsText.width - 20;
coinsText.y = 20;
var robotsText = new Text2('Robots: ' + storage.robots, {
	size: 60,
	fill: 0x3498DB
});
robotsText.anchor.set(0, 0);
LK.gui.topRight.addChild(robotsText);
robotsText.x = -robotsText.width - 20;
robotsText.y = 100;
var partsPerClickText = new Text2('Parts per click: ' + storage.partsPerClick, {
	size: 40,
	fill: 0xFFFFFF
});
partsPerClickText.anchor.set(0.5, 0.5);
partsPerClickText.x = partsIcon.x;
partsPerClickText.y = partsIcon.y + 120;
partsArea.addChild(partsPerClickText);
var autoPartsText = new Text2('Auto parts: ' + storage.autoPartsPerSecond + '/sec', {
	size: 40,
	fill: 0xFFFFFF
});
autoPartsText.anchor.set(0.5, 0.5);
autoPartsText.x = partsIcon.x;
autoPartsText.y = partsIcon.y + 180;
partsArea.addChild(autoPartsText);
var ordersCompletedText = new Text2('Orders completed: ' + storage.completedOrders, {
	size: 50,
	fill: 0xE74C3C
});
ordersCompletedText.anchor.set(0.5, 0);
ordersCompletedText.y = 20;
LK.gui.top.addChild(ordersCompletedText);
// Create production station
var productionStation = new ProductionStation();
productionStation.x = 2048 / 2;
productionStation.y = 800;
game.addChild(productionStation);
var productionLabel = new Text2('Robot Production Station\n(Click to build robot - costs 5 parts)', {
	size: 40,
	fill: 0xFFFFFF,
	align: "center"
});
productionLabel.anchor.set(0.5, 0.5);
productionLabel.x = productionStation.x;
productionLabel.y = productionStation.y - 200;
game.addChild(productionLabel);
// Create robot display
var robotDisplay = new Robot();
robotDisplay.x = 1600;
robotDisplay.y = 800;
robotDisplay.scale.set(1.5, 1.5);
game.addChild(robotDisplay);
robotDisplay.startAnimations();
// Create upgrade buttons
var clickUpgradeButton = new UpgradeButton('click', 'Upgrade Click');
clickUpgradeButton.x = 400;
clickUpgradeButton.y = 1200;
game.addChild(clickUpgradeButton);
var autoUpgradeButton = new UpgradeButton('auto', 'Upgrade Auto');
autoUpgradeButton.x = 1000;
autoUpgradeButton.y = 1200;
game.addChild(autoUpgradeButton);
var speedUpgradeButton = new UpgradeButton('speed', 'Upgrade Speed');
speedUpgradeButton.x = 1600;
speedUpgradeButton.y = 1200;
game.addChild(speedUpgradeButton);
// Variable to track current order
var currentOrder = null;
// Function to create new orders
function createNewOrder() {
	if (currentOrder) {
		game.removeChild(currentOrder);
	}
	// Create progressively harder orders as player completes more
	var requiredRobots = Math.max(1, Math.floor(storage.orderLevel * 1.5));
	var reward = Math.floor(requiredRobots * 50 * (1 + storage.orderLevel * 0.2));
	currentOrder = new OrderCard(requiredRobots, reward);
	currentOrder.x = 2048 / 2;
	currentOrder.y = 1600;
	game.addChild(currentOrder);
}
// Function to check if order can be completed
function checkOrders() {
	if (currentOrder && currentOrder.checkCompletion()) {
		// Visual hint that order can be completed
		tween(currentOrder, {
			scaleX: 1.1,
			scaleY: 1.1
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(currentOrder, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 300
				});
			}
		});
	}
}
// Create initial order
createNewOrder();
// Handle part production on clicks
partsIcon.interactive = true;
partsIcon.down = function (x, y, obj) {
	// Flash and scale effect for the click
	tween(partsIcon, {
		scaleX: 3.3,
		scaleY: 3.3
	}, {
		duration: 100,
		onFinish: function onFinish() {
			tween(partsIcon, {
				scaleX: 3,
				scaleY: 3
			}, {
				duration: 100
			});
		}
	});
	// Add resources based on upgrades
	storage.coins += storage.partsPerClick * storage.partCost;
	coinsText.setText('Coins: ' + storage.coins);
};
// Process idle time when game loads
var currentTime = Date.now();
var idleTime = Math.floor((currentTime - storage.lastTimestamp) / 1000);
if (idleTime > 0 && storage.autoPartsPerSecond > 0) {
	var idleCoins = Math.floor(idleTime * storage.autoPartsPerSecond * storage.partCost * 0.5); // 50% efficiency when idle
	storage.coins += idleCoins;
	// Show welcome back message
	var welcomeBack = new Text2('Welcome Back!\nYou earned ' + idleCoins + ' coins while away.', {
		size: 60,
		fill: 0xF1C40F,
		align: "center"
	});
	welcomeBack.anchor.set(0.5, 0.5);
	welcomeBack.x = 2048 / 2;
	welcomeBack.y = 2732 / 2;
	game.addChild(welcomeBack);
	// Fade out welcome message
	tween(welcomeBack, {
		alpha: 0
	}, {
		duration: 3000,
		onFinish: function onFinish() {
			game.removeChild(welcomeBack);
		}
	});
}
// Game update loop
var lastUpdateTime = Date.now();
game.update = function () {
	var currentTime = Date.now();
	var deltaTime = currentTime - lastUpdateTime;
	lastUpdateTime = currentTime;
	// Auto parts generation
	if (storage.autoPartsPerSecond > 0) {
		var partsGenerated = deltaTime / 1000 * storage.autoPartsPerSecond;
		storage.coins += partsGenerated * storage.partCost;
		coinsText.setText('Coins: ' + Math.floor(storage.coins));
	}
	// Update production station
	productionStation.updateProgress(deltaTime);
	// Save timestamp for idle calculations
	storage.lastTimestamp = currentTime;
};
// Add game description to the UI
var gameTitle = new Text2('IdleBot Manufacturing', {
	size: 70,
	fill: 0x3498DB
});
gameTitle.anchor.set(0, 0);
gameTitle.x = 30;
gameTitle.y = 20;
LK.gui.topLeft.addChild(gameTitle); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	coins: 100,
	robots: 0,
	partsPerClick: 1,
	autoPartsPerSecond: 0,
	partCost: 10,
	clickUpgradeCost: 50,
	autoUpgradeCost: 100,
	baseProduceTime: 5000,
	lastTimestamp: undefined,
	completedOrders: 0,
	orderLevel: 1
});
/**** 
* Classes
****/ 
var OrderCard = Container.expand(function (requiredRobots, reward) {
	var self = Container.call(this);
	self.requiredRobots = requiredRobots;
	self.reward = reward;
	var cardBase = self.attachAsset('order_card', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var titleText = new Text2('Robot Order', {
		size: 40,
		fill: 0x333333
	});
	titleText.anchor.set(0.5, 0.5);
	titleText.y = -70;
	self.addChild(titleText);
	var requirementText = new Text2('Need: ' + requiredRobots + ' robots', {
		size: 30,
		fill: 0x333333
	});
	requirementText.anchor.set(0.5, 0.5);
	requirementText.y = -20;
	self.addChild(requirementText);
	var rewardText = new Text2('Reward: ' + reward + ' coins', {
		size: 30,
		fill: 0x333333
	});
	rewardText.anchor.set(0.5, 0.5);
	rewardText.y = 20;
	self.addChild(rewardText);
	var statusText = new Text2('In Progress', {
		size: 35,
		fill: 0xE74C3C
	});
	statusText.anchor.set(0.5, 0.5);
	statusText.y = 70;
	self.addChild(statusText);
	self.checkCompletion = function () {
		if (storage.robots >= self.requiredRobots) {
			return true;
		}
		return false;
	};
	self.complete = function () {
		storage.robots -= self.requiredRobots;
		storage.coins += self.reward;
		storage.completedOrders += 1;
		// Increase order level every 3 completed orders
		if (storage.completedOrders % 3 === 0) {
			storage.orderLevel += 1;
		}
		robotsText.setText('Robots: ' + storage.robots);
		coinsText.setText('Coins: ' + storage.coins);
		ordersCompletedText.setText('Orders completed: ' + storage.completedOrders);
		statusText.setText('Completed!');
		tween(statusText, {
			fill: 0x27AE60
		}, {
			duration: 300
		});
		LK.getSound('order_complete').play();
		// Replace this order with a new one after a short delay
		LK.setTimeout(function () {
			createNewOrder();
		}, 2000);
	};
	self.down = function (x, y, obj) {
		if (self.checkCompletion()) {
			self.complete();
		}
	};
	return self;
});
var ProductionStation = Container.expand(function () {
	var self = Container.call(this);
	var stationBase = self.attachAsset('production_station', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var progressBar = self.attachAsset('robot_arm', {
		anchorX: 0,
		anchorY: 0.5,
		x: -120,
		y: 80,
		scaleX: 4,
		scaleY: 0.5
	});
	var progressText = new Text2('0%', {
		size: 40,
		fill: 0xFFFFFF
	});
	progressText.anchor.set(0.5, 0.5);
	progressText.x = 0;
	progressText.y = 80;
	self.addChild(progressText);
	self.isProducing = false;
	self.progress = 0;
	self.produceTime = storage.baseProduceTime;
	self.updateProgress = function (delta) {
		if (self.isProducing) {
			self.progress += delta;
			var percent = Math.min(100, Math.floor(self.progress / self.produceTime * 100));
			progressText.setText(percent + '%');
			progressBar.scaleX = 4 * (percent / 100);
			if (self.progress >= self.produceTime) {
				self.completeProduction();
			}
		}
	};
	self.startProduction = function () {
		if (!self.isProducing) {
			self.isProducing = true;
			self.progress = 0;
			progressText.setText('0%');
			progressBar.scaleX = 0;
		}
	};
	self.completeProduction = function () {
		self.isProducing = false;
		self.progress = 0;
		progressText.setText('Ready');
		progressBar.scaleX = 4;
		storage.robots += 1;
		LK.getSound('build_robot').play();
		robotsText.setText('Robots: ' + storage.robots);
		// See if we can complete any pending orders
		checkOrders();
	};
	self.down = function (x, y, obj) {
		if (!self.isProducing && storage.coins >= storage.partCost * 5) {
			storage.coins -= storage.partCost * 5;
			coinsText.setText('Coins: ' + storage.coins);
			self.startProduction();
		}
	};
	progressText.setText('Ready');
	return self;
});
var Robot = Container.expand(function () {
	var self = Container.call(this);
	var base = self.attachAsset('robot_base', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.8,
		scaleY: 0.8
	});
	var head = self.attachAsset('robot_head', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: -80
	});
	var leftArm = self.attachAsset('robot_arm', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: -100,
		y: -50,
		rotation: -0.2
	});
	var rightArm = self.attachAsset('robot_arm', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: 100,
		y: -50,
		rotation: 0.2
	});
	var leftLeg = self.attachAsset('robot_leg', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: -60,
		y: 80
	});
	var rightLeg = self.attachAsset('robot_leg', {
		anchorX: 0.5,
		anchorY: 0.1,
		x: 60,
		y: 80
	});
	self.animateIdle = function () {
		// Head bobbing animation
		tween(head, {
			y: -75
		}, {
			duration: 1500,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(head, {
					y: -80
				}, {
					duration: 1500,
					easing: tween.easeInOut,
					onFinish: self.animateIdle
				});
			}
		});
		// Arm swaying animations
		tween(leftArm, {
			rotation: -0.3
		}, {
			duration: 2000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(leftArm, {
					rotation: -0.2
				}, {
					duration: 2000,
					easing: tween.easeInOut
				});
			}
		});
		tween(rightArm, {
			rotation: 0.3
		}, {
			duration: 2000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(rightArm, {
					rotation: 0.2
				}, {
					duration: 2000,
					easing: tween.easeInOut
				});
			}
		});
	};
	self.startAnimations = function () {
		self.animateIdle();
	};
	return self;
});
var UpgradeButton = Container.expand(function (upgradeType, upgradeText) {
	var self = Container.call(this);
	self.upgradeType = upgradeType;
	var buttonBase = self.attachAsset('upgrade_button', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var label = new Text2(upgradeText, {
		size: 40,
		fill: 0xFFFFFF
	});
	label.anchor.set(0.5, 0.5);
	self.addChild(label);
	var costText = new Text2('', {
		size: 30,
		fill: 0xFFFFFF
	});
	costText.anchor.set(0.5, 0.5);
	costText.y = 50;
	self.addChild(costText);
	self.updateCost = function () {
		if (self.upgradeType === 'click') {
			costText.setText('Cost: ' + storage.clickUpgradeCost);
		} else if (self.upgradeType === 'auto') {
			costText.setText('Cost: ' + storage.autoUpgradeCost);
		} else if (self.upgradeType === 'speed') {
			costText.setText('Cost: ' + Math.floor(storage.baseProduceTime / 2));
		}
	};
	self.down = function (x, y, obj) {
		if (self.upgradeType === 'click') {
			if (storage.coins >= storage.clickUpgradeCost) {
				storage.coins -= storage.clickUpgradeCost;
				storage.partsPerClick += 1;
				storage.clickUpgradeCost = Math.floor(storage.clickUpgradeCost * 1.5);
				LK.getSound('upgrade_purchased').play();
				partsPerClickText.setText('Parts per click: ' + storage.partsPerClick);
				self.updateCost();
				coinsText.setText('Coins: ' + storage.coins);
			}
		} else if (self.upgradeType === 'auto') {
			if (storage.coins >= storage.autoUpgradeCost) {
				storage.coins -= storage.autoUpgradeCost;
				storage.autoPartsPerSecond += 1;
				storage.autoUpgradeCost = Math.floor(storage.autoUpgradeCost * 1.8);
				LK.getSound('upgrade_purchased').play();
				autoPartsText.setText('Auto parts: ' + storage.autoPartsPerSecond + '/sec');
				self.updateCost();
				coinsText.setText('Coins: ' + storage.coins);
			}
		} else if (self.upgradeType === 'speed') {
			var speedCost = Math.floor(storage.baseProduceTime / 2);
			if (storage.coins >= speedCost && storage.baseProduceTime > 1000) {
				storage.coins -= speedCost;
				storage.baseProduceTime = Math.max(1000, storage.baseProduceTime - 500);
				productionStation.produceTime = storage.baseProduceTime;
				LK.getSound('upgrade_purchased').play();
				self.updateCost();
				coinsText.setText('Coins: ' + storage.coins);
			}
		}
	};
	self.updateCost();
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x34495e
});
/**** 
* Game Code
****/ 
LK.playMusic('factory_ambience');
// Create factory floor
var factoryFloor = LK.getAsset('factory_floor', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 1500 / 2 + 400
});
game.addChild(factoryFloor);
// Create parts production area
var partsArea = new Container();
partsArea.x = 400;
partsArea.y = 600;
game.addChild(partsArea);
var partsIcon = LK.getAsset('resource_icon', {
	anchorX: 0.5,
	anchorY: 0.5,
	scaleX: 3,
	scaleY: 3,
	x: 0,
	y: 0
});
partsArea.addChild(partsIcon);
// Create UI elements
var coinsText = new Text2('Coins: ' + storage.coins, {
	size: 60,
	fill: 0xF1C40F
});
coinsText.anchor.set(0, 0);
LK.gui.topRight.addChild(coinsText);
coinsText.x = -coinsText.width - 20;
coinsText.y = 20;
var robotsText = new Text2('Robots: ' + storage.robots, {
	size: 60,
	fill: 0x3498DB
});
robotsText.anchor.set(0, 0);
LK.gui.topRight.addChild(robotsText);
robotsText.x = -robotsText.width - 20;
robotsText.y = 100;
var partsPerClickText = new Text2('Parts per click: ' + storage.partsPerClick, {
	size: 40,
	fill: 0xFFFFFF
});
partsPerClickText.anchor.set(0.5, 0.5);
partsPerClickText.x = partsIcon.x;
partsPerClickText.y = partsIcon.y + 120;
partsArea.addChild(partsPerClickText);
var autoPartsText = new Text2('Auto parts: ' + storage.autoPartsPerSecond + '/sec', {
	size: 40,
	fill: 0xFFFFFF
});
autoPartsText.anchor.set(0.5, 0.5);
autoPartsText.x = partsIcon.x;
autoPartsText.y = partsIcon.y + 180;
partsArea.addChild(autoPartsText);
var ordersCompletedText = new Text2('Orders completed: ' + storage.completedOrders, {
	size: 50,
	fill: 0xE74C3C
});
ordersCompletedText.anchor.set(0.5, 0);
ordersCompletedText.y = 20;
LK.gui.top.addChild(ordersCompletedText);
// Create production station
var productionStation = new ProductionStation();
productionStation.x = 2048 / 2;
productionStation.y = 800;
game.addChild(productionStation);
var productionLabel = new Text2('Robot Production Station\n(Click to build robot - costs 5 parts)', {
	size: 40,
	fill: 0xFFFFFF,
	align: "center"
});
productionLabel.anchor.set(0.5, 0.5);
productionLabel.x = productionStation.x;
productionLabel.y = productionStation.y - 200;
game.addChild(productionLabel);
// Create robot display
var robotDisplay = new Robot();
robotDisplay.x = 1600;
robotDisplay.y = 800;
robotDisplay.scale.set(1.5, 1.5);
game.addChild(robotDisplay);
robotDisplay.startAnimations();
// Create upgrade buttons
var clickUpgradeButton = new UpgradeButton('click', 'Upgrade Click');
clickUpgradeButton.x = 400;
clickUpgradeButton.y = 1200;
game.addChild(clickUpgradeButton);
var autoUpgradeButton = new UpgradeButton('auto', 'Upgrade Auto');
autoUpgradeButton.x = 1000;
autoUpgradeButton.y = 1200;
game.addChild(autoUpgradeButton);
var speedUpgradeButton = new UpgradeButton('speed', 'Upgrade Speed');
speedUpgradeButton.x = 1600;
speedUpgradeButton.y = 1200;
game.addChild(speedUpgradeButton);
// Variable to track current order
var currentOrder = null;
// Function to create new orders
function createNewOrder() {
	if (currentOrder) {
		game.removeChild(currentOrder);
	}
	// Create progressively harder orders as player completes more
	var requiredRobots = Math.max(1, Math.floor(storage.orderLevel * 1.5));
	var reward = Math.floor(requiredRobots * 50 * (1 + storage.orderLevel * 0.2));
	currentOrder = new OrderCard(requiredRobots, reward);
	currentOrder.x = 2048 / 2;
	currentOrder.y = 1600;
	game.addChild(currentOrder);
}
// Function to check if order can be completed
function checkOrders() {
	if (currentOrder && currentOrder.checkCompletion()) {
		// Visual hint that order can be completed
		tween(currentOrder, {
			scaleX: 1.1,
			scaleY: 1.1
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(currentOrder, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 300
				});
			}
		});
	}
}
// Create initial order
createNewOrder();
// Handle part production on clicks
partsIcon.interactive = true;
partsIcon.down = function (x, y, obj) {
	// Flash and scale effect for the click
	tween(partsIcon, {
		scaleX: 3.3,
		scaleY: 3.3
	}, {
		duration: 100,
		onFinish: function onFinish() {
			tween(partsIcon, {
				scaleX: 3,
				scaleY: 3
			}, {
				duration: 100
			});
		}
	});
	// Add resources based on upgrades
	storage.coins += storage.partsPerClick * storage.partCost;
	coinsText.setText('Coins: ' + storage.coins);
};
// Process idle time when game loads
var currentTime = Date.now();
var idleTime = Math.floor((currentTime - storage.lastTimestamp) / 1000);
if (idleTime > 0 && storage.autoPartsPerSecond > 0) {
	var idleCoins = Math.floor(idleTime * storage.autoPartsPerSecond * storage.partCost * 0.5); // 50% efficiency when idle
	storage.coins += idleCoins;
	// Show welcome back message
	var welcomeBack = new Text2('Welcome Back!\nYou earned ' + idleCoins + ' coins while away.', {
		size: 60,
		fill: 0xF1C40F,
		align: "center"
	});
	welcomeBack.anchor.set(0.5, 0.5);
	welcomeBack.x = 2048 / 2;
	welcomeBack.y = 2732 / 2;
	game.addChild(welcomeBack);
	// Fade out welcome message
	tween(welcomeBack, {
		alpha: 0
	}, {
		duration: 3000,
		onFinish: function onFinish() {
			game.removeChild(welcomeBack);
		}
	});
}
// Game update loop
var lastUpdateTime = Date.now();
game.update = function () {
	var currentTime = Date.now();
	var deltaTime = currentTime - lastUpdateTime;
	lastUpdateTime = currentTime;
	// Auto parts generation
	if (storage.autoPartsPerSecond > 0) {
		var partsGenerated = deltaTime / 1000 * storage.autoPartsPerSecond;
		storage.coins += partsGenerated * storage.partCost;
		coinsText.setText('Coins: ' + Math.floor(storage.coins));
	}
	// Update production station
	productionStation.updateProgress(deltaTime);
	// Save timestamp for idle calculations
	storage.lastTimestamp = currentTime;
};
// Add game description to the UI
var gameTitle = new Text2('IdleBot Manufacturing', {
	size: 70,
	fill: 0x3498DB
});
gameTitle.anchor.set(0, 0);
gameTitle.x = 30;
gameTitle.y = 20;
LK.gui.topLeft.addChild(gameTitle);