/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var Balloon = Container.expand(function (color, speed) {
	var self = Container.call(this);
	self.color = color || 'redBalloon';
	self.speed = speed || 2;
	self.value = 1;
	if (self.color === 'goldBalloon') {
		self.value = 5;
	}
	var balloonGraphics = self.attachAsset(self.color, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var stringGraphics = self.attachAsset('string', {
		anchorX: 0.5,
		anchorY: 0
	});
	stringGraphics.y = balloonGraphics.height / 2;
	self.radius = balloonGraphics.width / 2;
	self.update = function () {
		self.y -= self.speed;
		// Apply slight horizontal drift
		if (LK.ticks % 30 === 0) {
			self.x += Math.random() * 2 - 1;
		}
	};
	self.pop = function () {
		// Play pop sound
		if (self.color === 'goldBalloon') {
			LK.getSound('goldPop').play();
		} else {
			LK.getSound('pop').play();
		}
		// Flash effect and destroy
		LK.effects.flashObject(self, 0xFFFFFF, 200);
		// Scale up and fade out animation
		tween(self, {
			scaleX: 1.5,
			scaleY: 1.5,
			alpha: 0
		}, {
			duration: 200,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.isPopped = true;
			}
		});
	};
	// Receive touch events
	self.down = function (x, y, obj) {
		self.pop();
		return true; // Event handled
	};
	return self;
});
var Bomb = Container.expand(function (speed) {
	var self = Container.call(this);
	self.speed = speed || 1.5;
	var bombGraphics = self.attachAsset('bomb', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var stringGraphics = self.attachAsset('string', {
		anchorX: 0.5,
		anchorY: 0
	});
	stringGraphics.y = bombGraphics.height / 2;
	self.radius = bombGraphics.width / 2;
	self.update = function () {
		self.y -= self.speed;
		// Apply slight horizontal drift
		if (LK.ticks % 30 === 0) {
			self.x += Math.random() * 2 - 1;
		}
	};
	self.explode = function () {
		// Play explosion sound
		LK.getSound('bombExplode').play();
		// Flash screen and bomb
		LK.effects.flashScreen(0xFF0000, 300);
		LK.effects.flashObject(self, 0xFF0000, 300);
		// Scale up and fade out animation
		tween(self, {
			scaleX: 2,
			scaleY: 2,
			alpha: 0
		}, {
			duration: 300,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.isExploded = true;
			}
		});
	};
	// Receive touch events
	self.down = function (x, y, obj) {
		self.explode();
		return true; // Event handled
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Sky blue background
});
/**** 
* Game Code
****/ 
// Game variables
var score = 0;
var level = 1;
var lives = 3;
var balloons = [];
var bombs = [];
var gameActive = true;
var spawnInterval = 60; // Frames between balloon spawns
var bombChance = 0.2; // Chance of spawning a bomb instead of a balloon
var goldChance = 0.1; // Chance of spawning a gold balloon
var lastSpawn = 0;
var highScore = storage.highScore || 0;
// UI elements
var scoreTxt = new Text2('Score: 0', {
	size: 70,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -scoreTxt.width - 20;
scoreTxt.y = 20;
var livesTxt = new Text2('Lives: 3', {
	size: 70,
	fill: 0xFFFFFF
});
livesTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(livesTxt);
livesTxt.x = -livesTxt.width - 20;
livesTxt.y = scoreTxt.y + scoreTxt.height + 10;
var levelTxt = new Text2('Level: 1', {
	size: 70,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.top.addChild(levelTxt);
levelTxt.y = 20;
// Function to update score display
function updateScore(points) {
	score += points;
	scoreTxt.setText('Score: ' + score);
	// Update high score if needed
	if (score > highScore) {
		highScore = score;
		storage.highScore = highScore;
	}
	// Level up every 100 points
	var newLevel = Math.floor(score / 100) + 1;
	if (newLevel > level) {
		level = newLevel;
		levelTxt.setText('Level: ' + level);
		// Increase difficulty
		spawnInterval = Math.max(20, 60 - level * 5);
		bombChance = Math.min(0.4, 0.2 + level * 0.03);
	}
	// Set LK engine score for leaderboards
	LK.setScore(score);
}
// Function to update lives display
function updateLives(change) {
	lives += change;
	livesTxt.setText('Lives: ' + lives);
	if (lives <= 0) {
		gameActive = false;
		LK.showGameOver();
	}
}
// Function to spawn a new balloon
function spawnBalloon() {
	// Random horizontal position
	var x = Math.random() * (2048 - 200) + 100;
	// Randomize balloon type
	var randomValue = Math.random();
	if (randomValue < bombChance) {
		// Spawn bomb
		var bomb = new Bomb(1.5 + level * 0.2);
		bomb.x = x;
		bomb.y = 2732 + bomb.radius;
		bombs.push(bomb);
		game.addChild(bomb);
	} else {
		// Spawn balloon
		var colorOptions = ['redBalloon', 'blueBalloon', 'greenBalloon'];
		var balloonColor = colorOptions[Math.floor(Math.random() * colorOptions.length)];
		// Gold balloon chance
		if (Math.random() < goldChance) {
			balloonColor = 'goldBalloon';
		}
		var balloon = new Balloon(balloonColor, 1.5 + level * 0.3);
		balloon.x = x;
		balloon.y = 2732 + balloon.radius;
		balloons.push(balloon);
		game.addChild(balloon);
	}
}
// Start background music
LK.playMusic('gameMusic', {
	fade: {
		start: 0,
		end: 0.4,
		duration: 1000
	}
});
// Game update function
game.update = function () {
	if (!gameActive) {
		return;
	}
	// Spawn new balloons based on interval
	if (LK.ticks - lastSpawn > spawnInterval) {
		spawnBalloon();
		lastSpawn = LK.ticks;
	}
	// Update balloons
	for (var i = balloons.length - 1; i >= 0; i--) {
		var balloon = balloons[i];
		// Check if balloon is off screen
		if (balloon.y < -balloon.radius) {
			// Missed a balloon
			game.removeChild(balloon);
			balloons.splice(i, 1);
			// Only lose a life if it's not a bomb
			updateLives(-1);
			continue;
		}
		// Check if balloon has been popped
		if (balloon.isPopped) {
			game.removeChild(balloon);
			balloons.splice(i, 1);
			updateScore(balloon.value);
		}
	}
	// Update bombs
	for (var j = bombs.length - 1; j >= 0; j--) {
		var bomb = bombs[j];
		// Check if bomb is off screen
		if (bomb.y < -bomb.radius) {
			// Bomb floated away safely
			game.removeChild(bomb);
			bombs.splice(j, 1);
			continue;
		}
		// Check if bomb has exploded
		if (bomb.isExploded) {
			game.removeChild(bomb);
			bombs.splice(j, 1);
			updateLives(-1);
		}
	}
};
// Mouse/touch events for game area (not needed since objects handle their own touch events)
game.down = function (x, y, obj) {
	// This is handled by the individual balloon/bomb objects
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var Balloon = Container.expand(function (color, speed) {
	var self = Container.call(this);
	self.color = color || 'redBalloon';
	self.speed = speed || 2;
	self.value = 1;
	if (self.color === 'goldBalloon') {
		self.value = 5;
	}
	var balloonGraphics = self.attachAsset(self.color, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var stringGraphics = self.attachAsset('string', {
		anchorX: 0.5,
		anchorY: 0
	});
	stringGraphics.y = balloonGraphics.height / 2;
	self.radius = balloonGraphics.width / 2;
	self.update = function () {
		self.y -= self.speed;
		// Apply slight horizontal drift
		if (LK.ticks % 30 === 0) {
			self.x += Math.random() * 2 - 1;
		}
	};
	self.pop = function () {
		// Play pop sound
		if (self.color === 'goldBalloon') {
			LK.getSound('goldPop').play();
		} else {
			LK.getSound('pop').play();
		}
		// Flash effect and destroy
		LK.effects.flashObject(self, 0xFFFFFF, 200);
		// Scale up and fade out animation
		tween(self, {
			scaleX: 1.5,
			scaleY: 1.5,
			alpha: 0
		}, {
			duration: 200,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.isPopped = true;
			}
		});
	};
	// Receive touch events
	self.down = function (x, y, obj) {
		self.pop();
		return true; // Event handled
	};
	return self;
});
var Bomb = Container.expand(function (speed) {
	var self = Container.call(this);
	self.speed = speed || 1.5;
	var bombGraphics = self.attachAsset('bomb', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var stringGraphics = self.attachAsset('string', {
		anchorX: 0.5,
		anchorY: 0
	});
	stringGraphics.y = bombGraphics.height / 2;
	self.radius = bombGraphics.width / 2;
	self.update = function () {
		self.y -= self.speed;
		// Apply slight horizontal drift
		if (LK.ticks % 30 === 0) {
			self.x += Math.random() * 2 - 1;
		}
	};
	self.explode = function () {
		// Play explosion sound
		LK.getSound('bombExplode').play();
		// Flash screen and bomb
		LK.effects.flashScreen(0xFF0000, 300);
		LK.effects.flashObject(self, 0xFF0000, 300);
		// Scale up and fade out animation
		tween(self, {
			scaleX: 2,
			scaleY: 2,
			alpha: 0
		}, {
			duration: 300,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.isExploded = true;
			}
		});
	};
	// Receive touch events
	self.down = function (x, y, obj) {
		self.explode();
		return true; // Event handled
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Sky blue background
});
/**** 
* Game Code
****/ 
// Game variables
var score = 0;
var level = 1;
var lives = 3;
var balloons = [];
var bombs = [];
var gameActive = true;
var spawnInterval = 60; // Frames between balloon spawns
var bombChance = 0.2; // Chance of spawning a bomb instead of a balloon
var goldChance = 0.1; // Chance of spawning a gold balloon
var lastSpawn = 0;
var highScore = storage.highScore || 0;
// UI elements
var scoreTxt = new Text2('Score: 0', {
	size: 70,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -scoreTxt.width - 20;
scoreTxt.y = 20;
var livesTxt = new Text2('Lives: 3', {
	size: 70,
	fill: 0xFFFFFF
});
livesTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(livesTxt);
livesTxt.x = -livesTxt.width - 20;
livesTxt.y = scoreTxt.y + scoreTxt.height + 10;
var levelTxt = new Text2('Level: 1', {
	size: 70,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.top.addChild(levelTxt);
levelTxt.y = 20;
// Function to update score display
function updateScore(points) {
	score += points;
	scoreTxt.setText('Score: ' + score);
	// Update high score if needed
	if (score > highScore) {
		highScore = score;
		storage.highScore = highScore;
	}
	// Level up every 100 points
	var newLevel = Math.floor(score / 100) + 1;
	if (newLevel > level) {
		level = newLevel;
		levelTxt.setText('Level: ' + level);
		// Increase difficulty
		spawnInterval = Math.max(20, 60 - level * 5);
		bombChance = Math.min(0.4, 0.2 + level * 0.03);
	}
	// Set LK engine score for leaderboards
	LK.setScore(score);
}
// Function to update lives display
function updateLives(change) {
	lives += change;
	livesTxt.setText('Lives: ' + lives);
	if (lives <= 0) {
		gameActive = false;
		LK.showGameOver();
	}
}
// Function to spawn a new balloon
function spawnBalloon() {
	// Random horizontal position
	var x = Math.random() * (2048 - 200) + 100;
	// Randomize balloon type
	var randomValue = Math.random();
	if (randomValue < bombChance) {
		// Spawn bomb
		var bomb = new Bomb(1.5 + level * 0.2);
		bomb.x = x;
		bomb.y = 2732 + bomb.radius;
		bombs.push(bomb);
		game.addChild(bomb);
	} else {
		// Spawn balloon
		var colorOptions = ['redBalloon', 'blueBalloon', 'greenBalloon'];
		var balloonColor = colorOptions[Math.floor(Math.random() * colorOptions.length)];
		// Gold balloon chance
		if (Math.random() < goldChance) {
			balloonColor = 'goldBalloon';
		}
		var balloon = new Balloon(balloonColor, 1.5 + level * 0.3);
		balloon.x = x;
		balloon.y = 2732 + balloon.radius;
		balloons.push(balloon);
		game.addChild(balloon);
	}
}
// Start background music
LK.playMusic('gameMusic', {
	fade: {
		start: 0,
		end: 0.4,
		duration: 1000
	}
});
// Game update function
game.update = function () {
	if (!gameActive) {
		return;
	}
	// Spawn new balloons based on interval
	if (LK.ticks - lastSpawn > spawnInterval) {
		spawnBalloon();
		lastSpawn = LK.ticks;
	}
	// Update balloons
	for (var i = balloons.length - 1; i >= 0; i--) {
		var balloon = balloons[i];
		// Check if balloon is off screen
		if (balloon.y < -balloon.radius) {
			// Missed a balloon
			game.removeChild(balloon);
			balloons.splice(i, 1);
			// Only lose a life if it's not a bomb
			updateLives(-1);
			continue;
		}
		// Check if balloon has been popped
		if (balloon.isPopped) {
			game.removeChild(balloon);
			balloons.splice(i, 1);
			updateScore(balloon.value);
		}
	}
	// Update bombs
	for (var j = bombs.length - 1; j >= 0; j--) {
		var bomb = bombs[j];
		// Check if bomb is off screen
		if (bomb.y < -bomb.radius) {
			// Bomb floated away safely
			game.removeChild(bomb);
			bombs.splice(j, 1);
			continue;
		}
		// Check if bomb has exploded
		if (bomb.isExploded) {
			game.removeChild(bomb);
			bombs.splice(j, 1);
			updateLives(-1);
		}
	}
};
// Mouse/touch events for game area (not needed since objects handle their own touch events)
game.down = function (x, y, obj) {
	// This is handled by the individual balloon/bomb objects
};