/**** 
* Classes
****/
var FlightBuff = Container.expand(function () {
	var self = Container.call(this);
	self.applyTo = function (bird) {
		bird.gravity = 0;
		bird.velocityY = 0;
		bird.isFlying = true;
		var buffIndicator = new BuffIndicator('flight');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		var moveHandler = function moveHandler(obj) {
			var pos = obj.event.getLocalPosition(game);
			bird.y = pos.y;
		};
		game.on('move', moveHandler);
		LK.setTimeout(function () {
			bird.gravity = 0.6;
			bird.isFlying = false;
			game.off('move', moveHandler);
			buffIndicator.destroy();
		}, 5000);
	};
});
var BuffIndicator = Container.expand(function (buffType) {
	var self = Container.call(this);
	var indicatorGraphics;
	switch (buffType) {
		case 'invincibility':
			indicatorGraphics = self.attachAsset('star', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			break;
		case 'multiplier':
			indicatorGraphics = self.attachAsset('diamond', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			break;
		case 'invincibility':
			indicatorGraphics = self.attachAsset('celestialGuardian', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			break;
	}
	self.alpha = 0.5; // Make the indicator semi-transparent
	self.lifetime = 5000; // The duration for which the indicator is displayed
	self.update = function () {
		self.lifetime -= LK.tickDuration;
		if (self.lifetime <= 0) {
			self.destroy();
		}
	};
});
var Bird = Container.expand(function () {
	var self = Container.call(this);
	self.initializePosition = function () {
		self.x = game.width / 2;
		self.y = game.height / 2;
	};
	var birdGraphics = self.attachAsset('bird', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocityY = 0;
	self.gravity = 0.6;
	self.invincible = false;
	self.scoreMultiplier = 1;
	self.flapPower = -15;
	self.isFlying = false;
	self.shieldActive = false;
	self.flap = function () {
		self.velocityY = self.flapPower;
		self.tweenUp = true;
		self.tweenUpTime = 0;
	};
	self.updatePhysics = function () {
		if (!self.isFrozen) {
			if (self.tweenUp) {
				self.tweenUpTime += 1;
				if (self.tweenUpTime > 10) {
					self.tweenUp = false;
					self.tweenUpTime = 0;
				}
			} else {
				self.velocityY += self.gravity;
			}
			self.y += self.velocityY;
			// Update bird's rotation based on velocity
			self.rotation = Math.max(Math.min(self.velocityY * 0.02, Math.PI / 2), -Math.PI / 2);
			// Lock the bird's horizontal position to the center of the screen
			self.x = game.width / 2;
			// Keep the bird within the game boundaries and check for ground collision
			if (self.y > game.height - ground.height) {
				self.y = game.height - ground.height;
				if (self.hasExtraLife) {
					// Use the extra life
					self.hasExtraLife = false;
					// Reset bird's position and physics
					self.y = game.height / 2;
					self.velocityY = 0;
					// Create particle effect for using extra life
					LK.effects.flashScreen(0x00ff00, 700);
				} else if (!self.knowledgeActive) {
					// Create particle effect for collision with ground
					LK.effects.flashObject(self, 0xff0000, 700);
					LK.effects.flashScreen(0xff0000, 1000);
					LK.showGameOver();
				}
			} else if (self.y < 0) {
				self.y = 0;
			}
		}
	};
});
var MovingObstacle = Container.expand(function () {
	var self = Container.call(this);
	var topPipe = self.attachAsset('largePipe', {
		anchorX: 0.5,
		anchorY: 1,
		flipY: 1 // Flip the top pipe vertically
	});
	var bottomPipe = self.attachAsset('largePipe', {
		anchorX: 0.5,
		anchorY: 0
	});
	var gapSize = 300;
	self.verticalSpeed = 2;
	self.direction = 1;
	self.setGap = function (gapY) {
		topPipe.y = gapY - gapSize / 2 - topPipe.height;
		bottomPipe.y = gapY + gapSize / 2;
	};
	self.move = function () {
		if (!game.paused) {
			self.x -= 4; // Move left
			if (self.y <= 0 || self.y >= game.height - bottomPipe.height - gapSize) {
				self.direction *= -1; // Change direction when hitting bounds
			}
			self.y += self.verticalSpeed * self.direction; // Move vertically
		}
	};
	self.resetPosition = function (newX, newY) {
		self.x = newX;
		self.y = newY;
	};
	self.getTopPipe = function () {
		return topPipe;
	};
	self.getBottomPipe = function () {
		return bottomPipe;
	};
});
var Button = Container.expand(function (label, callback) {
	var self = Container.call(this);
	var buttonGraphics = self.attachAsset('ground', {
		width: 100,
		height: 50,
		color: 0x0000ff,
		shape: 'box'
	});
	var buttonText = new Text2(label, {
		size: 50,
		fill: '#ffffff'
	});
	buttonText.anchor.set(0.5, 0.5);
	buttonText.x = buttonGraphics.width / 2;
	buttonText.y = buttonGraphics.height / 2;
	self.addChild(buttonText);
	self.interactive = true;
	self.buttonMode = true;
	self.on('down', function (obj) {
		callback();
	});
});
var PowerUp = Container.expand(function (rarity) {
	var self = Container.call(this);
	self.rarity = rarity;
	self.move = function () {
		self.x -= 4;
	};
	self.collect = function (bird) {
		// This method should be overridden by specific power-up classes
	};
	return self;
});
var MultiplierPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this);
	self.attachAsset('diamond', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		bird.scoreMultiplier = 2;
		LK.setTimeout(function () {
			bird.scoreMultiplier = 1;
		}, 5000);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('multiplier');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('MULTIPLIER', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 5000);
		self.destroy();
	};
	return self;
});
var FlightPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this);
	self.attachAsset('wings', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		var flightBuff = new FlightBuff();
		game.addChild(flightBuff);
		flightBuff.applyTo(bird);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('flight');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('FLIGHT', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 5000);
		self.destroy();
	};
	return self;
});
var TimeWarpPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this);
	self.attachAsset('timeWarpIcon', {
		width: 100,
		height: 100,
		id: 'your_time_warp_icon_id'
	});
	self.collect = function (bird) {
		// Freeze the bird and pipes
		bird.isFrozen = true;
		game.paused = true;
		// Show the pause menu with choices
		var pauseMenu = new PauseMenu(function (choice) {
			// Implement the choices for the pause menu
			bird.isFrozen = false;
			LK.setGameSpeed(1);
		});
		game.addChild(pauseMenu);
		self.destroy();
	};
	return self;
});
var ShieldPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this, Rarity.RARE);
	self.attachAsset('shield', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		bird.shieldActive = true;
		LK.setTimeout(function () {
			bird.shieldActive = false;
		}, 5000);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('shield');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('SHIELD', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 5000);
		self.destroy();
	};
	return self;
});
var CelestialGuardianPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this, Rarity.MYTHICAL);
	self.attachAsset('celestialGuardian', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		bird.invincible = true;
		LK.setTimeout(function () {
			bird.invincible = false;
		}, 15000);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('invincibility');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('GUARDIAN', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 15000);
		self.destroy();
	};
	return self;
});
// Set up the game tick event for the level
var DifficultyManager = Container.expand(function () {
	var self = Container.call(this);
	self.basePipeCreationInterval = 1500;
	self.pipeCreationInterval = self.basePipeCreationInterval;
	self.difficultyIncrement = 100;
	self.scoreThreshold = 5;
	self.adjustDifficulty = function (currentScore) {
		if (currentScore > 0 && currentScore % self.scoreThreshold === 0) {
			self.pipeCreationInterval = Math.max(self.basePipeCreationInterval - self.difficultyIncrement * (currentScore / self.scoreThreshold), 500);
		}
	};
});
// Initialize ground asset
var PauseMenu = Container.expand(function () {
	var self = Container.call(this);
	var background = self.attachAsset('bgLayer2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	background.x = game.width / 2;
	background.y = game.height / 2;
	var wealthButton = new Button('Wealth', function () {
		LK.setScore(LK.getScore() + 10);
		bird.isFrozen = false;
		game.paused = false;
		bird.updatePhysics();
		startGame();
		self.destroy();
	});
	wealthButton.x = game.width / 2;
	wealthButton.y = game.height / 2 - 150;
	self.addChild(wealthButton);
	var powerButton = new Button('Power', function () {
		bird.invincible = true;
		bird.isFrozen = false;
		game.paused = false;
		bird.updatePhysics();
		LK.setTimeout(function () {
			bird.invincible = false;
		}, 10000);
		self.destroy();
	});
	powerButton.x = game.width / 2;
	powerButton.y = game.height / 2;
	self.addChild(powerButton);
	var lifeButton = new Button('Life', function () {
		bird.hasExtraLife = true;
		bird.isFrozen = false;
		game.paused = false;
		bird.updatePhysics();
		startGame();
		self.destroy();
	});
	lifeButton.x = game.width / 2;
	lifeButton.y = game.height / 2 + 150;
	self.addChild(lifeButton);
});
/**** 
* Initialize Game
****/
// Inside your PowerUp creation logic:
// Create a new instance of TimeWarpPowerUp when needed
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Init game with sky blue background
});
/**** 
* Game Code
****/
// Create a new instance of TimeWarpPowerUp when needed
// Inside your PowerUp creation logic:
var Rarity = {
	COMMON: 'common',
	RARE: 'rare',
	EPIC: 'epic',
	LEGENDARY: 'legendary',
	MYTHICAL: 'mythical'
};
var timeWarpPowerUp = new TimeWarpPowerUp();
timeWarpPowerUp.x = game.width;
timeWarpPowerUp.y = Math.random() * (game.height - 200) + 100;
game.addChild(timeWarpPowerUp);
var ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0.0,
	// Left anchor x-coordinate
	anchorY: 1.0,
	// Bottom anchor y-coordinate
	x: 0,
	// Position x-coordinate
	y: game.height // Position y-coordinate
}));
function startGame() {
	var powerUps = [];
	// Clear the main menu
	if (mainMenu) {
		mainMenu.destroy();
		mainMenu = null;
	}
	// Create and initialize the bird
	if (!bird) {
		bird = game.addChild(new Bird());
		bird.initializePosition();
	}
	var obstacles = [];
	// Add touch event to make the bird flap and jump only if it's not flying
	game.on('down', function (obj) {
		if (bird.timeWarpActivated) {
			bird.timeWarpActivated = false;
			bird.x = bird.timeWarpPosition.x;
			bird.y = bird.timeWarpPosition.y;
			LK.setScore(bird.timeWarpScore);
			LK.effects.flashObject(bird, 0x00ff00, 700);
		} else if (bird && !bird.isFlying) {
			bird.flap();
		}
		// Update bird's position immediately after flapping
		bird.updatePhysics();
	});
	var difficultyManager = game.addChild(new DifficultyManager());
	var lastPipeCreationTime = Date.now();
	LK.on('tick', function () {
		// Update bird physics
		bird.updatePhysics();
		// Move obstacles and create new ones at intervals
		if (obstacles) {
			for (var i = obstacles.length - 1; i >= 0; i--) {
				obstacles[i].move();
				// Remove off-screen obstacles
				if (obstacles[i].x < -obstacles[i].width) {
					obstacles[i].destroy();
					obstacles.splice(i, 1);
				}
			}
			// Check if it's time to create a new pipe
			if (!game.paused && Date.now() - lastPipeCreationTime > difficultyManager.pipeCreationInterval) {
				var obstacle = game.addChild(new MovingObstacle());
				obstacle.setGap(Math.random() * (game.height - 200) + 100);
				obstacle.x = game.width;
				obstacles.push(obstacle);
				lastPipeCreationTime = Date.now();
			}
		}
		// Generate power-ups at intervals
		if (LK.ticks % 600 == 0) {
			var rarityRoll = Math.random() * 100;
			var powerUp;
			if (rarityRoll < 60) {
				powerUp = new MultiplierPowerUp(Rarity.RARE);
			} else if (rarityRoll < 75) {
				powerUp = new FlightPowerUp(Rarity.EPIC);
			} else if (rarityRoll < 85) {
				powerUp = new ShieldPowerUp(Rarity.RARE);
			} else {
				if (rarityRoll < 90) {
					powerUp = new CelestialGuardianPowerUp(Rarity.LEGENDARY);
				} else {
					powerUp = new TimeWarpPowerUp(Rarity.LEGENDARY);
				}
			}
			powerUp.x = game.width;
			powerUp.y = Math.random() * (game.height - 200) + 100;
			game.addChild(powerUp);
			powerUps.push(powerUp);
		}
		// Move power-ups and check for collection
		for (var p = powerUps.length - 1; p >= 0; p--) {
			powerUps[p].move();
			if (bird.intersects(powerUps[p])) {
				powerUps[p].collect(bird);
				powerUps.splice(p, 1);
			} else if (powerUps[p].x < -powerUps[p].width) {
				powerUps[p].destroy();
				powerUps.splice(p, 1);
			}
		}
		// Check for collision between the bird and the pipes and increment score
		var passedPipe = false;
		for (var i = obstacles.length - 1; i >= 0; i--) {
			var obstacle = obstacles[i];
			var topPipe = obstacle.getTopPipe();
			var bottomPipe = obstacle.getBottomPipe();
			if (bird.invincible || bird.shieldActive) {
				if (bird.intersects(topPipe) || bird.intersects(bottomPipe)) {
					obstacles[i].destroy();
					obstacles.splice(i, 1);
					LK.effects.flashObject(topPipe, 0xffff00, 300);
					LK.effects.flashObject(bottomPipe, 0xffff00, 300);
				}
			} else if (bird.intersects(topPipe) || bird.intersects(bottomPipe)) {
				LK.effects.flashObject(bird, 0xff0000, 700);
				LK.effects.flashScreen(0xff0000, 1000);
				LK.showGameOver();
				break;
			} else if (bird.x > topPipe.x + topPipe.width && !obstacle.passed) {
				obstacle.passed = true;
				passedPipe = true;
			}
		}
		if (passedPipe) {
			var currentScore = LK.getScore() + 1 * bird.scoreMultiplier;
			LK.setScore(currentScore);
			scoreTxt.setText(currentScore);
			// Create particle effect for scoring
			LK.effects.flashObject(bird, 0xffff00, 500);
			// Adjust game difficulty based on current score
			difficultyManager.adjustDifficulty(currentScore);
		}
	});
}
var mainMenu = null;
var obstacles = [];
var powerUps = [];
var bird = null;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff",
	anchorX: 0.5,
	anchorY: 0
});
LK.gui.top.addChild(scoreTxt);
startGame(); /**** 
* Classes
****/
var FlightBuff = Container.expand(function () {
	var self = Container.call(this);
	self.applyTo = function (bird) {
		bird.gravity = 0;
		bird.velocityY = 0;
		bird.isFlying = true;
		var buffIndicator = new BuffIndicator('flight');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		var moveHandler = function moveHandler(obj) {
			var pos = obj.event.getLocalPosition(game);
			bird.y = pos.y;
		};
		game.on('move', moveHandler);
		LK.setTimeout(function () {
			bird.gravity = 0.6;
			bird.isFlying = false;
			game.off('move', moveHandler);
			buffIndicator.destroy();
		}, 5000);
	};
});
var BuffIndicator = Container.expand(function (buffType) {
	var self = Container.call(this);
	var indicatorGraphics;
	switch (buffType) {
		case 'invincibility':
			indicatorGraphics = self.attachAsset('star', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			break;
		case 'multiplier':
			indicatorGraphics = self.attachAsset('diamond', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			break;
		case 'invincibility':
			indicatorGraphics = self.attachAsset('celestialGuardian', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			break;
	}
	self.alpha = 0.5; // Make the indicator semi-transparent
	self.lifetime = 5000; // The duration for which the indicator is displayed
	self.update = function () {
		self.lifetime -= LK.tickDuration;
		if (self.lifetime <= 0) {
			self.destroy();
		}
	};
});
var Bird = Container.expand(function () {
	var self = Container.call(this);
	self.initializePosition = function () {
		self.x = game.width / 2;
		self.y = game.height / 2;
	};
	var birdGraphics = self.attachAsset('bird', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocityY = 0;
	self.gravity = 0.6;
	self.invincible = false;
	self.scoreMultiplier = 1;
	self.flapPower = -15;
	self.isFlying = false;
	self.shieldActive = false;
	self.flap = function () {
		self.velocityY = self.flapPower;
		self.tweenUp = true;
		self.tweenUpTime = 0;
	};
	self.updatePhysics = function () {
		if (!self.isFrozen) {
			if (self.tweenUp) {
				self.tweenUpTime += 1;
				if (self.tweenUpTime > 10) {
					self.tweenUp = false;
					self.tweenUpTime = 0;
				}
			} else {
				self.velocityY += self.gravity;
			}
			self.y += self.velocityY;
			// Update bird's rotation based on velocity
			self.rotation = Math.max(Math.min(self.velocityY * 0.02, Math.PI / 2), -Math.PI / 2);
			// Lock the bird's horizontal position to the center of the screen
			self.x = game.width / 2;
			// Keep the bird within the game boundaries and check for ground collision
			if (self.y > game.height - ground.height) {
				self.y = game.height - ground.height;
				if (self.hasExtraLife) {
					// Use the extra life
					self.hasExtraLife = false;
					// Reset bird's position and physics
					self.y = game.height / 2;
					self.velocityY = 0;
					// Create particle effect for using extra life
					LK.effects.flashScreen(0x00ff00, 700);
				} else if (!self.knowledgeActive) {
					// Create particle effect for collision with ground
					LK.effects.flashObject(self, 0xff0000, 700);
					LK.effects.flashScreen(0xff0000, 1000);
					LK.showGameOver();
				}
			} else if (self.y < 0) {
				self.y = 0;
			}
		}
	};
});
var MovingObstacle = Container.expand(function () {
	var self = Container.call(this);
	var topPipe = self.attachAsset('largePipe', {
		anchorX: 0.5,
		anchorY: 1,
		flipY: 1 // Flip the top pipe vertically
	});
	var bottomPipe = self.attachAsset('largePipe', {
		anchorX: 0.5,
		anchorY: 0
	});
	var gapSize = 300;
	self.verticalSpeed = 2;
	self.direction = 1;
	self.setGap = function (gapY) {
		topPipe.y = gapY - gapSize / 2 - topPipe.height;
		bottomPipe.y = gapY + gapSize / 2;
	};
	self.move = function () {
		if (!game.paused) {
			self.x -= 4; // Move left
			if (self.y <= 0 || self.y >= game.height - bottomPipe.height - gapSize) {
				self.direction *= -1; // Change direction when hitting bounds
			}
			self.y += self.verticalSpeed * self.direction; // Move vertically
		}
	};
	self.resetPosition = function (newX, newY) {
		self.x = newX;
		self.y = newY;
	};
	self.getTopPipe = function () {
		return topPipe;
	};
	self.getBottomPipe = function () {
		return bottomPipe;
	};
});
var Button = Container.expand(function (label, callback) {
	var self = Container.call(this);
	var buttonGraphics = self.attachAsset('ground', {
		width: 100,
		height: 50,
		color: 0x0000ff,
		shape: 'box'
	});
	var buttonText = new Text2(label, {
		size: 50,
		fill: '#ffffff'
	});
	buttonText.anchor.set(0.5, 0.5);
	buttonText.x = buttonGraphics.width / 2;
	buttonText.y = buttonGraphics.height / 2;
	self.addChild(buttonText);
	self.interactive = true;
	self.buttonMode = true;
	self.on('down', function (obj) {
		callback();
	});
});
var PowerUp = Container.expand(function (rarity) {
	var self = Container.call(this);
	self.rarity = rarity;
	self.move = function () {
		self.x -= 4;
	};
	self.collect = function (bird) {
		// This method should be overridden by specific power-up classes
	};
	return self;
});
var MultiplierPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this);
	self.attachAsset('diamond', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		bird.scoreMultiplier = 2;
		LK.setTimeout(function () {
			bird.scoreMultiplier = 1;
		}, 5000);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('multiplier');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('MULTIPLIER', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 5000);
		self.destroy();
	};
	return self;
});
var FlightPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this);
	self.attachAsset('wings', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		var flightBuff = new FlightBuff();
		game.addChild(flightBuff);
		flightBuff.applyTo(bird);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('flight');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('FLIGHT', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 5000);
		self.destroy();
	};
	return self;
});
var TimeWarpPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this);
	self.attachAsset('timeWarpIcon', {
		width: 100,
		height: 100,
		id: 'your_time_warp_icon_id'
	});
	self.collect = function (bird) {
		// Freeze the bird and pipes
		bird.isFrozen = true;
		game.paused = true;
		// Show the pause menu with choices
		var pauseMenu = new PauseMenu(function (choice) {
			// Implement the choices for the pause menu
			bird.isFrozen = false;
			LK.setGameSpeed(1);
		});
		game.addChild(pauseMenu);
		self.destroy();
	};
	return self;
});
var ShieldPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this, Rarity.RARE);
	self.attachAsset('shield', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		bird.shieldActive = true;
		LK.setTimeout(function () {
			bird.shieldActive = false;
		}, 5000);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('shield');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('SHIELD', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 5000);
		self.destroy();
	};
	return self;
});
var CelestialGuardianPowerUp = PowerUp.expand(function () {
	var self = PowerUp.call(this, Rarity.MYTHICAL);
	self.attachAsset('celestialGuardian', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collect = function (bird) {
		bird.invincible = true;
		LK.setTimeout(function () {
			bird.invincible = false;
		}, 15000);
		// Create a visual indicator for the active power-up
		var buffIndicator = new BuffIndicator('invincibility');
		buffIndicator.x = bird.x;
		buffIndicator.y = bird.y - bird.height;
		game.addChild(buffIndicator);
		// Add text to the bird that describes the function of the power-up it collected
		var powerUpText = new Text2('GUARDIAN', {
			size: 50,
			fill: '#ffffff'
		});
		powerUpText.anchor.set(0.5, 0);
		powerUpText.x = bird.x;
		powerUpText.y = bird.y - bird.height - 50;
		game.addChild(powerUpText);
		LK.setTimeout(function () {
			powerUpText.destroy();
		}, 15000);
		self.destroy();
	};
	return self;
});
// Set up the game tick event for the level
var DifficultyManager = Container.expand(function () {
	var self = Container.call(this);
	self.basePipeCreationInterval = 1500;
	self.pipeCreationInterval = self.basePipeCreationInterval;
	self.difficultyIncrement = 100;
	self.scoreThreshold = 5;
	self.adjustDifficulty = function (currentScore) {
		if (currentScore > 0 && currentScore % self.scoreThreshold === 0) {
			self.pipeCreationInterval = Math.max(self.basePipeCreationInterval - self.difficultyIncrement * (currentScore / self.scoreThreshold), 500);
		}
	};
});
// Initialize ground asset
var PauseMenu = Container.expand(function () {
	var self = Container.call(this);
	var background = self.attachAsset('bgLayer2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	background.x = game.width / 2;
	background.y = game.height / 2;
	var wealthButton = new Button('Wealth', function () {
		LK.setScore(LK.getScore() + 10);
		bird.isFrozen = false;
		game.paused = false;
		bird.updatePhysics();
		startGame();
		self.destroy();
	});
	wealthButton.x = game.width / 2;
	wealthButton.y = game.height / 2 - 150;
	self.addChild(wealthButton);
	var powerButton = new Button('Power', function () {
		bird.invincible = true;
		bird.isFrozen = false;
		game.paused = false;
		bird.updatePhysics();
		LK.setTimeout(function () {
			bird.invincible = false;
		}, 10000);
		self.destroy();
	});
	powerButton.x = game.width / 2;
	powerButton.y = game.height / 2;
	self.addChild(powerButton);
	var lifeButton = new Button('Life', function () {
		bird.hasExtraLife = true;
		bird.isFrozen = false;
		game.paused = false;
		bird.updatePhysics();
		startGame();
		self.destroy();
	});
	lifeButton.x = game.width / 2;
	lifeButton.y = game.height / 2 + 150;
	self.addChild(lifeButton);
});
/**** 
* Initialize Game
****/
// Inside your PowerUp creation logic:
// Create a new instance of TimeWarpPowerUp when needed
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Init game with sky blue background
});
/**** 
* Game Code
****/
// Create a new instance of TimeWarpPowerUp when needed
// Inside your PowerUp creation logic:
var Rarity = {
	COMMON: 'common',
	RARE: 'rare',
	EPIC: 'epic',
	LEGENDARY: 'legendary',
	MYTHICAL: 'mythical'
};
var timeWarpPowerUp = new TimeWarpPowerUp();
timeWarpPowerUp.x = game.width;
timeWarpPowerUp.y = Math.random() * (game.height - 200) + 100;
game.addChild(timeWarpPowerUp);
var ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0.0,
	// Left anchor x-coordinate
	anchorY: 1.0,
	// Bottom anchor y-coordinate
	x: 0,
	// Position x-coordinate
	y: game.height // Position y-coordinate
}));
function startGame() {
	var powerUps = [];
	// Clear the main menu
	if (mainMenu) {
		mainMenu.destroy();
		mainMenu = null;
	}
	// Create and initialize the bird
	if (!bird) {
		bird = game.addChild(new Bird());
		bird.initializePosition();
	}
	var obstacles = [];
	// Add touch event to make the bird flap and jump only if it's not flying
	game.on('down', function (obj) {
		if (bird.timeWarpActivated) {
			bird.timeWarpActivated = false;
			bird.x = bird.timeWarpPosition.x;
			bird.y = bird.timeWarpPosition.y;
			LK.setScore(bird.timeWarpScore);
			LK.effects.flashObject(bird, 0x00ff00, 700);
		} else if (bird && !bird.isFlying) {
			bird.flap();
		}
		// Update bird's position immediately after flapping
		bird.updatePhysics();
	});
	var difficultyManager = game.addChild(new DifficultyManager());
	var lastPipeCreationTime = Date.now();
	LK.on('tick', function () {
		// Update bird physics
		bird.updatePhysics();
		// Move obstacles and create new ones at intervals
		if (obstacles) {
			for (var i = obstacles.length - 1; i >= 0; i--) {
				obstacles[i].move();
				// Remove off-screen obstacles
				if (obstacles[i].x < -obstacles[i].width) {
					obstacles[i].destroy();
					obstacles.splice(i, 1);
				}
			}
			// Check if it's time to create a new pipe
			if (!game.paused && Date.now() - lastPipeCreationTime > difficultyManager.pipeCreationInterval) {
				var obstacle = game.addChild(new MovingObstacle());
				obstacle.setGap(Math.random() * (game.height - 200) + 100);
				obstacle.x = game.width;
				obstacles.push(obstacle);
				lastPipeCreationTime = Date.now();
			}
		}
		// Generate power-ups at intervals
		if (LK.ticks % 600 == 0) {
			var rarityRoll = Math.random() * 100;
			var powerUp;
			if (rarityRoll < 60) {
				powerUp = new MultiplierPowerUp(Rarity.RARE);
			} else if (rarityRoll < 75) {
				powerUp = new FlightPowerUp(Rarity.EPIC);
			} else if (rarityRoll < 85) {
				powerUp = new ShieldPowerUp(Rarity.RARE);
			} else {
				if (rarityRoll < 90) {
					powerUp = new CelestialGuardianPowerUp(Rarity.LEGENDARY);
				} else {
					powerUp = new TimeWarpPowerUp(Rarity.LEGENDARY);
				}
			}
			powerUp.x = game.width;
			powerUp.y = Math.random() * (game.height - 200) + 100;
			game.addChild(powerUp);
			powerUps.push(powerUp);
		}
		// Move power-ups and check for collection
		for (var p = powerUps.length - 1; p >= 0; p--) {
			powerUps[p].move();
			if (bird.intersects(powerUps[p])) {
				powerUps[p].collect(bird);
				powerUps.splice(p, 1);
			} else if (powerUps[p].x < -powerUps[p].width) {
				powerUps[p].destroy();
				powerUps.splice(p, 1);
			}
		}
		// Check for collision between the bird and the pipes and increment score
		var passedPipe = false;
		for (var i = obstacles.length - 1; i >= 0; i--) {
			var obstacle = obstacles[i];
			var topPipe = obstacle.getTopPipe();
			var bottomPipe = obstacle.getBottomPipe();
			if (bird.invincible || bird.shieldActive) {
				if (bird.intersects(topPipe) || bird.intersects(bottomPipe)) {
					obstacles[i].destroy();
					obstacles.splice(i, 1);
					LK.effects.flashObject(topPipe, 0xffff00, 300);
					LK.effects.flashObject(bottomPipe, 0xffff00, 300);
				}
			} else if (bird.intersects(topPipe) || bird.intersects(bottomPipe)) {
				LK.effects.flashObject(bird, 0xff0000, 700);
				LK.effects.flashScreen(0xff0000, 1000);
				LK.showGameOver();
				break;
			} else if (bird.x > topPipe.x + topPipe.width && !obstacle.passed) {
				obstacle.passed = true;
				passedPipe = true;
			}
		}
		if (passedPipe) {
			var currentScore = LK.getScore() + 1 * bird.scoreMultiplier;
			LK.setScore(currentScore);
			scoreTxt.setText(currentScore);
			// Create particle effect for scoring
			LK.effects.flashObject(bird, 0xffff00, 500);
			// Adjust game difficulty based on current score
			difficultyManager.adjustDifficulty(currentScore);
		}
	});
}
var mainMenu = null;
var obstacles = [];
var powerUps = [];
var bird = null;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff",
	anchorX: 0.5,
	anchorY: 0
});
LK.gui.top.addChild(scoreTxt);
startGame();