/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Bird = Container.expand(function () {
	var self = Container.call(this);
	var birdGraphics = self.attachAsset('bird', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = 0;
	self.gravity = 0.8;
	self.flapPower = -18;
	self.flap = function () {
		self.velocity = self.flapPower;
		LK.getSound('flap').play();
		// Animate bird rotation for flap effect
		tween(birdGraphics, {
			rotation: -0.3
		}, {
			duration: 100
		});
		tween(birdGraphics, {
			rotation: 0.3
		}, {
			duration: 200,
			onFinish: function onFinish() {
				tween(birdGraphics, {
					rotation: 0
				}, {
					duration: 200
				});
			}
		});
	};
	self.update = function () {
		// Only apply physics if game has started
		if (gameStarted) {
			self.velocity += self.gravity;
			self.y += self.velocity;
			// Rotate bird based on velocity
			birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
		} else {
			// Keep bird hovering in place on title screen
			self.velocity = 0;
			birdGraphics.rotation = 0;
		}
	};
	return self;
});
var Pipe = Container.expand(function () {
	var self = Container.call(this);
	self.speed = -4;
	self.scored = false;
	self.gapSize = 300;
	// Create top pipe
	self.topPipe = self.attachAsset('pipe', {
		anchorX: 0.5,
		anchorY: 1
	});
	// Create bottom pipe
	self.bottomPipe = self.attachAsset('pipe', {
		anchorX: 0.5,
		anchorY: 0
	});
	self.setGapPosition = function (gapY) {
		// Top pipe extends from ceiling down to gap
		self.topPipe.y = gapY - self.gapSize / 2;
		// Bottom pipe extends from ground up to gap
		self.bottomPipe.y = gapY + self.gapSize / 2;
	};
	self.update = function () {
		self.x += self.speed;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var bird;
var pipes = [];
var ground;
var gameStarted = false;
var gameOver = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 90; // 1.5 seconds at 60fps
// Create score display
var scoreTxt = new Text2('0', {
	size: 120,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366; // Center of screen height
bird.visible = true; // Show bird on title screen
// Create ground
ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0,
	anchorY: 0
}));
ground.x = 0;
ground.y = 2732 - 100;
// Create title screen
var titleTxt = new Text2('Flappy Bird', {
	size: 120,
	fill: 0xFFFFFF
});
titleTxt.anchor.set(0.5, 0.5);
titleTxt.x = 1024;
titleTxt.y = 800;
game.addChild(titleTxt);
// Create title bird image
var titleBirdImg = game.addChild(LK.getAsset('titleBird', {
	anchorX: 0.5,
	anchorY: 0.5
}));
titleBirdImg.x = 1024;
titleBirdImg.y = 600;
// Create play button
var playButton = new Text2('PLAY', {
	size: 100,
	fill: 0x00FF00
});
playButton.anchor.set(0.5, 0.5);
playButton.x = 1024;
playButton.y = 1200;
game.addChild(playButton);
// Add click handler for play button
playButton.down = function (x, y, obj) {
	if (!gameStarted) {
		gameStarted = true;
		titleTxt.destroy();
		titleBirdImg.destroy();
		playButton.destroy();
		bird.flap(); // Allow jump immediately when starting
	}
};
function spawnPipe() {
	var pipe = new Pipe();
	pipe.x = 2048 + 60; // Start off-screen
	// Random gap position, keeping it away from ground and top
	var minGapY = 200;
	var maxGapY = 2732 - 300 - 100; // Account for ground height
	var gapY = minGapY + Math.random() * (maxGapY - minGapY);
	pipe.setGapPosition(gapY);
	pipes.push(pipe);
	game.addChild(pipe);
}
function checkCollisions() {
	// Don't check collisions if game hasn't started
	if (!gameStarted) {
		return false;
	}
	// Check ground collision
	if (bird.y + 30 >= ground.y) {
		return true;
	}
	// Check ceiling collision
	if (bird.y - 30 <= 0) {
		return true;
	}
	// Check pipe collisions
	for (var i = 0; i < pipes.length; i++) {
		var pipe = pipes[i];
		// Simple collision detection
		if (bird.x + 30 > pipe.x - 60 && bird.x - 30 < pipe.x + 60) {
			// Check if bird is not in the gap
			if (bird.y - 30 < pipe.topPipe.y || bird.y + 30 > pipe.bottomPipe.y) {
				return true;
			}
		}
	}
	return false;
}
function updateScore() {
	for (var i = 0; i < pipes.length; i++) {
		var pipe = pipes[i];
		if (!pipe.scored && bird.x > pipe.x) {
			pipe.scored = true;
			LK.setScore(LK.getScore() + 1);
			scoreTxt.setText(LK.getScore());
			LK.getSound('score').play();
			// Check if player reached 50 score
			if (LK.getScore() >= 50) {
				gameOver = true;
				LK.showYouWin();
				return;
			}
		}
	}
}
function cleanupPipes() {
	for (var i = pipes.length - 1; i >= 0; i--) {
		var pipe = pipes[i];
		if (pipe.x < -120) {
			pipe.destroy();
			pipes.splice(i, 1);
		}
	}
}
game.down = function (x, y, obj) {
	if (gameOver) {
		return;
	}
	if (gameStarted) {
		bird.flap();
	}
};
game.update = function () {
	if (!gameStarted || gameOver) {
		return;
	}
	// Always update bird (physics are controlled within bird.update)
	bird.update();
	// Check collisions
	if (checkCollisions()) {
		gameOver = true;
		LK.showGameOver();
		return;
	}
	// Spawn pipes
	pipeSpawnTimer++;
	if (pipeSpawnTimer >= pipeSpawnInterval) {
		spawnPipe();
		pipeSpawnTimer = 0;
	}
	// Update pipes
	for (var i = 0; i < pipes.length; i++) {
		pipes[i].update();
	}
	// Update score
	updateScore();
	// Cleanup off-screen pipes
	cleanupPipes();
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Bird = Container.expand(function () {
	var self = Container.call(this);
	var birdGraphics = self.attachAsset('bird', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = 0;
	self.gravity = 0.8;
	self.flapPower = -18;
	self.flap = function () {
		self.velocity = self.flapPower;
		LK.getSound('flap').play();
		// Animate bird rotation for flap effect
		tween(birdGraphics, {
			rotation: -0.3
		}, {
			duration: 100
		});
		tween(birdGraphics, {
			rotation: 0.3
		}, {
			duration: 200,
			onFinish: function onFinish() {
				tween(birdGraphics, {
					rotation: 0
				}, {
					duration: 200
				});
			}
		});
	};
	self.update = function () {
		// Only apply physics if game has started
		if (gameStarted) {
			self.velocity += self.gravity;
			self.y += self.velocity;
			// Rotate bird based on velocity
			birdGraphics.rotation = Math.max(-0.5, Math.min(0.5, self.velocity * 0.05));
		} else {
			// Keep bird hovering in place on title screen
			self.velocity = 0;
			birdGraphics.rotation = 0;
		}
	};
	return self;
});
var Pipe = Container.expand(function () {
	var self = Container.call(this);
	self.speed = -4;
	self.scored = false;
	self.gapSize = 300;
	// Create top pipe
	self.topPipe = self.attachAsset('pipe', {
		anchorX: 0.5,
		anchorY: 1
	});
	// Create bottom pipe
	self.bottomPipe = self.attachAsset('pipe', {
		anchorX: 0.5,
		anchorY: 0
	});
	self.setGapPosition = function (gapY) {
		// Top pipe extends from ceiling down to gap
		self.topPipe.y = gapY - self.gapSize / 2;
		// Bottom pipe extends from ground up to gap
		self.bottomPipe.y = gapY + self.gapSize / 2;
	};
	self.update = function () {
		self.x += self.speed;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var bird;
var pipes = [];
var ground;
var gameStarted = false;
var gameOver = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 90; // 1.5 seconds at 60fps
// Create score display
var scoreTxt = new Text2('0', {
	size: 120,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366; // Center of screen height
bird.visible = true; // Show bird on title screen
// Create ground
ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0,
	anchorY: 0
}));
ground.x = 0;
ground.y = 2732 - 100;
// Create title screen
var titleTxt = new Text2('Flappy Bird', {
	size: 120,
	fill: 0xFFFFFF
});
titleTxt.anchor.set(0.5, 0.5);
titleTxt.x = 1024;
titleTxt.y = 800;
game.addChild(titleTxt);
// Create title bird image
var titleBirdImg = game.addChild(LK.getAsset('titleBird', {
	anchorX: 0.5,
	anchorY: 0.5
}));
titleBirdImg.x = 1024;
titleBirdImg.y = 600;
// Create play button
var playButton = new Text2('PLAY', {
	size: 100,
	fill: 0x00FF00
});
playButton.anchor.set(0.5, 0.5);
playButton.x = 1024;
playButton.y = 1200;
game.addChild(playButton);
// Add click handler for play button
playButton.down = function (x, y, obj) {
	if (!gameStarted) {
		gameStarted = true;
		titleTxt.destroy();
		titleBirdImg.destroy();
		playButton.destroy();
		bird.flap(); // Allow jump immediately when starting
	}
};
function spawnPipe() {
	var pipe = new Pipe();
	pipe.x = 2048 + 60; // Start off-screen
	// Random gap position, keeping it away from ground and top
	var minGapY = 200;
	var maxGapY = 2732 - 300 - 100; // Account for ground height
	var gapY = minGapY + Math.random() * (maxGapY - minGapY);
	pipe.setGapPosition(gapY);
	pipes.push(pipe);
	game.addChild(pipe);
}
function checkCollisions() {
	// Don't check collisions if game hasn't started
	if (!gameStarted) {
		return false;
	}
	// Check ground collision
	if (bird.y + 30 >= ground.y) {
		return true;
	}
	// Check ceiling collision
	if (bird.y - 30 <= 0) {
		return true;
	}
	// Check pipe collisions
	for (var i = 0; i < pipes.length; i++) {
		var pipe = pipes[i];
		// Simple collision detection
		if (bird.x + 30 > pipe.x - 60 && bird.x - 30 < pipe.x + 60) {
			// Check if bird is not in the gap
			if (bird.y - 30 < pipe.topPipe.y || bird.y + 30 > pipe.bottomPipe.y) {
				return true;
			}
		}
	}
	return false;
}
function updateScore() {
	for (var i = 0; i < pipes.length; i++) {
		var pipe = pipes[i];
		if (!pipe.scored && bird.x > pipe.x) {
			pipe.scored = true;
			LK.setScore(LK.getScore() + 1);
			scoreTxt.setText(LK.getScore());
			LK.getSound('score').play();
			// Check if player reached 50 score
			if (LK.getScore() >= 50) {
				gameOver = true;
				LK.showYouWin();
				return;
			}
		}
	}
}
function cleanupPipes() {
	for (var i = pipes.length - 1; i >= 0; i--) {
		var pipe = pipes[i];
		if (pipe.x < -120) {
			pipe.destroy();
			pipes.splice(i, 1);
		}
	}
}
game.down = function (x, y, obj) {
	if (gameOver) {
		return;
	}
	if (gameStarted) {
		bird.flap();
	}
};
game.update = function () {
	if (!gameStarted || gameOver) {
		return;
	}
	// Always update bird (physics are controlled within bird.update)
	bird.update();
	// Check collisions
	if (checkCollisions()) {
		gameOver = true;
		LK.showGameOver();
		return;
	}
	// Spawn pipes
	pipeSpawnTimer++;
	if (pipeSpawnTimer >= pipeSpawnInterval) {
		spawnPipe();
		pipeSpawnTimer = 0;
	}
	// Update pipes
	for (var i = 0; i < pipes.length; i++) {
		pipes[i].update();
	}
	// Update score
	updateScore();
	// Cleanup off-screen pipes
	cleanupPipes();
};