/**** 
* Classes
****/ 
// Obstacle class representing the obstacles the pig must avoid
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var sizeMultiplier = Math.random() * (8 - 3) + 3;
	self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: sizeMultiplier,
		scaleY: sizeMultiplier
	});
	self.speed = -5;
	// Update function to move the obstacle
	self.update = function () {
		self.x += self.speed;
		// Remove obstacle if it goes off screen
		if (self.x < -self.width) {
			self.destroy();
		}
	};
});
// Pig class representing the player character
var Pig = Container.expand(function () {
	var self = Container.call(this);
	self.attachAsset('pig', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 3,
		scaleY: 3
	});
	self.gravity = 0.5;
	self.lift = -12; // Adjusted for better jumping physics
	self.velocity = 0;
	// Update function to apply gravity and lift
	self.update = function () {
		self.velocity += self.gravity;
		self.y += self.velocity;
		self.rotation = Math.min(Math.max(self.velocity / 10, -Math.PI / 8), Math.PI / 4);
		if (self.y <= 0) {
			self.y = 0;
			self.velocity = 0;
		}
	};
	// Function to make the pig jump
	self.jump = function () {
		self.velocity = self.lift;
		self.rotation = Math.min(self.velocity / 10, Math.PI / 4);
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Light blue background to represent the sky
});
/**** 
* Game Code
****/ 
var background = game.attachAsset('background', {
	anchorX: 0,
	anchorY: 0,
	x: 0,
	y: 0
});
// Initialize game variables
var pig = game.addChild(new Pig());
pig.x = 2048 / 4; // Start pig at 1/4th of the screen width
pig.y = 2732 / 2; // Center pig vertically
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Add a timer to increment the score every second
LK.setInterval(function () {
	score++;
	scoreTxt.setText(score);
}, 1000);
// Function to create a new obstacle
function createObstacle() {
	var obstacle = new Obstacle();
	obstacle.x = 2048;
	obstacle.y = Math.random() * (2732 - obstacle.height); // Random vertical position
	obstacles.push(obstacle);
	game.addChild(obstacle);
}
// Simple collision detection
function checkCollision(a, b) {
	return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
}
// Handle game updates
game.update = function () {
	pig.update();
	// Check for collisions with obstacles
	for (var i = obstacles.length - 1; i >= 0; i--) {
		obstacles[i].update();
		if (checkCollision(pig, obstacles[i]) && pig.intersects(obstacles[i])) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
			return;
		}
		// Remove off-screen obstacles and increment score
		if (obstacles[i].x < -obstacles[i].width) {
			obstacles[i].destroy();
			obstacles.splice(i, 1);
			score++;
			scoreTxt.setText(score);
		}
	}
	// Create new obstacles periodically
	if (LK.ticks % 120 === 0) {
		createObstacle();
	}
	// Check if pig falls below the screen or goes too far up
	if (pig.y > 2732 || pig.y <= 0) {
		LK.effects.flashScreen(0xff0000, 1000);
		LK.showGameOver();
	}
};
// Handle touch events to make the pig jump
game.down = function (x, y, obj) {
	pig.jump();
}; /**** 
* Classes
****/ 
// Obstacle class representing the obstacles the pig must avoid
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var sizeMultiplier = Math.random() * (8 - 3) + 3;
	self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: sizeMultiplier,
		scaleY: sizeMultiplier
	});
	self.speed = -5;
	// Update function to move the obstacle
	self.update = function () {
		self.x += self.speed;
		// Remove obstacle if it goes off screen
		if (self.x < -self.width) {
			self.destroy();
		}
	};
});
// Pig class representing the player character
var Pig = Container.expand(function () {
	var self = Container.call(this);
	self.attachAsset('pig', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 3,
		scaleY: 3
	});
	self.gravity = 0.5;
	self.lift = -12; // Adjusted for better jumping physics
	self.velocity = 0;
	// Update function to apply gravity and lift
	self.update = function () {
		self.velocity += self.gravity;
		self.y += self.velocity;
		self.rotation = Math.min(Math.max(self.velocity / 10, -Math.PI / 8), Math.PI / 4);
		if (self.y <= 0) {
			self.y = 0;
			self.velocity = 0;
		}
	};
	// Function to make the pig jump
	self.jump = function () {
		self.velocity = self.lift;
		self.rotation = Math.min(self.velocity / 10, Math.PI / 4);
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Light blue background to represent the sky
});
/**** 
* Game Code
****/ 
var background = game.attachAsset('background', {
	anchorX: 0,
	anchorY: 0,
	x: 0,
	y: 0
});
// Initialize game variables
var pig = game.addChild(new Pig());
pig.x = 2048 / 4; // Start pig at 1/4th of the screen width
pig.y = 2732 / 2; // Center pig vertically
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Add a timer to increment the score every second
LK.setInterval(function () {
	score++;
	scoreTxt.setText(score);
}, 1000);
// Function to create a new obstacle
function createObstacle() {
	var obstacle = new Obstacle();
	obstacle.x = 2048;
	obstacle.y = Math.random() * (2732 - obstacle.height); // Random vertical position
	obstacles.push(obstacle);
	game.addChild(obstacle);
}
// Simple collision detection
function checkCollision(a, b) {
	return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
}
// Handle game updates
game.update = function () {
	pig.update();
	// Check for collisions with obstacles
	for (var i = obstacles.length - 1; i >= 0; i--) {
		obstacles[i].update();
		if (checkCollision(pig, obstacles[i]) && pig.intersects(obstacles[i])) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
			return;
		}
		// Remove off-screen obstacles and increment score
		if (obstacles[i].x < -obstacles[i].width) {
			obstacles[i].destroy();
			obstacles.splice(i, 1);
			score++;
			scoreTxt.setText(score);
		}
	}
	// Create new obstacles periodically
	if (LK.ticks % 120 === 0) {
		createObstacle();
	}
	// Check if pig falls below the screen or goes too far up
	if (pig.y > 2732 || pig.y <= 0) {
		LK.effects.flashScreen(0xff0000, 1000);
		LK.showGameOver();
	}
};
// Handle touch events to make the pig jump
game.down = function (x, y, obj) {
	pig.jump();
};