/**** 
* Classes
****/ 
// Coin class
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = gameStartDelay ? 0 : 5;
	self.zIndex = 15;
	self.update = function () {
		if (gameStartDelay) {
			self.velocity = 0;
		} else if (self.velocity === 0) {
			self.velocity = 5;
		}
		self.x -= self.velocity;
	};
});
// NEW: EndlessBG3 class using the same scroll system as background2
var EndlessBG3 = Container.expand(function () {
	var self = Container.call(this);
	var bg1 = LK.getAsset('endlessbg3', {
		anchorX: 0,
		anchorY: 0
	});
	bg1.x = 0;
	bg1.y = 0;
	self.addChild(bg1);
	var bg2 = LK.getAsset('endlessbg3', {
		anchorX: 1,
		anchorY: 0
	});
	bg2.scaleX = -1;
	bg2.x = bg1.width;
	bg2.y = 0;
	self.addChild(bg2);
	self.speed = 2;
	self.update = function () {
		bg1.x -= self.speed;
		bg2.x -= self.speed;
		if (bg1.x + bg1.width <= 0) {
			bg1.x = bg2.x + bg2.width;
		}
		if (bg2.x + bg2.width <= 0) {
			bg2.x = bg1.x + bg1.width;
		}
	};
});
// Enemy class
var Enemy = Container.expand(function () {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('enemy', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	self.zIndex = 10;
	self.canDamage = true;
	self.lastY = 0;
	self.lastWasMushroomOnGround = true;
	self.update = function () {
		self.lastY = self.y;
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
		}
		self.x -= self.speed;
		if (self.x < -50) {
			self.destroy();
		}
	};
});
// FlyinEnemy class
var FlyinEnemy = Container.expand(function () {
	var self = Container.call(this);
	var flyinEnemyGraphics = self.attachAsset('flyin_enemy', {
		anchorX: 0.5,
		anchorY: 0
	});
	self.speed = gameStartDelay ? 0 : 5;
	self.zIndex = 10;
	self.canDamage = true;
	self.homing = false;
	self.vx = 0;
	self.vy = 0;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
			self.vx = 0;
			self.vy = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
			if (self.homing && self.origVx !== undefined && self.origVy !== undefined) {
				self.vx = self.origVx;
				self.vy = self.origVy;
			}
		}
		if (!self.homing) {
			self.x -= self.speed;
			if (self.x < -50) {
				self.destroy();
			}
		} else {
			if (gameStartDelay) {
				// Store original velocities if not already stored
				if (self.origVx === undefined && self.origVy === undefined) {
					self.origVx = self.vx;
					self.origVy = self.vy;
				}
			}
			self.x += self.vx;
			self.y += self.vy;
			if (self.x < -200 || self.x > 2248 || self.y < -200 || self.y > 2932) {
				self.destroy();
			}
		}
	};
});
// Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	self.jumpHeight = 30;
	self.isJumping = false;
	self.velocityY = 0;
	self.isFalling = false;
	self.fallSpeed = 0;
	self.fallAcceleration = gameStartDelay ? 0 : 0.7;
	self.fallTargetY = 2732 / 2 - 7;
	// Karakterin diğer öğelerin üzerinde görünmesi için yüksek z-index
	self.zIndex = 20;
	self.hearts = typeof remainingHearts !== 'undefined' ? remainingHearts : 3;
	self.update = function () {
		self.prevY = self.y;
		if (gameStartDelay) {
			self.fallAcceleration = 0;
		} else if (self.fallAcceleration === 0) {
			self.fallAcceleration = 0.7;
		}
		if (self.isFalling && !gameStartDelay) {
			self.y += self.fallSpeed;
			self.fallSpeed += self.fallAcceleration;
			if (self.y >= self.fallTargetY) {
				self.y = self.fallTargetY;
				self.isFalling = false;
				self.fallSpeed = 0;
				game.weaponEnabled = true;
			}
		}
		self.prevY = self.y;
		if (self.isJumping && !gameStartDelay) {
			self.y += self.velocityY;
			self.velocityY += 0.7;
			if (self.y >= 2732 / 2 - 3) {
				self.y = 2732 / 2 - 9;
				self.isJumping = false;
				self.velocityY = 0;
			}
		}
		// Check for tube teleportation
		for (var i = 0; i < game.children.length; i++) {
			var child = game.children[i];
			// Check if player enters first tube (tup_1)
			if (child instanceof Tube && self.intersects(child) && !self.teleporting) {
				self.teleporting = true;
				// Find second tube (tube_2) to teleport to
				for (var j = 0; j < game.children.length; j++) {
					var target = game.children[j];
					if (target instanceof Tube2) {
						// Teleport to second tube
						self.x = target.x;
						self.y = target.y;
						// Create teleport particle effect
						var particleEffect = LK.getAsset('particle', {
							anchorX: 0.5,
							anchorY: 0.5,
							color: 0x00ff00
						});
						particleEffect.x = self.x;
						particleEffect.y = self.y;
						particleEffect.zIndex = 300;
						game.addChild(particleEffect);
						LK.setTimeout(function () {
							particleEffect.destroy();
						}, 150);
						// Reset teleporting flag after a short delay
						LK.setTimeout(function () {
							self.teleporting = false;
						}, 500);
						break;
					}
				}
			}
		}
	};
	self.invulnerable = false;
	self.hit = function () {
		if (!self.invulnerable) {
			self.loseHeart();
			self.invulnerable = true;
			LK.setTimeout(function () {
				self.invulnerable = false;
			}, 500);
		}
	};
	self.jump = function () {
		if (!self.isJumping) {
			self.isJumping = true;
			self.velocityY = -self.jumpHeight;
		}
	};
	self.loseHeart = function () {
		self.hearts--;
		playerDeaths++;
		if (playerDeaths === 1 && hearts[0]) {
			hearts[0].destroy();
		} else if (playerDeaths === 2 && hearts[1]) {
			hearts[1].destroy();
		} else if (playerDeaths === 3 && hearts[2]) {
			hearts[2].destroy();
		}
		remainingHearts = self.hearts;
		if (hearts.length === 0) {
			// Apply score multipliers based on active game states
			var finalScore = coinCounter;
			// Apply 2x multiplier if control is active
			if (game.controlActive) {
				finalScore *= 2;
			}
			// Apply 3x multiplier if yesil1 is active (has 3+ points)
			var yesil1Active = false;
			for (var i = 0; i < game.children.length; i++) {
				var child = game.children[i];
				if (child.points && child.points >= 3) {
					finalScore *= 3;
					break;
				}
			}
			// Make sure LK score system gets updated with the final multiplied score
			LK.setScore(finalScore);
			for (var i = enemies.length - 1; i >= 0; i--) {
				enemies[i].destroy();
				enemies.splice(i, 1);
			}
			for (var i = flyinEnemies.length - 1; i >= 0; i--) {
				flyinEnemies[i].destroy();
				flyinEnemies.splice(i, 1);
			}
			for (var i = zigzagEnemies.length - 1; i >= 0; i--) {
				zigzagEnemies[i].destroy();
				zigzagEnemies.splice(i, 1);
			}
			for (var i = trainEnemies.length - 1; i >= 0; i--) {
				trainEnemies[i].destroy();
				trainEnemies.splice(i, 1);
			}
			for (var i = coins.length - 1; i >= 0; i--) {
				coins[i].destroy();
				coins.splice(i, 1);
			}
			self.destroy();
			game.weaponEnabled = false;
		}
	};
});
// ScrollingBackground class
var ScrollingBackground = Container.expand(function () {
	var self = Container.call(this);
	self.bg1 = LK.getAsset('background', {
		anchorX: 0,
		anchorY: 0
	});
	self.bg1.x = 0;
	self.bg1.y = 0;
	self.addChild(self.bg1);
	self.bg2 = LK.getAsset('background', {
		anchorX: 1,
		anchorY: 0
	});
	self.bg2.scaleX = -1;
	self.bg2.x = self.bg1.width;
	self.bg2.y = 0;
	self.addChild(self.bg2);
	self.speed = gameStartDelay ? 0 : 2;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 2;
		}
		self.bg1.x -= self.speed;
		self.bg2.x -= self.speed;
		if (self.bg1.x + self.bg1.width <= 0) {
			self.bg1.x = self.bg2.x + self.bg2.width;
		}
		if (self.bg2.x + self.bg2.width <= 0) {
			self.bg2.x = self.bg1.x + self.bg1.width;
		}
	};
});
// ScrollingBackground2 class
var ScrollingBackground2 = Container.expand(function () {
	var self = Container.call(this);
	var bg1 = LK.getAsset('background2', {
		anchorX: 0,
		anchorY: 0
	});
	bg1.x = 0;
	bg1.y = 0;
	self.addChild(bg1);
	var bg2 = LK.getAsset('background2', {
		anchorX: 1,
		anchorY: 0
	});
	bg2.scaleX = -1;
	bg2.x = bg1.width;
	bg2.y = 0;
	self.addChild(bg2);
	self.speed = gameStartDelay ? 0 : 2;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 2;
		}
		bg1.x -= self.speed;
		bg2.x -= self.speed;
		if (bg1.x + bg1.width <= 0) {
			bg1.x = bg2.x + bg2.width;
		}
		if (bg2.x + bg2.width <= 0) {
			bg2.x = bg1.x + bg1.width;
		}
	};
});
// Tube class
var Tube = Container.expand(function () {
	var self = Container.call(this);
	var tubeGraphics = self.attachAsset('tup_1', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	// Tube'ların arka plan gibi gözükmesi için düşük z-index
	self.zIndex = 0;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
		}
		self.x -= self.speed;
		if (self.x < -50) {
			self.destroy();
		}
	};
});
// Tube2 class (yesil_1 3 puandan sonra spawn olacak ve ekranda görünecek)
var Tube2 = Container.expand(function () {
	var self = Container.call(this);
	var tubeGraphics = self.attachAsset('tube_2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	// Tube2'nin diğer öğelerin üzerinde görünmesi için orta seviyede bir z-index
	self.zIndex = 21;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
		}
		self.x -= self.speed;
		if (self.x < -self.width) {
			self.destroy();
		}
		// Check if player is intersecting with this tube
		if (player && player.intersects(self) && !player.teleporting) {
			player.teleporting = true;
			// Find the first tube to teleport back to
			for (var i = 0; i < game.children.length; i++) {
				var target = game.children[i];
				if (target instanceof Tube) {
					// Teleport to first tube
					player.x = target.x;
					player.y = target.y;
					// Create teleport particle effect
					var particleEffect = LK.getAsset('particle', {
						anchorX: 0.5,
						anchorY: 0.5,
						color: 0x00ff00
					});
					particleEffect.x = player.x;
					particleEffect.y = player.y;
					particleEffect.zIndex = 300;
					game.addChild(particleEffect);
					LK.setTimeout(function () {
						particleEffect.destroy();
					}, 150);
					// Reset teleporting flag after a short delay
					LK.setTimeout(function () {
						player.teleporting = false;
					}, 500);
					break;
				}
			}
		}
	};
});
// Weapon class (Weapon'ın da diğer ön plandaki öğeler arasında gözükmesi için yüksek z-index eklenmiştir)
var Weapon = Container.expand(function () {
	var self = Container.call(this);
	var weaponGraphics = self.attachAsset('weapon', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Weapon'ın arka planın üzerinde görünmesi için yüksek z-index
	self.zIndex = 30;
	self.speed = 40;
	self.update = function () {
		self.x += self.directionX * self.speed;
		self.y += self.directionY * self.speed;
		weaponGraphics.rotation += 0.3;
		if (Math.abs(game.down.x - self.x) < self.speed && Math.abs(game.down.y - self.y) < self.speed) {
			self.destroy();
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	width: 2048,
	height: 2732,
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
var control_2 = LK.getAsset('control_2', {
	anchorX: 0.5,
	anchorY: 0.5
});
/**** 
* Global Variables & Flags
****/ 
// Add a flag to prevent spawning and movement during the first 4 seconds
var gameStartDelay = true;
// Add intro image to center of screen
var introImage = LK.getAsset('intro', {
	anchorX: 0.5,
	anchorY: 0.5
});
introImage.x = 2048 / 2;
introImage.y = 2732 / 2;
introImage.zIndex = 100; // High z-index to display on top
game.addChild(introImage);
LK.setTimeout(function () {
	gameStartDelay = false;
	// Fade out and remove intro image
	var fadeSteps = 20;
	var fadeInterval = LK.setInterval(function () {
		introImage.alpha -= 1 / fadeSteps;
		if (introImage.alpha <= 0) {
			introImage.destroy();
			LK.clearInterval(fadeInterval);
		}
	}, 50);
	console.log("Game start delay ended - all objects resuming normal speed");
}, 4000); // 4 seconds delay
var endlessbg3SpawnerInterval;
var endlessbg3 = LK.getAsset('endlessbg3', {
	anchorX: 0.5,
	anchorY: 0.5
});
// Arka plan varlıklarımızın diğer nesnelerin arkasında kalması için düşük z-index
endlessbg3.zIndex = 1;
var cc = LK.getAsset('cc', {
	anchorX: 0.5,
	anchorY: 0.5
});
cc.x = 2048 / 2;
cc.y = 2732 / 2;
game.addChild(cc);
var startNormalEnemy = true;
var coinCounter = 0;
var flyinEnemies = [];
var zigzagEnemies = [];
var trainEnemies = [];
var playerDeaths = 0;
var hearts = [];
var remainingHearts = 3;
game.controlActive = false;
var coins = [];
var firstControlTriggered = false;
var enemies = [];
var enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
var enemySpawnCounter = 0;
var stopSpawn = false;
game.weaponEnabled = true;
// Yeni endlessbg3 spawn fonksiyonu (spawn aralığı 3000ms olarak ayarlandı)
function spawnEndlessBG3() {
	// Don't spawn anything when intro is on screen
	if (gameStartDelay) {
		return;
	}
	// endlessbg3 varlığını oluşturuyoruz.
	var bg3 = LK.getAsset('endlessbg3', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// bg3'ün x konumunu ayarla (sol kenarın ekranın sağ kenarıyla hizalanması için)
	bg3.x = game.width + bg3.width / 2;
	// bg3'ün y konumunu ayarlayın
	bg3.y = 2732 / 2 + 150;
	// Arka plan öğesi olarak diğerlerinin altında kalması için z-index düşük
	bg3.zIndex = 1;
	// Hızı arttırıldı; bg3 son ile eş zamanlı hareket edecek
	bg3.speed = 4;
	bg3.update = function () {
		this.x -= this.speed;
	};
	game.addChild(bg3);
}
// Spawn aralığını 3000ms yapıyoruz
var spawnInterval = 3000;
var endlessBG3SpawnScheduled = false;
function scheduleNextEndlessBG3() {
	if (!gameStartDelay) {
		spawnEndlessBG3();
	}
	LK.setTimeout(scheduleNextEndlessBG3, spawnInterval);
}
/**** 
* (A) Eski FlyinEnemy Spawn (sağdan sola, homing false)
****/ 
function spawnFlyinEnemy() {
	if (stopSpawn) {
		return;
	}
	var delay = Math.random() * 2000 + 2000;
	LK.setTimeout(function () {
		if (stopSpawn) {
			return;
		}
		// Only spawn if game start delay is over
		if (!gameStartDelay) {
			var fe = new FlyinEnemy();
			fe.x = 2048;
			fe.y = 449;
			fe.zIndex = 10;
			game.addChild(fe);
			flyinEnemies.push(fe);
		}
		spawnFlyinEnemy();
	}, delay);
}
// Start the spawn chain, even during gameStartDelay
spawnFlyinEnemy();
/**** 
* (B) Wave şeklinde, homing davranışlı FlyinEnemy Spawn 
* (Modified to remove top-to-bottom enemy)
****/ 
function spawnWaveOfFlyinEnemies() {
	var spawnPoints = [{
		x: 0,
		y: 0
	}, {
		x: 2048,
		y: 0
	}, {
		x: 0,
		y: 2732
	}, {
		x: 2048,
		y: 2732
	}, {
		x: 2048,
		y: 1366
	}];
	// Removed top-to-bottom spawn point (1024, 0)
	for (var i = spawnPoints.length - 1; i > 0; i--) {
		var j = Math.floor(Math.random() * (i + 1));
		var temp = spawnPoints[i];
		spawnPoints[i] = spawnPoints[j];
		spawnPoints[j] = temp;
	}
	spawnPoints.forEach(function (point, index) {
		LK.setTimeout(function () {
			var enemy = new FlyinEnemy();
			enemy.homing = true;
			enemy.wave = true;
			enemy.x = point.x;
			enemy.y = point.y;
			var dx = player.x - enemy.x;
			var dy = player.y - enemy.y;
			var distance = Math.sqrt(dx * dx + dy * dy) || 1;
			enemy.vx = dx / distance * enemy.speed;
			enemy.vy = dy / distance * enemy.speed;
			enemy.zIndex = 10;
			if (enemy.x < 1024) {
				enemy.scaleX = -1;
			}
			game.addChild(enemy);
			flyinEnemies.push(enemy);
		}, index * 800);
		if (index === spawnPoints.length - 1) {
			LK.setTimeout(function () {
				spawnFlyinEnemy();
			}, 10000);
		}
	});
}
/**** 
* Normal Enemy Spawn (sağdan sola, homing false)
****/ 
game.update = function () {
	scrollingBackground.update();
	player.update();
	if (game.scrollingBg2 && game.scrollingBg2.update) {
		game.scrollingBg2.update();
	}
	for (var t = 0; t < game.children.length; t++) {
		var child = game.children[t];
		if (child instanceof Tube || child === yesil1) {
			child.update();
		}
		if (child.children) {
			for (var c = 0; c < child.children.length; c++) {
				var subChild = child.children[c];
				if (subChild.update) {
					subChild.update();
				}
			}
		}
	}
	enemySpawnCounter++;
	if (startNormalEnemy && !stopSpawn && !gameStartDelay && enemySpawnCounter >= enemySpawnInterval && !(LK.ticks >= 876 && LK.ticks <= 936) && !(LK.ticks >= 1776 && LK.ticks <= 1836) && !(LK.ticks >= 2676 && LK.ticks <= 2736) && !(LK.ticks >= 3576 && LK.ticks <= 3636) && !(LK.ticks >= 4476 && LK.ticks <= 4536) && !(LK.ticks >= 5376 && LK.ticks <= 5436) && !(LK.ticks >= 6276 && LK.ticks <= 6336) && !(LK.ticks >= 7776 && LK.ticks <= 7836)) {
		var canSpawn = true;
		for (var i = 0; i < enemies.length; i++) {
			if (enemies[i].x > 1800) {
				canSpawn = false;
				break;
			}
		}
		if (canSpawn) {
			var tubeCollision = false;
			for (var t = 0; t < game.children.length; t++) {
				var child = game.children[t];
				if (child.asset && child.asset.name === 'tup_1') {
					var enemyRight = 2048 + 75;
					var enemyLeft = 2048 - 75;
					var tubeRight = child.x + 125;
					var tubeLeft = child.x - 125;
					if (!(enemyLeft > tubeRight || enemyRight < tubeLeft)) {
						tubeCollision = true;
						break;
					}
				}
			}
			if (!tubeCollision) {
				LK.setTimeout(function () {
					if (stopSpawn) {
						return;
					}
					var enemy = new Enemy();
					enemy.x = 2048;
					enemy.y = 2732 / 2 + 100;
					enemy.zIndex = 10;
					enemies.push(enemy);
					game.addChild(enemy);
				}, 100);
			}
			enemySpawnCounter = 0;
			enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
		}
		enemySpawnCounter = 0;
		enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
	}
	game.children.sort(function (a, b) {
		return (a.zIndex || 0) - (b.zIndex || 0);
	});
	// --- Enemy - Player ---
	for (var j = enemies.length - 1; j >= 0; j--) {
		if (enemies[j]) {
			enemies[j].update();
		}
		if (player.intersects(enemies[j]) && enemies[j].canDamage) {
			enemies[j].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.hit();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(enemies[j])) {
				var coin = new Coin();
				coin.x = enemies[j].x;
				coin.y = enemies[j].y;
				coin.velocity = 5;
				game.addChild(coin);
				coins.push(coin);
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particle.zindex = 300;
				particleEffect.x = enemies[j].x - 15;
				particleEffect.y = enemies[j].y;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				enemies[j].destroy();
				child.destroy();
				enemies.splice(j, 1);
				break;
			}
		}
	}
	// --- ZigzagEnemy - Player ---
	for (var z = zigzagEnemies.length - 1; z >= 0; z--) {
		if (zigzagEnemies[z]) {
			zigzagEnemies[z].update();
		}
		if (player.intersects(zigzagEnemies[z]) && zigzagEnemies[z].canDamage) {
			zigzagEnemies[z].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(zigzagEnemies[z])) {
				// Ölüm anındaki koordinatlar
				var px = zigzagEnemies[z].x,
					py = zigzagEnemies[z].y;
				// Partikül efekti
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = px;
				particleEffect.y = py;
				particleEffect.zIndex = 300;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 100);
				zigzagEnemies[z].destroy();
				child.destroy();
				zigzagEnemies.splice(z, 1);
				break;
			}
		}
	}
	// --- FlyinEnemy - Player ---
	for (var n = flyinEnemies.length - 1; n >= 0; n--) {
		if (flyinEnemies[n]) {
			flyinEnemies[n].update();
		}
		if (player.intersects(flyinEnemies[n]) && flyinEnemies[n].canDamage) {
			flyinEnemies[n].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.hit();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(flyinEnemies[n])) {
				if (!flyinEnemies[n].wave) {
					var coin = new Coin();
					coin.x = flyinEnemies[n].x;
					coin.y = flyinEnemies[n].y + 60;
					coin.velocity = 5;
					game.addChild(coin);
					coins.push(coin);
				}
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = flyinEnemies[n].x - 30;
				particleEffect.y = flyinEnemies[n].y + 30;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				flyinEnemies[n].destroy();
				child.destroy();
				flyinEnemies.splice(n, 1);
				break;
			}
		}
		if (flyinEnemies[n] && !flyinEnemies[n].homing && flyinEnemies[n].x < -50) {
			flyinEnemies[n].destroy();
			flyinEnemies.splice(n, 1);
		}
	}
	// --- TrainEnemy - Player ---
	for (var t = trainEnemies.length - 1; t >= 0; t--) {
		if (trainEnemies[t]) {
			trainEnemies[t].update();
		}
		if (player.intersects(trainEnemies[t]) && trainEnemies[t].canDamage) {
			trainEnemies[t].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(trainEnemies[t])) {
				child.destroy();
				break;
			}
		}
	}
	// --- Coin Toplanması ---
	for (var m = coins.length - 1; m >= 0; m--) {
		var coin = coins[m];
		coin.x -= coin.velocity;
		if (coin.x < -100) {
			coin.destroy();
			coins.splice(m, 1);
		} else if (player.intersects(coin)) {
			coinCounter++;
			scoreText.setText(coinCounter.toString());
			if (coinCounter > 9 && !scoreText.movedLeft) {
				scoreText.x -= 20;
				scoreText.movedLeft = true;
			}
			coin.destroy();
			coins.splice(m, 1);
		}
	}
};
LK.stage.addChild(game);
/**** 
* Tube Spawn (her 15 saniyede bir)
****/ 
function spawnTube() {
	if (stopSpawn || gameStartDelay) {
		return;
	}
	var tube = new Tube();
	tube.x = 2048 + 125;
	tube.y = 2732 / 2 - 120;
	game.addChild(tube);
	// yesil_1 nesnesi; puan başlangıcı 0 olarak ayarlandı
	var yesil1 = LK.getAsset('yesil_1', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var yesil1PointsCounter = 0;
	yesil1.x = tube.x;
	yesil1.y = tube.y - 170;
	yesil1.speed = 10;
	yesil1.points = 0;
	yesil1.fadeBonusApplied = false;
	yesil1.coinBonusApplied = false;
	yesil1.touchBonusApplied = false;
	yesil1.update = function () {
		yesil1.x -= yesil1.speed;
		if (yesil1.x < -yesil1.width) {
			yesil1.destroy();
		}
		function applyBonus(condition, bonusFlag, bonusName) {
			if (condition && !yesil1[bonusFlag]) {
				yesil1[bonusFlag] = true;
				yesil1.points += 1;
				console.log(bonusName + " applied. Yesil1 points: " + yesil1.points);
			}
		}
		applyBonus(game.fadeTriggered, 'fadeBonusApplied', "Fade bonus");
		applyBonus(coinCounter >= 50, 'coinBonusApplied', "Coin bonus");
		applyBonus(player.intersects(yesil1) && yesil1.points >= 2, 'touchBonusApplied', "Touch bonus");
		// Eğer yesil1 puanı 3 veya daha fazla olursa:
		if (yesil1.points >= 3) {
			yesil1PointsCounter++;
			if (yesil1PointsCounter === 1) {
				if (!yesil1.spawnScheduled) {
					yesil1.spawnScheduled = true;
					console.log("Yesil1 3 puana ulaştı. Tube2, zigzagenemy ve trainenemy spawn ediliyor.");
					// Trainenemy'yi 2 saniye sonra spawn ediyoruz
					LK.setTimeout(function () {
						var trainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						trainEnemy.zIndex = 20;
						trainEnemy.x = 2048; // Ekranın en sağında
						trainEnemy.y = 2732 / 2; // Ekranın ortasında
						trainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(trainEnemy);
						trainEnemies.push(trainEnemy); // Add to trainEnemies array
						// Add invisible FlyinEnemy inside TrainEnemy
						var invisibleFlyinEnemy = new FlyinEnemy();
						invisibleFlyinEnemy.x = trainEnemy.x;
						invisibleFlyinEnemy.y = trainEnemy.y;
						invisibleFlyinEnemy.alpha = 0; // Make it invisible
						invisibleFlyinEnemy.canDamage = true; // Ensure it can damage the player
						game.addChild(invisibleFlyinEnemy);
						trainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
								invisibleFlyinEnemy.destroy(); // Destroy the invisible flyin enemy as well
								// Remove from trainEnemies array
								var index = trainEnemies.indexOf(this);
								if (index !== -1) {
									trainEnemies.splice(index, 1);
								}
							}
						};
					}, 2000);
					// Spawn another trainenemy 6 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var thirdTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						thirdTrainEnemy.zIndex = 20;
						thirdTrainEnemy.x = 2048; // Ekranın en sağında
						thirdTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						thirdTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(thirdTrainEnemy);
						trainEnemies.push(thirdTrainEnemy); // Add to trainEnemies array
						thirdTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 6000);
					// Spawn another trainenemy 8 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var fourthTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						fourthTrainEnemy.zIndex = 20;
						fourthTrainEnemy.x = 2048; // Ekranın en sağında
						fourthTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						fourthTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(fourthTrainEnemy);
						trainEnemies.push(fourthTrainEnemy); // Add to trainEnemies array
						fourthTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 8000);
					// Spawn another trainenemy 10 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var fifthTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						fifthTrainEnemy.zIndex = 20;
						fifthTrainEnemy.x = 2048; // Ekranın en sağında
						fifthTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						fifthTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(fifthTrainEnemy);
						trainEnemies.push(fifthTrainEnemy); // Add to trainEnemies array
						fifthTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 10000);
					// Spawn another trainenemy 12 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var sixthTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						sixthTrainEnemy.zIndex = 20;
						sixthTrainEnemy.x = 2048; // Ekranın en sağında
						sixthTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						sixthTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(sixthTrainEnemy);
						trainEnemies.push(sixthTrainEnemy); // Add to trainEnemies array
						sixthTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 12000);
					// Spawn another trainenemy 3 seconds after the first one
					LK.setTimeout(function () {
						var secondTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						secondTrainEnemy.zIndex = 20;
						secondTrainEnemy.x = 2048; // Ekranın en sağında
						secondTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						secondTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(secondTrainEnemy);
						trainEnemies.push(secondTrainEnemy); // Add to trainEnemies array
						secondTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 3000);
					// Tube2'yi hemen spawn ediyoruz
					var tube2 = new Tube2();
					// Tube2'nin ekranda görüneceği konumu ayarlıyoruz
					tube2.x = 2048 / 2 + 140 - 150;
					tube2.y = tube2.height / 2 - 60;
					game.addChild(tube2);
					// Tube2'yi 15 saniye sonra sola hareket ettir
					LK.setTimeout(function () {
						tube2.update = function () {
							this.x -= this.speed;
							if (this.x < -this.width) {
								this.destroy();
							}
						};
					}, 15000);
					// Schedule ZigzagEnemy to spawn 15 seconds after yesil_1 reaches 3 points
					LK.setTimeout(function () {
						function spawnZigzagEnemy() {
							var zigzagEnemy = LK.getAsset('zigzagenemy', {
								anchorX: 0.5,
								anchorY: 0.5
							});
							zigzagEnemy.zIndex = 20;
							zigzagEnemy.x = 2048; // Spawn at the right edge of the screen
							zigzagEnemy.y = 2732 / 2; // Vertically centered
							zigzagEnemy.canDamage = true; // Add this line to ensure damage works
							game.addChild(zigzagEnemy);
							zigzagEnemies.push(zigzagEnemy); // Add enemy to the zigzagEnemies array
							zigzagEnemy.update = function () {
								this.x -= 8 * 0.96; // 4% slower than original (was 8)
								if (!this.movingUp) {
									this.y += 12 * 0.96; // 4% slower than original (was 12)
									if (this.y >= 2732 / 2) {
										// Middle of the screen
										this.movingUp = true;
									}
								} else {
									this.y -= 12 * 0.96; // 4% slower than original (was 12)
									if (this.y <= 650) {
										// 650 pixels down from the top of the screen
										this.movingUp = false;
									}
								}
								if (this.x < -this.width) {
									this.destroy();
									// Remove from the zigzagEnemies array
									var index = zigzagEnemies.indexOf(this);
									if (index !== -1) {
										zigzagEnemies.splice(index, 1);
									}
								}
							};
							// Schedule the next zigzagEnemy spawn
							var nextSpawnTime = Math.random() * 1000 + 1000; // Random time between 1 and 2 seconds
							LK.setTimeout(spawnZigzagEnemy, nextSpawnTime);
						}
						spawnZigzagEnemy();
					}, 15000);
					// Karakter yeniden spawn edilsin:
					LK.setTimeout(function () {
						var newCharacter = new Player();
						newCharacter.x = 2048 / 2 - 30;
						newCharacter.y = 0;
						newCharacter.isFalling = true;
						newCharacter.fallSpeed = 0;
						newCharacter.fallAcceleration = 0.7;
						newCharacter.fallTargetY = 2732 / 2 + 3;
						game.addChild(newCharacter);
						player = newCharacter;
						game.weaponEnabled = true;
					}, 1000);
					// bg3son ve endlessbg3’nin spawn sürelerini senkronize etmek için spawn intervalini azaltıyoruz
					LK.setTimeout(function () {
						spawnEndlessBG3();
						// Artık spawn aralığı 3000ms
						yesil1.endlessBG3Interval = LK.setInterval(function () {
							spawnEndlessBG3();
						}, 3000);
					}, 15000);
				}
			}
			console.log("Freeze game triggered because yesil1 points reached: " + yesil1.points);
			stopSpawn = true;
			var bb = LK.getAsset('bb', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			bb.x = 2048 - bb.width / 2;
			bb.y = 2732 / 2 + 150;
			game.addChild(bb);
			for (var i = 0; i < game.children.length; i++) {
				var obj = game.children[i];
				if (obj.update) {
					obj.update = function () {};
				}
			}
			// Arka plan ve tube güncellemelerini 5 saniyelik bir süre sonra geri veriyoruz
			LK.setTimeout(function () {
				for (var i = 0; i < game.children.length; i++) {
					var obj = game.children[i];
					if (obj instanceof ScrollingBackground2 || obj instanceof Tube) {
						obj.update = function () {
							this.x -= this.speed;
							if (this.x < -this.width) {
								this.destroy();
							}
						};
					}
				}
			}, 5000);
			var kara = LK.getAsset('kara', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			kara.x = 2048 / 2;
			kara.y = 2732 / 2;
			kara.alpha = 0;
			kara.zIndex = 1;
			game.addChild(kara);
			var fadeInSteps = 30;
			var fadeInStepTime = 500 / fadeInSteps;
			var fadeInStep = 0;
			var fadeInInterval = LK.setInterval(function () {
				if (fadeInStep < fadeInSteps) {
					kara.alpha = fadeInStep / fadeInSteps;
					fadeInStep++;
				} else {
					LK.clearInterval(fadeInInterval);
					var fadeOutSteps = 30;
					var fadeOutStepTime = 500 / fadeOutSteps;
					var fadeOutStep = 0;
					var fadeOutInterval = LK.setInterval(function () {
						if (fadeOutStep < fadeOutSteps) {
							kara.alpha = 1 - fadeOutStep / fadeOutSteps;
							fadeOutStep++;
						} else {
							LK.clearInterval(fadeOutInterval);
							kara.destroy();
						}
						// bg3son ve endlessbg3 aynı anda harekete başlamalı
						var bg3son = LK.getAsset('bg3son', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						bg3son.zIndex = 1;
						bg3son.x = 2048 / 2;
						bg3son.y = 2732 / 2 + 150;
						game.addChild(bg3son);
						endlessbg3 = LK.getAsset('endlessbg3', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						endlessbg3.zIndex = 1;
						endlessbg3.x = bg3son.x + bg3son.width / 2 + 450;
						endlessbg3.y = 2732 / 2 + 150;
						game.addChild(endlessbg3);
						var endlessbg3Child = LK.getAsset('endlessbg3', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						endlessbg3Child.zIndex = 1;
						endlessbg3Child.x = endlessbg3.x + 600;
						endlessbg3Child.y = endlessbg3.y;
						game.addChild(endlessbg3Child);
						// Artık bg3son'un hareketi için delay yok; her ikisi de aynı anda sola hareket edecek
						LK.setTimeout(function () {
							bg3son.update = function () {
								this.x -= 4;
							};
						}, 14000);
					}, fadeOutStepTime);
				}
			}, fadeInStepTime);
			LK.setTimeout(function () {
				player.destroy();
				function destroyAndRemove(objects) {
					for (var i = objects.length - 1; i >= 0; i--) {
						objects[i].destroy();
						objects.splice(i, 1);
					}
				}
				destroyAndRemove(enemies);
				destroyAndRemove(flyinEnemies);
				destroyAndRemove(coins);
				for (var i = game.children.length - 1; i >= 0; i--) {
					var child = game.children[i];
					if (child.asset && (child.asset.name === 'background' || child.asset.name === 'tup_1')) {
						child.destroy();
					}
				}
			}, 500);
			if (!game.isFading) {
				console.log("Triggering fade effect because yesil1 is active with points: " + yesil1.points);
				enhancedFadeEffect();
			}
		}
	};
	game.addChild(yesil1);
	var control = LK.getAsset('control', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	control.x = 0;
	control.y = -177.5;
	tube.addChild(control);
	control.update = function () {
		if (player.intersects(control)) {
			if (!firstControlTriggered && coinCounter >= 20) {
				var toggleSpawn = function toggleSpawn(state, delay) {
					stopSpawn = state;
					if (!state) {
						LK.setTimeout(function () {
							stopSpawn = false;
						}, delay);
					}
				};
				game.controlActive = true;
				control.update = function () {};
				console.log("Control event triggered: coinCounter = " + coinCounter);
				startNormalEnemy = false;
				coins.forEach(function (coin) {
					coin.velocity = 0;
				});
				LK.setTimeout(function () {
					coins.forEach(function (coin) {
						coin.velocity = 5;
					});
				}, 4000);
				toggleSpawn(true, 5000);
				game.controlActive = true;
				LK.setTimeout(function () {
					for (var i = enemies.length - 1; i >= 0; i--) {
						enemies[i].destroy();
						enemies.splice(i, 1);
					}
					for (var i = flyinEnemies.length - 1; i >= 0; i--) {
						flyinEnemies[i].destroy();
						flyinEnemies.splice(i, 1);
					}
				}, 400);
				for (var i = 0; i < game.children.length; i++) {
					var obj = game.children[i];
					if (obj.update) {
						obj.update = function () {};
					}
				}
				LK.setTimeout(function () {
					player.destroy();
				}, 330);
				enhancedFadeEffect();
				firstControlTriggered = true;
			}
		}
	};
	if (!stopSpawn) {
		LK.setTimeout(spawnTube, 15000);
	}
}
LK.setTimeout(function () {
	if (!stopSpawn && !gameStartDelay) {
		spawnTube();
	}
}, 4000); // Wait 4 seconds before spawning the first tube
/**** 
* Arka Plan ve Oyuncu
****/ 
var scrollingBackground = new ScrollingBackground();
game.addChild(scrollingBackground);
var player = game.addChild(new Player());
player.x = 2048 / 2 - 30;
player.y = 2732 / 2 + 3;
/**** 
* Skor GUI ve Kalp
****/ 
var counterBackground = LK.getAsset('counter_background', {
	anchorX: 0.5,
	anchorY: 0.5
});
LK.gui.top.addChild(counterBackground);
counterBackground.x = LK.gui.top.width / 2 - 62;
counterBackground.y = 45;
var scoreText = new Text2('0', {
	size: 80,
	fill: 0xFFFFFF
});
LK.gui.top.addChild(scoreText);
scoreText.x = LK.gui.top.width / 2 - 85;
scoreText.y = 0;
for (var i = 0; i < 3; i++) {
	var heart = LK.getAsset('heart', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	LK.gui.top.addChild(heart);
	heart.x = 50 + i * 110 + 390; // Moved 15 pixels right from current position (was 375)
	heart.y = 55; // Moved 5 pixels down from current position (was 50)
	hearts.push(heart);
}
/**** 
* enhancedFadeEffect: Fade-out, clear scene, spawn background2, Tube2, then new player
****/ 
function enhancedFadeEffect() {
	if (game.isFading || game.fadeCompleted || !game.controlActive) {
		return;
	}
	game.isFading = true;
	game.fadeTriggered = true;
	var overlay = LK.getAsset('overlay', {
		anchorX: 0,
		anchorY: 0
	});
	overlay.alpha = 0;
	overlay.zIndex = 1;
	if (!overlay.parent) {
		LK.stage.addChild(overlay);
	}
	var steps = 20;
	var duration = 500;
	var stepTime = duration / steps;
	var currentStep = 0;
	var fadeOutInterval = LK.setInterval(function () {
		if (currentStep < steps) {
			overlay.alpha = currentStep / steps;
			currentStep++;
		} else {
			LK.clearInterval(fadeOutInterval);
			currentStep = 0;
			stopSpawn = true;
			while (game.children.length > 0) {
				game.removeChild(game.children[0]);
				// kodun bir kısmı bu hata şu : 
				// [insert intended functionality or leave empty if not needed]
			}
			var scrollingBg2 = new ScrollingBackground2();
			scrollingBg2.alpha = 0;
			game.scrollingBg2 = scrollingBg2;
			game.addChild(scrollingBg2);
			var tube2 = new Tube2();
			control_2.x = 0;
			control_2.y = -177.5;
			tube2.addChild(control_2);
			control_2.update = function () {
				if (player && player.intersects(control_2)) {
					if (coinCounter >= 50) {
						console.log("Control_2 event triggered: coinCounter = " + coinCounter);
						for (var i = 0; i < game.children.length; i++) {
							var obj = game.children[i];
							if (obj.update) {
								obj.update = function () {};
							}
						}
					}
				}
			};
			tube2.x = 2048 / 2 + 140;
			tube2.y = tube2.height / 2 - 60;
			game.addChild(tube2);
			LK.setTimeout(function () {
				var newPlayer = new Player();
				newPlayer.hearts = remainingHearts;
				newPlayer.x = 2048 / 2 - 30;
				newPlayer.y = tube2.y;
				newPlayer.isFalling = true;
				newPlayer.fallSpeed = 0;
				newPlayer.fallAcceleration = 0.7;
				newPlayer.fallTargetY = 2732 / 2 - 7;
				game.addChild(newPlayer);
				player = newPlayer;
				game.weaponEnabled = true;
				LK.setTimeout(function () {
					spawnWaveOfFlyinEnemies();
				}, 800);
				LK.setTimeout(function () {
					startNormalEnemy = true;
				}, 10500);
			}, 500);
			var fadeInDuration = 500;
			var fadeInStepTime = fadeInDuration / steps;
			var fadeInInterval = LK.setInterval(function () {
				if (currentStep < steps) {
					if (game.scrollingBg2) {
						game.scrollingBg2.alpha = currentStep / steps;
					}
					currentStep++;
				} else {
					LK.clearInterval(fadeInInterval);
					if (overlay.parent) {
						overlay.parent.removeChild(overlay);
					}
					var oldScrollingSpeed = game.scrollingBg2.speed;
					var oldTubeSpeed = tube2.speed;
					game.scrollingBg2.speed = 0;
					tube2.speed = 0;
					LK.setTimeout(function () {
						game.scrollingBg2.speed = oldScrollingSpeed;
						tube2.speed = oldTubeSpeed;
						stopSpawn = false;
						game.isFading = false;
						game.fadeCompleted = true;
					}, 8000);
				}
			}, fadeInStepTime);
		}
	}, stepTime);
}
/**** 
* Oyun Döngüsü
****/ 
game.update = function () {
	scrollingBackground.update();
	player.update();
	if (game.scrollingBg2 && game.scrollingBg2.update) {
		game.scrollingBg2.update();
	}
	for (var t = 0; t < game.children.length; t++) {
		var child = game.children[t];
		if (child instanceof Tube) {
			child.update();
		}
		if (child.children) {
			for (var c = 0; c < child.children.length; c++) {
				var subChild = child.children[c];
				if (subChild.update) {
					subChild.update();
				}
			}
		}
	}
	enemySpawnCounter++;
	if (startNormalEnemy && !stopSpawn && enemySpawnCounter >= enemySpawnInterval && !(LK.ticks >= 876 && LK.ticks <= 936) && !(LK.ticks >= 1776 && LK.ticks <= 1836) && !(LK.ticks >= 2676 && LK.ticks <= 2736) && !(LK.ticks >= 3576 && LK.ticks <= 3636) && !(LK.ticks >= 4476 && LK.ticks <= 4536) && !(LK.ticks >= 5376 && LK.ticks <= 5436) && !(LK.ticks >= 6276 && LK.ticks <= 6336) && !(LK.ticks >= 7776 && LK.ticks <= 7836)) {
		var canSpawn = true;
		for (var i = 0; i < enemies.length; i++) {
			if (enemies[i].x > 1800) {
				canSpawn = false;
				break;
			}
		}
		if (canSpawn) {
			var tubeCollision = false;
			for (var t = 0; t < game.children.length; t++) {
				var child = game.children[t];
				if (child.asset && child.asset.name === 'tup_1') {
					var enemyRight = 2048 + 75;
					var enemyLeft = 2048 - 75;
					var tubeRight = child.x + 125;
					var tubeLeft = child.x - 125;
					if (!(enemyLeft > tubeRight || enemyRight < tubeLeft)) {
						tubeCollision = true;
						break;
					}
				}
			}
			if (!tubeCollision) {
				LK.setTimeout(function () {
					if (stopSpawn) {
						return;
					}
					var enemy = new Enemy();
					enemy.x = 2048;
					enemy.y = 2732 / 2 + 100;
					enemy.zIndex = 10;
					enemies.push(enemy);
					game.addChild(enemy);
				}, 100);
			}
			enemySpawnCounter = 0;
			enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
		}
		enemySpawnCounter = 0;
		enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
	}
	game.children.sort(function (a, b) {
		return (a.zIndex || 0) - (b.zIndex || 0);
	});
	// Çarpışma Kontrolleri
	for (var j = enemies.length - 1; j >= 0; j--) {
		if (enemies[j]) {
			enemies[j].update();
		}
		// Check if player is above the enemy (mushroom) and falling down onto it
		var isPlayerAboveEnemy = player.y < enemies[j].y - 50;
		var isPlayerFallingDown = player.prevY < player.y;
		var isIntersecting = player.intersects(enemies[j]);
		// If player jumps on top of the mushroom
		if (isIntersecting && isPlayerAboveEnemy && isPlayerFallingDown) {
			// Add to score
			coinCounter++;
			scoreText.setText(coinCounter.toString());
			// Set LK score system to match coin counter
			LK.setScore(coinCounter);
			if (coinCounter > 9 && !scoreText.movedLeft) {
				scoreText.x -= 20;
				scoreText.movedLeft = true;
			}
			// Create particle effect
			var particleEffect = LK.getAsset('particle', {
				anchorX: 0.5,
				anchorY: 0.5,
				color: 0x808080
			});
			particleEffect.x = enemies[j].x;
			particleEffect.y = enemies[j].y;
			particleEffect.zIndex = 300;
			game.addChild(particleEffect);
			LK.setTimeout(function () {
				particleEffect.destroy();
			}, 150);
			// Make player bounce
			player.velocityY = -15;
			player.isJumping = true;
			// Destroy mushroom
			enemies[j].destroy();
			enemies.splice(j, 1);
			continue;
		}
		// Normal collision damage logic
		else if (isIntersecting && enemies[j].canDamage) {
			enemies[j].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(enemies[j])) {
				var coin = new Coin();
				coin.x = enemies[j].x;
				coin.y = enemies[j].y;
				coin.velocity = 5;
				game.addChild(coin);
				coins.push(coin);
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = enemies[j].x - 15;
				particleEffect.y = enemies[j].y;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				enemies[j].destroy();
				child.destroy();
				enemies.splice(j, 1);
				break;
			}
		}
	}
	// --- ZigzagEnemy - Player ---
	for (var z = zigzagEnemies.length - 1; z >= 0; z--) {
		var zig = zigzagEnemies[z];
		if (!zig) {
			continue;
		}
		zig.update();
		// Çarpışma ile hasar
		if (player.intersects(zig) && zig.canDamage) {
			zig.canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		// Silahla kesişme = ölüm + particle
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(zig)) {
				// Ölüm anındaki koordinatlar
				var px = zig.x,
					py = zig.y;
				// Partikül efekti
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = px;
				particleEffect.y = py;
				particleEffect.zIndex = 300;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 100);
				// Spawn a coin at the zigzag enemy's position
				var coin = new Coin();
				coin.x = px;
				coin.y = py;
				coin.velocity = 5; // Move to the left
				game.addChild(coin);
				coins.push(coin);
				zig.destroy();
				child.destroy();
				zigzagEnemies.splice(z, 1);
				break;
			}
		}
	}
	for (var n = flyinEnemies.length - 1; n >= 0; n--) {
		if (flyinEnemies[n]) {
			flyinEnemies[n].update();
		}
		if (player.intersects(flyinEnemies[n]) && flyinEnemies[n].canDamage) {
			flyinEnemies[n].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(flyinEnemies[n])) {
				if (!flyinEnemies[n].wave) {
					var coin = new Coin();
					coin.x = flyinEnemies[n].x;
					coin.y = flyinEnemies[n].y + 60;
					coin.velocity = 5;
					game.addChild(coin);
					coins.push(coin);
				}
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = flyinEnemies[n].x - 30;
				particleEffect.y = flyinEnemies[n].y + 30;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				flyinEnemies[n].destroy();
				child.destroy();
				flyinEnemies.splice(n, 1);
				break;
			}
		}
		if (flyinEnemies[n] && !flyinEnemies[n].homing && flyinEnemies[n].x < -50) {
			flyinEnemies[n].destroy();
			flyinEnemies.splice(n, 1);
		}
	}
	// --- TrainEnemy - Player ---
	for (var t = trainEnemies.length - 1; t >= 0; t--) {
		var train = trainEnemies[t];
		if (!train) {
			continue;
		}
		train.update();
		// Çarpışma ile hasar
		if (player.intersects(train) && train.canDamage) {
			train.canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		// Silahla kesişme = particle + (isterseniz destroy)
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(train)) {
				// Ölüm anı veya çarpışma noktası
				var tx = train.x,
					ty = train.y;
				// Partikül efekti
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = tx;
				particleEffect.y = ty;
				particleEffect.zIndex = 300;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 100);
				// Silahı yok et
				child.destroy();
				break;
			}
		}
	}
	for (var m = coins.length - 1; m >= 0; m--) {
		var coin = coins[m];
		coin.x -= coin.velocity;
		if (coin.x < -100) {
			coin.destroy();
			coins.splice(m, 1);
		} else if (player.intersects(coin)) {
			coinCounter++;
			// Update both the visible score text and the internal LK score
			scoreText.setText(coinCounter.toString());
			// Set LK score system to match coin counter
			LK.setScore(coinCounter);
			if (coinCounter > 9 && !scoreText.movedLeft) {
				scoreText.x -= 20;
				scoreText.movedLeft = true;
			}
			coin.destroy();
			coins.splice(m, 1);
		}
	}
};
LK.stage.addChild(game);
// Dokunma/Kontrol
game.down = function (x, y, obj) {
	if (player.isJumping && game.weaponEnabled) {
		if (!game.lastWeaponTime || Date.now() - game.lastWeaponTime > 350) {
			var weapon = new Weapon();
			weapon.x = player.x;
			weapon.y = player.y;
			var dx = x - weapon.x;
			var dy = y - weapon.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			weapon.directionX = dx / distance;
			weapon.directionY = dy / distance;
			var angle = Math.acos(weapon.directionY / Math.sqrt(weapon.directionX * weapon.directionX + weapon.directionY * weapon.directionY));
			var angleInDegrees = angle * (180 / Math.PI);
			if (angleInDegrees <= 30) {
				weapon.speed *= 1.3;
			}
			game.addChild(weapon);
			LK.setTimeout(function () {
				weapon.destroy();
			}, 9000);
			game.lastWeaponTime = Date.now();
		}
	}
	player.jump();
};
// Oyunu Sahneye Ekle
LK.stage.addChild(game); /**** 
* Classes
****/ 
// Coin class
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = gameStartDelay ? 0 : 5;
	self.zIndex = 15;
	self.update = function () {
		if (gameStartDelay) {
			self.velocity = 0;
		} else if (self.velocity === 0) {
			self.velocity = 5;
		}
		self.x -= self.velocity;
	};
});
// NEW: EndlessBG3 class using the same scroll system as background2
var EndlessBG3 = Container.expand(function () {
	var self = Container.call(this);
	var bg1 = LK.getAsset('endlessbg3', {
		anchorX: 0,
		anchorY: 0
	});
	bg1.x = 0;
	bg1.y = 0;
	self.addChild(bg1);
	var bg2 = LK.getAsset('endlessbg3', {
		anchorX: 1,
		anchorY: 0
	});
	bg2.scaleX = -1;
	bg2.x = bg1.width;
	bg2.y = 0;
	self.addChild(bg2);
	self.speed = 2;
	self.update = function () {
		bg1.x -= self.speed;
		bg2.x -= self.speed;
		if (bg1.x + bg1.width <= 0) {
			bg1.x = bg2.x + bg2.width;
		}
		if (bg2.x + bg2.width <= 0) {
			bg2.x = bg1.x + bg1.width;
		}
	};
});
// Enemy class
var Enemy = Container.expand(function () {
	var self = Container.call(this);
	var enemyGraphics = self.attachAsset('enemy', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	self.zIndex = 10;
	self.canDamage = true;
	self.lastY = 0;
	self.lastWasMushroomOnGround = true;
	self.update = function () {
		self.lastY = self.y;
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
		}
		self.x -= self.speed;
		if (self.x < -50) {
			self.destroy();
		}
	};
});
// FlyinEnemy class
var FlyinEnemy = Container.expand(function () {
	var self = Container.call(this);
	var flyinEnemyGraphics = self.attachAsset('flyin_enemy', {
		anchorX: 0.5,
		anchorY: 0
	});
	self.speed = gameStartDelay ? 0 : 5;
	self.zIndex = 10;
	self.canDamage = true;
	self.homing = false;
	self.vx = 0;
	self.vy = 0;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
			self.vx = 0;
			self.vy = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
			if (self.homing && self.origVx !== undefined && self.origVy !== undefined) {
				self.vx = self.origVx;
				self.vy = self.origVy;
			}
		}
		if (!self.homing) {
			self.x -= self.speed;
			if (self.x < -50) {
				self.destroy();
			}
		} else {
			if (gameStartDelay) {
				// Store original velocities if not already stored
				if (self.origVx === undefined && self.origVy === undefined) {
					self.origVx = self.vx;
					self.origVy = self.vy;
				}
			}
			self.x += self.vx;
			self.y += self.vy;
			if (self.x < -200 || self.x > 2248 || self.y < -200 || self.y > 2932) {
				self.destroy();
			}
		}
	};
});
// Player class
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	self.jumpHeight = 30;
	self.isJumping = false;
	self.velocityY = 0;
	self.isFalling = false;
	self.fallSpeed = 0;
	self.fallAcceleration = gameStartDelay ? 0 : 0.7;
	self.fallTargetY = 2732 / 2 - 7;
	// Karakterin diğer öğelerin üzerinde görünmesi için yüksek z-index
	self.zIndex = 20;
	self.hearts = typeof remainingHearts !== 'undefined' ? remainingHearts : 3;
	self.update = function () {
		self.prevY = self.y;
		if (gameStartDelay) {
			self.fallAcceleration = 0;
		} else if (self.fallAcceleration === 0) {
			self.fallAcceleration = 0.7;
		}
		if (self.isFalling && !gameStartDelay) {
			self.y += self.fallSpeed;
			self.fallSpeed += self.fallAcceleration;
			if (self.y >= self.fallTargetY) {
				self.y = self.fallTargetY;
				self.isFalling = false;
				self.fallSpeed = 0;
				game.weaponEnabled = true;
			}
		}
		self.prevY = self.y;
		if (self.isJumping && !gameStartDelay) {
			self.y += self.velocityY;
			self.velocityY += 0.7;
			if (self.y >= 2732 / 2 - 3) {
				self.y = 2732 / 2 - 9;
				self.isJumping = false;
				self.velocityY = 0;
			}
		}
		// Check for tube teleportation
		for (var i = 0; i < game.children.length; i++) {
			var child = game.children[i];
			// Check if player enters first tube (tup_1)
			if (child instanceof Tube && self.intersects(child) && !self.teleporting) {
				self.teleporting = true;
				// Find second tube (tube_2) to teleport to
				for (var j = 0; j < game.children.length; j++) {
					var target = game.children[j];
					if (target instanceof Tube2) {
						// Teleport to second tube
						self.x = target.x;
						self.y = target.y;
						// Create teleport particle effect
						var particleEffect = LK.getAsset('particle', {
							anchorX: 0.5,
							anchorY: 0.5,
							color: 0x00ff00
						});
						particleEffect.x = self.x;
						particleEffect.y = self.y;
						particleEffect.zIndex = 300;
						game.addChild(particleEffect);
						LK.setTimeout(function () {
							particleEffect.destroy();
						}, 150);
						// Reset teleporting flag after a short delay
						LK.setTimeout(function () {
							self.teleporting = false;
						}, 500);
						break;
					}
				}
			}
		}
	};
	self.invulnerable = false;
	self.hit = function () {
		if (!self.invulnerable) {
			self.loseHeart();
			self.invulnerable = true;
			LK.setTimeout(function () {
				self.invulnerable = false;
			}, 500);
		}
	};
	self.jump = function () {
		if (!self.isJumping) {
			self.isJumping = true;
			self.velocityY = -self.jumpHeight;
		}
	};
	self.loseHeart = function () {
		self.hearts--;
		playerDeaths++;
		if (playerDeaths === 1 && hearts[0]) {
			hearts[0].destroy();
		} else if (playerDeaths === 2 && hearts[1]) {
			hearts[1].destroy();
		} else if (playerDeaths === 3 && hearts[2]) {
			hearts[2].destroy();
		}
		remainingHearts = self.hearts;
		if (hearts.length === 0) {
			// Apply score multipliers based on active game states
			var finalScore = coinCounter;
			// Apply 2x multiplier if control is active
			if (game.controlActive) {
				finalScore *= 2;
			}
			// Apply 3x multiplier if yesil1 is active (has 3+ points)
			var yesil1Active = false;
			for (var i = 0; i < game.children.length; i++) {
				var child = game.children[i];
				if (child.points && child.points >= 3) {
					finalScore *= 3;
					break;
				}
			}
			// Make sure LK score system gets updated with the final multiplied score
			LK.setScore(finalScore);
			for (var i = enemies.length - 1; i >= 0; i--) {
				enemies[i].destroy();
				enemies.splice(i, 1);
			}
			for (var i = flyinEnemies.length - 1; i >= 0; i--) {
				flyinEnemies[i].destroy();
				flyinEnemies.splice(i, 1);
			}
			for (var i = zigzagEnemies.length - 1; i >= 0; i--) {
				zigzagEnemies[i].destroy();
				zigzagEnemies.splice(i, 1);
			}
			for (var i = trainEnemies.length - 1; i >= 0; i--) {
				trainEnemies[i].destroy();
				trainEnemies.splice(i, 1);
			}
			for (var i = coins.length - 1; i >= 0; i--) {
				coins[i].destroy();
				coins.splice(i, 1);
			}
			self.destroy();
			game.weaponEnabled = false;
		}
	};
});
// ScrollingBackground class
var ScrollingBackground = Container.expand(function () {
	var self = Container.call(this);
	self.bg1 = LK.getAsset('background', {
		anchorX: 0,
		anchorY: 0
	});
	self.bg1.x = 0;
	self.bg1.y = 0;
	self.addChild(self.bg1);
	self.bg2 = LK.getAsset('background', {
		anchorX: 1,
		anchorY: 0
	});
	self.bg2.scaleX = -1;
	self.bg2.x = self.bg1.width;
	self.bg2.y = 0;
	self.addChild(self.bg2);
	self.speed = gameStartDelay ? 0 : 2;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 2;
		}
		self.bg1.x -= self.speed;
		self.bg2.x -= self.speed;
		if (self.bg1.x + self.bg1.width <= 0) {
			self.bg1.x = self.bg2.x + self.bg2.width;
		}
		if (self.bg2.x + self.bg2.width <= 0) {
			self.bg2.x = self.bg1.x + self.bg1.width;
		}
	};
});
// ScrollingBackground2 class
var ScrollingBackground2 = Container.expand(function () {
	var self = Container.call(this);
	var bg1 = LK.getAsset('background2', {
		anchorX: 0,
		anchorY: 0
	});
	bg1.x = 0;
	bg1.y = 0;
	self.addChild(bg1);
	var bg2 = LK.getAsset('background2', {
		anchorX: 1,
		anchorY: 0
	});
	bg2.scaleX = -1;
	bg2.x = bg1.width;
	bg2.y = 0;
	self.addChild(bg2);
	self.speed = gameStartDelay ? 0 : 2;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 2;
		}
		bg1.x -= self.speed;
		bg2.x -= self.speed;
		if (bg1.x + bg1.width <= 0) {
			bg1.x = bg2.x + bg2.width;
		}
		if (bg2.x + bg2.width <= 0) {
			bg2.x = bg1.x + bg1.width;
		}
	};
});
// Tube class
var Tube = Container.expand(function () {
	var self = Container.call(this);
	var tubeGraphics = self.attachAsset('tup_1', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	// Tube'ların arka plan gibi gözükmesi için düşük z-index
	self.zIndex = 0;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
		}
		self.x -= self.speed;
		if (self.x < -50) {
			self.destroy();
		}
	};
});
// Tube2 class (yesil_1 3 puandan sonra spawn olacak ve ekranda görünecek)
var Tube2 = Container.expand(function () {
	var self = Container.call(this);
	var tubeGraphics = self.attachAsset('tube_2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = gameStartDelay ? 0 : 5;
	// Tube2'nin diğer öğelerin üzerinde görünmesi için orta seviyede bir z-index
	self.zIndex = 21;
	self.update = function () {
		if (gameStartDelay) {
			self.speed = 0;
		} else if (self.speed === 0) {
			self.speed = 5;
		}
		self.x -= self.speed;
		if (self.x < -self.width) {
			self.destroy();
		}
		// Check if player is intersecting with this tube
		if (player && player.intersects(self) && !player.teleporting) {
			player.teleporting = true;
			// Find the first tube to teleport back to
			for (var i = 0; i < game.children.length; i++) {
				var target = game.children[i];
				if (target instanceof Tube) {
					// Teleport to first tube
					player.x = target.x;
					player.y = target.y;
					// Create teleport particle effect
					var particleEffect = LK.getAsset('particle', {
						anchorX: 0.5,
						anchorY: 0.5,
						color: 0x00ff00
					});
					particleEffect.x = player.x;
					particleEffect.y = player.y;
					particleEffect.zIndex = 300;
					game.addChild(particleEffect);
					LK.setTimeout(function () {
						particleEffect.destroy();
					}, 150);
					// Reset teleporting flag after a short delay
					LK.setTimeout(function () {
						player.teleporting = false;
					}, 500);
					break;
				}
			}
		}
	};
});
// Weapon class (Weapon'ın da diğer ön plandaki öğeler arasında gözükmesi için yüksek z-index eklenmiştir)
var Weapon = Container.expand(function () {
	var self = Container.call(this);
	var weaponGraphics = self.attachAsset('weapon', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Weapon'ın arka planın üzerinde görünmesi için yüksek z-index
	self.zIndex = 30;
	self.speed = 40;
	self.update = function () {
		self.x += self.directionX * self.speed;
		self.y += self.directionY * self.speed;
		weaponGraphics.rotation += 0.3;
		if (Math.abs(game.down.x - self.x) < self.speed && Math.abs(game.down.y - self.y) < self.speed) {
			self.destroy();
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	width: 2048,
	height: 2732,
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
var control_2 = LK.getAsset('control_2', {
	anchorX: 0.5,
	anchorY: 0.5
});
/**** 
* Global Variables & Flags
****/ 
// Add a flag to prevent spawning and movement during the first 4 seconds
var gameStartDelay = true;
// Add intro image to center of screen
var introImage = LK.getAsset('intro', {
	anchorX: 0.5,
	anchorY: 0.5
});
introImage.x = 2048 / 2;
introImage.y = 2732 / 2;
introImage.zIndex = 100; // High z-index to display on top
game.addChild(introImage);
LK.setTimeout(function () {
	gameStartDelay = false;
	// Fade out and remove intro image
	var fadeSteps = 20;
	var fadeInterval = LK.setInterval(function () {
		introImage.alpha -= 1 / fadeSteps;
		if (introImage.alpha <= 0) {
			introImage.destroy();
			LK.clearInterval(fadeInterval);
		}
	}, 50);
	console.log("Game start delay ended - all objects resuming normal speed");
}, 4000); // 4 seconds delay
var endlessbg3SpawnerInterval;
var endlessbg3 = LK.getAsset('endlessbg3', {
	anchorX: 0.5,
	anchorY: 0.5
});
// Arka plan varlıklarımızın diğer nesnelerin arkasında kalması için düşük z-index
endlessbg3.zIndex = 1;
var cc = LK.getAsset('cc', {
	anchorX: 0.5,
	anchorY: 0.5
});
cc.x = 2048 / 2;
cc.y = 2732 / 2;
game.addChild(cc);
var startNormalEnemy = true;
var coinCounter = 0;
var flyinEnemies = [];
var zigzagEnemies = [];
var trainEnemies = [];
var playerDeaths = 0;
var hearts = [];
var remainingHearts = 3;
game.controlActive = false;
var coins = [];
var firstControlTriggered = false;
var enemies = [];
var enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
var enemySpawnCounter = 0;
var stopSpawn = false;
game.weaponEnabled = true;
// Yeni endlessbg3 spawn fonksiyonu (spawn aralığı 3000ms olarak ayarlandı)
function spawnEndlessBG3() {
	// Don't spawn anything when intro is on screen
	if (gameStartDelay) {
		return;
	}
	// endlessbg3 varlığını oluşturuyoruz.
	var bg3 = LK.getAsset('endlessbg3', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// bg3'ün x konumunu ayarla (sol kenarın ekranın sağ kenarıyla hizalanması için)
	bg3.x = game.width + bg3.width / 2;
	// bg3'ün y konumunu ayarlayın
	bg3.y = 2732 / 2 + 150;
	// Arka plan öğesi olarak diğerlerinin altında kalması için z-index düşük
	bg3.zIndex = 1;
	// Hızı arttırıldı; bg3 son ile eş zamanlı hareket edecek
	bg3.speed = 4;
	bg3.update = function () {
		this.x -= this.speed;
	};
	game.addChild(bg3);
}
// Spawn aralığını 3000ms yapıyoruz
var spawnInterval = 3000;
var endlessBG3SpawnScheduled = false;
function scheduleNextEndlessBG3() {
	if (!gameStartDelay) {
		spawnEndlessBG3();
	}
	LK.setTimeout(scheduleNextEndlessBG3, spawnInterval);
}
/**** 
* (A) Eski FlyinEnemy Spawn (sağdan sola, homing false)
****/ 
function spawnFlyinEnemy() {
	if (stopSpawn) {
		return;
	}
	var delay = Math.random() * 2000 + 2000;
	LK.setTimeout(function () {
		if (stopSpawn) {
			return;
		}
		// Only spawn if game start delay is over
		if (!gameStartDelay) {
			var fe = new FlyinEnemy();
			fe.x = 2048;
			fe.y = 449;
			fe.zIndex = 10;
			game.addChild(fe);
			flyinEnemies.push(fe);
		}
		spawnFlyinEnemy();
	}, delay);
}
// Start the spawn chain, even during gameStartDelay
spawnFlyinEnemy();
/**** 
* (B) Wave şeklinde, homing davranışlı FlyinEnemy Spawn 
* (Modified to remove top-to-bottom enemy)
****/ 
function spawnWaveOfFlyinEnemies() {
	var spawnPoints = [{
		x: 0,
		y: 0
	}, {
		x: 2048,
		y: 0
	}, {
		x: 0,
		y: 2732
	}, {
		x: 2048,
		y: 2732
	}, {
		x: 2048,
		y: 1366
	}];
	// Removed top-to-bottom spawn point (1024, 0)
	for (var i = spawnPoints.length - 1; i > 0; i--) {
		var j = Math.floor(Math.random() * (i + 1));
		var temp = spawnPoints[i];
		spawnPoints[i] = spawnPoints[j];
		spawnPoints[j] = temp;
	}
	spawnPoints.forEach(function (point, index) {
		LK.setTimeout(function () {
			var enemy = new FlyinEnemy();
			enemy.homing = true;
			enemy.wave = true;
			enemy.x = point.x;
			enemy.y = point.y;
			var dx = player.x - enemy.x;
			var dy = player.y - enemy.y;
			var distance = Math.sqrt(dx * dx + dy * dy) || 1;
			enemy.vx = dx / distance * enemy.speed;
			enemy.vy = dy / distance * enemy.speed;
			enemy.zIndex = 10;
			if (enemy.x < 1024) {
				enemy.scaleX = -1;
			}
			game.addChild(enemy);
			flyinEnemies.push(enemy);
		}, index * 800);
		if (index === spawnPoints.length - 1) {
			LK.setTimeout(function () {
				spawnFlyinEnemy();
			}, 10000);
		}
	});
}
/**** 
* Normal Enemy Spawn (sağdan sola, homing false)
****/ 
game.update = function () {
	scrollingBackground.update();
	player.update();
	if (game.scrollingBg2 && game.scrollingBg2.update) {
		game.scrollingBg2.update();
	}
	for (var t = 0; t < game.children.length; t++) {
		var child = game.children[t];
		if (child instanceof Tube || child === yesil1) {
			child.update();
		}
		if (child.children) {
			for (var c = 0; c < child.children.length; c++) {
				var subChild = child.children[c];
				if (subChild.update) {
					subChild.update();
				}
			}
		}
	}
	enemySpawnCounter++;
	if (startNormalEnemy && !stopSpawn && !gameStartDelay && enemySpawnCounter >= enemySpawnInterval && !(LK.ticks >= 876 && LK.ticks <= 936) && !(LK.ticks >= 1776 && LK.ticks <= 1836) && !(LK.ticks >= 2676 && LK.ticks <= 2736) && !(LK.ticks >= 3576 && LK.ticks <= 3636) && !(LK.ticks >= 4476 && LK.ticks <= 4536) && !(LK.ticks >= 5376 && LK.ticks <= 5436) && !(LK.ticks >= 6276 && LK.ticks <= 6336) && !(LK.ticks >= 7776 && LK.ticks <= 7836)) {
		var canSpawn = true;
		for (var i = 0; i < enemies.length; i++) {
			if (enemies[i].x > 1800) {
				canSpawn = false;
				break;
			}
		}
		if (canSpawn) {
			var tubeCollision = false;
			for (var t = 0; t < game.children.length; t++) {
				var child = game.children[t];
				if (child.asset && child.asset.name === 'tup_1') {
					var enemyRight = 2048 + 75;
					var enemyLeft = 2048 - 75;
					var tubeRight = child.x + 125;
					var tubeLeft = child.x - 125;
					if (!(enemyLeft > tubeRight || enemyRight < tubeLeft)) {
						tubeCollision = true;
						break;
					}
				}
			}
			if (!tubeCollision) {
				LK.setTimeout(function () {
					if (stopSpawn) {
						return;
					}
					var enemy = new Enemy();
					enemy.x = 2048;
					enemy.y = 2732 / 2 + 100;
					enemy.zIndex = 10;
					enemies.push(enemy);
					game.addChild(enemy);
				}, 100);
			}
			enemySpawnCounter = 0;
			enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
		}
		enemySpawnCounter = 0;
		enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
	}
	game.children.sort(function (a, b) {
		return (a.zIndex || 0) - (b.zIndex || 0);
	});
	// --- Enemy - Player ---
	for (var j = enemies.length - 1; j >= 0; j--) {
		if (enemies[j]) {
			enemies[j].update();
		}
		if (player.intersects(enemies[j]) && enemies[j].canDamage) {
			enemies[j].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.hit();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(enemies[j])) {
				var coin = new Coin();
				coin.x = enemies[j].x;
				coin.y = enemies[j].y;
				coin.velocity = 5;
				game.addChild(coin);
				coins.push(coin);
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particle.zindex = 300;
				particleEffect.x = enemies[j].x - 15;
				particleEffect.y = enemies[j].y;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				enemies[j].destroy();
				child.destroy();
				enemies.splice(j, 1);
				break;
			}
		}
	}
	// --- ZigzagEnemy - Player ---
	for (var z = zigzagEnemies.length - 1; z >= 0; z--) {
		if (zigzagEnemies[z]) {
			zigzagEnemies[z].update();
		}
		if (player.intersects(zigzagEnemies[z]) && zigzagEnemies[z].canDamage) {
			zigzagEnemies[z].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(zigzagEnemies[z])) {
				// Ölüm anındaki koordinatlar
				var px = zigzagEnemies[z].x,
					py = zigzagEnemies[z].y;
				// Partikül efekti
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = px;
				particleEffect.y = py;
				particleEffect.zIndex = 300;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 100);
				zigzagEnemies[z].destroy();
				child.destroy();
				zigzagEnemies.splice(z, 1);
				break;
			}
		}
	}
	// --- FlyinEnemy - Player ---
	for (var n = flyinEnemies.length - 1; n >= 0; n--) {
		if (flyinEnemies[n]) {
			flyinEnemies[n].update();
		}
		if (player.intersects(flyinEnemies[n]) && flyinEnemies[n].canDamage) {
			flyinEnemies[n].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.hit();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(flyinEnemies[n])) {
				if (!flyinEnemies[n].wave) {
					var coin = new Coin();
					coin.x = flyinEnemies[n].x;
					coin.y = flyinEnemies[n].y + 60;
					coin.velocity = 5;
					game.addChild(coin);
					coins.push(coin);
				}
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = flyinEnemies[n].x - 30;
				particleEffect.y = flyinEnemies[n].y + 30;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				flyinEnemies[n].destroy();
				child.destroy();
				flyinEnemies.splice(n, 1);
				break;
			}
		}
		if (flyinEnemies[n] && !flyinEnemies[n].homing && flyinEnemies[n].x < -50) {
			flyinEnemies[n].destroy();
			flyinEnemies.splice(n, 1);
		}
	}
	// --- TrainEnemy - Player ---
	for (var t = trainEnemies.length - 1; t >= 0; t--) {
		if (trainEnemies[t]) {
			trainEnemies[t].update();
		}
		if (player.intersects(trainEnemies[t]) && trainEnemies[t].canDamage) {
			trainEnemies[t].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(trainEnemies[t])) {
				child.destroy();
				break;
			}
		}
	}
	// --- Coin Toplanması ---
	for (var m = coins.length - 1; m >= 0; m--) {
		var coin = coins[m];
		coin.x -= coin.velocity;
		if (coin.x < -100) {
			coin.destroy();
			coins.splice(m, 1);
		} else if (player.intersects(coin)) {
			coinCounter++;
			scoreText.setText(coinCounter.toString());
			if (coinCounter > 9 && !scoreText.movedLeft) {
				scoreText.x -= 20;
				scoreText.movedLeft = true;
			}
			coin.destroy();
			coins.splice(m, 1);
		}
	}
};
LK.stage.addChild(game);
/**** 
* Tube Spawn (her 15 saniyede bir)
****/ 
function spawnTube() {
	if (stopSpawn || gameStartDelay) {
		return;
	}
	var tube = new Tube();
	tube.x = 2048 + 125;
	tube.y = 2732 / 2 - 120;
	game.addChild(tube);
	// yesil_1 nesnesi; puan başlangıcı 0 olarak ayarlandı
	var yesil1 = LK.getAsset('yesil_1', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var yesil1PointsCounter = 0;
	yesil1.x = tube.x;
	yesil1.y = tube.y - 170;
	yesil1.speed = 10;
	yesil1.points = 0;
	yesil1.fadeBonusApplied = false;
	yesil1.coinBonusApplied = false;
	yesil1.touchBonusApplied = false;
	yesil1.update = function () {
		yesil1.x -= yesil1.speed;
		if (yesil1.x < -yesil1.width) {
			yesil1.destroy();
		}
		function applyBonus(condition, bonusFlag, bonusName) {
			if (condition && !yesil1[bonusFlag]) {
				yesil1[bonusFlag] = true;
				yesil1.points += 1;
				console.log(bonusName + " applied. Yesil1 points: " + yesil1.points);
			}
		}
		applyBonus(game.fadeTriggered, 'fadeBonusApplied', "Fade bonus");
		applyBonus(coinCounter >= 50, 'coinBonusApplied', "Coin bonus");
		applyBonus(player.intersects(yesil1) && yesil1.points >= 2, 'touchBonusApplied', "Touch bonus");
		// Eğer yesil1 puanı 3 veya daha fazla olursa:
		if (yesil1.points >= 3) {
			yesil1PointsCounter++;
			if (yesil1PointsCounter === 1) {
				if (!yesil1.spawnScheduled) {
					yesil1.spawnScheduled = true;
					console.log("Yesil1 3 puana ulaştı. Tube2, zigzagenemy ve trainenemy spawn ediliyor.");
					// Trainenemy'yi 2 saniye sonra spawn ediyoruz
					LK.setTimeout(function () {
						var trainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						trainEnemy.zIndex = 20;
						trainEnemy.x = 2048; // Ekranın en sağında
						trainEnemy.y = 2732 / 2; // Ekranın ortasında
						trainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(trainEnemy);
						trainEnemies.push(trainEnemy); // Add to trainEnemies array
						// Add invisible FlyinEnemy inside TrainEnemy
						var invisibleFlyinEnemy = new FlyinEnemy();
						invisibleFlyinEnemy.x = trainEnemy.x;
						invisibleFlyinEnemy.y = trainEnemy.y;
						invisibleFlyinEnemy.alpha = 0; // Make it invisible
						invisibleFlyinEnemy.canDamage = true; // Ensure it can damage the player
						game.addChild(invisibleFlyinEnemy);
						trainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
								invisibleFlyinEnemy.destroy(); // Destroy the invisible flyin enemy as well
								// Remove from trainEnemies array
								var index = trainEnemies.indexOf(this);
								if (index !== -1) {
									trainEnemies.splice(index, 1);
								}
							}
						};
					}, 2000);
					// Spawn another trainenemy 6 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var thirdTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						thirdTrainEnemy.zIndex = 20;
						thirdTrainEnemy.x = 2048; // Ekranın en sağında
						thirdTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						thirdTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(thirdTrainEnemy);
						trainEnemies.push(thirdTrainEnemy); // Add to trainEnemies array
						thirdTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 6000);
					// Spawn another trainenemy 8 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var fourthTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						fourthTrainEnemy.zIndex = 20;
						fourthTrainEnemy.x = 2048; // Ekranın en sağında
						fourthTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						fourthTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(fourthTrainEnemy);
						trainEnemies.push(fourthTrainEnemy); // Add to trainEnemies array
						fourthTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 8000);
					// Spawn another trainenemy 10 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var fifthTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						fifthTrainEnemy.zIndex = 20;
						fifthTrainEnemy.x = 2048; // Ekranın en sağında
						fifthTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						fifthTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(fifthTrainEnemy);
						trainEnemies.push(fifthTrainEnemy); // Add to trainEnemies array
						fifthTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 10000);
					// Spawn another trainenemy 12 seconds after yesil_1 has 3 points
					LK.setTimeout(function () {
						var sixthTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						sixthTrainEnemy.zIndex = 20;
						sixthTrainEnemy.x = 2048; // Ekranın en sağında
						sixthTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						sixthTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(sixthTrainEnemy);
						trainEnemies.push(sixthTrainEnemy); // Add to trainEnemies array
						sixthTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 12000);
					// Spawn another trainenemy 3 seconds after the first one
					LK.setTimeout(function () {
						var secondTrainEnemy = LK.getAsset('Trainenemy', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						secondTrainEnemy.zIndex = 20;
						secondTrainEnemy.x = 2048; // Ekranın en sağında
						secondTrainEnemy.y = 2732 / 2; // Ekranın ortasında
						secondTrainEnemy.canDamage = true; // Add this line to ensure damage works
						game.addChild(secondTrainEnemy);
						trainEnemies.push(secondTrainEnemy); // Add to trainEnemies array
						secondTrainEnemy.update = function () {
							var _this = this;
							if (!this.slowed) {
								this.slowed = true;
								this.originalSpeed = 16.05; // 7% faster than 15
								this.x -= 15 * 0.96 / 2; // 4% slower than original (was 15), half speed for initial delay
								LK.setTimeout(function () {
									_this.x -= 15 * 0.96; // 4% slower than original (was 15), full speed after delay
								}, 500);
							} else {
								this.x -= 15 * 0.96; // 4% slower than original (was 15)
							}
							// Check if trainEnemy has reached the middle of the screen
							if (this.x <= 2048 / 2 && !this.falling) {
								this.falling = true;
								this.fallSpeed = 0; // Initial fall speed
								var _this = this;
								LK.setTimeout(function () {
									_this.fallSpeed = 5; // Set fall speed after 0.5 seconds
								}, 500);
							}
							// If falling, move downwards
							if (this.falling) {
								this.y += this.fallSpeed;
							}
							if (this.x < -this.width || this.y > 2732) {
								this.destroy();
							}
						};
					}, 3000);
					// Tube2'yi hemen spawn ediyoruz
					var tube2 = new Tube2();
					// Tube2'nin ekranda görüneceği konumu ayarlıyoruz
					tube2.x = 2048 / 2 + 140 - 150;
					tube2.y = tube2.height / 2 - 60;
					game.addChild(tube2);
					// Tube2'yi 15 saniye sonra sola hareket ettir
					LK.setTimeout(function () {
						tube2.update = function () {
							this.x -= this.speed;
							if (this.x < -this.width) {
								this.destroy();
							}
						};
					}, 15000);
					// Schedule ZigzagEnemy to spawn 15 seconds after yesil_1 reaches 3 points
					LK.setTimeout(function () {
						function spawnZigzagEnemy() {
							var zigzagEnemy = LK.getAsset('zigzagenemy', {
								anchorX: 0.5,
								anchorY: 0.5
							});
							zigzagEnemy.zIndex = 20;
							zigzagEnemy.x = 2048; // Spawn at the right edge of the screen
							zigzagEnemy.y = 2732 / 2; // Vertically centered
							zigzagEnemy.canDamage = true; // Add this line to ensure damage works
							game.addChild(zigzagEnemy);
							zigzagEnemies.push(zigzagEnemy); // Add enemy to the zigzagEnemies array
							zigzagEnemy.update = function () {
								this.x -= 8 * 0.96; // 4% slower than original (was 8)
								if (!this.movingUp) {
									this.y += 12 * 0.96; // 4% slower than original (was 12)
									if (this.y >= 2732 / 2) {
										// Middle of the screen
										this.movingUp = true;
									}
								} else {
									this.y -= 12 * 0.96; // 4% slower than original (was 12)
									if (this.y <= 650) {
										// 650 pixels down from the top of the screen
										this.movingUp = false;
									}
								}
								if (this.x < -this.width) {
									this.destroy();
									// Remove from the zigzagEnemies array
									var index = zigzagEnemies.indexOf(this);
									if (index !== -1) {
										zigzagEnemies.splice(index, 1);
									}
								}
							};
							// Schedule the next zigzagEnemy spawn
							var nextSpawnTime = Math.random() * 1000 + 1000; // Random time between 1 and 2 seconds
							LK.setTimeout(spawnZigzagEnemy, nextSpawnTime);
						}
						spawnZigzagEnemy();
					}, 15000);
					// Karakter yeniden spawn edilsin:
					LK.setTimeout(function () {
						var newCharacter = new Player();
						newCharacter.x = 2048 / 2 - 30;
						newCharacter.y = 0;
						newCharacter.isFalling = true;
						newCharacter.fallSpeed = 0;
						newCharacter.fallAcceleration = 0.7;
						newCharacter.fallTargetY = 2732 / 2 + 3;
						game.addChild(newCharacter);
						player = newCharacter;
						game.weaponEnabled = true;
					}, 1000);
					// bg3son ve endlessbg3’nin spawn sürelerini senkronize etmek için spawn intervalini azaltıyoruz
					LK.setTimeout(function () {
						spawnEndlessBG3();
						// Artık spawn aralığı 3000ms
						yesil1.endlessBG3Interval = LK.setInterval(function () {
							spawnEndlessBG3();
						}, 3000);
					}, 15000);
				}
			}
			console.log("Freeze game triggered because yesil1 points reached: " + yesil1.points);
			stopSpawn = true;
			var bb = LK.getAsset('bb', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			bb.x = 2048 - bb.width / 2;
			bb.y = 2732 / 2 + 150;
			game.addChild(bb);
			for (var i = 0; i < game.children.length; i++) {
				var obj = game.children[i];
				if (obj.update) {
					obj.update = function () {};
				}
			}
			// Arka plan ve tube güncellemelerini 5 saniyelik bir süre sonra geri veriyoruz
			LK.setTimeout(function () {
				for (var i = 0; i < game.children.length; i++) {
					var obj = game.children[i];
					if (obj instanceof ScrollingBackground2 || obj instanceof Tube) {
						obj.update = function () {
							this.x -= this.speed;
							if (this.x < -this.width) {
								this.destroy();
							}
						};
					}
				}
			}, 5000);
			var kara = LK.getAsset('kara', {
				anchorX: 0.5,
				anchorY: 0.5
			});
			kara.x = 2048 / 2;
			kara.y = 2732 / 2;
			kara.alpha = 0;
			kara.zIndex = 1;
			game.addChild(kara);
			var fadeInSteps = 30;
			var fadeInStepTime = 500 / fadeInSteps;
			var fadeInStep = 0;
			var fadeInInterval = LK.setInterval(function () {
				if (fadeInStep < fadeInSteps) {
					kara.alpha = fadeInStep / fadeInSteps;
					fadeInStep++;
				} else {
					LK.clearInterval(fadeInInterval);
					var fadeOutSteps = 30;
					var fadeOutStepTime = 500 / fadeOutSteps;
					var fadeOutStep = 0;
					var fadeOutInterval = LK.setInterval(function () {
						if (fadeOutStep < fadeOutSteps) {
							kara.alpha = 1 - fadeOutStep / fadeOutSteps;
							fadeOutStep++;
						} else {
							LK.clearInterval(fadeOutInterval);
							kara.destroy();
						}
						// bg3son ve endlessbg3 aynı anda harekete başlamalı
						var bg3son = LK.getAsset('bg3son', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						bg3son.zIndex = 1;
						bg3son.x = 2048 / 2;
						bg3son.y = 2732 / 2 + 150;
						game.addChild(bg3son);
						endlessbg3 = LK.getAsset('endlessbg3', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						endlessbg3.zIndex = 1;
						endlessbg3.x = bg3son.x + bg3son.width / 2 + 450;
						endlessbg3.y = 2732 / 2 + 150;
						game.addChild(endlessbg3);
						var endlessbg3Child = LK.getAsset('endlessbg3', {
							anchorX: 0.5,
							anchorY: 0.5
						});
						endlessbg3Child.zIndex = 1;
						endlessbg3Child.x = endlessbg3.x + 600;
						endlessbg3Child.y = endlessbg3.y;
						game.addChild(endlessbg3Child);
						// Artık bg3son'un hareketi için delay yok; her ikisi de aynı anda sola hareket edecek
						LK.setTimeout(function () {
							bg3son.update = function () {
								this.x -= 4;
							};
						}, 14000);
					}, fadeOutStepTime);
				}
			}, fadeInStepTime);
			LK.setTimeout(function () {
				player.destroy();
				function destroyAndRemove(objects) {
					for (var i = objects.length - 1; i >= 0; i--) {
						objects[i].destroy();
						objects.splice(i, 1);
					}
				}
				destroyAndRemove(enemies);
				destroyAndRemove(flyinEnemies);
				destroyAndRemove(coins);
				for (var i = game.children.length - 1; i >= 0; i--) {
					var child = game.children[i];
					if (child.asset && (child.asset.name === 'background' || child.asset.name === 'tup_1')) {
						child.destroy();
					}
				}
			}, 500);
			if (!game.isFading) {
				console.log("Triggering fade effect because yesil1 is active with points: " + yesil1.points);
				enhancedFadeEffect();
			}
		}
	};
	game.addChild(yesil1);
	var control = LK.getAsset('control', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	control.x = 0;
	control.y = -177.5;
	tube.addChild(control);
	control.update = function () {
		if (player.intersects(control)) {
			if (!firstControlTriggered && coinCounter >= 20) {
				var toggleSpawn = function toggleSpawn(state, delay) {
					stopSpawn = state;
					if (!state) {
						LK.setTimeout(function () {
							stopSpawn = false;
						}, delay);
					}
				};
				game.controlActive = true;
				control.update = function () {};
				console.log("Control event triggered: coinCounter = " + coinCounter);
				startNormalEnemy = false;
				coins.forEach(function (coin) {
					coin.velocity = 0;
				});
				LK.setTimeout(function () {
					coins.forEach(function (coin) {
						coin.velocity = 5;
					});
				}, 4000);
				toggleSpawn(true, 5000);
				game.controlActive = true;
				LK.setTimeout(function () {
					for (var i = enemies.length - 1; i >= 0; i--) {
						enemies[i].destroy();
						enemies.splice(i, 1);
					}
					for (var i = flyinEnemies.length - 1; i >= 0; i--) {
						flyinEnemies[i].destroy();
						flyinEnemies.splice(i, 1);
					}
				}, 400);
				for (var i = 0; i < game.children.length; i++) {
					var obj = game.children[i];
					if (obj.update) {
						obj.update = function () {};
					}
				}
				LK.setTimeout(function () {
					player.destroy();
				}, 330);
				enhancedFadeEffect();
				firstControlTriggered = true;
			}
		}
	};
	if (!stopSpawn) {
		LK.setTimeout(spawnTube, 15000);
	}
}
LK.setTimeout(function () {
	if (!stopSpawn && !gameStartDelay) {
		spawnTube();
	}
}, 4000); // Wait 4 seconds before spawning the first tube
/**** 
* Arka Plan ve Oyuncu
****/ 
var scrollingBackground = new ScrollingBackground();
game.addChild(scrollingBackground);
var player = game.addChild(new Player());
player.x = 2048 / 2 - 30;
player.y = 2732 / 2 + 3;
/**** 
* Skor GUI ve Kalp
****/ 
var counterBackground = LK.getAsset('counter_background', {
	anchorX: 0.5,
	anchorY: 0.5
});
LK.gui.top.addChild(counterBackground);
counterBackground.x = LK.gui.top.width / 2 - 62;
counterBackground.y = 45;
var scoreText = new Text2('0', {
	size: 80,
	fill: 0xFFFFFF
});
LK.gui.top.addChild(scoreText);
scoreText.x = LK.gui.top.width / 2 - 85;
scoreText.y = 0;
for (var i = 0; i < 3; i++) {
	var heart = LK.getAsset('heart', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	LK.gui.top.addChild(heart);
	heart.x = 50 + i * 110 + 390; // Moved 15 pixels right from current position (was 375)
	heart.y = 55; // Moved 5 pixels down from current position (was 50)
	hearts.push(heart);
}
/**** 
* enhancedFadeEffect: Fade-out, clear scene, spawn background2, Tube2, then new player
****/ 
function enhancedFadeEffect() {
	if (game.isFading || game.fadeCompleted || !game.controlActive) {
		return;
	}
	game.isFading = true;
	game.fadeTriggered = true;
	var overlay = LK.getAsset('overlay', {
		anchorX: 0,
		anchorY: 0
	});
	overlay.alpha = 0;
	overlay.zIndex = 1;
	if (!overlay.parent) {
		LK.stage.addChild(overlay);
	}
	var steps = 20;
	var duration = 500;
	var stepTime = duration / steps;
	var currentStep = 0;
	var fadeOutInterval = LK.setInterval(function () {
		if (currentStep < steps) {
			overlay.alpha = currentStep / steps;
			currentStep++;
		} else {
			LK.clearInterval(fadeOutInterval);
			currentStep = 0;
			stopSpawn = true;
			while (game.children.length > 0) {
				game.removeChild(game.children[0]);
				// kodun bir kısmı bu hata şu : 
				// [insert intended functionality or leave empty if not needed]
			}
			var scrollingBg2 = new ScrollingBackground2();
			scrollingBg2.alpha = 0;
			game.scrollingBg2 = scrollingBg2;
			game.addChild(scrollingBg2);
			var tube2 = new Tube2();
			control_2.x = 0;
			control_2.y = -177.5;
			tube2.addChild(control_2);
			control_2.update = function () {
				if (player && player.intersects(control_2)) {
					if (coinCounter >= 50) {
						console.log("Control_2 event triggered: coinCounter = " + coinCounter);
						for (var i = 0; i < game.children.length; i++) {
							var obj = game.children[i];
							if (obj.update) {
								obj.update = function () {};
							}
						}
					}
				}
			};
			tube2.x = 2048 / 2 + 140;
			tube2.y = tube2.height / 2 - 60;
			game.addChild(tube2);
			LK.setTimeout(function () {
				var newPlayer = new Player();
				newPlayer.hearts = remainingHearts;
				newPlayer.x = 2048 / 2 - 30;
				newPlayer.y = tube2.y;
				newPlayer.isFalling = true;
				newPlayer.fallSpeed = 0;
				newPlayer.fallAcceleration = 0.7;
				newPlayer.fallTargetY = 2732 / 2 - 7;
				game.addChild(newPlayer);
				player = newPlayer;
				game.weaponEnabled = true;
				LK.setTimeout(function () {
					spawnWaveOfFlyinEnemies();
				}, 800);
				LK.setTimeout(function () {
					startNormalEnemy = true;
				}, 10500);
			}, 500);
			var fadeInDuration = 500;
			var fadeInStepTime = fadeInDuration / steps;
			var fadeInInterval = LK.setInterval(function () {
				if (currentStep < steps) {
					if (game.scrollingBg2) {
						game.scrollingBg2.alpha = currentStep / steps;
					}
					currentStep++;
				} else {
					LK.clearInterval(fadeInInterval);
					if (overlay.parent) {
						overlay.parent.removeChild(overlay);
					}
					var oldScrollingSpeed = game.scrollingBg2.speed;
					var oldTubeSpeed = tube2.speed;
					game.scrollingBg2.speed = 0;
					tube2.speed = 0;
					LK.setTimeout(function () {
						game.scrollingBg2.speed = oldScrollingSpeed;
						tube2.speed = oldTubeSpeed;
						stopSpawn = false;
						game.isFading = false;
						game.fadeCompleted = true;
					}, 8000);
				}
			}, fadeInStepTime);
		}
	}, stepTime);
}
/**** 
* Oyun Döngüsü
****/ 
game.update = function () {
	scrollingBackground.update();
	player.update();
	if (game.scrollingBg2 && game.scrollingBg2.update) {
		game.scrollingBg2.update();
	}
	for (var t = 0; t < game.children.length; t++) {
		var child = game.children[t];
		if (child instanceof Tube) {
			child.update();
		}
		if (child.children) {
			for (var c = 0; c < child.children.length; c++) {
				var subChild = child.children[c];
				if (subChild.update) {
					subChild.update();
				}
			}
		}
	}
	enemySpawnCounter++;
	if (startNormalEnemy && !stopSpawn && enemySpawnCounter >= enemySpawnInterval && !(LK.ticks >= 876 && LK.ticks <= 936) && !(LK.ticks >= 1776 && LK.ticks <= 1836) && !(LK.ticks >= 2676 && LK.ticks <= 2736) && !(LK.ticks >= 3576 && LK.ticks <= 3636) && !(LK.ticks >= 4476 && LK.ticks <= 4536) && !(LK.ticks >= 5376 && LK.ticks <= 5436) && !(LK.ticks >= 6276 && LK.ticks <= 6336) && !(LK.ticks >= 7776 && LK.ticks <= 7836)) {
		var canSpawn = true;
		for (var i = 0; i < enemies.length; i++) {
			if (enemies[i].x > 1800) {
				canSpawn = false;
				break;
			}
		}
		if (canSpawn) {
			var tubeCollision = false;
			for (var t = 0; t < game.children.length; t++) {
				var child = game.children[t];
				if (child.asset && child.asset.name === 'tup_1') {
					var enemyRight = 2048 + 75;
					var enemyLeft = 2048 - 75;
					var tubeRight = child.x + 125;
					var tubeLeft = child.x - 125;
					if (!(enemyLeft > tubeRight || enemyRight < tubeLeft)) {
						tubeCollision = true;
						break;
					}
				}
			}
			if (!tubeCollision) {
				LK.setTimeout(function () {
					if (stopSpawn) {
						return;
					}
					var enemy = new Enemy();
					enemy.x = 2048;
					enemy.y = 2732 / 2 + 100;
					enemy.zIndex = 10;
					enemies.push(enemy);
					game.addChild(enemy);
				}, 100);
			}
			enemySpawnCounter = 0;
			enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
		}
		enemySpawnCounter = 0;
		enemySpawnInterval = Math.floor(Math.random() * 100) + 30;
	}
	game.children.sort(function (a, b) {
		return (a.zIndex || 0) - (b.zIndex || 0);
	});
	// Çarpışma Kontrolleri
	for (var j = enemies.length - 1; j >= 0; j--) {
		if (enemies[j]) {
			enemies[j].update();
		}
		// Check if player is above the enemy (mushroom) and falling down onto it
		var isPlayerAboveEnemy = player.y < enemies[j].y - 50;
		var isPlayerFallingDown = player.prevY < player.y;
		var isIntersecting = player.intersects(enemies[j]);
		// If player jumps on top of the mushroom
		if (isIntersecting && isPlayerAboveEnemy && isPlayerFallingDown) {
			// Add to score
			coinCounter++;
			scoreText.setText(coinCounter.toString());
			// Set LK score system to match coin counter
			LK.setScore(coinCounter);
			if (coinCounter > 9 && !scoreText.movedLeft) {
				scoreText.x -= 20;
				scoreText.movedLeft = true;
			}
			// Create particle effect
			var particleEffect = LK.getAsset('particle', {
				anchorX: 0.5,
				anchorY: 0.5,
				color: 0x808080
			});
			particleEffect.x = enemies[j].x;
			particleEffect.y = enemies[j].y;
			particleEffect.zIndex = 300;
			game.addChild(particleEffect);
			LK.setTimeout(function () {
				particleEffect.destroy();
			}, 150);
			// Make player bounce
			player.velocityY = -15;
			player.isJumping = true;
			// Destroy mushroom
			enemies[j].destroy();
			enemies.splice(j, 1);
			continue;
		}
		// Normal collision damage logic
		else if (isIntersecting && enemies[j].canDamage) {
			enemies[j].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(enemies[j])) {
				var coin = new Coin();
				coin.x = enemies[j].x;
				coin.y = enemies[j].y;
				coin.velocity = 5;
				game.addChild(coin);
				coins.push(coin);
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = enemies[j].x - 15;
				particleEffect.y = enemies[j].y;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				enemies[j].destroy();
				child.destroy();
				enemies.splice(j, 1);
				break;
			}
		}
	}
	// --- ZigzagEnemy - Player ---
	for (var z = zigzagEnemies.length - 1; z >= 0; z--) {
		var zig = zigzagEnemies[z];
		if (!zig) {
			continue;
		}
		zig.update();
		// Çarpışma ile hasar
		if (player.intersects(zig) && zig.canDamage) {
			zig.canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		// Silahla kesişme = ölüm + particle
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(zig)) {
				// Ölüm anındaki koordinatlar
				var px = zig.x,
					py = zig.y;
				// Partikül efekti
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = px;
				particleEffect.y = py;
				particleEffect.zIndex = 300;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 100);
				// Spawn a coin at the zigzag enemy's position
				var coin = new Coin();
				coin.x = px;
				coin.y = py;
				coin.velocity = 5; // Move to the left
				game.addChild(coin);
				coins.push(coin);
				zig.destroy();
				child.destroy();
				zigzagEnemies.splice(z, 1);
				break;
			}
		}
	}
	for (var n = flyinEnemies.length - 1; n >= 0; n--) {
		if (flyinEnemies[n]) {
			flyinEnemies[n].update();
		}
		if (player.intersects(flyinEnemies[n]) && flyinEnemies[n].canDamage) {
			flyinEnemies[n].canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(flyinEnemies[n])) {
				if (!flyinEnemies[n].wave) {
					var coin = new Coin();
					coin.x = flyinEnemies[n].x;
					coin.y = flyinEnemies[n].y + 60;
					coin.velocity = 5;
					game.addChild(coin);
					coins.push(coin);
				}
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = flyinEnemies[n].x - 30;
				particleEffect.y = flyinEnemies[n].y + 30;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 150);
				flyinEnemies[n].destroy();
				child.destroy();
				flyinEnemies.splice(n, 1);
				break;
			}
		}
		if (flyinEnemies[n] && !flyinEnemies[n].homing && flyinEnemies[n].x < -50) {
			flyinEnemies[n].destroy();
			flyinEnemies.splice(n, 1);
		}
	}
	// --- TrainEnemy - Player ---
	for (var t = trainEnemies.length - 1; t >= 0; t--) {
		var train = trainEnemies[t];
		if (!train) {
			continue;
		}
		train.update();
		// Çarpışma ile hasar
		if (player.intersects(train) && train.canDamage) {
			train.canDamage = false;
			LK.effects.flashScreen(0xff0000, 750, 0.0001);
			player.loseHeart();
			if (player.hearts <= 0) {
				LK.showGameOver();
			}
		}
		// Silahla kesişme = particle + (isterseniz destroy)
		for (var k = game.children.length - 1; k >= 0; k--) {
			var child = game.children[k];
			if (child instanceof Weapon && child.intersects(train)) {
				// Ölüm anı veya çarpışma noktası
				var tx = train.x,
					ty = train.y;
				// Partikül efekti
				var particleEffect = LK.getAsset('particle', {
					anchorX: 0.5,
					anchorY: 0.5,
					color: 0x808080
				});
				particleEffect.x = tx;
				particleEffect.y = ty;
				particleEffect.zIndex = 300;
				game.addChild(particleEffect);
				LK.setTimeout(function () {
					particleEffect.destroy();
				}, 100);
				// Silahı yok et
				child.destroy();
				break;
			}
		}
	}
	for (var m = coins.length - 1; m >= 0; m--) {
		var coin = coins[m];
		coin.x -= coin.velocity;
		if (coin.x < -100) {
			coin.destroy();
			coins.splice(m, 1);
		} else if (player.intersects(coin)) {
			coinCounter++;
			// Update both the visible score text and the internal LK score
			scoreText.setText(coinCounter.toString());
			// Set LK score system to match coin counter
			LK.setScore(coinCounter);
			if (coinCounter > 9 && !scoreText.movedLeft) {
				scoreText.x -= 20;
				scoreText.movedLeft = true;
			}
			coin.destroy();
			coins.splice(m, 1);
		}
	}
};
LK.stage.addChild(game);
// Dokunma/Kontrol
game.down = function (x, y, obj) {
	if (player.isJumping && game.weaponEnabled) {
		if (!game.lastWeaponTime || Date.now() - game.lastWeaponTime > 350) {
			var weapon = new Weapon();
			weapon.x = player.x;
			weapon.y = player.y;
			var dx = x - weapon.x;
			var dy = y - weapon.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			weapon.directionX = dx / distance;
			weapon.directionY = dy / distance;
			var angle = Math.acos(weapon.directionY / Math.sqrt(weapon.directionX * weapon.directionX + weapon.directionY * weapon.directionY));
			var angleInDegrees = angle * (180 / Math.PI);
			if (angleInDegrees <= 30) {
				weapon.speed *= 1.3;
			}
			game.addChild(weapon);
			LK.setTimeout(function () {
				weapon.destroy();
			}, 9000);
			game.lastWeaponTime = Date.now();
		}
	}
	player.jump();
};
// Oyunu Sahneye Ekle
LK.stage.addChild(game);
:quality(85)/https://cdn.frvr.ai/67589443e1c6df8afc3f7fd3.png%3F3) 
 Single 2D Mario Character. In-Game asset. 2d. Blank background.
