/**** 
* Classes
****/ 
// Bullet class for player and enemy bullets
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 15;
	self.update = function () {
		self.y -= self.speed;
	};
});
// Enemy class representing enemy characters
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.update = function () {
		// Enemy moves down the screen
		self.y += self.speed;
		// Game over if enemy touches player
		// Removed game over condition on enemy-player collision
	};
});
//<Assets used in the game will automatically appear here>
// Player class representing the main character
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 120;
	self.update = function () {
		// Prevent player from moving off screen
		if (self.x < 0) {
			self.x = 0;
		} else if (self.x > 2048 - self.width) {
			self.x = 2048 - self.width;
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
if (game.children.filter(function (child) {
	return child instanceof Player;
}).length < 5) {
	var player = game.addChild(new Player());
}
// Removed the x variable
var leftArrow = LK.getAsset('leftArrow', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 100,
	y: 2732 - 150
});
game.addChild(leftArrow);
// Create right arrow button
var rightArrow = LK.getAsset('rightArrow', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 250,
	y: 2732 - 150
});
game.addChild(rightArrow);
// Add swipe event listener to move player
var startX = 0;
var startY = 0;
game.down = function (x, y, obj) {
	startX = x;
	startY = y;
	shootBullet();
};
game.move = function (x, y, obj) {
	var deltaX = x - startX;
	if (deltaX > 0) {
		player.x += player.speed / 2; // Increase the speed of player movement on swipe
	} else if (deltaX < 0) {
		player.x -= player.speed / 2; // Increase the speed of player movement on swipe
	}
	startX = x;
	startY = y;
};
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize enemies array
var enemies = [];
// Initialize bullets array
var bullets = [];
// Function to spawn enemies
var enemySpeed = 5;
function spawnEnemy() {
	if (enemies.length < 6) {
		var enemy = new Enemy();
		enemy.x = Math.random() * (2048 - enemy.width) + enemy.width / 2;
		enemy.y = 0;
		enemy.speed = enemySpeed - 2.08;
		enemies.push(enemy);
		game.addChild(enemy);
	}
}
// Function to handle player shooting
var bulletSpeed = 15;
function shootBullet() {
	var bullet = new Bullet();
	bullet.x = player.x;
	bullet.y = player.y;
	bullet.speed = bulletSpeed;
	bullets.push(bullet);
	game.addChild(bullet);
}
// Game update loop
game.update = function () {
	// Update player
	player.update();
	// Update enemies
	for (var i = enemies.length - 1; i >= 0; i--) {
		enemies[i].update();
		if (enemies[i].y > 2732) {
			enemies[i].destroy();
			enemies.splice(i, 1);
			LK.showGameOver(); // End the game if any enemy reaches the bottom of the screen
		}
	}
	// Update bullets
	for (var j = bullets.length - 1; j >= 0; j--) {
		bullets[j].update();
		if (bullets[j].y < 0) {
			bullets[j].destroy();
			bullets.splice(j, 1);
		}
	}
	// Initialize coin counter
	var coins = 0;
	// Create a score bar at the top middle of the screen
	var scoreBar = new Text2('0', {
		size: 150,
		fill: "#ffffff"
	});
	scoreBar.anchor.set(0.5, 0);
	scoreBar.x = 2048 / 2;
	scoreBar.y = 0;
	LK.gui.top.addChild(scoreBar);
	// Check for collisions between bullets and enemies
	for (var k = bullets.length - 1; k >= 0; k--) {
		for (var l = enemies.length - 1; l >= 0; l--) {
			if (bullets[k].intersects(enemies[l])) {
				bullets[k].destroy();
				bullets.splice(k, 1);
				enemies[l].destroy();
				enemies.splice(l, 1);
				// Increment coin counter when an enemy is killed
				coins += 1;
				scoreBar.setText("Score: " + coins);
				// Display congratulatory message after killing 10 enemies
				if (coins === 10) {
					var congratsText = new Text2('Good Job!', {
						size: 200,
						fill: "#00ff00"
					});
					congratsText.anchor.set(0.5, 0.5);
					congratsText.x = 2048 / 2;
					congratsText.y = 2732 / 2;
					game.addChild(congratsText);
					// Remove the message after 2 seconds
					LK.setTimeout(function () {
						congratsText.destroy();
					}, 2000);
				}
				break;
			}
		}
	}
	// Check for collisions between player and enemies
	for (var m = enemies.length - 1; m >= 0; m--) {
		if (player.intersects(enemies[m])) {
			LK.showGameOver();
			break;
		}
	}
};
var spawnInterval = 2000;
LK.setInterval(function () {
	if (enemies.length < 6) {
		spawnEnemy();
	}
	spawnInterval -= 20;
	if (spawnInterval < 200) {
		spawnInterval = 200;
	}
	LK.clearInterval(spawnInterval);
	LK.setInterval(spawnEnemy, spawnInterval);
}, spawnInterval);
// Add swipe event listener to move player
var startX = 0;
var startY = 0;
// Keyboard and mouse events are not supported in the LK engine /**** 
* Classes
****/ 
// Bullet class for player and enemy bullets
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 15;
	self.update = function () {
		self.y -= self.speed;
	};
});
// Enemy class representing enemy characters
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.update = function () {
		// Enemy moves down the screen
		self.y += self.speed;
		// Game over if enemy touches player
		// Removed game over condition on enemy-player collision
	};
});
//<Assets used in the game will automatically appear here>
// Player class representing the main character
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 120;
	self.update = function () {
		// Prevent player from moving off screen
		if (self.x < 0) {
			self.x = 0;
		} else if (self.x > 2048 - self.width) {
			self.x = 2048 - self.width;
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
if (game.children.filter(function (child) {
	return child instanceof Player;
}).length < 5) {
	var player = game.addChild(new Player());
}
// Removed the x variable
var leftArrow = LK.getAsset('leftArrow', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 100,
	y: 2732 - 150
});
game.addChild(leftArrow);
// Create right arrow button
var rightArrow = LK.getAsset('rightArrow', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 250,
	y: 2732 - 150
});
game.addChild(rightArrow);
// Add swipe event listener to move player
var startX = 0;
var startY = 0;
game.down = function (x, y, obj) {
	startX = x;
	startY = y;
	shootBullet();
};
game.move = function (x, y, obj) {
	var deltaX = x - startX;
	if (deltaX > 0) {
		player.x += player.speed / 2; // Increase the speed of player movement on swipe
	} else if (deltaX < 0) {
		player.x -= player.speed / 2; // Increase the speed of player movement on swipe
	}
	startX = x;
	startY = y;
};
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize enemies array
var enemies = [];
// Initialize bullets array
var bullets = [];
// Function to spawn enemies
var enemySpeed = 5;
function spawnEnemy() {
	if (enemies.length < 6) {
		var enemy = new Enemy();
		enemy.x = Math.random() * (2048 - enemy.width) + enemy.width / 2;
		enemy.y = 0;
		enemy.speed = enemySpeed - 2.08;
		enemies.push(enemy);
		game.addChild(enemy);
	}
}
// Function to handle player shooting
var bulletSpeed = 15;
function shootBullet() {
	var bullet = new Bullet();
	bullet.x = player.x;
	bullet.y = player.y;
	bullet.speed = bulletSpeed;
	bullets.push(bullet);
	game.addChild(bullet);
}
// Game update loop
game.update = function () {
	// Update player
	player.update();
	// Update enemies
	for (var i = enemies.length - 1; i >= 0; i--) {
		enemies[i].update();
		if (enemies[i].y > 2732) {
			enemies[i].destroy();
			enemies.splice(i, 1);
			LK.showGameOver(); // End the game if any enemy reaches the bottom of the screen
		}
	}
	// Update bullets
	for (var j = bullets.length - 1; j >= 0; j--) {
		bullets[j].update();
		if (bullets[j].y < 0) {
			bullets[j].destroy();
			bullets.splice(j, 1);
		}
	}
	// Initialize coin counter
	var coins = 0;
	// Create a score bar at the top middle of the screen
	var scoreBar = new Text2('0', {
		size: 150,
		fill: "#ffffff"
	});
	scoreBar.anchor.set(0.5, 0);
	scoreBar.x = 2048 / 2;
	scoreBar.y = 0;
	LK.gui.top.addChild(scoreBar);
	// Check for collisions between bullets and enemies
	for (var k = bullets.length - 1; k >= 0; k--) {
		for (var l = enemies.length - 1; l >= 0; l--) {
			if (bullets[k].intersects(enemies[l])) {
				bullets[k].destroy();
				bullets.splice(k, 1);
				enemies[l].destroy();
				enemies.splice(l, 1);
				// Increment coin counter when an enemy is killed
				coins += 1;
				scoreBar.setText("Score: " + coins);
				// Display congratulatory message after killing 10 enemies
				if (coins === 10) {
					var congratsText = new Text2('Good Job!', {
						size: 200,
						fill: "#00ff00"
					});
					congratsText.anchor.set(0.5, 0.5);
					congratsText.x = 2048 / 2;
					congratsText.y = 2732 / 2;
					game.addChild(congratsText);
					// Remove the message after 2 seconds
					LK.setTimeout(function () {
						congratsText.destroy();
					}, 2000);
				}
				break;
			}
		}
	}
	// Check for collisions between player and enemies
	for (var m = enemies.length - 1; m >= 0; m--) {
		if (player.intersects(enemies[m])) {
			LK.showGameOver();
			break;
		}
	}
};
var spawnInterval = 2000;
LK.setInterval(function () {
	if (enemies.length < 6) {
		spawnEnemy();
	}
	spawnInterval -= 20;
	if (spawnInterval < 200) {
		spawnInterval = 200;
	}
	LK.clearInterval(spawnInterval);
	LK.setInterval(spawnEnemy, spawnInterval);
}, spawnInterval);
// Add swipe event listener to move player
var startX = 0;
var startY = 0;
// Keyboard and mouse events are not supported in the LK engine