/**** 
* Classes
****/ 
// MeatballStick class
var MeatballStick = Container.expand(function () {
	var self = Container.call(this);
	var stickGraphics = self.attachAsset('meatballStick', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.extend = function () {
		// Extend the stick
	};
	self.retract = function () {
		// Retract the stick
	};
});
// NPC class
var NPC = Container.expand(function () {
	var self = Container.call(this);
	var npcGraphics = self.attachAsset('npc', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self._update_migrated = function () {
		// NPC movement or behavior logic
		var dx = self.x - player.x;
		var dy = self.y - player.y;
		var distance = Math.sqrt(dx * dx + dy * dy);
		var moveX = Math.random() * 50 - 25; // Random movement in x direction
		var moveY = Math.random() * 50 - 25; // Random movement in y direction
		if (distance < 200) {
			moveX = dx / distance * 5;
			moveY = dy / distance * 5;
		}
		// Check if NPC is within the walking area and within a certain distance from their current position
		if (self.x + moveX > 0 && self.x + moveX < 2048 && Math.abs(moveX) < 50 && self.y + moveY > 0 && self.y + moveY < 2732 && Math.abs(moveY) < 50) {
			self.x += moveX;
			self.y += moveY;
		}
		// Add a delay to the NPC's movement to make it more fluid and less jittery
		LK.setTimeout(function () {
			self.x += moveX;
			self.y += moveY;
		}, 100);
	};
});
// Child class
var Child = NPC.expand(function () {
	var self = NPC.call(this);
});
// Adult class
var Adult = NPC.expand(function () {
	var self = NPC.call(this);
});
var Teacher = Adult.expand(function () {
	var self = Adult.call(this);
});
var Politician = Adult.expand(function () {
	var self = Adult.call(this);
});
var Nurse = Adult.expand(function () {
	var self = Adult.call(this);
});
var Mechanic = Adult.expand(function () {
	var self = Adult.call(this);
});
var Lawyer = Adult.expand(function () {
	var self = Adult.call(this);
});
var Judge = Adult.expand(function () {
	var self = Adult.call(this);
});
var Firefighter = Adult.expand(function () {
	var self = Adult.call(this);
});
var Doctor = Adult.expand(function () {
	var self = Adult.call(this);
});
// Occupation specific classes
var Cop = Adult.expand(function () {
	var self = Adult.call(this);
});
var Accountant = Adult.expand(function () {
	var self = Adult.call(this);
});
// Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	self.playerRest = self.attachAsset('playerRest', {
		anchorX: 0.5,
		anchorY: 0.5,
		id: 'AURL'
	});
	self.playerAttack = self.attachAsset('playerAttack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.playerAttack.visible = false;
	self.lastTap = Date.now();
	self._move_migrated = function (x, y) {
		self.x = x;
		self.y = y;
	};
	self.attack = function () {
		if (self.playerAttack.visible == false) {
			self.playerRest.visible = false;
			self.playerAttack = self.attachAsset('attack', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			self.playerAttack.visible = true;
			self.scaleX = 2; // Double the size of the player horizontally
			self.scaleY = 2; // Double the size of the player vertically
			var projectile = new Projectile();
			projectile.x = self.x;
			projectile.y = self.y;
			game.addChild(projectile);
			LK.setTimeout(function () {
				self.playerRest.visible = true;
				self.playerAttack.visible = false;
				self.scaleX = 1; // Reset the size of the player horizontally
				self.scaleY = 1; // Reset the size of the player vertically
			}, 250);
		}
	};
});
// Projectile class
var Projectile = Container.expand(function () {
	var self = Container.call(this);
	var projectileGraphics = self.attachAsset('playerAttack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	self.update = function () {
		self.y -= self.speed;
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
// Game tick event
LK.on('tick', function () {
	// Update each NPC and check for collisions with the player
	for (var i = npcs.length - 1; i >= 0; i--) {
		npcs[i]._update_migrated();
		if (player.intersects(npcs[i])) {
			// Increase score and remove the NPC
			score++;
			scoreTxt.setText(score.toString());
			npcs[i].destroy();
			npcs.splice(i, 1);
			// Spawn a new NPC to replace the one removed
			spawnNPC();
		}
	}
	// Update each projectile and check for collisions with NPCs
	for (var i = projectiles.length - 1; i >= 0; i--) {
		projectiles[i].update();
		for (var j = npcs.length - 1; j >= 0; j--) {
			if (projectiles[i].intersects(npcs[j])) {
				// Increase score and remove the NPC
				score++;
				scoreTxt.setText(score.toString());
				npcs[j].destroy();
				npcs.splice(j, 1);
				// Spawn a new NPC to replace the one removed
				spawnNPC();
				// Remove the projectile
				projectiles[i].destroy();
				projectiles.splice(i, 1);
				break;
			}
		}
	}
});
var background = game.addChild(LK.getAsset('background', {
	anchorX: 0.0,
	anchorY: 0.0,
	x: 0,
	y: 0,
	scaleX: 2048 / 800,
	// Scale horizontally to fit the game screen
	scaleY: 2732 / 800 // Scale vertically to fit the game screen
}));
game.on('down', function (x, y, obj) {
	var now = Date.now();
	if (now - player.lastTap < 300) {
		// 300ms threshold for a rapid double tap
		player.attack();
	}
	player.lastTap = now;
});
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 1366; // Center vertically
var npcs = []; // Array to hold NPC instances
var projectiles = []; // Array to hold Projectile instances
var score = 0; // Initialize score
var scoreTxt = new Text2(score.toString(), {
	size: 150,
	fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt); // Add score text to the top-center of the screen
// Function to spawn NPCs
function spawnNPC() {
	var npc = new NPC();
	npc.x = Math.random() * 2048; // Random horizontal position
	npc.y = Math.random() * 2732; // Random vertical position
	game.addChild(npc);
	npcs.push(npc);
}
// Spawn initial NPCs
for (var i = 0; i < 5; i++) {
	spawnNPC();
}
// Handle touch move events to move the player
game.on('move', function (x, y, obj) {
	var pos = game.toLocal(obj.global);
	player._move_migrated(pos.x, pos.y);
});
// Game tick event
LK.on('tick', function () {
	// Update each NPC and check for collisions with the player
	for (var i = npcs.length - 1; i >= 0; i--) {
		npcs[i]._update_migrated();
		if (player.intersects(npcs[i])) {
			// Increase score and remove the NPC
			score++;
			scoreTxt.setText(score.toString());
			npcs[i].destroy();
			npcs.splice(i, 1);
			// Spawn a new NPC to replace the one removed
			spawnNPC();
		}
	}
}); /**** 
* Classes
****/ 
// MeatballStick class
var MeatballStick = Container.expand(function () {
	var self = Container.call(this);
	var stickGraphics = self.attachAsset('meatballStick', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.extend = function () {
		// Extend the stick
	};
	self.retract = function () {
		// Retract the stick
	};
});
// NPC class
var NPC = Container.expand(function () {
	var self = Container.call(this);
	var npcGraphics = self.attachAsset('npc', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self._update_migrated = function () {
		// NPC movement or behavior logic
		var dx = self.x - player.x;
		var dy = self.y - player.y;
		var distance = Math.sqrt(dx * dx + dy * dy);
		var moveX = Math.random() * 50 - 25; // Random movement in x direction
		var moveY = Math.random() * 50 - 25; // Random movement in y direction
		if (distance < 200) {
			moveX = dx / distance * 5;
			moveY = dy / distance * 5;
		}
		// Check if NPC is within the walking area and within a certain distance from their current position
		if (self.x + moveX > 0 && self.x + moveX < 2048 && Math.abs(moveX) < 50 && self.y + moveY > 0 && self.y + moveY < 2732 && Math.abs(moveY) < 50) {
			self.x += moveX;
			self.y += moveY;
		}
		// Add a delay to the NPC's movement to make it more fluid and less jittery
		LK.setTimeout(function () {
			self.x += moveX;
			self.y += moveY;
		}, 100);
	};
});
// Child class
var Child = NPC.expand(function () {
	var self = NPC.call(this);
});
// Adult class
var Adult = NPC.expand(function () {
	var self = NPC.call(this);
});
var Teacher = Adult.expand(function () {
	var self = Adult.call(this);
});
var Politician = Adult.expand(function () {
	var self = Adult.call(this);
});
var Nurse = Adult.expand(function () {
	var self = Adult.call(this);
});
var Mechanic = Adult.expand(function () {
	var self = Adult.call(this);
});
var Lawyer = Adult.expand(function () {
	var self = Adult.call(this);
});
var Judge = Adult.expand(function () {
	var self = Adult.call(this);
});
var Firefighter = Adult.expand(function () {
	var self = Adult.call(this);
});
var Doctor = Adult.expand(function () {
	var self = Adult.call(this);
});
// Occupation specific classes
var Cop = Adult.expand(function () {
	var self = Adult.call(this);
});
var Accountant = Adult.expand(function () {
	var self = Adult.call(this);
});
// Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	self.playerRest = self.attachAsset('playerRest', {
		anchorX: 0.5,
		anchorY: 0.5,
		id: 'AURL'
	});
	self.playerAttack = self.attachAsset('playerAttack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.playerAttack.visible = false;
	self.lastTap = Date.now();
	self._move_migrated = function (x, y) {
		self.x = x;
		self.y = y;
	};
	self.attack = function () {
		if (self.playerAttack.visible == false) {
			self.playerRest.visible = false;
			self.playerAttack = self.attachAsset('attack', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			self.playerAttack.visible = true;
			self.scaleX = 2; // Double the size of the player horizontally
			self.scaleY = 2; // Double the size of the player vertically
			var projectile = new Projectile();
			projectile.x = self.x;
			projectile.y = self.y;
			game.addChild(projectile);
			LK.setTimeout(function () {
				self.playerRest.visible = true;
				self.playerAttack.visible = false;
				self.scaleX = 1; // Reset the size of the player horizontally
				self.scaleY = 1; // Reset the size of the player vertically
			}, 250);
		}
	};
});
// Projectile class
var Projectile = Container.expand(function () {
	var self = Container.call(this);
	var projectileGraphics = self.attachAsset('playerAttack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	self.update = function () {
		self.y -= self.speed;
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
// Game tick event
LK.on('tick', function () {
	// Update each NPC and check for collisions with the player
	for (var i = npcs.length - 1; i >= 0; i--) {
		npcs[i]._update_migrated();
		if (player.intersects(npcs[i])) {
			// Increase score and remove the NPC
			score++;
			scoreTxt.setText(score.toString());
			npcs[i].destroy();
			npcs.splice(i, 1);
			// Spawn a new NPC to replace the one removed
			spawnNPC();
		}
	}
	// Update each projectile and check for collisions with NPCs
	for (var i = projectiles.length - 1; i >= 0; i--) {
		projectiles[i].update();
		for (var j = npcs.length - 1; j >= 0; j--) {
			if (projectiles[i].intersects(npcs[j])) {
				// Increase score and remove the NPC
				score++;
				scoreTxt.setText(score.toString());
				npcs[j].destroy();
				npcs.splice(j, 1);
				// Spawn a new NPC to replace the one removed
				spawnNPC();
				// Remove the projectile
				projectiles[i].destroy();
				projectiles.splice(i, 1);
				break;
			}
		}
	}
});
var background = game.addChild(LK.getAsset('background', {
	anchorX: 0.0,
	anchorY: 0.0,
	x: 0,
	y: 0,
	scaleX: 2048 / 800,
	// Scale horizontally to fit the game screen
	scaleY: 2732 / 800 // Scale vertically to fit the game screen
}));
game.on('down', function (x, y, obj) {
	var now = Date.now();
	if (now - player.lastTap < 300) {
		// 300ms threshold for a rapid double tap
		player.attack();
	}
	player.lastTap = now;
});
var player = game.addChild(new Player());
player.x = 1024; // Center horizontally
player.y = 1366; // Center vertically
var npcs = []; // Array to hold NPC instances
var projectiles = []; // Array to hold Projectile instances
var score = 0; // Initialize score
var scoreTxt = new Text2(score.toString(), {
	size: 150,
	fill: "#ffffff"
});
LK.gui.top.addChild(scoreTxt); // Add score text to the top-center of the screen
// Function to spawn NPCs
function spawnNPC() {
	var npc = new NPC();
	npc.x = Math.random() * 2048; // Random horizontal position
	npc.y = Math.random() * 2732; // Random vertical position
	game.addChild(npc);
	npcs.push(npc);
}
// Spawn initial NPCs
for (var i = 0; i < 5; i++) {
	spawnNPC();
}
// Handle touch move events to move the player
game.on('move', function (x, y, obj) {
	var pos = game.toLocal(obj.global);
	player._move_migrated(pos.x, pos.y);
});
// Game tick event
LK.on('tick', function () {
	// Update each NPC and check for collisions with the player
	for (var i = npcs.length - 1; i >= 0; i--) {
		npcs[i]._update_migrated();
		if (player.intersects(npcs[i])) {
			// Increase score and remove the NPC
			score++;
			scoreTxt.setText(score.toString());
			npcs[i].destroy();
			npcs.splice(i, 1);
			// Spawn a new NPC to replace the one removed
			spawnNPC();
		}
	}
});