/**** 
* Classes
****/ 
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.direction = {
		x: 0,
		y: 0
	};
	self.speed = 10 * snowballSpeed;
	self.width *= 0.5 * snowballSize;
	self.height *= 0.5 * snowballSize;
	self._move_migrated = function () {
		self.x += self.direction.x * self.speed;
		self.y += self.direction.y * self.speed;
	};
});
var FloorTile = Container.expand(function () {
	var self = Container.call(this);
	var floorTileGraphics = self.attachAsset('floorTile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
var IceBlock = Container.expand(function () {
	var self = Container.call(this);
	var iceBlockGraphics = self.attachAsset('iceBlock', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = {
		x: 0,
		y: 0
	};
	self.speed = 15;
	self.shoot = function () {
		for (var i = 0; i < 16; i++) {
			var bullet = new Bullet();
			bullet.x = self.x;
			bullet.y = self.y;
			var angle = Math.PI * 2 / 16 * i;
			bullet.direction.x = Math.cos(angle);
			bullet.direction.y = Math.sin(angle);
			bullets.push(bullet);
			self.addChild(bullet);
		}
	};
	self.moveTowards = function (pos) {
		var dx = pos.x - self.x;
		var dy = pos.y - self.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
		self.velocity.x += dx * self.speed / magnitude;
		self.velocity.y += dy * self.speed / magnitude;
	};
	self.moveAwayFrom = function (pos) {
		var dx = pos.x - self.x;
		var dy = pos.y - self.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
		self.velocity.x -= dx * self.speed / magnitude;
		self.velocity.y -= dy * self.speed / magnitude;
	};
	self._move_migrated = function () {
		self.x += self.velocity.x;
		self.y += self.velocity.y;
		self.velocity.x *= 0.95;
		self.velocity.y *= 0.95;
	};
});
var Powerup = Container.expand(function () {
	var self = Container.call(this);
	var powerupGraphics = self.attachAsset('arrowDown', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width *= 2;
	self.height *= 2;
	self.timer = 0;
	self.blinking = false;
	self._update_migrated = function () {
		self.timer += 1;
		if (self.timer > 240) {
			self.blinking = true;
		}
		if (self.blinking) {
			powerupGraphics.alpha = 0.5 + 0.5 * Math.sin(self.timer / 10);
		}
	};
});
var SnowManEnemy = Container.expand(function () {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('arrowUp', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width *= 2;
	self.height *= 2;
	self.speed = 2 + Math.random() * 0.5;
	self._move_migrated = function (player) {
		var dx = player.x - self.x;
		var dy = player.y - self.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
		self.x += dx * self.speed / magnitude;
		self.y += dy * self.speed / magnitude;
	};
});
var Snowflake = Container.expand(function () {
	var self = Container.call(this);
	var snowflakeGraphics = self.attachAsset('snowflake', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	snowflakeGraphics.width = 10;
	snowflakeGraphics.height = 10;
	self.speed = Math.random() * 4 + 2;
	self._move_migrated = function () {
		self.y += self.speed;
		self.x += Math.sin(self.y / 100) * 5;
		if (self.y > 2800) {
			self.y = -10;
		}
	};
});
var Upgrade = Container.expand(function () {
	var self = Container.call(this);
	var upgradeGraphics = self.attachAsset('upgrade', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
var iceBlocks = [];
var blockSize = 200;
var worldWidth = blockSize * 11;
var worldHeight = blockSize * 15;
//console.log('Game world size:', worldWidth, 'x', worldHeight);
//console.log('game.height:', game.height);
for (var i = 0; i < worldWidth / blockSize; i++) {
	for (var j = 0; j < worldHeight / blockSize; j++) {
		if (i === 0 || i === worldWidth / blockSize - 1 || j === 0 || j === worldHeight / blockSize - 1) {
			var iceBlock = new IceBlock();
			iceBlock.x = i * blockSize;
			iceBlock.y = j * blockSize;
			game.addChild(iceBlock);
		} else {
			if (true) {
				var floorTile = new FloorTile();
				floorTile.x = i * blockSize;
				floorTile.y = j * blockSize;
				game.addChild(floorTile);
			}
		}
	}
}
var player = game.addChild(new Player());
var scoreLabel = new Text2('Score: 0', {
	size: 80,
	fill: '#113355',
	align: 'left'
});
scoreLabel.x = worldWidth - 560;
scoreLabel.y = 160;
scoreLabel.anchor.set(0.5, 0.5);
game.addChild(scoreLabel);
var playerScore = 0;
var instructions = new Text2('Tap to move and throw\nsnowballs at the evil snowmen', {
	size: 100,
	fill: '#446688',
	align: 'center',
	dropShadow: true
});
instructions.x = worldWidth / 2 - 100;
instructions.y = worldHeight / 2 + 340;
instructions.anchor.set(0.5, 0.5);
game.addChild(instructions);
var playButton = game.attachAsset('playButton', {
	anchorX: 0.5,
	anchorY: 0.5
});
playButton.x = worldWidth / 2 - 100;
playButton.y = worldHeight / 2;
playButton.width = 400;
playButton.height = 400;
/*
playButton.on('down', function () {
	instructions.visible = false;
	playButton.visible = false;
	LK.emit('playButtonPressed');
});*/
var bullets = [];
var enemies = [];
var powerups = [];
var upgrades = [];
var upgradeLevel = 0;
var maxUpgradeLevel = 6;
var numSnowballs = 1;
var numBullets = 1; // Define numBullets variable
var snowballSpeed = 1;
var snowballFrequency = 2000;
var snowballSize = 1;
var upgradeMessages = ['+Extra Snowball', '+Snowball Speed', '+Snowball Frequency', '+Extra Snowball', '+Snowball Size', '+Extra Snowball'];
var enemySpawnInterval;
var bulletInterval;
var updateSpawnSpeedInterval;
var initialSpawnSpeed = 2000;
var spawnSpeedNow = initialSpawnSpeed;
var powerupSpawnInterval;
var upgradeSpawnInterval;
spawnPowerup = function spawnPowerup() {
	var powerup = new Powerup();
	do {
		var xx = Math.random() * worldWidth;
		var yy = Math.random() * worldHeight;
		var dx = xx - player.x;
		var dy = yy - player.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
	} while (magnitude < 800);
	powerup.x = xx;
	powerup.y = yy;
	powerups.push(powerup);
	game.addChild(powerup);
};
spawnEnemy = function spawnEnemy() {
	var snowManEnemy = new SnowManEnemy();
	if (Math.random() < 0.5) {
		var yy = Math.random() * worldHeight;
		if (Math.random() < 0.5) {
			var xx = -snowManEnemy.width;
		} else {
			var xx = worldWidth + snowManEnemy.width;
		}
	} else {
		var xx = Math.random() * worldHeight;
		if (Math.random() < 0.5) {
			var yy = -snowManEnemy.height;
		} else {
			var yy = worldHeight + snowManEnemy.height;
		}
	}
	snowManEnemy.x = xx;
	snowManEnemy.y = yy;
	enemies.push(snowManEnemy);
	game.addChild(snowManEnemy);
};
spawnUpgrade = function spawnUpgrade() {
	var upgrade = new Upgrade();
	do {
		var xx = 200 + Math.random() * (worldWidth - 400);
		var yy = 200 + Math.random() * (worldHeight - 400);
		var dx = xx - player.x;
		var dy = yy - player.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
	} while (magnitude < 800);
	upgrade.x = xx;
	upgrade.y = yy;
	upgrades.push(upgrade);
	game.addChild(upgrade);
};
setBulletInterval = function setBulletInterval(time) {
	LK.clearInterval(bulletInterval);
	bulletInterval = LK.setInterval(function () {
		for (var i = 0; i < numSnowballs; i++) {
			var bullet = new Bullet();
			bullet.x = player.x;
			bullet.y = player.y;
			var pos = {
				x: 0,
				y: 0
			};
			pos = lastTapPos;
			var dx = pos.x - player.x + i * 40;
			var dy = pos.y - player.y + i * 40;
			var magnitude = Math.sqrt(dx * dx + dy * dy);
			bullet.direction.x = dx / magnitude;
			bullet.direction.y = dy / magnitude;
			bullets.push(bullet);
			game.addChild(bullet);
		}
	}, time);
};
intersectsTolerance = function intersectsTolerance(a, b, tolerance) {
	tolerance *= tolerance;
	if (!a || !b) {
		return false;
	}
	var dx = a.x - b.x;
	if (dx * dx < tolerance) {
		var dy = a.y - b.y;
		if (dy * dy < tolerance) {
			return true;
		}
	}
	return false;
};
triggerUpgrade = function triggerUpgrade(x, y) {
	showSign(upgradeMessages[upgradeLevel], x, y);
	upgradeLevel++;
	if (upgradeLevel == 1 || upgradeLevel == 4 || upgradeLevel == 6) {
		numSnowballs++;
	} else if (upgradeLevel == 2) {
		snowballSpeed *= 1.5;
	} else if (upgradeLevel == 3) {
		snowballFrequency *= 0.5;
		setBulletInterval(snowballFrequency);
	} else {
		snowballSize *= 1.5;
	}
};
showSign = function showSign(message, x, y) {
	var signText = new Text2(message, {
		size: 80,
		fill: '#ffcc00',
		align: 'center',
		dropShadow: true
	});
	// Ensure the message is fully on screen
	signText.x = Math.max(signText.width / 2, Math.min(worldWidth - signText.width / 2, x));
	signText.y = Math.max(signText.height / 2, Math.min(worldHeight - signText.height / 2, y));
	signText.anchor.set(0.5, 0.5);
	game.addChild(signText);
	// Play upgrade sound effect
	var upgradeSound = LK.getSound('upgradeSound');
	upgradeSound.play();
	// Remove the sign after a brief period
	LK.setTimeout(function () {
		signText.destroy();
	}, 2000);
};
triggerBomb = function triggerBomb() {
	for (var i = 0; i < 16; i++) {
		var bullet = new Bullet();
		bullet.x = player.x;
		bullet.y = player.y;
		var angle = Math.PI * 2 / 16 * i;
		bullet.direction.x = Math.cos(angle);
		bullet.direction.y = Math.sin(angle);
		bullets.push(bullet);
		game.addChild(bullet);
	}
};
var gameStarted = false;
LK.on('playButtonPressed', function () {
	gameStarted = true;
	updateSpawnSpeedInterval = LK.setInterval(function () {
		if (spawnSpeedNow > 200) {
			spawnSpeedNow -= 100;
			LK.clearInterval(enemySpawnInterval);
			enemySpawnInterval = LK.setInterval(function () {
				spawnEnemy();
			}, spawnSpeedNow);
		}
	}, 10000);
	upgradeSpawnInterval = LK.setInterval(function () {
		if (upgradeLevel < 6) {
			spawnUpgrade();
		}
	}, 15000);
	enemySpawnInterval = LK.setInterval(function () {
		spawnEnemy();
	}, initialSpawnSpeed);
	setBulletInterval(snowballFrequency);
	powerupSpawnInterval = LK.setInterval(function () {
		spawnPowerup();
	}, 12000);
});
var snowflakes = [];
for (var i = 0; i < 30; i++) {
	var snowflake = new Snowflake();
	snowflake.x = Math.random() * 1800;
	snowflake.y = Math.random() * game.height;
	snowflakes.push(snowflake);
	game.addChild(snowflake);
}
player.x = 330;
player.y = 300;
LK.on('tick', function () {
	player._move_migrated();
	for (var i = 0; i < bullets.length; i++) {
		bullets[i]._move_migrated();
		if (bullets[i].x < 0 || bullets[i].x > worldWidth || bullets[i].y < 0 || bullets[i].y > worldHeight) {
			bullets[i].destroy();
			bullets.splice(i, 1);
			continue;
		}
		for (var j = 0; j < enemies.length; j++) {
			//if (bullets[i].intersects(enemies[j])) {
			if (intersectsTolerance(bullets[i], enemies[j], 100)) {
				enemies[j].destroy();
				playerScore += 100;
				scoreLabel.setText("Score: " + playerScore);
				enemies.splice(j, 1);
				break;
			}
		}
	}
	for (var i = 0; i < enemies.length; i++) {
		enemies[i]._move_migrated(player);
		//if (player.intersects(enemies[i])) {
		if (intersectsTolerance(player, enemies[i], 100)) {
			LK.setScore(playerScore);
			LK.showGameOver();
		}
	}
	for (var i = 0; i < snowflakes.length; i++) {
		snowflakes[i]._move_migrated();
	}
	for (var i = 0; i < powerups.length; i++) {
		powerups[i]._update_migrated();
		if (powerups[i].timer > 480) {
			powerups[i].destroy();
			powerups.splice(i, 1);
		} else {
			if (player.intersects(powerups[i])) {
				triggerBomb();
				// Play powerup sound effect
				var powerupSound = LK.getSound('powerupSound');
				powerupSound.play();
				powerups[i].destroy();
				powerups.splice(i, 1);
			}
		}
	}
	for (var i = 0; i < upgrades.length; i++) {
		if (player.intersects(upgrades[i])) {
			//triggerBomb();
			triggerUpgrade(upgrades[i].x, upgrades[i].y);
			upgrades[i].destroy();
			upgrades.splice(i, 1);
		}
	}
	if (LK.ticks % 15 == 0) {
		console.log('enemies length: ' + enemies.length);
		console.log('bullets length: ' + bullets.length);
		console.log('powerups length: ' + powerups.length);
		console.log('upgrades length: ' + upgrades.length);
	}
});
var lastTapPos = {
	x: 0,
	y: 0
};
game.on('down', function (x, y, obj) {
	if (!gameStarted) {
		instructions.visible = false;
		playButton.visible = false;
		LK.emit('playButtonPressed');
	} else {
		var pos = game.toLocal(obj.global);
		lastTapPos = pos;
		player.moveTowards(pos);
	}
}); /**** 
* Classes
****/ 
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.direction = {
		x: 0,
		y: 0
	};
	self.speed = 10 * snowballSpeed;
	self.width *= 0.5 * snowballSize;
	self.height *= 0.5 * snowballSize;
	self._move_migrated = function () {
		self.x += self.direction.x * self.speed;
		self.y += self.direction.y * self.speed;
	};
});
var FloorTile = Container.expand(function () {
	var self = Container.call(this);
	var floorTileGraphics = self.attachAsset('floorTile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
var IceBlock = Container.expand(function () {
	var self = Container.call(this);
	var iceBlockGraphics = self.attachAsset('iceBlock', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = {
		x: 0,
		y: 0
	};
	self.speed = 15;
	self.shoot = function () {
		for (var i = 0; i < 16; i++) {
			var bullet = new Bullet();
			bullet.x = self.x;
			bullet.y = self.y;
			var angle = Math.PI * 2 / 16 * i;
			bullet.direction.x = Math.cos(angle);
			bullet.direction.y = Math.sin(angle);
			bullets.push(bullet);
			self.addChild(bullet);
		}
	};
	self.moveTowards = function (pos) {
		var dx = pos.x - self.x;
		var dy = pos.y - self.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
		self.velocity.x += dx * self.speed / magnitude;
		self.velocity.y += dy * self.speed / magnitude;
	};
	self.moveAwayFrom = function (pos) {
		var dx = pos.x - self.x;
		var dy = pos.y - self.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
		self.velocity.x -= dx * self.speed / magnitude;
		self.velocity.y -= dy * self.speed / magnitude;
	};
	self._move_migrated = function () {
		self.x += self.velocity.x;
		self.y += self.velocity.y;
		self.velocity.x *= 0.95;
		self.velocity.y *= 0.95;
	};
});
var Powerup = Container.expand(function () {
	var self = Container.call(this);
	var powerupGraphics = self.attachAsset('arrowDown', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width *= 2;
	self.height *= 2;
	self.timer = 0;
	self.blinking = false;
	self._update_migrated = function () {
		self.timer += 1;
		if (self.timer > 240) {
			self.blinking = true;
		}
		if (self.blinking) {
			powerupGraphics.alpha = 0.5 + 0.5 * Math.sin(self.timer / 10);
		}
	};
});
var SnowManEnemy = Container.expand(function () {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('arrowUp', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width *= 2;
	self.height *= 2;
	self.speed = 2 + Math.random() * 0.5;
	self._move_migrated = function (player) {
		var dx = player.x - self.x;
		var dy = player.y - self.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
		self.x += dx * self.speed / magnitude;
		self.y += dy * self.speed / magnitude;
	};
});
var Snowflake = Container.expand(function () {
	var self = Container.call(this);
	var snowflakeGraphics = self.attachAsset('snowflake', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	snowflakeGraphics.width = 10;
	snowflakeGraphics.height = 10;
	self.speed = Math.random() * 4 + 2;
	self._move_migrated = function () {
		self.y += self.speed;
		self.x += Math.sin(self.y / 100) * 5;
		if (self.y > 2800) {
			self.y = -10;
		}
	};
});
var Upgrade = Container.expand(function () {
	var self = Container.call(this);
	var upgradeGraphics = self.attachAsset('upgrade', {
		anchorX: 0.5,
		anchorY: 0.5
	});
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
var iceBlocks = [];
var blockSize = 200;
var worldWidth = blockSize * 11;
var worldHeight = blockSize * 15;
//console.log('Game world size:', worldWidth, 'x', worldHeight);
//console.log('game.height:', game.height);
for (var i = 0; i < worldWidth / blockSize; i++) {
	for (var j = 0; j < worldHeight / blockSize; j++) {
		if (i === 0 || i === worldWidth / blockSize - 1 || j === 0 || j === worldHeight / blockSize - 1) {
			var iceBlock = new IceBlock();
			iceBlock.x = i * blockSize;
			iceBlock.y = j * blockSize;
			game.addChild(iceBlock);
		} else {
			if (true) {
				var floorTile = new FloorTile();
				floorTile.x = i * blockSize;
				floorTile.y = j * blockSize;
				game.addChild(floorTile);
			}
		}
	}
}
var player = game.addChild(new Player());
var scoreLabel = new Text2('Score: 0', {
	size: 80,
	fill: '#113355',
	align: 'left'
});
scoreLabel.x = worldWidth - 560;
scoreLabel.y = 160;
scoreLabel.anchor.set(0.5, 0.5);
game.addChild(scoreLabel);
var playerScore = 0;
var instructions = new Text2('Tap to move and throw\nsnowballs at the evil snowmen', {
	size: 100,
	fill: '#446688',
	align: 'center',
	dropShadow: true
});
instructions.x = worldWidth / 2 - 100;
instructions.y = worldHeight / 2 + 340;
instructions.anchor.set(0.5, 0.5);
game.addChild(instructions);
var playButton = game.attachAsset('playButton', {
	anchorX: 0.5,
	anchorY: 0.5
});
playButton.x = worldWidth / 2 - 100;
playButton.y = worldHeight / 2;
playButton.width = 400;
playButton.height = 400;
/*
playButton.on('down', function () {
	instructions.visible = false;
	playButton.visible = false;
	LK.emit('playButtonPressed');
});*/
var bullets = [];
var enemies = [];
var powerups = [];
var upgrades = [];
var upgradeLevel = 0;
var maxUpgradeLevel = 6;
var numSnowballs = 1;
var numBullets = 1; // Define numBullets variable
var snowballSpeed = 1;
var snowballFrequency = 2000;
var snowballSize = 1;
var upgradeMessages = ['+Extra Snowball', '+Snowball Speed', '+Snowball Frequency', '+Extra Snowball', '+Snowball Size', '+Extra Snowball'];
var enemySpawnInterval;
var bulletInterval;
var updateSpawnSpeedInterval;
var initialSpawnSpeed = 2000;
var spawnSpeedNow = initialSpawnSpeed;
var powerupSpawnInterval;
var upgradeSpawnInterval;
spawnPowerup = function spawnPowerup() {
	var powerup = new Powerup();
	do {
		var xx = Math.random() * worldWidth;
		var yy = Math.random() * worldHeight;
		var dx = xx - player.x;
		var dy = yy - player.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
	} while (magnitude < 800);
	powerup.x = xx;
	powerup.y = yy;
	powerups.push(powerup);
	game.addChild(powerup);
};
spawnEnemy = function spawnEnemy() {
	var snowManEnemy = new SnowManEnemy();
	if (Math.random() < 0.5) {
		var yy = Math.random() * worldHeight;
		if (Math.random() < 0.5) {
			var xx = -snowManEnemy.width;
		} else {
			var xx = worldWidth + snowManEnemy.width;
		}
	} else {
		var xx = Math.random() * worldHeight;
		if (Math.random() < 0.5) {
			var yy = -snowManEnemy.height;
		} else {
			var yy = worldHeight + snowManEnemy.height;
		}
	}
	snowManEnemy.x = xx;
	snowManEnemy.y = yy;
	enemies.push(snowManEnemy);
	game.addChild(snowManEnemy);
};
spawnUpgrade = function spawnUpgrade() {
	var upgrade = new Upgrade();
	do {
		var xx = 200 + Math.random() * (worldWidth - 400);
		var yy = 200 + Math.random() * (worldHeight - 400);
		var dx = xx - player.x;
		var dy = yy - player.y;
		var magnitude = Math.sqrt(dx * dx + dy * dy);
	} while (magnitude < 800);
	upgrade.x = xx;
	upgrade.y = yy;
	upgrades.push(upgrade);
	game.addChild(upgrade);
};
setBulletInterval = function setBulletInterval(time) {
	LK.clearInterval(bulletInterval);
	bulletInterval = LK.setInterval(function () {
		for (var i = 0; i < numSnowballs; i++) {
			var bullet = new Bullet();
			bullet.x = player.x;
			bullet.y = player.y;
			var pos = {
				x: 0,
				y: 0
			};
			pos = lastTapPos;
			var dx = pos.x - player.x + i * 40;
			var dy = pos.y - player.y + i * 40;
			var magnitude = Math.sqrt(dx * dx + dy * dy);
			bullet.direction.x = dx / magnitude;
			bullet.direction.y = dy / magnitude;
			bullets.push(bullet);
			game.addChild(bullet);
		}
	}, time);
};
intersectsTolerance = function intersectsTolerance(a, b, tolerance) {
	tolerance *= tolerance;
	if (!a || !b) {
		return false;
	}
	var dx = a.x - b.x;
	if (dx * dx < tolerance) {
		var dy = a.y - b.y;
		if (dy * dy < tolerance) {
			return true;
		}
	}
	return false;
};
triggerUpgrade = function triggerUpgrade(x, y) {
	showSign(upgradeMessages[upgradeLevel], x, y);
	upgradeLevel++;
	if (upgradeLevel == 1 || upgradeLevel == 4 || upgradeLevel == 6) {
		numSnowballs++;
	} else if (upgradeLevel == 2) {
		snowballSpeed *= 1.5;
	} else if (upgradeLevel == 3) {
		snowballFrequency *= 0.5;
		setBulletInterval(snowballFrequency);
	} else {
		snowballSize *= 1.5;
	}
};
showSign = function showSign(message, x, y) {
	var signText = new Text2(message, {
		size: 80,
		fill: '#ffcc00',
		align: 'center',
		dropShadow: true
	});
	// Ensure the message is fully on screen
	signText.x = Math.max(signText.width / 2, Math.min(worldWidth - signText.width / 2, x));
	signText.y = Math.max(signText.height / 2, Math.min(worldHeight - signText.height / 2, y));
	signText.anchor.set(0.5, 0.5);
	game.addChild(signText);
	// Play upgrade sound effect
	var upgradeSound = LK.getSound('upgradeSound');
	upgradeSound.play();
	// Remove the sign after a brief period
	LK.setTimeout(function () {
		signText.destroy();
	}, 2000);
};
triggerBomb = function triggerBomb() {
	for (var i = 0; i < 16; i++) {
		var bullet = new Bullet();
		bullet.x = player.x;
		bullet.y = player.y;
		var angle = Math.PI * 2 / 16 * i;
		bullet.direction.x = Math.cos(angle);
		bullet.direction.y = Math.sin(angle);
		bullets.push(bullet);
		game.addChild(bullet);
	}
};
var gameStarted = false;
LK.on('playButtonPressed', function () {
	gameStarted = true;
	updateSpawnSpeedInterval = LK.setInterval(function () {
		if (spawnSpeedNow > 200) {
			spawnSpeedNow -= 100;
			LK.clearInterval(enemySpawnInterval);
			enemySpawnInterval = LK.setInterval(function () {
				spawnEnemy();
			}, spawnSpeedNow);
		}
	}, 10000);
	upgradeSpawnInterval = LK.setInterval(function () {
		if (upgradeLevel < 6) {
			spawnUpgrade();
		}
	}, 15000);
	enemySpawnInterval = LK.setInterval(function () {
		spawnEnemy();
	}, initialSpawnSpeed);
	setBulletInterval(snowballFrequency);
	powerupSpawnInterval = LK.setInterval(function () {
		spawnPowerup();
	}, 12000);
});
var snowflakes = [];
for (var i = 0; i < 30; i++) {
	var snowflake = new Snowflake();
	snowflake.x = Math.random() * 1800;
	snowflake.y = Math.random() * game.height;
	snowflakes.push(snowflake);
	game.addChild(snowflake);
}
player.x = 330;
player.y = 300;
LK.on('tick', function () {
	player._move_migrated();
	for (var i = 0; i < bullets.length; i++) {
		bullets[i]._move_migrated();
		if (bullets[i].x < 0 || bullets[i].x > worldWidth || bullets[i].y < 0 || bullets[i].y > worldHeight) {
			bullets[i].destroy();
			bullets.splice(i, 1);
			continue;
		}
		for (var j = 0; j < enemies.length; j++) {
			//if (bullets[i].intersects(enemies[j])) {
			if (intersectsTolerance(bullets[i], enemies[j], 100)) {
				enemies[j].destroy();
				playerScore += 100;
				scoreLabel.setText("Score: " + playerScore);
				enemies.splice(j, 1);
				break;
			}
		}
	}
	for (var i = 0; i < enemies.length; i++) {
		enemies[i]._move_migrated(player);
		//if (player.intersects(enemies[i])) {
		if (intersectsTolerance(player, enemies[i], 100)) {
			LK.setScore(playerScore);
			LK.showGameOver();
		}
	}
	for (var i = 0; i < snowflakes.length; i++) {
		snowflakes[i]._move_migrated();
	}
	for (var i = 0; i < powerups.length; i++) {
		powerups[i]._update_migrated();
		if (powerups[i].timer > 480) {
			powerups[i].destroy();
			powerups.splice(i, 1);
		} else {
			if (player.intersects(powerups[i])) {
				triggerBomb();
				// Play powerup sound effect
				var powerupSound = LK.getSound('powerupSound');
				powerupSound.play();
				powerups[i].destroy();
				powerups.splice(i, 1);
			}
		}
	}
	for (var i = 0; i < upgrades.length; i++) {
		if (player.intersects(upgrades[i])) {
			//triggerBomb();
			triggerUpgrade(upgrades[i].x, upgrades[i].y);
			upgrades[i].destroy();
			upgrades.splice(i, 1);
		}
	}
	if (LK.ticks % 15 == 0) {
		console.log('enemies length: ' + enemies.length);
		console.log('bullets length: ' + bullets.length);
		console.log('powerups length: ' + powerups.length);
		console.log('upgrades length: ' + upgrades.length);
	}
});
var lastTapPos = {
	x: 0,
	y: 0
};
game.on('down', function (x, y, obj) {
	if (!gameStarted) {
		instructions.visible = false;
		playButton.visible = false;
		LK.emit('playButtonPressed');
	} else {
		var pos = game.toLocal(obj.global);
		lastTapPos = pos;
		player.moveTowards(pos);
	}
});
:quality(85)/https://cdn.frvr.ai/655cd770d5d2cd42f668fba1.png%3F3) 
 a penguin engineer Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655d8f0ad5d2cd42f668fd15.png%3F3) 
 rectangular ice wall section, top down view Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655d91bad5d2cd42f668fd4b.png%3F3) 
 ice floor texture tile top down view Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655d95ccd5d2cd42f668fda8.png%3F3) 
 one cartoony snowball. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655da53f7ffb7ee3f752b511.png%3F3) 
 white dot. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655da9e57ffb7ee3f752b56c.png%3F3) 
 button with arrow key pointing left. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655dbf33d5d2cd42f668fe54.png%3F3) 
 a large round playbutton Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655dc4ffd5d2cd42f668fe6f.png%3F3) 
 cartoony evil snowman character Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/655de762be406eb4a5a71f3f.png%3F3) 
 icy treasure chest Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/674a5609f545ec9e1c092d51.png%3F3) 
 A snowcovered christmas tree decorated with snowballs.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.