/**** 
* Classes
****/ 
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class to handle ball behavior
var Ball = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedX = 10;
	self.speedY = 10;
	self.update = function () {
		if (ballOnPaddle) {
			// If the ball is on the paddle, update the ball's position to follow the paddle
			self.x = paddle.x;
			self.y = paddle.y - paddle.height / 2 - self.height / 2;
		} else {
			self.x += self.speedX;
			self.y += self.speedY;
			// Bounce off walls
			if (self.x <= 0 || self.x >= 2048) {
				self.speedX *= -1;
			}
			if (self.y <= 0 || self.y >= 2732) {
				self.speedY *= -1;
			}
		}
	};
});
// Block class to handle block behavior
var Block = Container.expand(function (color) {
	var self = Container.call(this);
	var blockGraphics = self.attachAsset(color, {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 200,
		height: 100
	});
	self["break"] = function () {
		// Logic for breaking the block
		self.destroy();
	};
});
// Paddle class to handle paddle behavior
var Paddle = Container.expand(function () {
	var self = Container.call(this);
	var paddleGraphics = self.attachAsset('paddle', {
		width: 400,
		height: 50,
		color: 0xFFFFFF,
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Paddle follows mouse or ball based on the value of paddleFollowsBall
		if (paddleFollowsBall && balls.length > 0) {
			self.x = balls[0].x;
		} else {
			self.x = game.mouseX;
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize score text
// Function to generate blocks
function generateBlocks() {
	var colors = ['block1', 'block2', 'block3', 'block4', 'block5'];
	for (var i = 0; i < 5; i++) {
		for (var j = 0; j < 10; j++) {
			var block;
			var color = colors[(i + j) % colors.length];
			block = game.addChild(new Block(color));
			block.x = initialBlockPositions[i * 10 + j].x;
			block.y = initialBlockPositions[i * 10 + j].y;
			blocks.push(block);
			game.addChild(block);
		}
	}
	// Update the level text
	levelTxt.setText('Level: ' + level);
	// Reset the ball position
	balls[0].x = 1024; // Center horizontally
	balls[0].y = 1366; // Position at the center of the screen
	balls[0].speedX = 5;
	balls[0].speedY = 5;
}
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize level text
var levelTxt = new Text2('Level: ' + level, {
	size: 50,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(levelTxt);
// Add Glaud text in top right corner
var glaudTxt = new Text2('Glaud', {
	size: 50,
	fill: 0xFFA500 // Orange color
});
glaudTxt.anchor.set(1, 0);
glaudTxt.y = levelTxt.height + 10; // Position below level text
LK.gui.topRight.addChild(glaudTxt);
// Initialize game elements
var balls = [];
var blocks = [];
var paddle = game.addChild(new Paddle());
paddle.y = 2500; // Position paddle near the bottom
// Initialize level
var level = 1;
// Initialize level display text
var levelDisplayTxt = new Text2('Level: ' + level, {
	size: 150,
	fill: 0xFFFFFF
});
levelDisplayTxt.anchor.set(0.5, 0.5);
levelDisplayTxt.x = 1024; // Center horizontally
levelDisplayTxt.y = 1366; // Center vertically
game.addChild(levelDisplayTxt);
// Function to display the current level for 3 seconds
function displayLevel() {
	levelDisplayTxt.setText('Level: ' + level);
	levelDisplayTxt.visible = true;
	LK.setTimeout(function () {
		levelDisplayTxt.visible = false;
	}, 3000);
}
// Display the current level at the start of the game
displayLevel();
// Create initial ball
var initialBall = new Ball();
initialBall.x = 1024; // Center horizontally
initialBall.y = 1366; // Position at the center of the screen
balls.push(initialBall);
game.addChild(initialBall);
// Add a boolean variable to track if the ball is on the paddle
var ballOnPaddle = true;
// Add a boolean variable to track if the paddle is following the ball
var paddleFollowsBall = false;
// Create blocks
var colors = ['block1', 'block2', 'block3', 'block4', 'block5'];
var initialBlockPositions = [];
for (var i = 0; i < 5; i++) {
	for (var j = 0; j < 10; j++) {
		var block;
		var color = colors[(i + j) % colors.length];
		block = game.addChild(new Block(color));
		var blockPosition = {
			x: 140 + j * 200,
			y: 400 + i * 100
		};
		block.x = blockPosition.x; // Increase the horizontal gap between blocks
		block.y = blockPosition.y; // Decrease the vertical gap between blocks
		blocks.push(block);
		initialBlockPositions.push(blockPosition);
		game.addChild(block);
	}
}
// Game update loop
game.update = function () {
	// Update paddle
	paddle.update();
	// Update balls
	for (var i = balls.length - 1; i >= 0; i--) {
		var ball = balls[i];
		ball.update();
		// Check collision with paddle
		if (ball.intersects(paddle)) {
			// Change the ball's direction based on its position relative to the paddle and the ball's incoming direction
			if (ball.x < paddle.x - paddle.width / 4) {
				// If the ball hits the left quarter of the paddle, make it move left
				ball.speedX = ball.speedX < 0 ? 5 : -5;
			} else if (ball.x > paddle.x + paddle.width / 4) {
				// If the ball hits the right quarter of the paddle, make it move right
				ball.speedX = ball.speedX < 0 ? -5 : 5;
			} else {
				// If the ball hits the middle half of the paddle, make it move straight up
				ball.speedX = ball.speedX < 0 ? -5 : 5;
			}
			// Change the ball's direction based on its incoming direction
			ball.speedY = ball.speedY < 0 ? 10 : -10;
			// Play 'sekme' sound
			LK.getSound('sekme').play();
		}
		// Check collision with blocks
		for (var j = blocks.length - 1; j >= 0; j--) {
			var block = blocks[j];
			if (ball.intersects(block)) {
				block["break"]();
				ball.speedY *= -1;
				blocks.splice(j, 1);
				// Update score
				LK.setScore(LK.getScore() + 1);
				scoreTxt.setText(LK.getScore());
				// Play 'sekme' sound
				LK.getSound('sekme').play();
			}
		}
		// Remove ball if it goes below the paddle
		if (ball.y > 2732) {
			ball.destroy();
			balls.splice(i, 1);
		}
	}
	// Check if all blocks are destroyed
	if (blocks.length === 0) {
		// Increment the level
		level++;
		// Generate new blocks
		generateBlocks();
		// Display the current level
		displayLevel();
	}
	// Check for game over
	if (balls.length === 0) {
		LK.showGameOver();
	}
};
// Mouse move event to control paddle
game.move = function (x, y, obj) {
	game.mouseX = x;
};
game.down = function (x, y, obj) {
	var currentTime = Date.now();
	if (game.lastClickTime && currentTime - game.lastClickTime < 1000) {
		game.clickCount = (game.clickCount || 0) + 1;
	} else {
		game.clickCount = 1;
	}
	game.lastClickTime = currentTime;
	if (game.clickCount === 2) {
		for (var i = blocks.length - 1; i >= 0; i--) {
			blocks[i]["break"]();
			blocks.splice(i, 1);
		}
		game.clickCount = 0;
	}
	// If the ball is on the paddle, launch the ball when the game is clicked
	if (ballOnPaddle) {
		ballOnPaddle = false;
	}
	// Toggle the value of paddleFollowsBall on click only if mouse position is on the paddle
	if (x >= paddle.x - paddle.width / 2 && x <= paddle.x + paddle.width / 2 && y >= paddle.y - paddle.height / 2 && y <= paddle.y + paddle.height / 2) {
		paddleFollowsBall = !paddleFollowsBall;
	}
}; /**** 
* Classes
****/ 
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Ball class to handle ball behavior
var Ball = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedX = 10;
	self.speedY = 10;
	self.update = function () {
		if (ballOnPaddle) {
			// If the ball is on the paddle, update the ball's position to follow the paddle
			self.x = paddle.x;
			self.y = paddle.y - paddle.height / 2 - self.height / 2;
		} else {
			self.x += self.speedX;
			self.y += self.speedY;
			// Bounce off walls
			if (self.x <= 0 || self.x >= 2048) {
				self.speedX *= -1;
			}
			if (self.y <= 0 || self.y >= 2732) {
				self.speedY *= -1;
			}
		}
	};
});
// Block class to handle block behavior
var Block = Container.expand(function (color) {
	var self = Container.call(this);
	var blockGraphics = self.attachAsset(color, {
		anchorX: 0.5,
		anchorY: 0.5,
		width: 200,
		height: 100
	});
	self["break"] = function () {
		// Logic for breaking the block
		self.destroy();
	};
});
// Paddle class to handle paddle behavior
var Paddle = Container.expand(function () {
	var self = Container.call(this);
	var paddleGraphics = self.attachAsset('paddle', {
		width: 400,
		height: 50,
		color: 0xFFFFFF,
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		// Paddle follows mouse or ball based on the value of paddleFollowsBall
		if (paddleFollowsBall && balls.length > 0) {
			self.x = balls[0].x;
		} else {
			self.x = game.mouseX;
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize score text
// Function to generate blocks
function generateBlocks() {
	var colors = ['block1', 'block2', 'block3', 'block4', 'block5'];
	for (var i = 0; i < 5; i++) {
		for (var j = 0; j < 10; j++) {
			var block;
			var color = colors[(i + j) % colors.length];
			block = game.addChild(new Block(color));
			block.x = initialBlockPositions[i * 10 + j].x;
			block.y = initialBlockPositions[i * 10 + j].y;
			blocks.push(block);
			game.addChild(block);
		}
	}
	// Update the level text
	levelTxt.setText('Level: ' + level);
	// Reset the ball position
	balls[0].x = 1024; // Center horizontally
	balls[0].y = 1366; // Position at the center of the screen
	balls[0].speedX = 5;
	balls[0].speedY = 5;
}
var scoreTxt = new Text2('0', {
	size: 150,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Initialize level text
var levelTxt = new Text2('Level: ' + level, {
	size: 50,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(levelTxt);
// Add Glaud text in top right corner
var glaudTxt = new Text2('Glaud', {
	size: 50,
	fill: 0xFFA500 // Orange color
});
glaudTxt.anchor.set(1, 0);
glaudTxt.y = levelTxt.height + 10; // Position below level text
LK.gui.topRight.addChild(glaudTxt);
// Initialize game elements
var balls = [];
var blocks = [];
var paddle = game.addChild(new Paddle());
paddle.y = 2500; // Position paddle near the bottom
// Initialize level
var level = 1;
// Initialize level display text
var levelDisplayTxt = new Text2('Level: ' + level, {
	size: 150,
	fill: 0xFFFFFF
});
levelDisplayTxt.anchor.set(0.5, 0.5);
levelDisplayTxt.x = 1024; // Center horizontally
levelDisplayTxt.y = 1366; // Center vertically
game.addChild(levelDisplayTxt);
// Function to display the current level for 3 seconds
function displayLevel() {
	levelDisplayTxt.setText('Level: ' + level);
	levelDisplayTxt.visible = true;
	LK.setTimeout(function () {
		levelDisplayTxt.visible = false;
	}, 3000);
}
// Display the current level at the start of the game
displayLevel();
// Create initial ball
var initialBall = new Ball();
initialBall.x = 1024; // Center horizontally
initialBall.y = 1366; // Position at the center of the screen
balls.push(initialBall);
game.addChild(initialBall);
// Add a boolean variable to track if the ball is on the paddle
var ballOnPaddle = true;
// Add a boolean variable to track if the paddle is following the ball
var paddleFollowsBall = false;
// Create blocks
var colors = ['block1', 'block2', 'block3', 'block4', 'block5'];
var initialBlockPositions = [];
for (var i = 0; i < 5; i++) {
	for (var j = 0; j < 10; j++) {
		var block;
		var color = colors[(i + j) % colors.length];
		block = game.addChild(new Block(color));
		var blockPosition = {
			x: 140 + j * 200,
			y: 400 + i * 100
		};
		block.x = blockPosition.x; // Increase the horizontal gap between blocks
		block.y = blockPosition.y; // Decrease the vertical gap between blocks
		blocks.push(block);
		initialBlockPositions.push(blockPosition);
		game.addChild(block);
	}
}
// Game update loop
game.update = function () {
	// Update paddle
	paddle.update();
	// Update balls
	for (var i = balls.length - 1; i >= 0; i--) {
		var ball = balls[i];
		ball.update();
		// Check collision with paddle
		if (ball.intersects(paddle)) {
			// Change the ball's direction based on its position relative to the paddle and the ball's incoming direction
			if (ball.x < paddle.x - paddle.width / 4) {
				// If the ball hits the left quarter of the paddle, make it move left
				ball.speedX = ball.speedX < 0 ? 5 : -5;
			} else if (ball.x > paddle.x + paddle.width / 4) {
				// If the ball hits the right quarter of the paddle, make it move right
				ball.speedX = ball.speedX < 0 ? -5 : 5;
			} else {
				// If the ball hits the middle half of the paddle, make it move straight up
				ball.speedX = ball.speedX < 0 ? -5 : 5;
			}
			// Change the ball's direction based on its incoming direction
			ball.speedY = ball.speedY < 0 ? 10 : -10;
			// Play 'sekme' sound
			LK.getSound('sekme').play();
		}
		// Check collision with blocks
		for (var j = blocks.length - 1; j >= 0; j--) {
			var block = blocks[j];
			if (ball.intersects(block)) {
				block["break"]();
				ball.speedY *= -1;
				blocks.splice(j, 1);
				// Update score
				LK.setScore(LK.getScore() + 1);
				scoreTxt.setText(LK.getScore());
				// Play 'sekme' sound
				LK.getSound('sekme').play();
			}
		}
		// Remove ball if it goes below the paddle
		if (ball.y > 2732) {
			ball.destroy();
			balls.splice(i, 1);
		}
	}
	// Check if all blocks are destroyed
	if (blocks.length === 0) {
		// Increment the level
		level++;
		// Generate new blocks
		generateBlocks();
		// Display the current level
		displayLevel();
	}
	// Check for game over
	if (balls.length === 0) {
		LK.showGameOver();
	}
};
// Mouse move event to control paddle
game.move = function (x, y, obj) {
	game.mouseX = x;
};
game.down = function (x, y, obj) {
	var currentTime = Date.now();
	if (game.lastClickTime && currentTime - game.lastClickTime < 1000) {
		game.clickCount = (game.clickCount || 0) + 1;
	} else {
		game.clickCount = 1;
	}
	game.lastClickTime = currentTime;
	if (game.clickCount === 2) {
		for (var i = blocks.length - 1; i >= 0; i--) {
			blocks[i]["break"]();
			blocks.splice(i, 1);
		}
		game.clickCount = 0;
	}
	// If the ball is on the paddle, launch the ball when the game is clicked
	if (ballOnPaddle) {
		ballOnPaddle = false;
	}
	// Toggle the value of paddleFollowsBall on click only if mouse position is on the paddle
	if (x >= paddle.x - paddle.width / 2 && x <= paddle.x + paddle.width / 2 && y >= paddle.y - paddle.height / 2 && y <= paddle.y + paddle.height / 2) {
		paddleFollowsBall = !paddleFollowsBall;
	}
};