/**** 
* Classes
****/ 
// Class for the different ages
var Age = Container.expand(function (age) {
	var self = Container.call(this);
	self.age = age;
	self.resources = [];
	self.buildings = [];
	self.characters = [];
	self.weapons = [];
	self.score = 0;
	self.addResource = function (resource) {
		self.resources.push(resource);
	};
	self.addBuilding = function (building) {
		self.buildings.push(building);
	};
	self.addCharacter = function (character) {
		self.characters.push(character);
	};
	self.addWeapon = function (weapon) {
		self.weapons.push(weapon);
	};
	self.updateScore = function (points) {
		self.score += points;
		if (self.score >= 5000) {
			self.nextAge();
		}
	};
	self.nextAge = function () {
		// Logic for transitioning to the next age
	};
	self.update = function () {
		// Logic for updating the age
	};
});
// Class for the Balls in the chain
var Ball = Container.expand(function (color) {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball_' + color, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.color = color;
	self.update = function () {
		// Logic for ball movement along the path
	};
});
// Class for the Ball Chain
var BallChain = Container.expand(function () {
	var self = Container.call(this);
	self.balls = [];
	self.addBall = function (ball) {
		self.balls.push(ball);
		self.addChild(ball);
	};
	self.update = function () {
		// Logic for moving the chain and checking for matches
	};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the Frog character
var Frog = Container.expand(function () {
	var self = Container.call(this);
	var frogGraphics = self.attachAsset('frog', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.shootBall = function (color) {
		var ball = new Ball(color);
		ball.x = self.x;
		ball.y = self.y;
		return ball;
	};
});
var Masa = Container.expand(function () {
	var self = Container.call(this);
	var masaGraphics = self.attachAsset('masa', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5; // Speed of movement
	self.direction = 1; // 1 for right, -1 for left
	self.update = function () {
		// Follow the top masa's x-coordinate
		self.x = topMasa.x;
		// Reverse direction if masa hits the screen edges
		if (self.x <= self.width / 2 || self.x >= 2048 - self.width / 2) {
			self.direction *= -1;
		}
		// Check for collision with redmarble on the front side only
		if (self.lastX <= redMarble.x && self.x > redMarble.x && self.intersects(redMarble) && redMarble.y > self.y) {
			// Collision detected on the front side, handle logic here
			console.log("Masa caught the redmarble on the front side!");
			redMarble.speedY *= -1; // Reverse Y direction of redmarble
			redMarble.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		}
		// Update lastX
		self.lastX = self.x;
		// Laser shooting logic
		if (self.enableLaser) {
			// Logic to shoot laser
			// For simplicity, assume laser is a straight line from masa
			var laser = new Graphics();
			laser.lineStyle(5, 0xff0000, 1);
			laser.moveTo(self.x, self.y);
			laser.lineTo(self.x, 0);
			game.addChild(laser);
			// Check for collisions with assets
			for (var i = assets.length - 1; i >= 0; i--) {
				var asset = assets[i];
				if (asset.x >= self.x - 5 && asset.x <= self.x + 5 && asset.y < self.y) {
					asset.destroy();
					assets.splice(i, 1);
				}
			}
			// Remove laser after a short duration
			LK.setTimeout(function () {
				laser.destroy();
			}, 100);
		}
	};
	// Initialize lastX
	self.lastX = self.x;
});
// Class for the RedMarble
var RedMarble = Container.expand(function () {
	var self = Container.call(this);
	var redMarbleGraphics = self.attachAsset('redmarble', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedX = 5; // Horizontal speed of the red marble
	self.speedY = -10; // Vertical speed of the red marble
	self.update = function () {
		self.y += self.speedY;
		self.x += self.speedX;
		// Check for collision with topMasa
		if (self.lastY > topMasa.y && self.y <= topMasa.y && self.intersects(topMasa)) {
			LK.getSound('x').play();
			self.speedY *= -1; // Reverse Y direction
		} else if (self.y <= 0) {
			// Allow redMarble to pass through the top area
			self.destroy();
			redMarbles.splice(redMarbles.indexOf(self), 1);
			// Decrease a heart
			if (lives > 0) {
				lives--;
				var heartToRemove = hearts.shift(); // Remove the top heart
				heartToRemove.destroy();
			}
			// Check if all hearts are lost
			if (lives === 0) {
				// Trigger game over screen
				LK.showGameOver();
			}
		}
		// Check for collision with side walls
		if (self.x <= 0 || self.x >= 2048) {
			LK.getSound('x').play();
			self.speedX *= -1; // Reverse X direction
		}
		// Check if red marble is off-screen
		if (self.y < -50) {
			self.destroy();
		}
		// Check for collision with masa while falling
		if (self.lastY <= masa.y && self.y > masa.y && self.intersects(masa)) {
			LK.getSound('x').play();
			self.speedY *= -1; // Reverse Y direction
			self.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		} else if (self.lastY <= topMasa.y && self.y > topMasa.y && self.intersects(topMasa) && self.x < topMasa.x) {
			LK.getSound('x').play();
			self.speedY *= -1; // Reverse Y direction
			self.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		}
		self.lastY = self.y;
	};
});
var TopMasaAI = Container.expand(function () {
	var self = Container.call(this);
	var masaGraphics = self.attachAsset('masa', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	masaGraphics.rotation = Math.PI; // Rotate the masa by 180 degrees to make it upside down
	self.speed = 5; // Speed of movement
	self.direction = 1; // 1 for right, -1 for left
	self.update = function () {
		// Move masa left and right autonomously
		self.x = masa.x; // Follow the bottom masa's x-coordinate
		// Check for collision with redmarble on both sides
		if (self.lastY <= redMarble.y && self.y > redMarble.y && self.intersects(redMarble)) {
			// Collision detected, handle logic here
			console.log("TopMasaAI caught the redmarble!");
			redMarble.speedY *= -1; // Reverse Y direction of redmarble
			redMarble.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		}
		// Update lastX
		self.lastX = self.x;
	};
	// Initialize lastX
	self.lastX = self.x;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize game elements 
var topMasa = new TopMasaAI();
topMasa.x = 2048 / 2; // Center the masa horizontally
topMasa.y = 150; // Move masa further down by 100 pixels
topMasa.speed = 5; // Set speed for automatic movement
topMasa.direction = 1; // Set initial direction for automatic movement
game.addChild(topMasa);
var fallingAssets = ['masa2', 'ray'];
var fallingObjects = [];
var lives = 3;
var hearts = [];
// Add heart images to the top of the screen
for (var i = 0; i < lives; i++) {
	var heart = LK.getAsset('kalp', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	heart.x = 50 + i * (heart.width + 20); // Position hearts with spacing
	heart.y = 50; // Position hearts at the top left corner
	game.addChild(heart);
	hearts.push(heart);
}
var redMarbles = [];
var redMarble = new RedMarble(); // Initialize redMarble in the global scope
var assets = [];
// Initialize game elements 
var masa = new Masa();
masa.x = 2048 / 2; // Center the masa horizontally
masa.y = 2732 - 50; // Position masa at the bottom wall
masa.speed = 0; // Disable automatic movement
game.addChild(masa);
var currentAge = new Age('Stone Age');
game.addChild(currentAge);
var frog = new Frog();
game.addChild(frog);
// Fill the screen with assets arranged in a grid pattern
var assetIds = ['bilye1', 'bilye10', 'bilye11', 'bilye12', 'bilye14', 'bilye15', 'bilye16', 'bilye3', 'bilye4', 'bilye5', 'bilye6', 'bilye7', 'bilye9', 'biyye8', 'f', 'red'];
var startX = 50; // Starting X position
var startY = 270; // Move assets further down by 100 pixels
var spacingX = 20; // Horizontal spacing between assets
var spacingY = 20; // Vertical spacing between assets
var numRows = Math.floor((2732 - startY) / (100 + spacingY)) + 2; // Calculate number of rows based on screen height and add two more rows
var numCols = Math.floor((2048 - startX) / (100 + spacingX)); // Calculate number of columns based on screen width
for (var row = 1; row < numRows - 5; row++) {
	// Start from the second row to remove the topmost row
	// Adjusted to add one more row at the end
	// Adjusted to remove the bottom 3 rows
	for (var col = 0; col < numCols - 2; col++) {
		// Restore the first column and remove the rightmost column
		var index = (row * numCols + col) % assetIds.length; // Loop through assetIds
		var id = assetIds[index];
		var asset = LK.getAsset(id, {
			anchorX: 0.5,
			anchorY: 0.5
		});
		asset.x = (2048 - (numCols - 2) * (asset.width + spacingX)) / 2 + col * (asset.width + spacingX); // Center assets horizontally
		asset.y = startY + row * (asset.height + spacingY); // Align assets vertically
		game.addChild(asset);
		assets.push(asset);
	}
}
// Game update loop
game.update = function () {
	currentAge.update();
	// Additional game logic
	// Update redmarble positions and check for collisions
	for (var i = redMarbles.length - 1; i >= 0; i--) {
		var redMarble = redMarbles[i];
		redMarble.update();
		// Check if redmarble is off-screen
		if (redMarble.y > 2732) {
			redMarble.destroy();
			redMarbles.splice(i, 1);
			// Decrease a heart
			if (lives > 0) {
				lives--;
				var heartToRemove = hearts.shift(); // Remove the top heart
				heartToRemove.destroy();
			}
			// Check if all hearts are lost
			if (lives === 0 || assets.length === 0 && fallingObjects.length === 0) {
				// Trigger game over screen
				LK.showGameOver();
				// Display final score
				var finalScore = LK.getScore();
				var scoreText = new Text2('Final Score: ' + finalScore, {
					size: 150,
					fill: 0xFFFFFF
				});
				scoreText.anchor.set(0.5, 0.5);
				LK.gui.center.addChild(scoreText);
				// Reset game state
				lives = 3;
				hearts = [];
				for (var h = 0; h < lives; h++) {
					var newHeart = LK.getAsset('kalp', {
						anchorX: 0.5,
						anchorY: 0.5
					});
					newHeart.x = 100 + h * (newHeart.width + 20);
					newHeart.y = 50;
					game.addChild(newHeart);
					hearts.push(newHeart);
				}
			}
			continue;
		}
		for (var j = assets.length - 1; j >= 0; j--) {
			var asset = assets[j];
			if (redMarble.intersects(asset)) {
				// Removed masa size increase when intersecting with assets
				asset.destroy();
				assets.splice(j, 1);
				// Reverse direction of redmarble on collision
				redMarble.speedY *= -1;
				LK.getSound('x').play();
				redMarble.speedX *= -1;
				// Update score based on collision
				LK.setScore(LK.getScore() + 100);
			}
		}
	}
	// Handle falling assets
	for (var f = fallingObjects.length - 1; f >= 0; f--) {
		var fallingObject = fallingObjects[f];
		fallingObject.y += 5; // Falling speed
		// Check if fallingObject is off-screen
		if (fallingObject.y > 2732) {
			fallingObject.destroy();
			fallingObjects.splice(f, 1);
			continue;
		}
		// Check for collision with redmarble
		if (redMarble && redMarble.intersects(fallingObject)) {
			if (fallingObject.assetId === 'masa2') {
				if (fallingObject.lastY <= masa.y && fallingObject.y > masa.y && fallingObject.intersects(masa)) {
					if (lives < 3) {
						lives++;
						var newHeart = LK.getAsset('kalp', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						newHeart.x = 100 + (lives - 1) * (newHeart.width + 20);
						newHeart.y = 50;
						game.addChild(newHeart);
						hearts.push(newHeart);
					}
					// Increase masa size when masa catches a falling masa2
					masa.scaleX *= 1.1;
					masa.scaleY *= 1.1;
				}
			} else if (fallingObject.assetId === 'lap') {
				// Check for intersection with red marble
				if (fallingObject.lastWasIntersectingRedMarble === false && fallingObject.intersects(redMarble)) {
					fallingObject.speedY = 10; // Increase falling speed
				}
				fallingObject.lastWasIntersectingRedMarble = fallingObject.intersects(redMarble);
				if (fallingObject.lastWasIntersectingMasa === false && fallingObject.intersects(masa)) {
					// Increase masa size only when masa catches a falling lap
					masa.scaleX *= 1.1;
					masa.scaleY *= 1.1;
					fallingObject.destroy(); // Ensure lap is destroyed after being caught
					fallingObjects.splice(f, 1);
				}
				fallingObject.lastWasIntersectingMasa = fallingObject.intersects(masa);
			} else if (fallingObject.assetId === 'ray') {
				if (fallingObject.lastY <= masa.y && fallingObject.y > masa.y && fallingObject.intersects(masa)) {
					// Enable shooting two rays from the sides of the table
					var leftRay = new Graphics();
					leftRay.lineStyle(5, 0xff0000, 1);
					leftRay.moveTo(masa.x - masa.width / 2, masa.y);
					leftRay.lineTo(masa.x - masa.width / 2, 0);
					game.addChild(leftRay);
					var rightRay = new Graphics();
					rightRay.lineStyle(5, 0xff0000, 1);
					rightRay.moveTo(masa.x + masa.width / 2, masa.y);
					rightRay.lineTo(masa.x + masa.width / 2, 0);
					game.addChild(rightRay);
					// Remove rays after a short duration
					LK.setTimeout(function () {
						leftRay.destroy();
						rightRay.destroy();
					}, 1000);
				}
				// Enable laser shooting for 10 seconds
				masa.enableLaser = true;
				LK.setTimeout(function () {
					masa.enableLaser = false;
				}, 10000);
			}
			fallingObject.destroy();
			fallingObjects.splice(f, 1);
		}
	}
	// Randomly add new falling assets
	if (LK.ticks % 180 === 0) {
		// Removed the falling behavior of the 'lap' object
		// Every 3 seconds
		var randomIndex = Math.floor(Math.random() * assets.length);
		if (randomIndex !== undefined && assets[randomIndex]) {
			if (randomIndex !== undefined && assets[randomIndex]) {
				var randomAsset = assets[randomIndex];
				if (randomAsset && randomAsset.assetId) {
					var newFallingObject = LK.getAsset(randomAsset.assetId, {
						anchorX: 0.5,
						anchorY: 0.5
					});
					if (newFallingObject) {
						newFallingObject.x = randomAsset.x;
						newFallingObject.y = randomAsset.y;
						newFallingObject.assetId = randomAsset.assetId;
						game.addChild(newFallingObject);
						fallingObjects.push(newFallingObject);
					}
				}
			}
		}
		if (newFallingObject) {
			newFallingObject.x = randomAsset.x;
			newFallingObject.y = randomAsset.y;
			newFallingObject.assetId = randomAsset.assetId;
			game.addChild(newFallingObject);
			fallingObjects.push(newFallingObject);
		}
	}
	// Check if the bottom row is empty and move assets down
	if (assets.length > 0 && assets[0].y > 2732) {
		for (var k = 0; k < assets.length; k++) {
			assets[k].y += 100 + spacingY;
		}
	}
};
// Event listeners for shooting balls
game.down = function (x, y, obj) {
	masa.x = x; // Move masa to the clicked x position
	// Shoot a redmarble from the masa only if there are no active redmarbles and the masa is not the topMasa
	if (redMarbles.length === 0 && masa !== topMasa) {
		var redMarble = new RedMarble();
		redMarble.x = masa.x;
		redMarble.y = masa.y - masa.height / 2; // Start above the masa
		redMarbles.push(redMarble);
		game.addChild(redMarble);
	}
};
game.move = function (x, y, obj) {
	masa.x = x; // Update masa's x position with mouse movement
}; /**** 
* Classes
****/ 
// Class for the different ages
var Age = Container.expand(function (age) {
	var self = Container.call(this);
	self.age = age;
	self.resources = [];
	self.buildings = [];
	self.characters = [];
	self.weapons = [];
	self.score = 0;
	self.addResource = function (resource) {
		self.resources.push(resource);
	};
	self.addBuilding = function (building) {
		self.buildings.push(building);
	};
	self.addCharacter = function (character) {
		self.characters.push(character);
	};
	self.addWeapon = function (weapon) {
		self.weapons.push(weapon);
	};
	self.updateScore = function (points) {
		self.score += points;
		if (self.score >= 5000) {
			self.nextAge();
		}
	};
	self.nextAge = function () {
		// Logic for transitioning to the next age
	};
	self.update = function () {
		// Logic for updating the age
	};
});
// Class for the Balls in the chain
var Ball = Container.expand(function (color) {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('ball_' + color, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.color = color;
	self.update = function () {
		// Logic for ball movement along the path
	};
});
// Class for the Ball Chain
var BallChain = Container.expand(function () {
	var self = Container.call(this);
	self.balls = [];
	self.addBall = function (ball) {
		self.balls.push(ball);
		self.addChild(ball);
	};
	self.update = function () {
		// Logic for moving the chain and checking for matches
	};
});
//<Assets used in the game will automatically appear here>
//<Write imports for supported plugins here>
// Class for the Frog character
var Frog = Container.expand(function () {
	var self = Container.call(this);
	var frogGraphics = self.attachAsset('frog', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.shootBall = function (color) {
		var ball = new Ball(color);
		ball.x = self.x;
		ball.y = self.y;
		return ball;
	};
});
var Masa = Container.expand(function () {
	var self = Container.call(this);
	var masaGraphics = self.attachAsset('masa', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5; // Speed of movement
	self.direction = 1; // 1 for right, -1 for left
	self.update = function () {
		// Follow the top masa's x-coordinate
		self.x = topMasa.x;
		// Reverse direction if masa hits the screen edges
		if (self.x <= self.width / 2 || self.x >= 2048 - self.width / 2) {
			self.direction *= -1;
		}
		// Check for collision with redmarble on the front side only
		if (self.lastX <= redMarble.x && self.x > redMarble.x && self.intersects(redMarble) && redMarble.y > self.y) {
			// Collision detected on the front side, handle logic here
			console.log("Masa caught the redmarble on the front side!");
			redMarble.speedY *= -1; // Reverse Y direction of redmarble
			redMarble.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		}
		// Update lastX
		self.lastX = self.x;
		// Laser shooting logic
		if (self.enableLaser) {
			// Logic to shoot laser
			// For simplicity, assume laser is a straight line from masa
			var laser = new Graphics();
			laser.lineStyle(5, 0xff0000, 1);
			laser.moveTo(self.x, self.y);
			laser.lineTo(self.x, 0);
			game.addChild(laser);
			// Check for collisions with assets
			for (var i = assets.length - 1; i >= 0; i--) {
				var asset = assets[i];
				if (asset.x >= self.x - 5 && asset.x <= self.x + 5 && asset.y < self.y) {
					asset.destroy();
					assets.splice(i, 1);
				}
			}
			// Remove laser after a short duration
			LK.setTimeout(function () {
				laser.destroy();
			}, 100);
		}
	};
	// Initialize lastX
	self.lastX = self.x;
});
// Class for the RedMarble
var RedMarble = Container.expand(function () {
	var self = Container.call(this);
	var redMarbleGraphics = self.attachAsset('redmarble', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedX = 5; // Horizontal speed of the red marble
	self.speedY = -10; // Vertical speed of the red marble
	self.update = function () {
		self.y += self.speedY;
		self.x += self.speedX;
		// Check for collision with topMasa
		if (self.lastY > topMasa.y && self.y <= topMasa.y && self.intersects(topMasa)) {
			LK.getSound('x').play();
			self.speedY *= -1; // Reverse Y direction
		} else if (self.y <= 0) {
			// Allow redMarble to pass through the top area
			self.destroy();
			redMarbles.splice(redMarbles.indexOf(self), 1);
			// Decrease a heart
			if (lives > 0) {
				lives--;
				var heartToRemove = hearts.shift(); // Remove the top heart
				heartToRemove.destroy();
			}
			// Check if all hearts are lost
			if (lives === 0) {
				// Trigger game over screen
				LK.showGameOver();
			}
		}
		// Check for collision with side walls
		if (self.x <= 0 || self.x >= 2048) {
			LK.getSound('x').play();
			self.speedX *= -1; // Reverse X direction
		}
		// Check if red marble is off-screen
		if (self.y < -50) {
			self.destroy();
		}
		// Check for collision with masa while falling
		if (self.lastY <= masa.y && self.y > masa.y && self.intersects(masa)) {
			LK.getSound('x').play();
			self.speedY *= -1; // Reverse Y direction
			self.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		} else if (self.lastY <= topMasa.y && self.y > topMasa.y && self.intersects(topMasa) && self.x < topMasa.x) {
			LK.getSound('x').play();
			self.speedY *= -1; // Reverse Y direction
			self.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		}
		self.lastY = self.y;
	};
});
var TopMasaAI = Container.expand(function () {
	var self = Container.call(this);
	var masaGraphics = self.attachAsset('masa', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	masaGraphics.rotation = Math.PI; // Rotate the masa by 180 degrees to make it upside down
	self.speed = 5; // Speed of movement
	self.direction = 1; // 1 for right, -1 for left
	self.update = function () {
		// Move masa left and right autonomously
		self.x = masa.x; // Follow the bottom masa's x-coordinate
		// Check for collision with redmarble on both sides
		if (self.lastY <= redMarble.y && self.y > redMarble.y && self.intersects(redMarble)) {
			// Collision detected, handle logic here
			console.log("TopMasaAI caught the redmarble!");
			redMarble.speedY *= -1; // Reverse Y direction of redmarble
			redMarble.speedX = (Math.random() - 0.5) * 10; // Randomize X direction slightly
		}
		// Update lastX
		self.lastX = self.x;
	};
	// Initialize lastX
	self.lastX = self.x;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background 
});
/**** 
* Game Code
****/ 
// Initialize game elements 
var topMasa = new TopMasaAI();
topMasa.x = 2048 / 2; // Center the masa horizontally
topMasa.y = 150; // Move masa further down by 100 pixels
topMasa.speed = 5; // Set speed for automatic movement
topMasa.direction = 1; // Set initial direction for automatic movement
game.addChild(topMasa);
var fallingAssets = ['masa2', 'ray'];
var fallingObjects = [];
var lives = 3;
var hearts = [];
// Add heart images to the top of the screen
for (var i = 0; i < lives; i++) {
	var heart = LK.getAsset('kalp', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	heart.x = 50 + i * (heart.width + 20); // Position hearts with spacing
	heart.y = 50; // Position hearts at the top left corner
	game.addChild(heart);
	hearts.push(heart);
}
var redMarbles = [];
var redMarble = new RedMarble(); // Initialize redMarble in the global scope
var assets = [];
// Initialize game elements 
var masa = new Masa();
masa.x = 2048 / 2; // Center the masa horizontally
masa.y = 2732 - 50; // Position masa at the bottom wall
masa.speed = 0; // Disable automatic movement
game.addChild(masa);
var currentAge = new Age('Stone Age');
game.addChild(currentAge);
var frog = new Frog();
game.addChild(frog);
// Fill the screen with assets arranged in a grid pattern
var assetIds = ['bilye1', 'bilye10', 'bilye11', 'bilye12', 'bilye14', 'bilye15', 'bilye16', 'bilye3', 'bilye4', 'bilye5', 'bilye6', 'bilye7', 'bilye9', 'biyye8', 'f', 'red'];
var startX = 50; // Starting X position
var startY = 270; // Move assets further down by 100 pixels
var spacingX = 20; // Horizontal spacing between assets
var spacingY = 20; // Vertical spacing between assets
var numRows = Math.floor((2732 - startY) / (100 + spacingY)) + 2; // Calculate number of rows based on screen height and add two more rows
var numCols = Math.floor((2048 - startX) / (100 + spacingX)); // Calculate number of columns based on screen width
for (var row = 1; row < numRows - 5; row++) {
	// Start from the second row to remove the topmost row
	// Adjusted to add one more row at the end
	// Adjusted to remove the bottom 3 rows
	for (var col = 0; col < numCols - 2; col++) {
		// Restore the first column and remove the rightmost column
		var index = (row * numCols + col) % assetIds.length; // Loop through assetIds
		var id = assetIds[index];
		var asset = LK.getAsset(id, {
			anchorX: 0.5,
			anchorY: 0.5
		});
		asset.x = (2048 - (numCols - 2) * (asset.width + spacingX)) / 2 + col * (asset.width + spacingX); // Center assets horizontally
		asset.y = startY + row * (asset.height + spacingY); // Align assets vertically
		game.addChild(asset);
		assets.push(asset);
	}
}
// Game update loop
game.update = function () {
	currentAge.update();
	// Additional game logic
	// Update redmarble positions and check for collisions
	for (var i = redMarbles.length - 1; i >= 0; i--) {
		var redMarble = redMarbles[i];
		redMarble.update();
		// Check if redmarble is off-screen
		if (redMarble.y > 2732) {
			redMarble.destroy();
			redMarbles.splice(i, 1);
			// Decrease a heart
			if (lives > 0) {
				lives--;
				var heartToRemove = hearts.shift(); // Remove the top heart
				heartToRemove.destroy();
			}
			// Check if all hearts are lost
			if (lives === 0 || assets.length === 0 && fallingObjects.length === 0) {
				// Trigger game over screen
				LK.showGameOver();
				// Display final score
				var finalScore = LK.getScore();
				var scoreText = new Text2('Final Score: ' + finalScore, {
					size: 150,
					fill: 0xFFFFFF
				});
				scoreText.anchor.set(0.5, 0.5);
				LK.gui.center.addChild(scoreText);
				// Reset game state
				lives = 3;
				hearts = [];
				for (var h = 0; h < lives; h++) {
					var newHeart = LK.getAsset('kalp', {
						anchorX: 0.5,
						anchorY: 0.5
					});
					newHeart.x = 100 + h * (newHeart.width + 20);
					newHeart.y = 50;
					game.addChild(newHeart);
					hearts.push(newHeart);
				}
			}
			continue;
		}
		for (var j = assets.length - 1; j >= 0; j--) {
			var asset = assets[j];
			if (redMarble.intersects(asset)) {
				// Removed masa size increase when intersecting with assets
				asset.destroy();
				assets.splice(j, 1);
				// Reverse direction of redmarble on collision
				redMarble.speedY *= -1;
				LK.getSound('x').play();
				redMarble.speedX *= -1;
				// Update score based on collision
				LK.setScore(LK.getScore() + 100);
			}
		}
	}
	// Handle falling assets
	for (var f = fallingObjects.length - 1; f >= 0; f--) {
		var fallingObject = fallingObjects[f];
		fallingObject.y += 5; // Falling speed
		// Check if fallingObject is off-screen
		if (fallingObject.y > 2732) {
			fallingObject.destroy();
			fallingObjects.splice(f, 1);
			continue;
		}
		// Check for collision with redmarble
		if (redMarble && redMarble.intersects(fallingObject)) {
			if (fallingObject.assetId === 'masa2') {
				if (fallingObject.lastY <= masa.y && fallingObject.y > masa.y && fallingObject.intersects(masa)) {
					if (lives < 3) {
						lives++;
						var newHeart = LK.getAsset('kalp', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						newHeart.x = 100 + (lives - 1) * (newHeart.width + 20);
						newHeart.y = 50;
						game.addChild(newHeart);
						hearts.push(newHeart);
					}
					// Increase masa size when masa catches a falling masa2
					masa.scaleX *= 1.1;
					masa.scaleY *= 1.1;
				}
			} else if (fallingObject.assetId === 'lap') {
				// Check for intersection with red marble
				if (fallingObject.lastWasIntersectingRedMarble === false && fallingObject.intersects(redMarble)) {
					fallingObject.speedY = 10; // Increase falling speed
				}
				fallingObject.lastWasIntersectingRedMarble = fallingObject.intersects(redMarble);
				if (fallingObject.lastWasIntersectingMasa === false && fallingObject.intersects(masa)) {
					// Increase masa size only when masa catches a falling lap
					masa.scaleX *= 1.1;
					masa.scaleY *= 1.1;
					fallingObject.destroy(); // Ensure lap is destroyed after being caught
					fallingObjects.splice(f, 1);
				}
				fallingObject.lastWasIntersectingMasa = fallingObject.intersects(masa);
			} else if (fallingObject.assetId === 'ray') {
				if (fallingObject.lastY <= masa.y && fallingObject.y > masa.y && fallingObject.intersects(masa)) {
					// Enable shooting two rays from the sides of the table
					var leftRay = new Graphics();
					leftRay.lineStyle(5, 0xff0000, 1);
					leftRay.moveTo(masa.x - masa.width / 2, masa.y);
					leftRay.lineTo(masa.x - masa.width / 2, 0);
					game.addChild(leftRay);
					var rightRay = new Graphics();
					rightRay.lineStyle(5, 0xff0000, 1);
					rightRay.moveTo(masa.x + masa.width / 2, masa.y);
					rightRay.lineTo(masa.x + masa.width / 2, 0);
					game.addChild(rightRay);
					// Remove rays after a short duration
					LK.setTimeout(function () {
						leftRay.destroy();
						rightRay.destroy();
					}, 1000);
				}
				// Enable laser shooting for 10 seconds
				masa.enableLaser = true;
				LK.setTimeout(function () {
					masa.enableLaser = false;
				}, 10000);
			}
			fallingObject.destroy();
			fallingObjects.splice(f, 1);
		}
	}
	// Randomly add new falling assets
	if (LK.ticks % 180 === 0) {
		// Removed the falling behavior of the 'lap' object
		// Every 3 seconds
		var randomIndex = Math.floor(Math.random() * assets.length);
		if (randomIndex !== undefined && assets[randomIndex]) {
			if (randomIndex !== undefined && assets[randomIndex]) {
				var randomAsset = assets[randomIndex];
				if (randomAsset && randomAsset.assetId) {
					var newFallingObject = LK.getAsset(randomAsset.assetId, {
						anchorX: 0.5,
						anchorY: 0.5
					});
					if (newFallingObject) {
						newFallingObject.x = randomAsset.x;
						newFallingObject.y = randomAsset.y;
						newFallingObject.assetId = randomAsset.assetId;
						game.addChild(newFallingObject);
						fallingObjects.push(newFallingObject);
					}
				}
			}
		}
		if (newFallingObject) {
			newFallingObject.x = randomAsset.x;
			newFallingObject.y = randomAsset.y;
			newFallingObject.assetId = randomAsset.assetId;
			game.addChild(newFallingObject);
			fallingObjects.push(newFallingObject);
		}
	}
	// Check if the bottom row is empty and move assets down
	if (assets.length > 0 && assets[0].y > 2732) {
		for (var k = 0; k < assets.length; k++) {
			assets[k].y += 100 + spacingY;
		}
	}
};
// Event listeners for shooting balls
game.down = function (x, y, obj) {
	masa.x = x; // Move masa to the clicked x position
	// Shoot a redmarble from the masa only if there are no active redmarbles and the masa is not the topMasa
	if (redMarbles.length === 0 && masa !== topMasa) {
		var redMarble = new RedMarble();
		redMarble.x = masa.x;
		redMarble.y = masa.y - masa.height / 2; // Start above the masa
		redMarbles.push(redMarble);
		game.addChild(redMarble);
	}
};
game.move = function (x, y, obj) {
	masa.x = x; // Update masa's x position with mouse movement
};
 RENKLİ CAM MİSKET. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 RENKLİ CAM MİSKET. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 COLORED GLASS MARBLE. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 COLORED GLASS MARBLE. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 COLORED GLASS MARBLE. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 COLORED GLASS MARBLE. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 COLORED GLASS MARBLE. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 COLORED GLASS MARBLE. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 red marble. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 long thick horizontal bar. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
 heart. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.