:quality(85)/https://cdn.frvr.ai/67d579ab540f369ec4071660.png%3F3) 
 marionun ingiliz boru anahtarı aleti. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67d58381540f369ec40716a5.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67d62caff0905a2251a97ad0.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67d757ae885017473a98979d.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67d76073885017473a989801.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67d966dd24ccde20f6adb0eb.png%3F3) 
 red heart mario. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67d97ff4ed4e4720c972d114.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67d9810d24ccde20f6adb16b.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67dacf6f4c81bb9f3792105a.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67db37c416a72d6be958a21e.png%3F3) 
 completely black simple counter without sharp edges
:quality(85)/https://cdn.frvr.ai/67e009637a7926bdcc2098b4.png%3F3) 
 sea and sky,pixel,realistic but detailles benzer renkler mavi ve mavi Single Game Texture. In-Game asset. 2d. Blank background. low contrast. No shadows
:quality(85)/https://cdn.frvr.ai/67e0b085d8b8882ac8b2756c.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67e8faa298f8528d5a204316.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67e8fbb000d0d3a9ffcc5787.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67e90c7caa93ed8b7ab6b5b3.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/67f28e924db359f66d9acbdd.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/68081af3f43c19059935a247.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6828d933095a67be3df303c2.png%3F3) 
 KÜÇÜK AYAKLARI OLAN MANTAR CANAVARI. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6828dc84095a67be3df303df.png%3F3) 
 ağazı açık kaktüsler 2D. In-Game asset. 2d. High contrast. No shadows