/**** 
* Classes
****/ 
// BonusEgg class for falling bonus eggs
var BonusEgg = Container.expand(function () {
	var self = Container.call(this);
	var bonusEggGraphics = self.attachAsset('bonuseg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// Egg class for falling eggs
var Egg = Container.expand(function () {
	var self = Container.call(this);
	var eggGraphics = self.attachAsset('egg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// GoldenEgg class for falling golden eggs
var GoldenEgg = Container.expand(function () {
	var self = Container.call(this);
	var goldenEggGraphics = self.attachAsset('goldenegg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
//<Assets used in the game will automatically appear here>
// Live class for falling lives
var Live = Container.expand(function () {
	var self = Container.call(this);
	var liveGraphics = self.attachAsset('live', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// RottenEgg class for falling rotten eggs
var RottenEgg = Container.expand(function () {
	var self = Container.call(this);
	var rottenEggGraphics = self.attachAsset('rottenEgg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2;
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// Wolf class to control the player character
var Wolf = Container.expand(function () {
	var self = Container.call(this);
	var wolfGraphics = self.attachAsset('wolf', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Update the wolf's y position based on the gravity
		this.y += this.gravity;
		// Apply gravity
		this.gravity += 1;
		// Make sure the wolf doesn't fall through the ground
		if (this.y > 2732 - 200) {
			this.y = 2732 - 200;
			this.gravity = 0;
			this.isJumping = false; // Wolf is not jumping anymore
		}
	};
	self.gravity = 0;
	self.isJumping = false; // Add a new property to check if the wolf is jumping
	self.jump = function () {
		// Define the jump height
		var jumpHeight = 40;
		// Make sure the wolf is not already jumping
		if (!this.isJumping) {
			// Make the wolf jump
			this.gravity = -jumpHeight;
			this.isJumping = true; // Wolf is now jumping
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2271b3 //Init game with black background
});
/**** 
* Game Code
****/ 
// Play the main theme music when the game starts and repeat until game over
var music = LK.getSound('music');
music.play();
music.loop = true;
game.on('gameOver', function () {
	music.stop();
});
var background = game.attachAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
// Add three chickens at the background
var chicken1 = game.attachAsset('chicken', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2 + 300
});
// Initialize game variables
var wolf = game.addChild(new Wolf());
wolf.x = 2048 / 2;
wolf.y = 2732 - 200;
var eggs = [];
var rottenEggs = [];
var goldenEggs = [];
var livesArray = [];
var score = 0;
var lastBonusScore = 0;
var lastGoldenEggScore = 0;
var lastLiveScore = 0;
var lives = 3;
var bonusEggs = [];
var protectionTimer = 0;
// Display score
var scoreTxt = new Text2('Score: 0', {
	size: 100,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Display lives
var livesTxt = new Text2('Lives: ' + lives, {
	size: 100,
	fill: 0xFFFFFF
});
livesTxt.anchor.set(0.5, 0);
livesTxt.y = 100; // Move the life counter some down
LK.gui.top.addChild(livesTxt);
// Add vayabi.net text below lives
var boltTxt = new Text2('vayabi.net', {
	size: 60,
	fill: 0xFFFFFF
});
boltTxt.anchor.set(0.5, 0);
boltTxt.y = 220; // 120 pixels below lives text (moved 20 pixels down)
LK.gui.top.addChild(boltTxt);
// Add logo in top right corner
var logo = game.attachAsset('logo', {
	anchorX: 1,
	anchorY: 0,
	x: 2048 - 50,
	y: 50,
	scaleX: 0.3,
	scaleY: 0.3
});
// Add a pause button
var pauseButton = new Text2('Pause', {
	size: 100,
	fill: 0xFFFFFF
});
pauseButton.anchor.set(0.5, 0);
pauseButton.x = 2048 - 200; // Position the pause button at the top right corner
LK.gui.top.addChild(pauseButton);
// Handle pause button click
pauseButton.down = function (x, y, obj) {
	LK.pauseGame();
};
// Handle wolf movement
game.move = function (x, y, obj) {
	if (x > wolf.x) {
		wolf.scaleX = 1; // face right
	} else if (x < wolf.x) {
		wolf.scaleX = -1; // face left
	}
	wolf.x = x;
};
game.down = function (x, y, obj) {
	wolf.jump();
};
// Update game state
game.update = function () {
	// Spawn eggs and rotten eggs
	if (LK.ticks % 60 == 0) {
		var newEgg = new Egg();
		newEgg.x = Math.random() * 2048;
		newEgg.y = -50;
		eggs.push(newEgg);
		game.addChild(newEgg);
	}
	if (LK.ticks % (360 - Math.floor(score / 10)) == 0) {
		var newRottenEgg = new RottenEgg();
		newRottenEgg.x = Math.random() * 2048;
		newRottenEgg.y = -50;
		rottenEggs.push(newRottenEgg);
		game.addChild(newRottenEgg);
	}
	if (score % 150 == 0 && score > 0 && score != lastGoldenEggScore) {
		var newGoldenEgg = new GoldenEgg();
		newGoldenEgg.x = Math.random() * 2048;
		newGoldenEgg.y = -50;
		goldenEggs.push(newGoldenEgg);
		game.addChild(newGoldenEgg);
		lastGoldenEggScore = score;
	}
	if (score != 0 && score % 400 == 0 && score != lastBonusScore) {
		var newBonusEgg = new BonusEgg();
		newBonusEgg.x = Math.random() * 2048;
		newBonusEgg.y = -50;
		bonusEggs.push(newBonusEgg);
		game.addChild(newBonusEgg);
		lastBonusScore = score;
	}
	// Filtreyle aktif olanları kontrol et
	var activeLives = livesArray.filter(function (obj) {
		return obj && obj.y <= 2732;
	});
	if (score != 0 && score % 100 == 0 && activeLives.length < 3 && score != lastLiveScore) {
		var newLive = new Live();
		newLive.x = Math.random() * 2048;
		newLive.y = -50;
		livesArray.push(newLive);
		game.addChild(newLive);
		lastLiveScore = score;
	}
	// Update eggs and check for collisions
	for (var i = eggs.length - 1; i >= 0; i--) {
		if (eggs[i]) {
			eggs[i].update();
			if (eggs[i].intersects(wolf)) {
				score += 100;
				scoreTxt.setText('Score: ' + score);
				eggs[i].destroy();
				eggs.splice(i, 1);
				// Check win condition
				if (score >= 100000) {
					console.log("You Win! Final score: " + score);
					music.stop();
					LK.showYouWin();
				}
				continue;
			} else if (eggs[i].y > 2732) {
				lives -= 1;
				livesTxt.setText('Lives: ' + lives);
				LK.effects.flashObject(wolf, 0xff0000, 1000);
				LK.getSound('heat').play();
				eggs[i].destroy();
				eggs.splice(i, 1);
				if (lives <= 0) {
					LK.effects.flashScreen(0xff0000, 1000);
					console.log("Game Over! Your score was: " + score);
					music.stop();
					LK.showGameOver();
				}
			}
		}
	}
	// Update rotten eggs and check for collisions
	for (var i = rottenEggs.length - 1; i >= 0; i--) {
		if (rottenEggs[i]) {
			rottenEggs[i].update();
			if (rottenEggs[i].intersects(wolf)) {
				if (protectionTimer <= 0) {
					lives -= 1;
					livesTxt.setText('Lives: ' + lives);
					LK.effects.flashObject(wolf, 0xff0000, 1000);
					LK.getSound('heat').play();
					protectionTimer = 120; // Set protection timer for 2 seconds (120 ticks at 60fps)
					if (lives <= 0) {
						LK.effects.flashScreen(0xff0000, 1000);
						console.log("Game Over! Your score was: " + score);
						music.stop();
						LK.showGameOver();
					}
				}
				rottenEggs[i].destroy();
				rottenEggs.splice(i, 1);
				continue;
			}
			if (rottenEggs[i].y > 2732) {
				rottenEggs[i].destroy();
				rottenEggs.splice(i, 1);
			}
		}
	}
	for (var i = goldenEggs.length - 1; i >= 0; i--) {
		if (goldenEggs[i]) {
			goldenEggs[i].update();
			if (goldenEggs[i].intersects(wolf)) {
				score = score * 10; // Multiply score by 10 for golden egg
				scoreTxt.setText('Score: ' + score);
				goldenEggs[i].destroy();
				goldenEggs.splice(i, 1);
				// Check win condition after golden egg
				if (score >= 100000) {
					console.log("You Win! Final score: " + score);
					music.stop();
					LK.showYouWin();
				}
				continue;
			}
			if (goldenEggs[i].y > 2732) {
				goldenEggs[i].destroy();
				goldenEggs.splice(i, 1);
			}
		}
	}
	for (var i = bonusEggs.length - 1; i >= 0; i--) {
		if (bonusEggs[i]) {
			bonusEggs[i].update();
			if (bonusEggs[i].intersects(wolf)) {
				score += 1000;
				scoreTxt.setText('Score: ' + score);
				bonusEggs[i].destroy();
				bonusEggs.splice(i, 1);
				// Check win condition after bonus egg
				if (score >= 100000) {
					console.log("You Win! Final score: " + score);
					music.stop();
					LK.showYouWin();
				}
				continue;
			} else if (bonusEggs[i].y > 2732) {
				bonusEggs[i].destroy();
				bonusEggs.splice(i, 1);
			}
		}
	}
	for (var i = livesArray.length - 1; i >= 0; i--) {
		if (livesArray[i]) {
			livesArray[i].update();
			if (livesArray[i].intersects(wolf)) {
				lives += 1;
				livesTxt.setText('Lives: ' + lives);
				livesArray[i].destroy();
				livesArray.splice(i, 1);
				continue;
			}
			if (livesArray[i].y > 2732) {
				livesArray[i].destroy();
				livesArray.splice(i, 1);
			}
		}
	}
	if (protectionTimer > 0) {
		protectionTimer--;
		wolf.alpha = 0.5; // Indicate protection with transparency
	} else {
		wolf.alpha = 1; // Reset wolf transparency
	}
}; /**** 
* Classes
****/ 
// BonusEgg class for falling bonus eggs
var BonusEgg = Container.expand(function () {
	var self = Container.call(this);
	var bonusEggGraphics = self.attachAsset('bonuseg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// Egg class for falling eggs
var Egg = Container.expand(function () {
	var self = Container.call(this);
	var eggGraphics = self.attachAsset('egg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// GoldenEgg class for falling golden eggs
var GoldenEgg = Container.expand(function () {
	var self = Container.call(this);
	var goldenEggGraphics = self.attachAsset('goldenegg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
//<Assets used in the game will automatically appear here>
// Live class for falling lives
var Live = Container.expand(function () {
	var self = Container.call(this);
	var liveGraphics = self.attachAsset('live', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2 + Math.floor(score / 100);
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// RottenEgg class for falling rotten eggs
var RottenEgg = Container.expand(function () {
	var self = Container.call(this);
	var rottenEggGraphics = self.attachAsset('rottenEgg', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 5 + 2;
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
// Wolf class to control the player character
var Wolf = Container.expand(function () {
	var self = Container.call(this);
	var wolfGraphics = self.attachAsset('wolf', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Update the wolf's y position based on the gravity
		this.y += this.gravity;
		// Apply gravity
		this.gravity += 1;
		// Make sure the wolf doesn't fall through the ground
		if (this.y > 2732 - 200) {
			this.y = 2732 - 200;
			this.gravity = 0;
			this.isJumping = false; // Wolf is not jumping anymore
		}
	};
	self.gravity = 0;
	self.isJumping = false; // Add a new property to check if the wolf is jumping
	self.jump = function () {
		// Define the jump height
		var jumpHeight = 40;
		// Make sure the wolf is not already jumping
		if (!this.isJumping) {
			// Make the wolf jump
			this.gravity = -jumpHeight;
			this.isJumping = true; // Wolf is now jumping
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2271b3 //Init game with black background
});
/**** 
* Game Code
****/ 
// Play the main theme music when the game starts and repeat until game over
var music = LK.getSound('music');
music.play();
music.loop = true;
game.on('gameOver', function () {
	music.stop();
});
var background = game.attachAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2
});
// Add three chickens at the background
var chicken1 = game.attachAsset('chicken', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 / 2,
	y: 2732 / 2 + 300
});
// Initialize game variables
var wolf = game.addChild(new Wolf());
wolf.x = 2048 / 2;
wolf.y = 2732 - 200;
var eggs = [];
var rottenEggs = [];
var goldenEggs = [];
var livesArray = [];
var score = 0;
var lastBonusScore = 0;
var lastGoldenEggScore = 0;
var lastLiveScore = 0;
var lives = 3;
var bonusEggs = [];
var protectionTimer = 0;
// Display score
var scoreTxt = new Text2('Score: 0', {
	size: 100,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Display lives
var livesTxt = new Text2('Lives: ' + lives, {
	size: 100,
	fill: 0xFFFFFF
});
livesTxt.anchor.set(0.5, 0);
livesTxt.y = 100; // Move the life counter some down
LK.gui.top.addChild(livesTxt);
// Add vayabi.net text below lives
var boltTxt = new Text2('vayabi.net', {
	size: 60,
	fill: 0xFFFFFF
});
boltTxt.anchor.set(0.5, 0);
boltTxt.y = 220; // 120 pixels below lives text (moved 20 pixels down)
LK.gui.top.addChild(boltTxt);
// Add logo in top right corner
var logo = game.attachAsset('logo', {
	anchorX: 1,
	anchorY: 0,
	x: 2048 - 50,
	y: 50,
	scaleX: 0.3,
	scaleY: 0.3
});
// Add a pause button
var pauseButton = new Text2('Pause', {
	size: 100,
	fill: 0xFFFFFF
});
pauseButton.anchor.set(0.5, 0);
pauseButton.x = 2048 - 200; // Position the pause button at the top right corner
LK.gui.top.addChild(pauseButton);
// Handle pause button click
pauseButton.down = function (x, y, obj) {
	LK.pauseGame();
};
// Handle wolf movement
game.move = function (x, y, obj) {
	if (x > wolf.x) {
		wolf.scaleX = 1; // face right
	} else if (x < wolf.x) {
		wolf.scaleX = -1; // face left
	}
	wolf.x = x;
};
game.down = function (x, y, obj) {
	wolf.jump();
};
// Update game state
game.update = function () {
	// Spawn eggs and rotten eggs
	if (LK.ticks % 60 == 0) {
		var newEgg = new Egg();
		newEgg.x = Math.random() * 2048;
		newEgg.y = -50;
		eggs.push(newEgg);
		game.addChild(newEgg);
	}
	if (LK.ticks % (360 - Math.floor(score / 10)) == 0) {
		var newRottenEgg = new RottenEgg();
		newRottenEgg.x = Math.random() * 2048;
		newRottenEgg.y = -50;
		rottenEggs.push(newRottenEgg);
		game.addChild(newRottenEgg);
	}
	if (score % 150 == 0 && score > 0 && score != lastGoldenEggScore) {
		var newGoldenEgg = new GoldenEgg();
		newGoldenEgg.x = Math.random() * 2048;
		newGoldenEgg.y = -50;
		goldenEggs.push(newGoldenEgg);
		game.addChild(newGoldenEgg);
		lastGoldenEggScore = score;
	}
	if (score != 0 && score % 400 == 0 && score != lastBonusScore) {
		var newBonusEgg = new BonusEgg();
		newBonusEgg.x = Math.random() * 2048;
		newBonusEgg.y = -50;
		bonusEggs.push(newBonusEgg);
		game.addChild(newBonusEgg);
		lastBonusScore = score;
	}
	// Filtreyle aktif olanları kontrol et
	var activeLives = livesArray.filter(function (obj) {
		return obj && obj.y <= 2732;
	});
	if (score != 0 && score % 100 == 0 && activeLives.length < 3 && score != lastLiveScore) {
		var newLive = new Live();
		newLive.x = Math.random() * 2048;
		newLive.y = -50;
		livesArray.push(newLive);
		game.addChild(newLive);
		lastLiveScore = score;
	}
	// Update eggs and check for collisions
	for (var i = eggs.length - 1; i >= 0; i--) {
		if (eggs[i]) {
			eggs[i].update();
			if (eggs[i].intersects(wolf)) {
				score += 100;
				scoreTxt.setText('Score: ' + score);
				eggs[i].destroy();
				eggs.splice(i, 1);
				// Check win condition
				if (score >= 100000) {
					console.log("You Win! Final score: " + score);
					music.stop();
					LK.showYouWin();
				}
				continue;
			} else if (eggs[i].y > 2732) {
				lives -= 1;
				livesTxt.setText('Lives: ' + lives);
				LK.effects.flashObject(wolf, 0xff0000, 1000);
				LK.getSound('heat').play();
				eggs[i].destroy();
				eggs.splice(i, 1);
				if (lives <= 0) {
					LK.effects.flashScreen(0xff0000, 1000);
					console.log("Game Over! Your score was: " + score);
					music.stop();
					LK.showGameOver();
				}
			}
		}
	}
	// Update rotten eggs and check for collisions
	for (var i = rottenEggs.length - 1; i >= 0; i--) {
		if (rottenEggs[i]) {
			rottenEggs[i].update();
			if (rottenEggs[i].intersects(wolf)) {
				if (protectionTimer <= 0) {
					lives -= 1;
					livesTxt.setText('Lives: ' + lives);
					LK.effects.flashObject(wolf, 0xff0000, 1000);
					LK.getSound('heat').play();
					protectionTimer = 120; // Set protection timer for 2 seconds (120 ticks at 60fps)
					if (lives <= 0) {
						LK.effects.flashScreen(0xff0000, 1000);
						console.log("Game Over! Your score was: " + score);
						music.stop();
						LK.showGameOver();
					}
				}
				rottenEggs[i].destroy();
				rottenEggs.splice(i, 1);
				continue;
			}
			if (rottenEggs[i].y > 2732) {
				rottenEggs[i].destroy();
				rottenEggs.splice(i, 1);
			}
		}
	}
	for (var i = goldenEggs.length - 1; i >= 0; i--) {
		if (goldenEggs[i]) {
			goldenEggs[i].update();
			if (goldenEggs[i].intersects(wolf)) {
				score = score * 10; // Multiply score by 10 for golden egg
				scoreTxt.setText('Score: ' + score);
				goldenEggs[i].destroy();
				goldenEggs.splice(i, 1);
				// Check win condition after golden egg
				if (score >= 100000) {
					console.log("You Win! Final score: " + score);
					music.stop();
					LK.showYouWin();
				}
				continue;
			}
			if (goldenEggs[i].y > 2732) {
				goldenEggs[i].destroy();
				goldenEggs.splice(i, 1);
			}
		}
	}
	for (var i = bonusEggs.length - 1; i >= 0; i--) {
		if (bonusEggs[i]) {
			bonusEggs[i].update();
			if (bonusEggs[i].intersects(wolf)) {
				score += 1000;
				scoreTxt.setText('Score: ' + score);
				bonusEggs[i].destroy();
				bonusEggs.splice(i, 1);
				// Check win condition after bonus egg
				if (score >= 100000) {
					console.log("You Win! Final score: " + score);
					music.stop();
					LK.showYouWin();
				}
				continue;
			} else if (bonusEggs[i].y > 2732) {
				bonusEggs[i].destroy();
				bonusEggs.splice(i, 1);
			}
		}
	}
	for (var i = livesArray.length - 1; i >= 0; i--) {
		if (livesArray[i]) {
			livesArray[i].update();
			if (livesArray[i].intersects(wolf)) {
				lives += 1;
				livesTxt.setText('Lives: ' + lives);
				livesArray[i].destroy();
				livesArray.splice(i, 1);
				continue;
			}
			if (livesArray[i].y > 2732) {
				livesArray[i].destroy();
				livesArray.splice(i, 1);
			}
		}
	}
	if (protectionTimer > 0) {
		protectionTimer--;
		wolf.alpha = 0.5; // Indicate protection with transparency
	} else {
		wolf.alpha = 1; // Reset wolf transparency
	}
};