/**** 
* Classes
****/ 
// Define the Life class
var Life = Container.expand(function () {
	var self = Container.call(this);
	var lifeGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5,
		shape: 'box',
		width: 50,
		height: 50
	});
	lifeGraphics.tint = 0xFF0000;
	return self;
});
// Define the Obstacle class
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.colors = [0xFF0000, 0x0000FF, 0x00FF00];
	self.colorIndex = Math.floor(Math.random() * self.colors.length);
	obstacleGraphics.tint = self.colors[self.colorIndex];
	self.speed = 10;
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.destroy();
		}
	};
	return self;
});
//<Assets used in the game will automatically appear here>
// Define the Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5,
		shape: 'box',
		width: 150,
		height: 150
	});
	self.speed = 10;
	self.colors = [0xFF0000, 0x0000FF, 0x00FF00];
	self.colorIndex = Math.floor(Math.random() * self.colors.length);
	playerGraphics.tint = self.colors[self.colorIndex];
	self.update = function () {
		if (self.x < 0) {
			self.x = 0;
		} else if (self.x > 2048) {
			self.x = 2048;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize player's lives
var lives = [];
for (var i = 0; i < 3; i++) {
	var life = new Life();
	life.x = 100 + i * 60;
	life.y = 2732 - 100;
	lives.push(life);
	game.addChild(life);
}
// Initialize obstacles array
var obstacles = [];
// Define the x-coordinates of the grid lines
var gridLines = [512, 1024, 1536];
// Function to handle player movement
function handleMove(x, y, obj) {
	// Find the closest grid line to the current mouse position
	var closestGridLine = gridLines.reduce(function (prev, curr) {
		return Math.abs(curr - x) < Math.abs(prev - x) ? curr : prev;
	});
	// Clamp the player's x position to the closest grid line
	player.x = closestGridLine;
	player.y = y;
}
// Function to spawn obstacles
function spawnObstacle() {
	// Spawn obstacles on the grid lines
	// Shuffle the colors array
	var colors = [0xFF0000, 0x0000FF, 0x00FF00];
	colors.sort(function () {
		return Math.random() - 0.5;
	});
	// Ensure that one color does not appear twice in a row
	if (colors[0] === colors[1]) {
		var temp = colors[1];
		colors[1] = colors[2];
		colors[2] = temp;
	}
	for (var i = 0; i < 3; i++) {
		var obstacle = new Obstacle();
		obstacle.x = gridLines[i];
		obstacle.y = -50;
		// Set the color of the obstacle to be different for each grid line
		obstacle.colorIndex = i;
		obstacle.children[0].tint = colors[i];
		obstacles.push(obstacle);
		game.addChild(obstacle);
	}
}
// Set interval to spawn obstacles
var obstacleInterval = LK.setInterval(spawnObstacle, 2000);
// Game update function
game.update = function () {
	for (var i = obstacles.length - 1; i >= 0; i--) {
		obstacles[i].update();
		if (player.intersects(obstacles[i]) && !obstacles[i].hit) {
			obstacles[i].hit = true;
			obstacles[i].destroy();
			// Check if the player and obstacle have the same color
			if (player.children[0].tint === obstacles[i].children[0].tint) {
				LK.setScore(LK.getScore() + 1);
				scoreTxt.setText(LK.getScore()); // Update the score display
				// Increase the obstacle spawn speed every 50 score
				if (LK.getScore() % 50 == 0) {
					LK.clearInterval(obstacleInterval);
					obstacleInterval = LK.setInterval(spawnObstacle, 2000 - LK.getScore() * 10);
				}
				// Change player color on hit
				player.colorIndex = Math.floor(Math.random() * player.colors.length);
				player.children[0].tint = player.colors[player.colorIndex];
			} else {
				// Reduce one life of the player
				var life = lives.pop();
				life.destroy();
				// Flash the screen red on wrong hit
				LK.effects.flashScreen(0xff0000, 1000);
				// End the game when all the lives are finished
				if (lives.length === 0) {
					LK.showGameOver();
				}
			}
			console.log("Player intersects with obstacle");
		}
	}
};
// Handle touch/mouse move
game.move = function (x, y, obj) {
	player.x = x;
};
// Handle touch/mouse down
game.down = function (x, y, obj) {
	player.x = x;
};
// Handle touch/mouse up
game.up = function (x, y, obj) {
	// No action needed on up event
};
// Add score display
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(1, 0); // Sets anchor to the right of the top edge of the text.
LK.gui.topRight.addChild(scoreTxt);
// Add event listener for 'esc' key to exit the game /**** 
* Classes
****/ 
// Define the Life class
var Life = Container.expand(function () {
	var self = Container.call(this);
	var lifeGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5,
		shape: 'box',
		width: 50,
		height: 50
	});
	lifeGraphics.tint = 0xFF0000;
	return self;
});
// Define the Obstacle class
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.colors = [0xFF0000, 0x0000FF, 0x00FF00];
	self.colorIndex = Math.floor(Math.random() * self.colors.length);
	obstacleGraphics.tint = self.colors[self.colorIndex];
	self.speed = 10;
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.destroy();
		}
	};
	return self;
});
//<Assets used in the game will automatically appear here>
// Define the Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5,
		shape: 'box',
		width: 150,
		height: 150
	});
	self.speed = 10;
	self.colors = [0xFF0000, 0x0000FF, 0x00FF00];
	self.colorIndex = Math.floor(Math.random() * self.colors.length);
	playerGraphics.tint = self.colors[self.colorIndex];
	self.update = function () {
		if (self.x < 0) {
			self.x = 0;
		} else if (self.x > 2048) {
			self.x = 2048;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize player
var player = game.addChild(new Player());
player.x = 2048 / 2;
player.y = 2732 - 200;
// Initialize player's lives
var lives = [];
for (var i = 0; i < 3; i++) {
	var life = new Life();
	life.x = 100 + i * 60;
	life.y = 2732 - 100;
	lives.push(life);
	game.addChild(life);
}
// Initialize obstacles array
var obstacles = [];
// Define the x-coordinates of the grid lines
var gridLines = [512, 1024, 1536];
// Function to handle player movement
function handleMove(x, y, obj) {
	// Find the closest grid line to the current mouse position
	var closestGridLine = gridLines.reduce(function (prev, curr) {
		return Math.abs(curr - x) < Math.abs(prev - x) ? curr : prev;
	});
	// Clamp the player's x position to the closest grid line
	player.x = closestGridLine;
	player.y = y;
}
// Function to spawn obstacles
function spawnObstacle() {
	// Spawn obstacles on the grid lines
	// Shuffle the colors array
	var colors = [0xFF0000, 0x0000FF, 0x00FF00];
	colors.sort(function () {
		return Math.random() - 0.5;
	});
	// Ensure that one color does not appear twice in a row
	if (colors[0] === colors[1]) {
		var temp = colors[1];
		colors[1] = colors[2];
		colors[2] = temp;
	}
	for (var i = 0; i < 3; i++) {
		var obstacle = new Obstacle();
		obstacle.x = gridLines[i];
		obstacle.y = -50;
		// Set the color of the obstacle to be different for each grid line
		obstacle.colorIndex = i;
		obstacle.children[0].tint = colors[i];
		obstacles.push(obstacle);
		game.addChild(obstacle);
	}
}
// Set interval to spawn obstacles
var obstacleInterval = LK.setInterval(spawnObstacle, 2000);
// Game update function
game.update = function () {
	for (var i = obstacles.length - 1; i >= 0; i--) {
		obstacles[i].update();
		if (player.intersects(obstacles[i]) && !obstacles[i].hit) {
			obstacles[i].hit = true;
			obstacles[i].destroy();
			// Check if the player and obstacle have the same color
			if (player.children[0].tint === obstacles[i].children[0].tint) {
				LK.setScore(LK.getScore() + 1);
				scoreTxt.setText(LK.getScore()); // Update the score display
				// Increase the obstacle spawn speed every 50 score
				if (LK.getScore() % 50 == 0) {
					LK.clearInterval(obstacleInterval);
					obstacleInterval = LK.setInterval(spawnObstacle, 2000 - LK.getScore() * 10);
				}
				// Change player color on hit
				player.colorIndex = Math.floor(Math.random() * player.colors.length);
				player.children[0].tint = player.colors[player.colorIndex];
			} else {
				// Reduce one life of the player
				var life = lives.pop();
				life.destroy();
				// Flash the screen red on wrong hit
				LK.effects.flashScreen(0xff0000, 1000);
				// End the game when all the lives are finished
				if (lives.length === 0) {
					LK.showGameOver();
				}
			}
			console.log("Player intersects with obstacle");
		}
	}
};
// Handle touch/mouse move
game.move = function (x, y, obj) {
	player.x = x;
};
// Handle touch/mouse down
game.down = function (x, y, obj) {
	player.x = x;
};
// Handle touch/mouse up
game.up = function (x, y, obj) {
	// No action needed on up event
};
// Add score display
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(1, 0); // Sets anchor to the right of the top edge of the text.
LK.gui.topRight.addChild(scoreTxt);
// Add event listener for 'esc' key to exit the game