/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Axe = Container.expand(function () {
	var self = Container.call(this);
	var axeGraphics = self.attachAsset('axe', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 40;
	self.gravity = 0.3; // Adjusted gravity effect
	self.velocityY = -self.speed; // Initial upward velocity
	self._move_migrated = function () {
		self.velocityY += self.gravity; // Apply gravity to velocity
		self.y += self.velocityY; // Update position with velocity
		self.rotation += 0.1;
	};
	// Add entrance animation when axe is created
	self.scale.set(0.1, 0.1);
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 200,
		easing: tween.bounceOut
	});
	tween(axeGraphics, {
		tint: 0xFFFFFF
	}, {
		duration: 300
	});
});
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('Coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 3; // Fall speed
	self.collected = false;
	self._move_migrated = function () {
		// Fall down
		self.y += self.speed;
		// Rotate for visual effect
		self.rotation += 0.05;
	};
	// Add entrance animation
	self.scale.set(0.1, 0.1);
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 300,
		easing: tween.bounceOut
	});
	// Add shimmer effect
	tween(coinGraphics, {
		tint: 0xFFFF00
	}, {
		duration: 500,
		easing: tween.easeInOut,
		loop: true,
		yoyo: true
	});
	return self;
});
var Enemy = Container.expand(function () {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('enemy', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.direction = 1;
	self.scaleFactor = 0.1; // Initial small scale
	self.growthRate = 0.01; // Rate at which the enemy grows
	self._move_migrated = function () {
		// On level 10+, some enemies target axes
		if (level >= 10 && self.tracksAxes && axes.length > 0) {
			// Find closest axe
			var closestAxe = null;
			var closestDistance = Infinity;
			for (var k = 0; k < axes.length; k++) {
				var axeDistance = Math.sqrt(Math.pow(self.x - axes[k].x, 2) + Math.pow(self.y - axes[k].y, 2));
				if (axeDistance < closestDistance) {
					closestDistance = axeDistance;
					closestAxe = axes[k];
				}
			}
			// Move towards closest axe
			if (closestAxe) {
				var angleToAxe = Math.atan2(closestAxe.y - self.y, closestAxe.x - self.x);
				self.direction = angleToAxe;
				// Increase speed when tracking
				self.x += self.speed * 1.5 * Math.cos(self.direction);
				self.y += self.speed * 1.5 * Math.sin(self.direction);
			} else {
				// Normal movement if no axes
				if (Math.random() < 0.05) {
					self.direction = Math.random() * 2 * Math.PI;
				}
				self.x += self.speed * Math.cos(self.direction);
				self.y += self.speed * Math.sin(self.direction);
			}
		} else {
			// Normal movement for non-tracking enemies or level < 10
			if (Math.random() < 0.05) {
				self.direction = Math.random() * 2 * Math.PI; // Random angle
			}
			// Update position based on direction
			self.x += self.speed * Math.cos(self.direction);
			self.y += self.speed * Math.sin(self.direction);
		}
		// Keep enemy within bounds
		if (self.x <= 0 || self.x >= 2048) {
			self.direction = Math.PI - self.direction; // Reflect horizontally
		}
		if (self.y <= 0 || self.y >= 2732) {
			self.direction = -self.direction; // Reflect vertically
		}
		// Increase size until normal size is reached
		if (self.scaleFactor < 1) {
			self.scaleFactor += self.growthRate;
			self.scale.set(self.scaleFactor, self.scaleFactor);
		}
		// Add pulsing effect when fully grown
		if (self.scaleFactor >= 1 && !self.pulseStarted) {
			self.pulseStarted = true;
			var _pulse = function pulse() {
				tween(self, {
					scaleX: 1.1,
					scaleY: 1.1
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: function onFinish() {
						tween(self, {
							scaleX: 1,
							scaleY: 1
						}, {
							duration: 1000,
							easing: tween.easeInOut,
							onFinish: _pulse
						});
					}
				});
			};
			_pulse();
		}
	};
});
var Target = Container.expand(function () {
	var self = Container.call(this);
	var targetGraphics = self.attachAsset('target', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.direction = 1;
	self._move_migrated = function () {
		// Random direction changes like enemies
		if (Math.random() < 0.05) {
			self.direction = Math.random() * 2 * Math.PI; // Random angle
		}
		// Update position based on direction
		self.x += self.speed * Math.cos(self.direction);
		self.y += self.speed * Math.sin(self.direction);
		// Keep target within bounds
		if (self.x <= 0 || self.x >= 2048) {
			self.direction = Math.PI - self.direction; // Reflect horizontally
		}
		if (self.y <= 0 || self.y >= 2732) {
			self.direction = -self.direction; // Reflect vertically
		}
	};
	// Add entrance animation
	self.alpha = 0;
	self.scale.set(0.1, 0.1);
	tween(self, {
		alpha: 1
	}, {
		duration: 500,
		easing: tween.easeOut
	});
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 800,
		easing: tween.elasticOut
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
// Start background music
LK.playMusic('backgroundmusic');
var background = game.attachAsset('green_grass_field', {});
background.width = 2048;
background.height = 2732;
// Create second background for seamless horizontal scrolling
var background2 = game.attachAsset('green_grass_field', {});
background2.width = 2048;
background2.height = 2732;
background2.x = 2048; // Position to the right of the first background
// Add both backgrounds to game
game.addChild(background);
game.addChild(background2);
// Animate background movement - floating slowly to the left like clouds
var backgroundSpeed = 0.5; // Slower speed for floating effect
var animateBackground = function animateBackground() {
	// Move backgrounds left (floating effect)
	background.x -= backgroundSpeed;
	background2.x -= backgroundSpeed;
	// Reset position when background moves off screen to the left
	if (background.x <= -2048) {
		background.x = background2.x + 2048;
	}
	if (background2.x <= -2048) {
		background2.x = background.x + 2048;
	}
};
// Start background animation
LK.on('tick', animateBackground);
var targets = [];
var axes = [];
var coins = [];
var score = 0;
var kills = 0;
var level = 1;
var health = 3;
var healthIcons = [];
var coinSpawnTimer = 0;
var coinSpawnInterval = 180; // 3 seconds at 60fps for "very rare" spawning
var levelTxt = new Text2('Level: 1', {
	size: 100,
	fill: 0xFFFFFF,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
levelTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(levelTxt);
// Add three health icons next to level text
for (var h = 0; h < 3; h++) {
	var healthIcon = new Text2('♥', {
		size: 80,
		fill: 0xFF0000,
		font: "Impact"
	});
	healthIcon.anchor.set(1, 0);
	healthIcon.x = -20 - h * 90; // Position relative to topRight
	healthIcon.y = 120; // Below level text
	LK.gui.topRight.addChild(healthIcon);
	healthIcons.push(healthIcon);
}
var targetSpeed = 5;
var enemies = [];
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
scoreTxt.anchor.set(.5, 0);
LK.gui.top.addChild(scoreTxt);
// Add axe counter display at bottom left
var axeCount = 5;
var axeCountTxt = new Text2('Axes: ' + axeCount, {
	size: 100,
	fill: 0xFFFFFF,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
axeCountTxt.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(axeCountTxt);
// Add button-1 to the bottom area
var button1 = game.attachAsset('button-1', {
	anchorX: 1.0,
	anchorY: 1.0,
	x: 2048,
	y: 2732
});
game.addChild(button1);
// Add button-2 next to button-1 at the bottom
var button2 = game.attachAsset('button-2', {
	anchorX: 1.0,
	anchorY: 1.0,
	x: 2048 - 310,
	y: 2732
});
game.addChild(button2);
// Initialize button cost
var buttonCost = 100;
// Display the price next to button-1
var buttonPriceTxt = new Text2(buttonCost.toString(), {
	size: 100,
	fill: 0xFF0000,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
buttonPriceTxt.anchor.set(1, 0);
buttonPriceTxt.x = button1.x - button1.width / 2;
buttonPriceTxt.y = button1.y - button1.height / 2;
game.addChild(buttonPriceTxt);
// Initialize button-2 cost
var button2Cost = 50;
// Display the price for button-2
var button2PriceTxt = new Text2(button2Cost.toString(), {
	size: 100,
	fill: 0xFF0000,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
button2PriceTxt.anchor.set(1, 0);
button2PriceTxt.x = button2.x - button2.width / 2;
button2PriceTxt.y = button2.y - button2.height / 2;
game.addChild(button2PriceTxt);
// Add event listener for button-1
button1.down = function (x, y, obj) {
	if (score >= buttonCost) {
		// Play button sound
		LK.getSound('button-1').play();
		// Smooth button press animation
		tween(button1, {
			scaleX: 0.85,
			scaleY: 0.85
		}, {
			duration: 100,
			easing: tween.easeIn,
			onFinish: function onFinish() {
				tween(button1, {
					scaleX: 1.1,
					scaleY: 1.1
				}, {
					duration: 150,
					easing: tween.bounceOut,
					onFinish: function onFinish() {
						tween(button1, {
							scaleX: 1.0,
							scaleY: 1.0
						}, {
							duration: 100
						});
					}
				});
			}
		});
		score -= buttonCost;
		scoreTxt.setText(score);
		buttonCost += 50;
		buttonPriceTxt.setText(buttonCost.toString());
		// Vaporize enemies with spinning and falling effect
		for (var i = 0; i < enemies.length; i++) {
			(function (enemy) {
				var _spinAndFall = function spinAndFall() {
					enemy.rotation += 0.1;
					enemy.speed = (enemy.speed || 10) * 1.05; // Increase speed by 5% each frame
					enemy.y += enemy.speed;
					if (enemy.y > 2732) {
						enemy.destroy();
					} else {
						LK.setTimeout(_spinAndFall, 16);
					}
				};
				_spinAndFall();
			})(enemies[i]);
		}
		enemies = [];
	}
};
// Add event listener for button-2
button2.down = function (x, y, obj) {
	if (score >= button2Cost) {
		// Play button sound
		LK.getSound('button-1').play();
		// Smooth button press animation
		tween(button2, {
			scaleX: 0.85,
			scaleY: 0.85
		}, {
			duration: 100,
			easing: tween.easeIn,
			onFinish: function onFinish() {
				tween(button2, {
					scaleX: 1.1,
					scaleY: 1.1
				}, {
					duration: 150,
					easing: tween.bounceOut,
					onFinish: function onFinish() {
						tween(button2, {
							scaleX: 1.0,
							scaleY: 1.0
						}, {
							duration: 100
						});
					}
				});
			}
		});
		score -= button2Cost;
		scoreTxt.setText(score);
		// Add 5 axes
		axeCount += 5;
		axeCountTxt.setText('Axes: ' + axeCount);
		// Increase button-2 cost by 50 like button-1
		button2Cost += 50;
		button2PriceTxt.setText(button2Cost.toString());
	}
};
var spawnTargets = function spawnTargets() {
	for (var i = 0; i < 3; i++) {
		var target = game.addChild(new Target());
		target.speed = targetSpeed;
		target.direction = Math.random() * 2 * Math.PI; // Random starting direction
		target.x = Math.random() * 2048;
		target.y = Math.random() * (2732 - target.height * 2);
		targets.push(target);
	}
	for (var i = 0; i < 1; i++) {
		var enemy = game.addChild(new Enemy());
		enemy.speed = targetSpeed;
		enemy.x = Math.random() * 2048;
		enemy.y = Math.random() * (2732 - enemy.height * 2);
		// On level 10+, 50% chance enemy tracks axes
		if (level >= 10 && Math.random() < 0.5) {
			enemy.tracksAxes = true;
			// Visual indicator for tracking enemies
			tween(enemy, {
				tint: 0xFF4444
			}, {
				duration: 500,
				easing: tween.easeOut
			});
		} else {
			enemy.tracksAxes = false;
		}
		enemies.push(enemy);
	}
};
LK.on('tick', function () {
	if (targets.length == 0) {
		spawnTargets();
	}
});
var axe;
var axeActive = false;
var axeCooldown = false;
game.down = function (x, y, obj) {
	if (axeActive || axeCooldown || axeCount <= 0) return;
	// Check if click is on button-1 or button-2 to prevent axe throwing
	var button1Bounds = {
		left: button1.x - button1.width,
		right: button1.x,
		top: button1.y - button1.height,
		bottom: button1.y
	};
	var button2Bounds = {
		left: button2.x - button2.width,
		right: button2.x,
		top: button2.y - button2.height,
		bottom: button2.y
	};
	// If click is within button bounds, don't throw axe
	if (x >= button1Bounds.left && x <= button1Bounds.right && y >= button1Bounds.top && y <= button1Bounds.bottom || x >= button2Bounds.left && x <= button2Bounds.right && y >= button2Bounds.top && y <= button2Bounds.bottom) {
		return;
	}
	axe = game.addChild(new Axe());
	LK.getSound('axe').play();
	axe.x = x;
	axe.y = 2732 - axe.height + 295;
	axes.push(axe);
	axeActive = true;
	axeCooldown = true;
	axeCount--;
	axeCountTxt.setText('Axes: ' + axeCount);
	LK.setTimeout(function () {
		axeCooldown = false;
	}, 1000);
};
LK.on('tick', function () {
	// Coin spawning logic - very rare
	coinSpawnTimer++;
	if (coinSpawnTimer >= coinSpawnInterval) {
		// Random chance to actually spawn (making it even more rare)
		if (Math.random() < 0.3) {
			// 30% chance every 3 seconds
			var coin = game.addChild(new Coin());
			coin.x = Math.random() * (2048 - 200) + 100; // Keep away from edges
			coin.y = -100; // Start above screen
			coins.push(coin);
		}
		coinSpawnTimer = 0;
		// Randomize next spawn interval for more organic feel
		coinSpawnInterval = 180 + Math.floor(Math.random() * 120); // 3-5 seconds
	}
	// Move and check coins
	for (var c = coins.length - 1; c >= 0; c--) {
		coins[c]._move_migrated();
		// Remove coins that fall off screen
		if (coins[c].y > 2732 + 100) {
			coins[c].destroy();
			coins.splice(c, 1);
			continue;
		}
		// Check collision with axes
		for (var a = 0; a < axes.length; a++) {
			if (!coins[c].collected && coins[c].intersects(axes[a])) {
				coins[c].collected = true;
				// Play coin collection sound
				LK.getSound('coin').play();
				// Add 500 points
				score += 500;
				scoreTxt.setText(score);
				// Collection animation
				tween(coins[c], {
					scaleX: 2,
					scaleY: 2,
					alpha: 0,
					rotation: coins[c].rotation + Math.PI * 2
				}, {
					duration: 500,
					easing: tween.easeOut,
					onFinish: function (coin) {
						return function () {
							coin.destroy();
						};
					}(coins[c])
				});
				// Flash score text gold
				tween(scoreTxt, {
					tint: 0xFFD700
				}, {
					duration: 200,
					easing: tween.easeOut,
					onFinish: function onFinish() {
						tween(scoreTxt, {
							tint: 0xFFFFFF
						}, {
							duration: 300,
							easing: tween.easeIn
						});
					}
				});
				coins.splice(c, 1);
				break;
			}
		}
	}
	for (var i = 0; i < targets.length; i++) {
		targets[i]._move_migrated();
	}
	for (var i = axes.length - 1; i >= 0; i--) {
		axes[i]._move_migrated();
		// Check if axe has been alive for 4 seconds (240 ticks at 60fps)
		if (!axes[i].lifeTimer) {
			axes[i].lifeTimer = 0;
		}
		axes[i].lifeTimer++;
		if (axes[i].lifeTimer >= 300) {
			// 5 seconds * 60 fps = 300 ticks
			// Evaporate axe with animation
			tween(axes[i], {
				alpha: 0,
				scaleX: 0,
				scaleY: 0
			}, {
				duration: 300,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					if (axes[i]) {
						axes[i].destroy();
					}
				}
			});
			axes.splice(i, 1);
			axeActive = false;
			axeCooldown = false;
			// Reduce health when axe evaporates after 4 seconds
			health--;
			if (health > 0 && healthIcons[health]) {
				// Hide health icon with animation
				tween(healthIcons[health], {
					alpha: 0,
					scaleX: 0,
					scaleY: 0
				}, {
					duration: 300,
					easing: tween.easeIn
				});
			}
			// Game over if no health left
			if (health <= 0) {
				LK.showGameOver();
				return;
			}
			continue;
		}
		if (axes[i].y < -50) {
			axes[i].destroy();
			axes.splice(i, 1);
			axeActive = false;
			// Reset cooldown if axe is gone before 1s
			axeCooldown = false;
			continue;
		}
		if (axes[i] && axes[i].y <= 0) {
			LK.showGameOver();
			axeActive = false;
			axeCooldown = false;
		}
	}
	for (var i = 0; i < enemies.length; i++) {
		enemies[i]._move_migrated();
		for (var j = 0; j < axes.length; j++) {
			if (axes[j].intersects(enemies[i])) {
				axeActive = false;
				axeCooldown = false;
				LK.showGameOver();
				return;
			}
		}
	}
	for (var i = axes.length - 1; i >= 0; i--) {
		if (!axes[i]) continue; // Skip if axe doesn't exist
		for (var j = targets.length - 1; j >= 0; j--) {
			if (!targets[j]) continue; // Skip if target doesn't exist
			if (axes[i].intersects(targets[j])) {
				var distance = Math.sqrt(Math.pow(axes[i].x - targets[j].x, 2) + Math.pow(axes[i].y - targets[j].y, 2));
				score += Math.max(0, 100 - distance) + 50;
				scoreTxt.setText(score);
				// Store references before destruction
				var currentAxe = axes[i];
				var currentTarget = targets[j];
				// Animate target destruction
				tween(currentTarget, {
					scaleX: 0,
					scaleY: 0,
					rotation: Math.PI * 2,
					alpha: 0
				}, {
					duration: 300,
					easing: tween.easeIn,
					onFinish: function onFinish() {
						currentTarget.destroy();
					}
				});
				// Animate axe hit effect
				tween(currentAxe, {
					scaleX: 1.5,
					scaleY: 1.5,
					alpha: 0
				}, {
					duration: 200,
					easing: tween.easeOut,
					onFinish: function onFinish() {
						currentAxe.destroy();
					}
				});
				axes.splice(i, 1);
				axeActive = false;
				axeCooldown = false;
				targets.splice(j, 1);
				kills++;
				if (kills % 3 == 0) {
					targetSpeed += 1;
					level++;
					levelTxt.setText('Level: ' + level);
					// Add level up flash effect
					LK.effects.flashScreen(0x00FF00, 500);
					// Animate level text
					tween(levelTxt, {
						scaleX: 1.5,
						scaleY: 1.5
					}, {
						duration: 300,
						easing: tween.bounceOut,
						onFinish: function onFinish() {
							tween(levelTxt, {
								scaleX: 1.0,
								scaleY: 1.0
							}, {
								duration: 200
							});
						}
					});
					// At level 10, slowly transition background from blue to orange
					if (level === 10) {
						// Create a dummy object to tween the background color
						var colorTransition = {
							color: 0x87CEEB
						}; // Start with current blue color
						tween(colorTransition, {
							color: 0xFFA500 // Orange color
						}, {
							duration: 5000,
							// 5 seconds for slow transition
							easing: tween.easeInOut,
							onFinish: function onFinish() {
								// Ensure final color is set
								game.setBackgroundColor(0xFFA500);
							}
						});
						// Update background color during tween
						var _updateColor = function updateColor() {
							if (colorTransition.color !== undefined) {
								game.setBackgroundColor(Math.floor(colorTransition.color));
								if (Math.floor(colorTransition.color) !== 0xFFA500) {
									LK.setTimeout(_updateColor, 16); // ~60fps update
								}
							}
						};
						_updateColor();
						// Make green grass field 50% black and 50% transparent
						tween(background, {
							tint: 0x808080,
							// 50% black (gray)
							alpha: 0.5 // 50% transparent
						}, {
							duration: 3000,
							// 3 seconds for slow transition
							easing: tween.easeInOut
						});
						tween(background2, {
							tint: 0x808080,
							// 50% black (gray) 
							alpha: 0.5 // 50% transparent
						}, {
							duration: 3000,
							// 3 seconds for slow transition
							easing: tween.easeInOut
						});
					}
				}
				break;
			}
		}
	}
}); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Axe = Container.expand(function () {
	var self = Container.call(this);
	var axeGraphics = self.attachAsset('axe', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 40;
	self.gravity = 0.3; // Adjusted gravity effect
	self.velocityY = -self.speed; // Initial upward velocity
	self._move_migrated = function () {
		self.velocityY += self.gravity; // Apply gravity to velocity
		self.y += self.velocityY; // Update position with velocity
		self.rotation += 0.1;
	};
	// Add entrance animation when axe is created
	self.scale.set(0.1, 0.1);
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 200,
		easing: tween.bounceOut
	});
	tween(axeGraphics, {
		tint: 0xFFFFFF
	}, {
		duration: 300
	});
});
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('Coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 3; // Fall speed
	self.collected = false;
	self._move_migrated = function () {
		// Fall down
		self.y += self.speed;
		// Rotate for visual effect
		self.rotation += 0.05;
	};
	// Add entrance animation
	self.scale.set(0.1, 0.1);
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 300,
		easing: tween.bounceOut
	});
	// Add shimmer effect
	tween(coinGraphics, {
		tint: 0xFFFF00
	}, {
		duration: 500,
		easing: tween.easeInOut,
		loop: true,
		yoyo: true
	});
	return self;
});
var Enemy = Container.expand(function () {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('enemy', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.direction = 1;
	self.scaleFactor = 0.1; // Initial small scale
	self.growthRate = 0.01; // Rate at which the enemy grows
	self._move_migrated = function () {
		// On level 10+, some enemies target axes
		if (level >= 10 && self.tracksAxes && axes.length > 0) {
			// Find closest axe
			var closestAxe = null;
			var closestDistance = Infinity;
			for (var k = 0; k < axes.length; k++) {
				var axeDistance = Math.sqrt(Math.pow(self.x - axes[k].x, 2) + Math.pow(self.y - axes[k].y, 2));
				if (axeDistance < closestDistance) {
					closestDistance = axeDistance;
					closestAxe = axes[k];
				}
			}
			// Move towards closest axe
			if (closestAxe) {
				var angleToAxe = Math.atan2(closestAxe.y - self.y, closestAxe.x - self.x);
				self.direction = angleToAxe;
				// Increase speed when tracking
				self.x += self.speed * 1.5 * Math.cos(self.direction);
				self.y += self.speed * 1.5 * Math.sin(self.direction);
			} else {
				// Normal movement if no axes
				if (Math.random() < 0.05) {
					self.direction = Math.random() * 2 * Math.PI;
				}
				self.x += self.speed * Math.cos(self.direction);
				self.y += self.speed * Math.sin(self.direction);
			}
		} else {
			// Normal movement for non-tracking enemies or level < 10
			if (Math.random() < 0.05) {
				self.direction = Math.random() * 2 * Math.PI; // Random angle
			}
			// Update position based on direction
			self.x += self.speed * Math.cos(self.direction);
			self.y += self.speed * Math.sin(self.direction);
		}
		// Keep enemy within bounds
		if (self.x <= 0 || self.x >= 2048) {
			self.direction = Math.PI - self.direction; // Reflect horizontally
		}
		if (self.y <= 0 || self.y >= 2732) {
			self.direction = -self.direction; // Reflect vertically
		}
		// Increase size until normal size is reached
		if (self.scaleFactor < 1) {
			self.scaleFactor += self.growthRate;
			self.scale.set(self.scaleFactor, self.scaleFactor);
		}
		// Add pulsing effect when fully grown
		if (self.scaleFactor >= 1 && !self.pulseStarted) {
			self.pulseStarted = true;
			var _pulse = function pulse() {
				tween(self, {
					scaleX: 1.1,
					scaleY: 1.1
				}, {
					duration: 1000,
					easing: tween.easeInOut,
					onFinish: function onFinish() {
						tween(self, {
							scaleX: 1,
							scaleY: 1
						}, {
							duration: 1000,
							easing: tween.easeInOut,
							onFinish: _pulse
						});
					}
				});
			};
			_pulse();
		}
	};
});
var Target = Container.expand(function () {
	var self = Container.call(this);
	var targetGraphics = self.attachAsset('target', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.direction = 1;
	self._move_migrated = function () {
		// Random direction changes like enemies
		if (Math.random() < 0.05) {
			self.direction = Math.random() * 2 * Math.PI; // Random angle
		}
		// Update position based on direction
		self.x += self.speed * Math.cos(self.direction);
		self.y += self.speed * Math.sin(self.direction);
		// Keep target within bounds
		if (self.x <= 0 || self.x >= 2048) {
			self.direction = Math.PI - self.direction; // Reflect horizontally
		}
		if (self.y <= 0 || self.y >= 2732) {
			self.direction = -self.direction; // Reflect vertically
		}
	};
	// Add entrance animation
	self.alpha = 0;
	self.scale.set(0.1, 0.1);
	tween(self, {
		alpha: 1
	}, {
		duration: 500,
		easing: tween.easeOut
	});
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 800,
		easing: tween.elasticOut
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
// Start background music
LK.playMusic('backgroundmusic');
var background = game.attachAsset('green_grass_field', {});
background.width = 2048;
background.height = 2732;
// Create second background for seamless horizontal scrolling
var background2 = game.attachAsset('green_grass_field', {});
background2.width = 2048;
background2.height = 2732;
background2.x = 2048; // Position to the right of the first background
// Add both backgrounds to game
game.addChild(background);
game.addChild(background2);
// Animate background movement - floating slowly to the left like clouds
var backgroundSpeed = 0.5; // Slower speed for floating effect
var animateBackground = function animateBackground() {
	// Move backgrounds left (floating effect)
	background.x -= backgroundSpeed;
	background2.x -= backgroundSpeed;
	// Reset position when background moves off screen to the left
	if (background.x <= -2048) {
		background.x = background2.x + 2048;
	}
	if (background2.x <= -2048) {
		background2.x = background.x + 2048;
	}
};
// Start background animation
LK.on('tick', animateBackground);
var targets = [];
var axes = [];
var coins = [];
var score = 0;
var kills = 0;
var level = 1;
var health = 3;
var healthIcons = [];
var coinSpawnTimer = 0;
var coinSpawnInterval = 180; // 3 seconds at 60fps for "very rare" spawning
var levelTxt = new Text2('Level: 1', {
	size: 100,
	fill: 0xFFFFFF,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
levelTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(levelTxt);
// Add three health icons next to level text
for (var h = 0; h < 3; h++) {
	var healthIcon = new Text2('♥', {
		size: 80,
		fill: 0xFF0000,
		font: "Impact"
	});
	healthIcon.anchor.set(1, 0);
	healthIcon.x = -20 - h * 90; // Position relative to topRight
	healthIcon.y = 120; // Below level text
	LK.gui.topRight.addChild(healthIcon);
	healthIcons.push(healthIcon);
}
var targetSpeed = 5;
var enemies = [];
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
scoreTxt.anchor.set(.5, 0);
LK.gui.top.addChild(scoreTxt);
// Add axe counter display at bottom left
var axeCount = 5;
var axeCountTxt = new Text2('Axes: ' + axeCount, {
	size: 100,
	fill: 0xFFFFFF,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
axeCountTxt.anchor.set(0, 1);
LK.gui.bottomLeft.addChild(axeCountTxt);
// Add button-1 to the bottom area
var button1 = game.attachAsset('button-1', {
	anchorX: 1.0,
	anchorY: 1.0,
	x: 2048,
	y: 2732
});
game.addChild(button1);
// Add button-2 next to button-1 at the bottom
var button2 = game.attachAsset('button-2', {
	anchorX: 1.0,
	anchorY: 1.0,
	x: 2048 - 310,
	y: 2732
});
game.addChild(button2);
// Initialize button cost
var buttonCost = 100;
// Display the price next to button-1
var buttonPriceTxt = new Text2(buttonCost.toString(), {
	size: 100,
	fill: 0xFF0000,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
buttonPriceTxt.anchor.set(1, 0);
buttonPriceTxt.x = button1.x - button1.width / 2;
buttonPriceTxt.y = button1.y - button1.height / 2;
game.addChild(buttonPriceTxt);
// Initialize button-2 cost
var button2Cost = 50;
// Display the price for button-2
var button2PriceTxt = new Text2(button2Cost.toString(), {
	size: 100,
	fill: 0xFF0000,
	font: "Impact",
	dropShadow: true,
	dropShadowColor: 0x000000
});
button2PriceTxt.anchor.set(1, 0);
button2PriceTxt.x = button2.x - button2.width / 2;
button2PriceTxt.y = button2.y - button2.height / 2;
game.addChild(button2PriceTxt);
// Add event listener for button-1
button1.down = function (x, y, obj) {
	if (score >= buttonCost) {
		// Play button sound
		LK.getSound('button-1').play();
		// Smooth button press animation
		tween(button1, {
			scaleX: 0.85,
			scaleY: 0.85
		}, {
			duration: 100,
			easing: tween.easeIn,
			onFinish: function onFinish() {
				tween(button1, {
					scaleX: 1.1,
					scaleY: 1.1
				}, {
					duration: 150,
					easing: tween.bounceOut,
					onFinish: function onFinish() {
						tween(button1, {
							scaleX: 1.0,
							scaleY: 1.0
						}, {
							duration: 100
						});
					}
				});
			}
		});
		score -= buttonCost;
		scoreTxt.setText(score);
		buttonCost += 50;
		buttonPriceTxt.setText(buttonCost.toString());
		// Vaporize enemies with spinning and falling effect
		for (var i = 0; i < enemies.length; i++) {
			(function (enemy) {
				var _spinAndFall = function spinAndFall() {
					enemy.rotation += 0.1;
					enemy.speed = (enemy.speed || 10) * 1.05; // Increase speed by 5% each frame
					enemy.y += enemy.speed;
					if (enemy.y > 2732) {
						enemy.destroy();
					} else {
						LK.setTimeout(_spinAndFall, 16);
					}
				};
				_spinAndFall();
			})(enemies[i]);
		}
		enemies = [];
	}
};
// Add event listener for button-2
button2.down = function (x, y, obj) {
	if (score >= button2Cost) {
		// Play button sound
		LK.getSound('button-1').play();
		// Smooth button press animation
		tween(button2, {
			scaleX: 0.85,
			scaleY: 0.85
		}, {
			duration: 100,
			easing: tween.easeIn,
			onFinish: function onFinish() {
				tween(button2, {
					scaleX: 1.1,
					scaleY: 1.1
				}, {
					duration: 150,
					easing: tween.bounceOut,
					onFinish: function onFinish() {
						tween(button2, {
							scaleX: 1.0,
							scaleY: 1.0
						}, {
							duration: 100
						});
					}
				});
			}
		});
		score -= button2Cost;
		scoreTxt.setText(score);
		// Add 5 axes
		axeCount += 5;
		axeCountTxt.setText('Axes: ' + axeCount);
		// Increase button-2 cost by 50 like button-1
		button2Cost += 50;
		button2PriceTxt.setText(button2Cost.toString());
	}
};
var spawnTargets = function spawnTargets() {
	for (var i = 0; i < 3; i++) {
		var target = game.addChild(new Target());
		target.speed = targetSpeed;
		target.direction = Math.random() * 2 * Math.PI; // Random starting direction
		target.x = Math.random() * 2048;
		target.y = Math.random() * (2732 - target.height * 2);
		targets.push(target);
	}
	for (var i = 0; i < 1; i++) {
		var enemy = game.addChild(new Enemy());
		enemy.speed = targetSpeed;
		enemy.x = Math.random() * 2048;
		enemy.y = Math.random() * (2732 - enemy.height * 2);
		// On level 10+, 50% chance enemy tracks axes
		if (level >= 10 && Math.random() < 0.5) {
			enemy.tracksAxes = true;
			// Visual indicator for tracking enemies
			tween(enemy, {
				tint: 0xFF4444
			}, {
				duration: 500,
				easing: tween.easeOut
			});
		} else {
			enemy.tracksAxes = false;
		}
		enemies.push(enemy);
	}
};
LK.on('tick', function () {
	if (targets.length == 0) {
		spawnTargets();
	}
});
var axe;
var axeActive = false;
var axeCooldown = false;
game.down = function (x, y, obj) {
	if (axeActive || axeCooldown || axeCount <= 0) return;
	// Check if click is on button-1 or button-2 to prevent axe throwing
	var button1Bounds = {
		left: button1.x - button1.width,
		right: button1.x,
		top: button1.y - button1.height,
		bottom: button1.y
	};
	var button2Bounds = {
		left: button2.x - button2.width,
		right: button2.x,
		top: button2.y - button2.height,
		bottom: button2.y
	};
	// If click is within button bounds, don't throw axe
	if (x >= button1Bounds.left && x <= button1Bounds.right && y >= button1Bounds.top && y <= button1Bounds.bottom || x >= button2Bounds.left && x <= button2Bounds.right && y >= button2Bounds.top && y <= button2Bounds.bottom) {
		return;
	}
	axe = game.addChild(new Axe());
	LK.getSound('axe').play();
	axe.x = x;
	axe.y = 2732 - axe.height + 295;
	axes.push(axe);
	axeActive = true;
	axeCooldown = true;
	axeCount--;
	axeCountTxt.setText('Axes: ' + axeCount);
	LK.setTimeout(function () {
		axeCooldown = false;
	}, 1000);
};
LK.on('tick', function () {
	// Coin spawning logic - very rare
	coinSpawnTimer++;
	if (coinSpawnTimer >= coinSpawnInterval) {
		// Random chance to actually spawn (making it even more rare)
		if (Math.random() < 0.3) {
			// 30% chance every 3 seconds
			var coin = game.addChild(new Coin());
			coin.x = Math.random() * (2048 - 200) + 100; // Keep away from edges
			coin.y = -100; // Start above screen
			coins.push(coin);
		}
		coinSpawnTimer = 0;
		// Randomize next spawn interval for more organic feel
		coinSpawnInterval = 180 + Math.floor(Math.random() * 120); // 3-5 seconds
	}
	// Move and check coins
	for (var c = coins.length - 1; c >= 0; c--) {
		coins[c]._move_migrated();
		// Remove coins that fall off screen
		if (coins[c].y > 2732 + 100) {
			coins[c].destroy();
			coins.splice(c, 1);
			continue;
		}
		// Check collision with axes
		for (var a = 0; a < axes.length; a++) {
			if (!coins[c].collected && coins[c].intersects(axes[a])) {
				coins[c].collected = true;
				// Play coin collection sound
				LK.getSound('coin').play();
				// Add 500 points
				score += 500;
				scoreTxt.setText(score);
				// Collection animation
				tween(coins[c], {
					scaleX: 2,
					scaleY: 2,
					alpha: 0,
					rotation: coins[c].rotation + Math.PI * 2
				}, {
					duration: 500,
					easing: tween.easeOut,
					onFinish: function (coin) {
						return function () {
							coin.destroy();
						};
					}(coins[c])
				});
				// Flash score text gold
				tween(scoreTxt, {
					tint: 0xFFD700
				}, {
					duration: 200,
					easing: tween.easeOut,
					onFinish: function onFinish() {
						tween(scoreTxt, {
							tint: 0xFFFFFF
						}, {
							duration: 300,
							easing: tween.easeIn
						});
					}
				});
				coins.splice(c, 1);
				break;
			}
		}
	}
	for (var i = 0; i < targets.length; i++) {
		targets[i]._move_migrated();
	}
	for (var i = axes.length - 1; i >= 0; i--) {
		axes[i]._move_migrated();
		// Check if axe has been alive for 4 seconds (240 ticks at 60fps)
		if (!axes[i].lifeTimer) {
			axes[i].lifeTimer = 0;
		}
		axes[i].lifeTimer++;
		if (axes[i].lifeTimer >= 300) {
			// 5 seconds * 60 fps = 300 ticks
			// Evaporate axe with animation
			tween(axes[i], {
				alpha: 0,
				scaleX: 0,
				scaleY: 0
			}, {
				duration: 300,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					if (axes[i]) {
						axes[i].destroy();
					}
				}
			});
			axes.splice(i, 1);
			axeActive = false;
			axeCooldown = false;
			// Reduce health when axe evaporates after 4 seconds
			health--;
			if (health > 0 && healthIcons[health]) {
				// Hide health icon with animation
				tween(healthIcons[health], {
					alpha: 0,
					scaleX: 0,
					scaleY: 0
				}, {
					duration: 300,
					easing: tween.easeIn
				});
			}
			// Game over if no health left
			if (health <= 0) {
				LK.showGameOver();
				return;
			}
			continue;
		}
		if (axes[i].y < -50) {
			axes[i].destroy();
			axes.splice(i, 1);
			axeActive = false;
			// Reset cooldown if axe is gone before 1s
			axeCooldown = false;
			continue;
		}
		if (axes[i] && axes[i].y <= 0) {
			LK.showGameOver();
			axeActive = false;
			axeCooldown = false;
		}
	}
	for (var i = 0; i < enemies.length; i++) {
		enemies[i]._move_migrated();
		for (var j = 0; j < axes.length; j++) {
			if (axes[j].intersects(enemies[i])) {
				axeActive = false;
				axeCooldown = false;
				LK.showGameOver();
				return;
			}
		}
	}
	for (var i = axes.length - 1; i >= 0; i--) {
		if (!axes[i]) continue; // Skip if axe doesn't exist
		for (var j = targets.length - 1; j >= 0; j--) {
			if (!targets[j]) continue; // Skip if target doesn't exist
			if (axes[i].intersects(targets[j])) {
				var distance = Math.sqrt(Math.pow(axes[i].x - targets[j].x, 2) + Math.pow(axes[i].y - targets[j].y, 2));
				score += Math.max(0, 100 - distance) + 50;
				scoreTxt.setText(score);
				// Store references before destruction
				var currentAxe = axes[i];
				var currentTarget = targets[j];
				// Animate target destruction
				tween(currentTarget, {
					scaleX: 0,
					scaleY: 0,
					rotation: Math.PI * 2,
					alpha: 0
				}, {
					duration: 300,
					easing: tween.easeIn,
					onFinish: function onFinish() {
						currentTarget.destroy();
					}
				});
				// Animate axe hit effect
				tween(currentAxe, {
					scaleX: 1.5,
					scaleY: 1.5,
					alpha: 0
				}, {
					duration: 200,
					easing: tween.easeOut,
					onFinish: function onFinish() {
						currentAxe.destroy();
					}
				});
				axes.splice(i, 1);
				axeActive = false;
				axeCooldown = false;
				targets.splice(j, 1);
				kills++;
				if (kills % 3 == 0) {
					targetSpeed += 1;
					level++;
					levelTxt.setText('Level: ' + level);
					// Add level up flash effect
					LK.effects.flashScreen(0x00FF00, 500);
					// Animate level text
					tween(levelTxt, {
						scaleX: 1.5,
						scaleY: 1.5
					}, {
						duration: 300,
						easing: tween.bounceOut,
						onFinish: function onFinish() {
							tween(levelTxt, {
								scaleX: 1.0,
								scaleY: 1.0
							}, {
								duration: 200
							});
						}
					});
					// At level 10, slowly transition background from blue to orange
					if (level === 10) {
						// Create a dummy object to tween the background color
						var colorTransition = {
							color: 0x87CEEB
						}; // Start with current blue color
						tween(colorTransition, {
							color: 0xFFA500 // Orange color
						}, {
							duration: 5000,
							// 5 seconds for slow transition
							easing: tween.easeInOut,
							onFinish: function onFinish() {
								// Ensure final color is set
								game.setBackgroundColor(0xFFA500);
							}
						});
						// Update background color during tween
						var _updateColor = function updateColor() {
							if (colorTransition.color !== undefined) {
								game.setBackgroundColor(Math.floor(colorTransition.color));
								if (Math.floor(colorTransition.color) !== 0xFFA500) {
									LK.setTimeout(_updateColor, 16); // ~60fps update
								}
							}
						};
						_updateColor();
						// Make green grass field 50% black and 50% transparent
						tween(background, {
							tint: 0x808080,
							// 50% black (gray)
							alpha: 0.5 // 50% transparent
						}, {
							duration: 3000,
							// 3 seconds for slow transition
							easing: tween.easeInOut
						});
						tween(background2, {
							tint: 0x808080,
							// 50% black (gray) 
							alpha: 0.5 // 50% transparent
						}, {
							duration: 3000,
							// 3 seconds for slow transition
							easing: tween.easeInOut
						});
					}
				}
				break;
			}
		}
	}
});