/**** 
* Classes
****/ 
// Chicken class
var Chicken = Container.expand(function () {
	var self = Container.call(this);
	var chickenGraphics = self.attachAsset('chicken', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.update = function () {
		// Update logic for chicken
	};
	self.move = function (x, y) {
		var tempX = x;
		var tempY = y;
		if (self.x !== tempX || self.y !== tempY) {
			self.x += (tempX - self.x) * 0.2;
			self.y += (tempY - self.y) * 0.2;
		}
	};
});
//<Assets used in the game will automatically appear here>
// ChickenWing class
var ChickenWing = Container.expand(function () {
	var self = Container.call(this);
	var wingGraphics = self.attachAsset('chickenWing', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2;
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -50;
			self.x = Math.random() * 2048;
			// No initial creation of chicken wings
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xF0EAD6 //Init game with egg-white background
});
/**** 
* Game Code
****/ 
function unlockMenuBar() {
	var menuBar = new Container();
	var menuBarGraphics = menuBar.attachAsset('menuBar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	menuBar.y = 2732 - 75; // Position menu bar at the bottom of the screen
	menuBar.x = 2048 / 2; // Center the menu bar horizontally
	for (var i = 0; i < 5; i++) {
		var slot = new Container();
		slot.width = 200;
		slot.height = 200;
		slot.x = (i - 2) * 220; // Space slots evenly, centered on the menu bar
		slot.attachAsset('slot', {
			anchorX: 0.5,
			anchorY: 0.5
		});
		if (i === 0) {
			var sword = slot.attachAsset('sword', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			// Removed sword click event
		}
		menuBar.addChild(slot);
	}
	game.addChild(menuBar);
	if (!tipMessageShown) {
		// Show tip text for 10 seconds
		var tipText = new Text2('Tip: Click to Chick', {
			size: 100,
			fill: "#8B4513"
		});
		tipText.anchor.set(0.5, 0.5);
		tipText.x = 2048 / 2;
		tipText.y = 2732 / 2;
		game.addChild(tipText);
		LK.setTimeout(function () {
			tipText.destroy();
		}, 10000);
		tipMessageShown = true;
	}
}
var chickenWingCount = 0;
var scoreIncrement = 1;
var scoreNeededForReset = 100;
var initialPlay = true;
var chickenWingInterval;
var menuBarUnlocked = false;
var tipMessageShown = false;
var chicken;
var tipText;
var score = 0;
var scoreTxt;
var chickenTxt;
// Initialize game elements
function initGame() {
	// Clear previous game state
	game.children.forEach(function (child) {
		if (child instanceof Chicken) {
			chicken = null;
		}
		child.destroy();
	});
	LK.gui.top.children.forEach(function (child) {
		child.destroy();
	});
	if (scoreTxt) {
		scoreTxt.destroy();
		scoreTxt = null;
	}
	if (chickenTxt) {
		chickenTxt.destroy();
		chickenTxt = null;
	}
	if (chickenTxt) {
		chickenTxt.destroy();
		chickenTxt = null;
	}
	score = 0;
	if (initialPlay) {
		scoreIncrement = 1;
		scoreNeededForReset = 100;
		initialPlay = false;
	} else {
		scoreIncrement += 1; // Increase score increment by 1
		scoreNeededForReset += 100; // Increase score needed for reset by 100
	}
	chickenWingCount = 0;
	menuBarUnlocked = false;
	tipMessageShown = false;
	// Initialize chickenWingInterval for controlling the frequency of chicken wings falling
	chickenWingInterval = LK.setInterval(function () {
		var numberOfWings = Math.floor(Math.random() * 3) + 1; // Randomly decide to drop 1 to 3 wings
		for (var i = 0; i < numberOfWings; i++) {
			var wing = game.addChild(new ChickenWing());
			wing.x = Math.random() * 2048;
			wing.y = -50;
		}
	}, 2000);
	if (chicken) {
		chicken.destroy();
		chicken = null;
	}
	chicken = game.addChild(new Chicken());
	chicken.rotating = false;
	chicken.x = 2048 / 2;
	chicken.y = 2732 - 200;
	chickenTxt = new Text2('Chicken', {
		size: 100,
		fill: "#000000"
	});
	chickenTxt.anchor.set(0.5, 0);
	LK.gui.top.addChild(chickenTxt);
	scoreTxt = new Text2('Score: 0', {
		size: 100,
		fill: "#000000"
	});
	scoreTxt.anchor.set(0.5, 0);
	scoreTxt.y = chickenTxt.height; // Position score text below 'Chicken' text
	LK.gui.top.addChild(scoreTxt);
}
// Update game elements
game.update = function () {
	chicken.update();
	var moveInterval = LK.setInterval(function () {
		chicken.move(chicken.x, chicken.y);
	}, 8); // 120 FPS
	game.children.forEach(function (child) {
		if (child instanceof ChickenWing) {
			child.update();
			if (chicken.intersects(child)) {
				child.destroy();
				score += scoreIncrement;
				scoreTxt.setText('Score: ' + score);
				if (score % 10 === 0 && score !== 0 && !chicken.rotating) {
					chicken.rotating = true;
					var originalRotation = chicken.rotation;
					chicken.rotation += Math.PI / 2; // Rotate chicken by 90 degrees
					LK.setTimeout(function () {
						chicken.rotation = originalRotation;
						chicken.rotating = false;
					}, 1000); // Reset rotation after 1 second
				}
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
				// Shake effect
				var originalX = chicken.x;
				var originalY = chicken.y;
				var shakeMagnitude = 5;
				var shakeDuration = 500;
				var shakeInterval = 50;
				var shakeCount = shakeDuration / shakeInterval;
				var shakeStep = 0;
				var shake = LK.setInterval(function () {
					if (shakeStep < shakeCount) {
						chicken.x = originalX + (Math.random() - 0.5) * shakeMagnitude;
						chicken.y = originalY + (Math.random() - 0.5) * shakeMagnitude;
						shakeStep++;
					} else {
						LK.clearInterval(shake);
						chicken.x = originalX;
						chicken.y = originalY;
					}
				}, shakeInterval);
				if (score >= 5 && !game.menuBarUnlocked) {
					game.menuBarUnlocked = true;
					unlockMenuBar();
				}
				// Increase chicken size more drastically at every milestone of ten score
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
				// Increase the frequency of chicken wings falling from the top of the screen once the player gets 15 score
				if (score >= 15) {
					LK.clearInterval(chickenWingInterval);
					chickenWingInterval = LK.setInterval(function () {
						var numberOfWings = Math.floor(Math.random() * 3) + 1; // Randomly decide to drop 1 to 3 wings
						for (var i = 0; i < numberOfWings; i++) {
							var wing = game.addChild(new ChickenWing());
							wing.x = Math.random() * 2048;
							wing.y = -50;
						}
					}, 1000); // Increase frequency to every 1 second
				}
				if (score >= scoreNeededForReset) {
					score = 0;
					chicken.scale.set(1, 1);
					initGame(); // Reinitialize game elements
				}
			}
		}
	});
};
// Handle touch events
game.down = function (x, y, obj) {
	var tempX = x;
	var tempY = y;
	if (chicken.x !== tempX || chicken.y !== tempY) {
		if (chicken.x !== tempX || chicken.y !== tempY) {
			chicken.move(tempX, tempY);
			var moveInterval = LK.setInterval(function () {
				chicken.move(tempX, tempY);
				if (Math.abs(chicken.x - tempX) < 1 && Math.abs(chicken.y - tempY) < 1) {
					LK.clearInterval(moveInterval);
				}
			}, 8); // 120 FPS
		}
	}
	var chickenWingCount = 0;
	if (menuBarUnlocked) {
		game.children.forEach(function (child) {
			if (child instanceof ChickenWing) {
				child.destroy();
				chickenWingCount += 1;
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
			}
		});
	}
	if (score >= 100) {
		score += chickenWingCount * 2;
	} else {
		score += chickenWingCount;
	}
	scoreTxt.setText('Score: ' + score);
	// Shake effect
	var originalX = chicken.x;
	var originalY = chicken.y;
	var shakeMagnitude = 5;
	var shakeDuration = 500;
	var shakeInterval = 50;
	var shakeCount = shakeDuration / shakeInterval;
	var shakeStep = 0;
	var shake = LK.setInterval(function () {
		if (shakeStep < shakeCount) {
			chicken.x = originalX + (Math.random() - 0.5) * shakeMagnitude;
			chicken.y = originalY + (Math.random() - 0.5) * shakeMagnitude;
			shakeStep++;
		} else {
			LK.clearInterval(shake);
			chicken.x = originalX;
			chicken.y = originalY;
		}
	}, shakeInterval);
	if (score >= 5 && !menuBarUnlocked) {
		menuBarUnlocked = true;
		unlockMenuBar();
	}
};
game.move = function (x, y, obj) {
	var tempX = x;
	var tempY = y;
	chicken.move(tempX, tempY);
	var moveInterval = LK.setInterval(function () {
		chicken.move(tempX, tempY);
		if (Math.abs(chicken.x - tempX) < 1 && Math.abs(chicken.y - tempY) < 1) {
			LK.clearInterval(moveInterval);
		}
	}, 16); // 60 FPS
};
game.up = function (x, y, obj) {
	// No action needed on touch up
};
// Initialize the game
initGame(); /**** 
* Classes
****/ 
// Chicken class
var Chicken = Container.expand(function () {
	var self = Container.call(this);
	var chickenGraphics = self.attachAsset('chicken', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.update = function () {
		// Update logic for chicken
	};
	self.move = function (x, y) {
		var tempX = x;
		var tempY = y;
		if (self.x !== tempX || self.y !== tempY) {
			self.x += (tempX - self.x) * 0.2;
			self.y += (tempY - self.y) * 0.2;
		}
	};
});
//<Assets used in the game will automatically appear here>
// ChickenWing class
var ChickenWing = Container.expand(function () {
	var self = Container.call(this);
	var wingGraphics = self.attachAsset('chickenWing', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2;
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -50;
			self.x = Math.random() * 2048;
			// No initial creation of chicken wings
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xF0EAD6 //Init game with egg-white background
});
/**** 
* Game Code
****/ 
function unlockMenuBar() {
	var menuBar = new Container();
	var menuBarGraphics = menuBar.attachAsset('menuBar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	menuBar.y = 2732 - 75; // Position menu bar at the bottom of the screen
	menuBar.x = 2048 / 2; // Center the menu bar horizontally
	for (var i = 0; i < 5; i++) {
		var slot = new Container();
		slot.width = 200;
		slot.height = 200;
		slot.x = (i - 2) * 220; // Space slots evenly, centered on the menu bar
		slot.attachAsset('slot', {
			anchorX: 0.5,
			anchorY: 0.5
		});
		if (i === 0) {
			var sword = slot.attachAsset('sword', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			// Removed sword click event
		}
		menuBar.addChild(slot);
	}
	game.addChild(menuBar);
	if (!tipMessageShown) {
		// Show tip text for 10 seconds
		var tipText = new Text2('Tip: Click to Chick', {
			size: 100,
			fill: "#8B4513"
		});
		tipText.anchor.set(0.5, 0.5);
		tipText.x = 2048 / 2;
		tipText.y = 2732 / 2;
		game.addChild(tipText);
		LK.setTimeout(function () {
			tipText.destroy();
		}, 10000);
		tipMessageShown = true;
	}
}
var chickenWingCount = 0;
var scoreIncrement = 1;
var scoreNeededForReset = 100;
var initialPlay = true;
var chickenWingInterval;
var menuBarUnlocked = false;
var tipMessageShown = false;
var chicken;
var tipText;
var score = 0;
var scoreTxt;
var chickenTxt;
// Initialize game elements
function initGame() {
	// Clear previous game state
	game.children.forEach(function (child) {
		if (child instanceof Chicken) {
			chicken = null;
		}
		child.destroy();
	});
	LK.gui.top.children.forEach(function (child) {
		child.destroy();
	});
	if (scoreTxt) {
		scoreTxt.destroy();
		scoreTxt = null;
	}
	if (chickenTxt) {
		chickenTxt.destroy();
		chickenTxt = null;
	}
	if (chickenTxt) {
		chickenTxt.destroy();
		chickenTxt = null;
	}
	score = 0;
	if (initialPlay) {
		scoreIncrement = 1;
		scoreNeededForReset = 100;
		initialPlay = false;
	} else {
		scoreIncrement += 1; // Increase score increment by 1
		scoreNeededForReset += 100; // Increase score needed for reset by 100
	}
	chickenWingCount = 0;
	menuBarUnlocked = false;
	tipMessageShown = false;
	// Initialize chickenWingInterval for controlling the frequency of chicken wings falling
	chickenWingInterval = LK.setInterval(function () {
		var numberOfWings = Math.floor(Math.random() * 3) + 1; // Randomly decide to drop 1 to 3 wings
		for (var i = 0; i < numberOfWings; i++) {
			var wing = game.addChild(new ChickenWing());
			wing.x = Math.random() * 2048;
			wing.y = -50;
		}
	}, 2000);
	if (chicken) {
		chicken.destroy();
		chicken = null;
	}
	chicken = game.addChild(new Chicken());
	chicken.rotating = false;
	chicken.x = 2048 / 2;
	chicken.y = 2732 - 200;
	chickenTxt = new Text2('Chicken', {
		size: 100,
		fill: "#000000"
	});
	chickenTxt.anchor.set(0.5, 0);
	LK.gui.top.addChild(chickenTxt);
	scoreTxt = new Text2('Score: 0', {
		size: 100,
		fill: "#000000"
	});
	scoreTxt.anchor.set(0.5, 0);
	scoreTxt.y = chickenTxt.height; // Position score text below 'Chicken' text
	LK.gui.top.addChild(scoreTxt);
}
// Update game elements
game.update = function () {
	chicken.update();
	var moveInterval = LK.setInterval(function () {
		chicken.move(chicken.x, chicken.y);
	}, 8); // 120 FPS
	game.children.forEach(function (child) {
		if (child instanceof ChickenWing) {
			child.update();
			if (chicken.intersects(child)) {
				child.destroy();
				score += scoreIncrement;
				scoreTxt.setText('Score: ' + score);
				if (score % 10 === 0 && score !== 0 && !chicken.rotating) {
					chicken.rotating = true;
					var originalRotation = chicken.rotation;
					chicken.rotation += Math.PI / 2; // Rotate chicken by 90 degrees
					LK.setTimeout(function () {
						chicken.rotation = originalRotation;
						chicken.rotating = false;
					}, 1000); // Reset rotation after 1 second
				}
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
				// Shake effect
				var originalX = chicken.x;
				var originalY = chicken.y;
				var shakeMagnitude = 5;
				var shakeDuration = 500;
				var shakeInterval = 50;
				var shakeCount = shakeDuration / shakeInterval;
				var shakeStep = 0;
				var shake = LK.setInterval(function () {
					if (shakeStep < shakeCount) {
						chicken.x = originalX + (Math.random() - 0.5) * shakeMagnitude;
						chicken.y = originalY + (Math.random() - 0.5) * shakeMagnitude;
						shakeStep++;
					} else {
						LK.clearInterval(shake);
						chicken.x = originalX;
						chicken.y = originalY;
					}
				}, shakeInterval);
				if (score >= 5 && !game.menuBarUnlocked) {
					game.menuBarUnlocked = true;
					unlockMenuBar();
				}
				// Increase chicken size more drastically at every milestone of ten score
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
				// Increase the frequency of chicken wings falling from the top of the screen once the player gets 15 score
				if (score >= 15) {
					LK.clearInterval(chickenWingInterval);
					chickenWingInterval = LK.setInterval(function () {
						var numberOfWings = Math.floor(Math.random() * 3) + 1; // Randomly decide to drop 1 to 3 wings
						for (var i = 0; i < numberOfWings; i++) {
							var wing = game.addChild(new ChickenWing());
							wing.x = Math.random() * 2048;
							wing.y = -50;
						}
					}, 1000); // Increase frequency to every 1 second
				}
				if (score >= scoreNeededForReset) {
					score = 0;
					chicken.scale.set(1, 1);
					initGame(); // Reinitialize game elements
				}
			}
		}
	});
};
// Handle touch events
game.down = function (x, y, obj) {
	var tempX = x;
	var tempY = y;
	if (chicken.x !== tempX || chicken.y !== tempY) {
		if (chicken.x !== tempX || chicken.y !== tempY) {
			chicken.move(tempX, tempY);
			var moveInterval = LK.setInterval(function () {
				chicken.move(tempX, tempY);
				if (Math.abs(chicken.x - tempX) < 1 && Math.abs(chicken.y - tempY) < 1) {
					LK.clearInterval(moveInterval);
				}
			}, 8); // 120 FPS
		}
	}
	var chickenWingCount = 0;
	if (menuBarUnlocked) {
		game.children.forEach(function (child) {
			if (child instanceof ChickenWing) {
				child.destroy();
				chickenWingCount += 1;
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
				chicken.scale.x += 0.1;
				chicken.scale.y += 0.1;
			}
		});
	}
	if (score >= 100) {
		score += chickenWingCount * 2;
	} else {
		score += chickenWingCount;
	}
	scoreTxt.setText('Score: ' + score);
	// Shake effect
	var originalX = chicken.x;
	var originalY = chicken.y;
	var shakeMagnitude = 5;
	var shakeDuration = 500;
	var shakeInterval = 50;
	var shakeCount = shakeDuration / shakeInterval;
	var shakeStep = 0;
	var shake = LK.setInterval(function () {
		if (shakeStep < shakeCount) {
			chicken.x = originalX + (Math.random() - 0.5) * shakeMagnitude;
			chicken.y = originalY + (Math.random() - 0.5) * shakeMagnitude;
			shakeStep++;
		} else {
			LK.clearInterval(shake);
			chicken.x = originalX;
			chicken.y = originalY;
		}
	}, shakeInterval);
	if (score >= 5 && !menuBarUnlocked) {
		menuBarUnlocked = true;
		unlockMenuBar();
	}
};
game.move = function (x, y, obj) {
	var tempX = x;
	var tempY = y;
	chicken.move(tempX, tempY);
	var moveInterval = LK.setInterval(function () {
		chicken.move(tempX, tempY);
		if (Math.abs(chicken.x - tempX) < 1 && Math.abs(chicken.y - tempY) < 1) {
			LK.clearInterval(moveInterval);
		}
	}, 16); // 60 FPS
};
game.up = function (x, y, obj) {
	// No action needed on touch up
};
// Initialize the game
initGame();