/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	self.direction = 0;
	self.update = function () {
		self.x += Math.cos(self.direction) * self.speed;
		self.y += Math.sin(self.direction) * self.speed;
	};
	return self;
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.rotation = 0;
	self.shoot = function () {
		if (self.canShoot) {
			var bullet = new Bullet();
			bullet.x = self.x;
			bullet.y = self.y;
			bullet.direction = self.rotation;
			bullets.push(bullet);
			game.addChild(bullet);
			LK.getSound('shoot').play();
			self.canShoot = false;
			LK.setTimeout(function () {
				self.canShoot = true;
			}, 100);
		}
	};
	return self;
});
var Zombie = Container.expand(function () {
	var self = Container.call(this);
	var zombieGraphics = self.attachAsset('zombie', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2 + Math.random() * 3;
	self.health = 2; // Add health property to track hits
	self.update = function () {
		var angle = Math.atan2(player.y - self.y, player.x - self.x);
		self.rotation = angle; // Make zombie point towards the player
		self.x += Math.cos(angle) * self.speed;
		self.y += Math.sin(angle) * self.speed;
	};
	return self;
});
var Zombie2 = Container.expand(function () {
	var self = Container.call(this);
	var zombieGraphics = self.attachAsset('Zombie2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2 + Math.random() * 3;
	self.health = 2; // Add health property to track hits
	self.update = function () {
		var angle = Math.atan2(player.y - self.y, player.x - self.x);
		self.rotation = angle; // Make zombie point towards the player
		self.x += Math.cos(angle) * self.speed;
		self.y += Math.sin(angle) * self.speed;
	};
	return self;
});
var Zombie3 = Container.expand(function () {
	var self = Container.call(this);
	var zombieGraphics = self.attachAsset('Zombie3', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2 + Math.random() * 3;
	self.health = 3; // Add health property to track hits
	self.update = function () {
		var angle = Math.atan2(player.y - self.y, player.x - self.x);
		self.rotation = angle; // Make zombie point towards the player
		self.x += Math.cos(angle) * self.speed;
		self.y += Math.sin(angle) * self.speed;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
// Add a full-screen background image behind all game elements
// Initialize assets used in this game. Scale them according to what is needed for the game.
var background = LK.getAsset('Background', {
	anchorX: 0.5,
	anchorY: 0.5,
	scaleX: 2048 / 100,
	// Scale to fit the screen width
	scaleY: 2732 / 100,
	// Scale to fit the screen height
	x: 2048 / 2,
	// Center horizontally
	y: 2732 / 2 // Center vertically
});
game.addChildAt(background, 0); // Add background at the lowest layer
game.setBackgroundColor(0x000000);
var player = new Player();
player.canShoot = true;
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
var bullets = [];
var zombies = [];
var score = 0;
// Create a Text2 object to display the score
var scoreTxt = new Text2('Score: 0', {
	size: 100,
	fill: 0xFFFFFF
});
// Set the anchor to the center of the top edge
scoreTxt.anchor.set(0.5, 0);
// Add the score text to the GUI overlay at the top center
LK.gui.top.addChild(scoreTxt);
function spawnZombie() {
	var zombie;
	var randomValue = Math.random();
	if (randomValue < 0.33) {
		zombie = new Zombie();
	} else if (randomValue < 0.66) {
		zombie = new Zombie2();
	} else {
		zombie = new Zombie3();
	}
	var edge = Math.floor(Math.random() * 4);
	switch (edge) {
		case 0:
			// Top
			zombie.x = Math.random() * 2048;
			zombie.y = -50;
			break;
		case 1:
			// Right
			zombie.x = 2048 + 50;
			zombie.y = Math.random() * 2732;
			break;
		case 2:
			// Bottom
			zombie.x = Math.random() * 2048;
			zombie.y = 2732 + 50;
			break;
		case 3:
			// Left
			zombie.x = -50;
			zombie.y = Math.random() * 2732;
			break;
	}
	zombies.push(zombie);
	game.addChild(zombie);
}
game.down = function (x, y, obj) {
	var angle = Math.atan2(y - player.y, x - player.x);
	player.rotation = angle; // Update player rotation to face the touch location
	player.shoot(); // Use the player's shoot method to create and shoot the bullet
};
game.update = function () {
	for (var i = bullets.length - 1; i >= 0; i--) {
		var bullet = bullets[i];
		bullet.update();
		if (bullet.x < 0 || bullet.x > 2048 || bullet.y < 0 || bullet.y > 2732) {
			bullet.destroy();
			bullets.splice(i, 1);
		}
	}
	for (var j = zombies.length - 1; j >= 0; j--) {
		var zombie = zombies[j];
		zombie.update();
		if (zombie.intersects(player)) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
			return;
		}
		for (var k = bullets.length - 1; k >= 0; k--) {
			var bullet = bullets[k];
			if (bullet.intersects(zombie)) {
				LK.getSound('zombieHit').play();
				score += 10;
				scoreTxt.setText('Score: ' + score);
				zombie.health -= 1; // Decrease zombie health by 1
				if (zombie.health <= 0) {
					// Check if zombie health is depleted
					score += 100; // Increase score by 100 when a zombie, zombie2, or zombie3 is killed
					scoreTxt.setText('Score: ' + score);
					zombie.destroy();
					zombies.splice(j, 1);
				}
			}
		}
	}
	if (LK.ticks % 30 === 0) {
		spawnZombie();
	}
};
LK.playMusic('backgroundMusic', {
	loop: true
});
; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	self.direction = 0;
	self.update = function () {
		self.x += Math.cos(self.direction) * self.speed;
		self.y += Math.sin(self.direction) * self.speed;
	};
	return self;
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.rotation = 0;
	self.shoot = function () {
		if (self.canShoot) {
			var bullet = new Bullet();
			bullet.x = self.x;
			bullet.y = self.y;
			bullet.direction = self.rotation;
			bullets.push(bullet);
			game.addChild(bullet);
			LK.getSound('shoot').play();
			self.canShoot = false;
			LK.setTimeout(function () {
				self.canShoot = true;
			}, 100);
		}
	};
	return self;
});
var Zombie = Container.expand(function () {
	var self = Container.call(this);
	var zombieGraphics = self.attachAsset('zombie', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2 + Math.random() * 3;
	self.health = 2; // Add health property to track hits
	self.update = function () {
		var angle = Math.atan2(player.y - self.y, player.x - self.x);
		self.rotation = angle; // Make zombie point towards the player
		self.x += Math.cos(angle) * self.speed;
		self.y += Math.sin(angle) * self.speed;
	};
	return self;
});
var Zombie2 = Container.expand(function () {
	var self = Container.call(this);
	var zombieGraphics = self.attachAsset('Zombie2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2 + Math.random() * 3;
	self.health = 2; // Add health property to track hits
	self.update = function () {
		var angle = Math.atan2(player.y - self.y, player.x - self.x);
		self.rotation = angle; // Make zombie point towards the player
		self.x += Math.cos(angle) * self.speed;
		self.y += Math.sin(angle) * self.speed;
	};
	return self;
});
var Zombie3 = Container.expand(function () {
	var self = Container.call(this);
	var zombieGraphics = self.attachAsset('Zombie3', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2 + Math.random() * 3;
	self.health = 3; // Add health property to track hits
	self.update = function () {
		var angle = Math.atan2(player.y - self.y, player.x - self.x);
		self.rotation = angle; // Make zombie point towards the player
		self.x += Math.cos(angle) * self.speed;
		self.y += Math.sin(angle) * self.speed;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
// Add a full-screen background image behind all game elements
// Initialize assets used in this game. Scale them according to what is needed for the game.
var background = LK.getAsset('Background', {
	anchorX: 0.5,
	anchorY: 0.5,
	scaleX: 2048 / 100,
	// Scale to fit the screen width
	scaleY: 2732 / 100,
	// Scale to fit the screen height
	x: 2048 / 2,
	// Center horizontally
	y: 2732 / 2 // Center vertically
});
game.addChildAt(background, 0); // Add background at the lowest layer
game.setBackgroundColor(0x000000);
var player = new Player();
player.canShoot = true;
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
var bullets = [];
var zombies = [];
var score = 0;
// Create a Text2 object to display the score
var scoreTxt = new Text2('Score: 0', {
	size: 100,
	fill: 0xFFFFFF
});
// Set the anchor to the center of the top edge
scoreTxt.anchor.set(0.5, 0);
// Add the score text to the GUI overlay at the top center
LK.gui.top.addChild(scoreTxt);
function spawnZombie() {
	var zombie;
	var randomValue = Math.random();
	if (randomValue < 0.33) {
		zombie = new Zombie();
	} else if (randomValue < 0.66) {
		zombie = new Zombie2();
	} else {
		zombie = new Zombie3();
	}
	var edge = Math.floor(Math.random() * 4);
	switch (edge) {
		case 0:
			// Top
			zombie.x = Math.random() * 2048;
			zombie.y = -50;
			break;
		case 1:
			// Right
			zombie.x = 2048 + 50;
			zombie.y = Math.random() * 2732;
			break;
		case 2:
			// Bottom
			zombie.x = Math.random() * 2048;
			zombie.y = 2732 + 50;
			break;
		case 3:
			// Left
			zombie.x = -50;
			zombie.y = Math.random() * 2732;
			break;
	}
	zombies.push(zombie);
	game.addChild(zombie);
}
game.down = function (x, y, obj) {
	var angle = Math.atan2(y - player.y, x - player.x);
	player.rotation = angle; // Update player rotation to face the touch location
	player.shoot(); // Use the player's shoot method to create and shoot the bullet
};
game.update = function () {
	for (var i = bullets.length - 1; i >= 0; i--) {
		var bullet = bullets[i];
		bullet.update();
		if (bullet.x < 0 || bullet.x > 2048 || bullet.y < 0 || bullet.y > 2732) {
			bullet.destroy();
			bullets.splice(i, 1);
		}
	}
	for (var j = zombies.length - 1; j >= 0; j--) {
		var zombie = zombies[j];
		zombie.update();
		if (zombie.intersects(player)) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
			return;
		}
		for (var k = bullets.length - 1; k >= 0; k--) {
			var bullet = bullets[k];
			if (bullet.intersects(zombie)) {
				LK.getSound('zombieHit').play();
				score += 10;
				scoreTxt.setText('Score: ' + score);
				zombie.health -= 1; // Decrease zombie health by 1
				if (zombie.health <= 0) {
					// Check if zombie health is depleted
					score += 100; // Increase score by 100 when a zombie, zombie2, or zombie3 is killed
					scoreTxt.setText('Score: ' + score);
					zombie.destroy();
					zombies.splice(j, 1);
				}
			}
		}
	}
	if (LK.ticks % 30 === 0) {
		spawnZombie();
	}
};
LK.playMusic('backgroundMusic', {
	loop: true
});
;