/**** 
* 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.velocityY = 0;
	self.gravity = 0.8;
	self.flapPower = -16;
	self.maxFallSpeed = 15;
	self.flap = function () {
		self.velocityY = self.flapPower;
		LK.getSound('flap').play();
		// Add a small rotation animation
		tween(birdGraphics, {
			rotation: -0.3
		}, {
			duration: 200
		});
		tween(birdGraphics, {
			rotation: 0.3
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(birdGraphics, {
					rotation: 0
				}, {
					duration: 200
				});
			}
		});
	};
	self.update = function () {
		// Apply gravity
		self.velocityY += self.gravity;
		// Limit fall speed
		if (self.velocityY > self.maxFallSpeed) {
			self.velocityY = self.maxFallSpeed;
		}
		// Update position
		self.y += self.velocityY;
		// Rotate bird based on velocity
		if (self.velocityY < 0) {
			birdGraphics.rotation = Math.max(-0.5, self.velocityY * 0.05);
		} else {
			birdGraphics.rotation = Math.min(0.5, self.velocityY * 0.05);
		}
	};
	return self;
});
var Pipe = Container.expand(function () {
	var self = Container.call(this);
	self.speed = -4;
	self.gapSize = 400;
	self.scored = false;
	// 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.setupPipes = function (gapCenterY) {
		self.topPipe.y = gapCenterY - self.gapSize / 2;
		self.bottomPipe.y = gapCenterY + self.gapSize / 2;
	};
	self.update = function () {
		self.x += self.speed;
	};
	self.checkBirdCollision = function (bird) {
		var birdBounds = {
			left: bird.x - 30,
			right: bird.x + 30,
			top: bird.y - 30,
			bottom: bird.y + 30
		};
		var pipeBounds = {
			left: self.x - 60,
			right: self.x + 60
		};
		// Check if bird is within pipe x bounds
		if (birdBounds.right > pipeBounds.left && birdBounds.left < pipeBounds.right) {
			// Check collision with top pipe
			if (birdBounds.top < self.topPipe.y) {
				return true;
			}
			// Check collision with bottom pipe
			if (birdBounds.bottom > self.bottomPipe.y) {
				return true;
			}
		}
		return false;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var bird;
var pipes = [];
var ground;
var scoreTxt;
var gameStarted = false;
var gameOver = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // 2 seconds at 60fps
// Initialize bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366; // Center of screen vertically
// Initialize ground
ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0,
	anchorY: 0
}));
ground.x = 0;
ground.y = 2732 - 100; // Bottom of screen
// Initialize score display
scoreTxt = new Text2('0', {
	size: 120,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Game input handling
game.down = function (x, y, obj) {
	if (!gameOver) {
		if (!gameStarted) {
			gameStarted = true;
		}
		bird.flap();
	}
};
// Spawn pipe function
function spawnPipe() {
	var pipe = game.addChild(new Pipe());
	pipe.x = 2048 + 60; // Start off-screen right
	// Random gap position (avoid too high or too low)
	var minY = 200 + pipe.gapSize / 2;
	var maxY = ground.y - 200 - pipe.gapSize / 2;
	var gapCenterY = minY + Math.random() * (maxY - minY);
	pipe.setupPipes(gapCenterY);
	pipes.push(pipe);
}
// Main game update loop
game.update = function () {
	if (!gameStarted || gameOver) {
		return;
	}
	// Update bird
	bird.update();
	// Check ground collision
	if (bird.y + 30 >= ground.y || bird.y - 30 <= 0) {
		gameOver = true;
		LK.effects.flashScreen(0xff0000, 1000);
		LK.setTimeout(function () {
			LK.showGameOver();
		}, 500);
		return;
	}
	// Spawn pipes
	pipeSpawnTimer++;
	if (pipeSpawnTimer >= pipeSpawnInterval) {
		spawnPipe();
		pipeSpawnTimer = 0;
	}
	// Update pipes and check collisions
	for (var i = pipes.length - 1; i >= 0; i--) {
		var pipe = pipes[i];
		pipe.update();
		// Check collision
		if (pipe.checkBirdCollision(bird)) {
			gameOver = true;
			LK.effects.flashScreen(0xff0000, 1000);
			LK.setTimeout(function () {
				LK.showGameOver();
			}, 500);
			return;
		}
		// Check scoring
		if (!pipe.scored && bird.x > pipe.x) {
			pipe.scored = true;
			LK.setScore(LK.getScore() + 1);
			scoreTxt.setText(LK.getScore());
			LK.getSound('score').play();
		}
		// Remove off-screen pipes
		if (pipe.x < -120) {
			pipe.destroy();
			pipes.splice(i, 1);
		}
	}
}; /**** 
* 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.velocityY = 0;
	self.gravity = 0.8;
	self.flapPower = -16;
	self.maxFallSpeed = 15;
	self.flap = function () {
		self.velocityY = self.flapPower;
		LK.getSound('flap').play();
		// Add a small rotation animation
		tween(birdGraphics, {
			rotation: -0.3
		}, {
			duration: 200
		});
		tween(birdGraphics, {
			rotation: 0.3
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(birdGraphics, {
					rotation: 0
				}, {
					duration: 200
				});
			}
		});
	};
	self.update = function () {
		// Apply gravity
		self.velocityY += self.gravity;
		// Limit fall speed
		if (self.velocityY > self.maxFallSpeed) {
			self.velocityY = self.maxFallSpeed;
		}
		// Update position
		self.y += self.velocityY;
		// Rotate bird based on velocity
		if (self.velocityY < 0) {
			birdGraphics.rotation = Math.max(-0.5, self.velocityY * 0.05);
		} else {
			birdGraphics.rotation = Math.min(0.5, self.velocityY * 0.05);
		}
	};
	return self;
});
var Pipe = Container.expand(function () {
	var self = Container.call(this);
	self.speed = -4;
	self.gapSize = 400;
	self.scored = false;
	// 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.setupPipes = function (gapCenterY) {
		self.topPipe.y = gapCenterY - self.gapSize / 2;
		self.bottomPipe.y = gapCenterY + self.gapSize / 2;
	};
	self.update = function () {
		self.x += self.speed;
	};
	self.checkBirdCollision = function (bird) {
		var birdBounds = {
			left: bird.x - 30,
			right: bird.x + 30,
			top: bird.y - 30,
			bottom: bird.y + 30
		};
		var pipeBounds = {
			left: self.x - 60,
			right: self.x + 60
		};
		// Check if bird is within pipe x bounds
		if (birdBounds.right > pipeBounds.left && birdBounds.left < pipeBounds.right) {
			// Check collision with top pipe
			if (birdBounds.top < self.topPipe.y) {
				return true;
			}
			// Check collision with bottom pipe
			if (birdBounds.bottom > self.bottomPipe.y) {
				return true;
			}
		}
		return false;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var bird;
var pipes = [];
var ground;
var scoreTxt;
var gameStarted = false;
var gameOver = false;
var pipeSpawnTimer = 0;
var pipeSpawnInterval = 120; // 2 seconds at 60fps
// Initialize bird
bird = game.addChild(new Bird());
bird.x = 400;
bird.y = 1366; // Center of screen vertically
// Initialize ground
ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0,
	anchorY: 0
}));
ground.x = 0;
ground.y = 2732 - 100; // Bottom of screen
// Initialize score display
scoreTxt = new Text2('0', {
	size: 120,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.y = 100;
// Game input handling
game.down = function (x, y, obj) {
	if (!gameOver) {
		if (!gameStarted) {
			gameStarted = true;
		}
		bird.flap();
	}
};
// Spawn pipe function
function spawnPipe() {
	var pipe = game.addChild(new Pipe());
	pipe.x = 2048 + 60; // Start off-screen right
	// Random gap position (avoid too high or too low)
	var minY = 200 + pipe.gapSize / 2;
	var maxY = ground.y - 200 - pipe.gapSize / 2;
	var gapCenterY = minY + Math.random() * (maxY - minY);
	pipe.setupPipes(gapCenterY);
	pipes.push(pipe);
}
// Main game update loop
game.update = function () {
	if (!gameStarted || gameOver) {
		return;
	}
	// Update bird
	bird.update();
	// Check ground collision
	if (bird.y + 30 >= ground.y || bird.y - 30 <= 0) {
		gameOver = true;
		LK.effects.flashScreen(0xff0000, 1000);
		LK.setTimeout(function () {
			LK.showGameOver();
		}, 500);
		return;
	}
	// Spawn pipes
	pipeSpawnTimer++;
	if (pipeSpawnTimer >= pipeSpawnInterval) {
		spawnPipe();
		pipeSpawnTimer = 0;
	}
	// Update pipes and check collisions
	for (var i = pipes.length - 1; i >= 0; i--) {
		var pipe = pipes[i];
		pipe.update();
		// Check collision
		if (pipe.checkBirdCollision(bird)) {
			gameOver = true;
			LK.effects.flashScreen(0xff0000, 1000);
			LK.setTimeout(function () {
				LK.showGameOver();
			}, 500);
			return;
		}
		// Check scoring
		if (!pipe.scored && bird.x > pipe.x) {
			pipe.scored = true;
			LK.setScore(LK.getScore() + 1);
			scoreTxt.setText(LK.getScore());
			LK.getSound('score').play();
		}
		// Remove off-screen pipes
		if (pipe.x < -120) {
			pipe.destroy();
			pipes.splice(i, 1);
		}
	}
};