/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0
});
/**** 
* Classes
****/ 
var Clam = Container.expand(function () {
	var self = Container.call(this);
	// Clam body
	var body = self.attachAsset('mine', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 5.0,
		// Increase scale to make it even more massive
		scaleY: 5.0,
		// Increase scale to make it even more massive
		tint: 0x8B4513,
		// Change color to brown
		texture: 'clamTexture',
		// Add texture for realism
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 10,
			// Shadow blur radius
			offsetX: 5,
			// Horizontal shadow offset
			offsetY: 5 // Vertical shadow offset
		}
	});
	// Pearl
	var pearl = LK.getAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: -body.height / 2 - 10,
		scaleX: 2.0,
		// Increase size of the pearl
		scaleY: 2.0,
		// Increase size of the pearl
		visible: false
	});
	self.addChild(pearl);
	// Open and release pearl every 10 seconds
	var releasePearl = function releasePearl() {
		// Open clam with animation
		tween(body, {
			scaleY: 6.0 // Open clam by increasing scale
		}, {
			duration: 1000,
			// 1 second to open
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				pearl.visible = true; // Show pearl when clam is open
				LK.setTimeout(function () {
					// Close clam with animation
					tween(body, {
						scaleY: 5.0 // Close clam by returning to original scale
					}, {
						duration: 2000,
						// 2 seconds to close
						easing: tween.easeInOut,
						onFinish: function onFinish() {
							pearl.visible = false; // Hide pearl when clam is closed
						}
					});
				}, 1000); // Pearl visible for 1 second
			}
		});
	};
	LK.setInterval(releasePearl, 10000);
	self.pearl = pearl;
	return self;
});
var Fish = Container.expand(function (type, level) {
	var self = Container.call(this);
	// Setup fish properties based on type
	self.fishType = type || 'smallFish';
	self.fishLevel = level || 1;
	// Size multiplier based on level
	var sizeMultiplier = 1;
	if (self.fishLevel === 2) {
		sizeMultiplier = 1.5;
	}
	if (self.fishLevel === 3) {
		sizeMultiplier = 2;
	}
	// Fish body
	var assetId = self.fishType;
	var body = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: sizeMultiplier * (Math.random() * 0.2 + 0.9),
		// Vary size slightly for realism
		scaleY: sizeMultiplier * (Math.random() * 0.2 + 0.9),
		// Vary size slightly for realism
		tint: 0x8A2BE2 // Add a more realistic purple tint
	});
	// Tail
	var tailColor = self.fishType === 'smallFish' ? 0xffb967 : self.fishType === 'mediumFish' ? 0xff6c67 : 0xc267ff;
	var tail = LK.getAsset('tail', {
		anchorX: 1.0,
		anchorY: 0.5,
		x: -body.width / 2,
		y: 0,
		scaleX: sizeMultiplier * (Math.random() * 0.3 + 0.8),
		// Vary size slightly for realism 
		scaleY: sizeMultiplier * (Math.random() * 0.3 + 0.8),
		// Vary size slightly for realism 
		tint: 0x8A2BE2,
		// Add a more realistic purple tint
		texture: 'fishTailTexture' // Add texture for realism
	});
	tail.tint = tailColor;
	self.addChild(tail);
	// Mouth
	var mouth = LK.getAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 2,
		y: body.height / 4,
		scaleX: sizeMultiplier * 0.5,
		scaleY: sizeMultiplier * 0.2,
		tint: 0x000000
	});
	self.addChild(mouth);
	// Eye
	var eye = self.attachAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4,
		y: -body.height / 4,
		scaleX: sizeMultiplier,
		scaleY: sizeMultiplier
	});
	// Pupil
	var pupil = self.attachAsset('pupil', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4 + 2,
		y: -body.height / 4,
		scaleX: sizeMultiplier,
		scaleY: sizeMultiplier
	});
	// Movement variables
	self.speedX = (Math.random() * 2 + 1) * (Math.random() < 0.5 ? 1 : -1);
	self.speedY = (Math.random() - 0.5) * 1.5;
	// Animation
	var _animateTail = function animateTail() {
		tween(tail, {
			scaleX: sizeMultiplier * 0.6
		}, {
			duration: 300,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(tail, {
					scaleX: sizeMultiplier * 0.8
				}, {
					duration: 300,
					easing: tween.easeInOut,
					onFinish: _animateTail
				});
			}
		});
	};
	_animateTail();
	// Update method called automatically by LK engine
	self.update = function () {
		// Move fish
		self.x += self.speedX;
		self.y += self.speedY;
		// Update rotation based on direction
		if (self.speedX < 0) {
			self.scale.x = -1;
		} else {
			self.scale.x = 1;
		}
		// Bounce off edges
		if (self.x < 0) {
			self.x = 0;
			self.speedX *= -1;
		} else if (self.x > 2048) {
			self.x = 2048;
			self.speedX *= -1;
		}
		// Limit vertical movement more subtly
		if (self.y < 0) {
			self.y = 0;
			self.speedY *= -1;
		} else if (self.y > 2732) {
			self.y = 2732;
			self.speedY *= -1;
		}
		// Ensure fish stay within bounds
		self.x = Math.max(0, Math.min(self.x, 2048));
		self.y = Math.max(0, Math.min(self.y, 2732));
		// Occasionally change vertical direction
		if (Math.random() < 0.01) {
			self.speedY = (Math.random() - 0.5) * 1.5;
		}
	};
	return self;
});
var GoldFish = Container.expand(function () {
	var self = Container.call(this);
	// GoldFish body
	var body = self.attachAsset('smallFish', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1.0,
		scaleY: 1.0,
		tint: 0xFFD700 // Gold color
	});
	// Movement variables
	self.speedX = (Math.random() * 3 + 2) * (Math.random() < 0.5 ? 1 : -1);
	self.speedY = (Math.random() - 0.5) * 2.0;
	// Update method called automatically by LK engine
	self.update = function () {
		// Move goldfish
		self.x += self.speedX;
		self.y += self.speedY;
		// Bounce off edges
		if (self.x < 0 || self.x > 2048) {
			self.speedX *= -1;
		}
		if (self.y < 0 || self.y > 2732) {
			self.speedY *= -1;
		}
	};
	return self;
});
var Mine = Container.expand(function () {
	var self = Container.call(this);
	// Mine body
	var body = self.attachAsset('mine', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Add spikes
	for (var i = 0; i < 8; i++) {
		var angle = i / 8 * Math.PI * 2;
		var spike = LK.getAsset('mine', {
			anchorX: 0.5,
			anchorY: 0.5,
			width: 15,
			height: 3,
			x: Math.cos(angle) * 25,
			y: Math.sin(angle) * 25,
			rotation: angle
		});
		self.addChild(spike);
	}
	// Small pulse animation
	var _animatePulse = function animatePulse() {
		tween(body, {
			scaleX: 1.1,
			scaleY: 1.1
		}, {
			duration: 800,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(body, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 800,
					easing: tween.easeInOut,
					onFinish: _animatePulse
				});
			}
		});
	};
	_animatePulse();
	return self;
});
var PlayerFish = Container.expand(function () {
	var self = Container.call(this);
	// Fish body
	var body = self.attachAsset('playerFish', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1.2,
		// Slightly larger for more detail
		scaleY: 1.2,
		// Slightly larger for more detail
		tint: 0x7cd1ff,
		// Base color
		texture: 'detailedFishTexture',
		// Add texture for realism
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 5,
			// Shadow blur radius
			offsetX: 3,
			// Horizontal shadow offset
			offsetY: 3 // Vertical shadow offset
		}
	});
	// Tail
	var tail = self.attachAsset('tail', {
		anchorX: 1.0,
		anchorY: 0.5,
		x: -body.width / 2,
		y: 0,
		scaleX: 1.1,
		// Slightly larger for more detail
		scaleY: 1.1,
		// Slightly larger for more detail
		tint: 0x7cd1ff,
		// Match body color
		texture: 'detailedTailTexture',
		// Add texture for realism
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 3,
			// Shadow blur radius
			offsetX: 2,
			// Horizontal shadow offset
			offsetY: 2 // Vertical shadow offset
		}
	});
	// Eye
	var eye = self.attachAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4,
		y: -body.height / 4,
		scaleX: 1.1,
		// Slightly larger for more detail
		scaleY: 1.1,
		// Slightly larger for more detail
		tint: 0xffffff,
		// White color for eye
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 2,
			// Shadow blur radius
			offsetX: 1,
			// Horizontal shadow offset
			offsetY: 1 // Vertical shadow offset
		}
	});
	// Pupil
	var pupil = self.attachAsset('pupil', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4 + 2,
		y: -body.height / 4,
		scaleX: 1.1,
		// Slightly larger for more detail
		scaleY: 1.1,
		// Slightly larger for more detail
		tint: 0x000000,
		// Black color for pupil
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 1,
			// Shadow blur radius
			offsetX: 0.5,
			// Horizontal shadow offset
			offsetY: 0.5 // Vertical shadow offset
		}
	});
	// Size/Level properties
	self.level = 1;
	self.initialWidth = body.width;
	self.initialHeight = body.height;
	// Movement target
	self.targetX = null;
	self.targetY = null;
	self.speed = 5;
	// Animation
	var _animateTail2 = function animateTail() {
		tween(tail, {
			scaleX: 0.7
		}, {
			duration: 300,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(tail, {
					scaleX: 1
				}, {
					duration: 300,
					easing: tween.easeInOut,
					onFinish: _animateTail2
				});
			}
		});
	};
	_animateTail2();
	// Growth function
	self.grow = function () {
		self.level++;
		// Calculate new size based on level (capped at level 5)
		var growthFactor = Math.min(self.level * 0.25 + 1, 2.5);
		tween(body, {
			width: self.initialWidth * growthFactor,
			height: self.initialHeight * growthFactor
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		tween(tail, {
			width: tail.width * 1.2,
			height: tail.height * 1.2,
			x: -(self.initialWidth * growthFactor) / 2
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		tween(eye, {
			x: self.initialWidth * growthFactor / 4,
			y: -(self.initialHeight * growthFactor) / 4
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		tween(pupil, {
			x: self.initialWidth * growthFactor / 4 + 2,
			y: -(self.initialHeight * growthFactor) / 4
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		// Play level up sound
		LK.getSound('levelUp').play();
	};
	// Update method called automatically by LK engine
	self.update = function () {
		if (self.targetX !== null && self.targetY !== null) {
			// Calculate direction vector
			var dx = self.targetX - self.x;
			var dy = self.targetY - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			// If we're close enough to the target, stop moving
			if (distance < self.speed) {
				self.x = self.targetX;
				self.y = self.targetY;
				self.targetX = null;
				self.targetY = null;
			} else {
				// Move towards target
				self.x += dx / distance * self.speed;
				self.y += dy / distance * self.speed;
				// Rotate fish in the direction of movement
				var angle = Math.atan2(dy, dx);
				// Only flip horizontally, no rotation
				if (dx < 0) {
					self.scale.x = -1;
				} else {
					self.scale.x = 1;
				}
			}
		}
		// Keep fish in bounds
		if (self.x < 0) {
			self.x = 0;
		}
		if (self.y < 0) {
			self.y = 0;
		}
		if (self.x > 2048) {
			self.x = 2048;
		}
		if (self.y > 2732) {
			self.y = 2732;
			self.shield.x = self.x;
			self.shield.y = self.y;
		}
	};
	// Health bar
	self.health = 3; // Initialize health
	self.healthBar = new Container();
	self.healthBar.x = -body.width / 2;
	self.healthBar.y = -body.height / 2 - 30; // Move health bar higher above the player fish 
	self.addChild(self.healthBar);
	for (var i = 0; i < self.health; i++) {
		var healthSegment = LK.getAsset('playerFish', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: i * 20,
			y: 0,
			scaleX: 0.2,
			scaleY: 0.2,
			tint: 0x00FF00 // Green color for health
		});
		self.healthBar.addChild(healthSegment);
	}
	// Shield
	var shield = self.attachAsset('playerFish', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 3.0,
		// Larger size 
		scaleY: 3.0,
		// Larger size 
		tint: 0x0000FF,
		// Blue color
		alpha: 0.5 // Semi-transparent
	});
	self.shield = shield;
	self.shieldActive = true; // Initialize shield as active
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x202438 // Dark blue cave background
});
/**** 
* Game Code
****/ 
var clam = null;
var player = null;
var fishes = [];
var mines = [];
var score = 0;
var gameActive = true;
// Background cave elements
var cave = game.addChild(LK.getAsset('cave', {
	anchorX: 0,
	anchorY: 0,
	x: 0,
	y: 0
}));
// Initialize player fish
player = new PlayerFish();
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
// UI
var scoreTxt = new Text2('Score: 0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -20;
scoreTxt.y = 20;
var timerTxt = new Text2('Time: 60', {
	size: 60,
	fill: 0xFFFFFF
});
timerTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(timerTxt);
timerTxt.x = 20;
timerTxt.y = 20;
var remainingTime = 60;
var timerInterval = LK.setInterval(function () {
	if (remainingTime > 0) {
		remainingTime--;
		timerTxt.setText('Time: ' + remainingTime);
	}
}, 1000);
var levelTxt = new Text2('Level: 1', {
	size: 60,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(levelTxt);
levelTxt.x = -levelTxt.width - 20;
levelTxt.y = 90;
var highScoreTxt = new Text2('High Score: ' + storage.highScore, {
	size: 50,
	fill: 0xFFDD00
});
highScoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.x = -highScoreTxt.width - 20;
highScoreTxt.y = 160;
// Initialize game elements
function initializeGame() {
	// Add a clam to the game
	clam = new Clam();
	clam.x = 2048 / 2;
	clam.y = 2732 - 50; // Position clam closer to the bottom
	clam.visible = true; // Make the clam visible
	game.addChild(clam);
	// Initialize player score and level
	score = 0;
	scoreTxt.setText('Score: ' + score);
	player.level = 1;
	player.shieldActive = true;
	// Set a 60-second timer to end the game
	LK.setTimeout(function () {
		gameActive = false;
		if (score >= 1000) {
			LK.showYouWin(); // Player wins if score is 1000 or more
		} else {
			LK.showGameOver(); // Player loses if score is less than 1000
		}
		LK.clearInterval(timerInterval); // Clear the timer interval when the game ends
	}, 60000); // 60 seconds
	LK.setTimeout(function () {
		player.shieldActive = false;
		player.shield.visible = false; // Hide the shield after 3 seconds
	}, 3000); // Shield lasts for 3 seconds
	// Start game at stage 1
	spawnFish(5, 'smallFish', 1);
	spawnFish(10, 'mediumFish', 2);
	spawnMines(10);
	// Start playing ambient cave sound
	LK.playMusic('caveAmbience', {
		loop: true,
		fade: {
			start: 0,
			end: 0.4,
			duration: 1000
		}
	});
}
// Spawn fish
function spawnFish(count, type, level) {
	for (var i = 0; i < count; i++) {
		var fish = new Fish(type, level);
		fish.x = Math.random() * 2048;
		fish.y = Math.random() * 2732;
		fishes.push(fish);
		game.addChild(fish);
	}
}
// Spawn mines
function spawnMines(count) {
	for (var i = 0; i < count; i++) {
		var mine = new Mine();
		mine.x = Math.random() * 2048;
		mine.y = Math.random() * 2732;
		mines.push(mine);
		game.addChild(mine);
	}
}
// Handle player movement
function handleMove(x, y, obj) {
	if (gameActive) {
		player.targetX = x;
		player.targetY = y;
		// Ensure player moves towards the target
		player.update();
	}
}
// Mouse/touch events
game.down = function (x, y, obj) {
	handleMove(x, y, obj);
	player.speed = 10; // Increase speed when mouse is held down
};
game.move = handleMove;
game.up = function (x, y, obj) {
	player.speed = 5; // Reset speed when mouse is released
};
// Main game loop
game.update = function () {
	if (!gameActive) {
		return;
	}
	// Update player movement
	player.update();
	// Check if player intersects with the pearl and can eat it
	if (clam.pearl.visible && player.intersects(clam.pearl)) {
		clam.pearl.visible = false; // Hide the pearl after eating
		LK.getSound('eat').play();
		score += 50; // Add score for eating the pearl
		scoreTxt.setText('Score: ' + score);
		// Check if player should level up
		if (score >= player.level * 50) {
			player.grow();
			levelTxt.setText('Level: ' + player.level);
		}
	}
	for (var i = fishes.length - 1; i >= 0; i--) {
		var fish = fishes[i];
		if (player.intersects(fish) && !fish.eaten) {
			fish.eaten = true;
			fish.destroy();
			fishes.splice(i, 1);
			// Determine if player can eat the fish
			if (player.level >= fish.fishLevel) {
				// Player eats fish
				fish.eaten = true;
				LK.getSound('eat').play();
				// Add score based on fish type
				var points = fish.fishLevel * 10;
				score += points;
				scoreTxt.setText('Score: ' + score);
				// Check if player should level up
				if (score >= player.level * 50) {
					player.grow();
					levelTxt.setText('Level: ' + player.level);
					// Spawn a replacement fish
					if (fish.fishLevel === 1) {
						spawnFish(1, 'smallFish', 1);
					} else if (fish.fishLevel === 2) {
						spawnFish(1, 'mediumFish', 2);
					} else {
						spawnFish(1, 'largeFish', 3);
					}
				}
				// Update high score if needed
				if (score > storage.highScore) {
					storage.highScore = score;
					highScoreTxt.setText('High Score: ' + storage.highScore);
				}
			} else if (!(fish instanceof GoldFish) && fish.fishLevel > player.level) {
				if (!player.shieldActive) {
					// Fish eats player - game over
					gameActive = false;
					LK.getSound('explode').play();
					// Flash player
					LK.effects.flashObject(player, 0xFF0000, 1000);
					// Show game over
					LK.setTimeout(function () {
						LK.showGameOver();
					}, 1500);
				}
			}
		}
	}
	// Check for collisions with mines
	for (var j = mines.length - 1; j >= 0; j--) {
		var mine = mines[j];
		if (player.intersects(mine)) {
			if (!player.shieldActive) {
				// Mine explodes - reduce health by 1/3
				player.health -= 1 / 3;
				LK.getSound('explode').play();
				LK.effects.flashObject(player, 0xFF0000, 500);
				// Update health bar
				var segmentsToRemove = Math.ceil(player.healthBar.children.length / 3);
				for (var k = 0; k < segmentsToRemove; k++) {
					if (player.healthBar.children.length > 0) {
						player.healthBar.removeChildAt(player.healthBar.children.length - 1);
					}
				}
				// Check if health is depleted
				if (player.health <= 0) {
					gameActive = false;
					// Flash screen
					LK.effects.flashScreen(0xFF0000, 1000);
					// Show game over 
					LK.setTimeout(function () {
						LK.showGameOver();
					}, 1500);
				}
				// Remove mine after explosion
				mine.destroy();
				mines.splice(j, 1);
			}
		}
	}
	// Occasionally spawn a new small fish
	if (Math.random() < 0.02 && fishes.length < 30) {
		// Increased spawn rate and max count
		spawnFish(1, 'smallFish', 1);
	}
};
// Initialize the game
initializeGame(); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0
});
/**** 
* Classes
****/ 
var Clam = Container.expand(function () {
	var self = Container.call(this);
	// Clam body
	var body = self.attachAsset('mine', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 5.0,
		// Increase scale to make it even more massive
		scaleY: 5.0,
		// Increase scale to make it even more massive
		tint: 0x8B4513,
		// Change color to brown
		texture: 'clamTexture',
		// Add texture for realism
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 10,
			// Shadow blur radius
			offsetX: 5,
			// Horizontal shadow offset
			offsetY: 5 // Vertical shadow offset
		}
	});
	// Pearl
	var pearl = LK.getAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: -body.height / 2 - 10,
		scaleX: 2.0,
		// Increase size of the pearl
		scaleY: 2.0,
		// Increase size of the pearl
		visible: false
	});
	self.addChild(pearl);
	// Open and release pearl every 10 seconds
	var releasePearl = function releasePearl() {
		// Open clam with animation
		tween(body, {
			scaleY: 6.0 // Open clam by increasing scale
		}, {
			duration: 1000,
			// 1 second to open
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				pearl.visible = true; // Show pearl when clam is open
				LK.setTimeout(function () {
					// Close clam with animation
					tween(body, {
						scaleY: 5.0 // Close clam by returning to original scale
					}, {
						duration: 2000,
						// 2 seconds to close
						easing: tween.easeInOut,
						onFinish: function onFinish() {
							pearl.visible = false; // Hide pearl when clam is closed
						}
					});
				}, 1000); // Pearl visible for 1 second
			}
		});
	};
	LK.setInterval(releasePearl, 10000);
	self.pearl = pearl;
	return self;
});
var Fish = Container.expand(function (type, level) {
	var self = Container.call(this);
	// Setup fish properties based on type
	self.fishType = type || 'smallFish';
	self.fishLevel = level || 1;
	// Size multiplier based on level
	var sizeMultiplier = 1;
	if (self.fishLevel === 2) {
		sizeMultiplier = 1.5;
	}
	if (self.fishLevel === 3) {
		sizeMultiplier = 2;
	}
	// Fish body
	var assetId = self.fishType;
	var body = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: sizeMultiplier * (Math.random() * 0.2 + 0.9),
		// Vary size slightly for realism
		scaleY: sizeMultiplier * (Math.random() * 0.2 + 0.9),
		// Vary size slightly for realism
		tint: 0x8A2BE2 // Add a more realistic purple tint
	});
	// Tail
	var tailColor = self.fishType === 'smallFish' ? 0xffb967 : self.fishType === 'mediumFish' ? 0xff6c67 : 0xc267ff;
	var tail = LK.getAsset('tail', {
		anchorX: 1.0,
		anchorY: 0.5,
		x: -body.width / 2,
		y: 0,
		scaleX: sizeMultiplier * (Math.random() * 0.3 + 0.8),
		// Vary size slightly for realism 
		scaleY: sizeMultiplier * (Math.random() * 0.3 + 0.8),
		// Vary size slightly for realism 
		tint: 0x8A2BE2,
		// Add a more realistic purple tint
		texture: 'fishTailTexture' // Add texture for realism
	});
	tail.tint = tailColor;
	self.addChild(tail);
	// Mouth
	var mouth = LK.getAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 2,
		y: body.height / 4,
		scaleX: sizeMultiplier * 0.5,
		scaleY: sizeMultiplier * 0.2,
		tint: 0x000000
	});
	self.addChild(mouth);
	// Eye
	var eye = self.attachAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4,
		y: -body.height / 4,
		scaleX: sizeMultiplier,
		scaleY: sizeMultiplier
	});
	// Pupil
	var pupil = self.attachAsset('pupil', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4 + 2,
		y: -body.height / 4,
		scaleX: sizeMultiplier,
		scaleY: sizeMultiplier
	});
	// Movement variables
	self.speedX = (Math.random() * 2 + 1) * (Math.random() < 0.5 ? 1 : -1);
	self.speedY = (Math.random() - 0.5) * 1.5;
	// Animation
	var _animateTail = function animateTail() {
		tween(tail, {
			scaleX: sizeMultiplier * 0.6
		}, {
			duration: 300,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(tail, {
					scaleX: sizeMultiplier * 0.8
				}, {
					duration: 300,
					easing: tween.easeInOut,
					onFinish: _animateTail
				});
			}
		});
	};
	_animateTail();
	// Update method called automatically by LK engine
	self.update = function () {
		// Move fish
		self.x += self.speedX;
		self.y += self.speedY;
		// Update rotation based on direction
		if (self.speedX < 0) {
			self.scale.x = -1;
		} else {
			self.scale.x = 1;
		}
		// Bounce off edges
		if (self.x < 0) {
			self.x = 0;
			self.speedX *= -1;
		} else if (self.x > 2048) {
			self.x = 2048;
			self.speedX *= -1;
		}
		// Limit vertical movement more subtly
		if (self.y < 0) {
			self.y = 0;
			self.speedY *= -1;
		} else if (self.y > 2732) {
			self.y = 2732;
			self.speedY *= -1;
		}
		// Ensure fish stay within bounds
		self.x = Math.max(0, Math.min(self.x, 2048));
		self.y = Math.max(0, Math.min(self.y, 2732));
		// Occasionally change vertical direction
		if (Math.random() < 0.01) {
			self.speedY = (Math.random() - 0.5) * 1.5;
		}
	};
	return self;
});
var GoldFish = Container.expand(function () {
	var self = Container.call(this);
	// GoldFish body
	var body = self.attachAsset('smallFish', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1.0,
		scaleY: 1.0,
		tint: 0xFFD700 // Gold color
	});
	// Movement variables
	self.speedX = (Math.random() * 3 + 2) * (Math.random() < 0.5 ? 1 : -1);
	self.speedY = (Math.random() - 0.5) * 2.0;
	// Update method called automatically by LK engine
	self.update = function () {
		// Move goldfish
		self.x += self.speedX;
		self.y += self.speedY;
		// Bounce off edges
		if (self.x < 0 || self.x > 2048) {
			self.speedX *= -1;
		}
		if (self.y < 0 || self.y > 2732) {
			self.speedY *= -1;
		}
	};
	return self;
});
var Mine = Container.expand(function () {
	var self = Container.call(this);
	// Mine body
	var body = self.attachAsset('mine', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Add spikes
	for (var i = 0; i < 8; i++) {
		var angle = i / 8 * Math.PI * 2;
		var spike = LK.getAsset('mine', {
			anchorX: 0.5,
			anchorY: 0.5,
			width: 15,
			height: 3,
			x: Math.cos(angle) * 25,
			y: Math.sin(angle) * 25,
			rotation: angle
		});
		self.addChild(spike);
	}
	// Small pulse animation
	var _animatePulse = function animatePulse() {
		tween(body, {
			scaleX: 1.1,
			scaleY: 1.1
		}, {
			duration: 800,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(body, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 800,
					easing: tween.easeInOut,
					onFinish: _animatePulse
				});
			}
		});
	};
	_animatePulse();
	return self;
});
var PlayerFish = Container.expand(function () {
	var self = Container.call(this);
	// Fish body
	var body = self.attachAsset('playerFish', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1.2,
		// Slightly larger for more detail
		scaleY: 1.2,
		// Slightly larger for more detail
		tint: 0x7cd1ff,
		// Base color
		texture: 'detailedFishTexture',
		// Add texture for realism
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 5,
			// Shadow blur radius
			offsetX: 3,
			// Horizontal shadow offset
			offsetY: 3 // Vertical shadow offset
		}
	});
	// Tail
	var tail = self.attachAsset('tail', {
		anchorX: 1.0,
		anchorY: 0.5,
		x: -body.width / 2,
		y: 0,
		scaleX: 1.1,
		// Slightly larger for more detail
		scaleY: 1.1,
		// Slightly larger for more detail
		tint: 0x7cd1ff,
		// Match body color
		texture: 'detailedTailTexture',
		// Add texture for realism
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 3,
			// Shadow blur radius
			offsetX: 2,
			// Horizontal shadow offset
			offsetY: 2 // Vertical shadow offset
		}
	});
	// Eye
	var eye = self.attachAsset('eyeball', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4,
		y: -body.height / 4,
		scaleX: 1.1,
		// Slightly larger for more detail
		scaleY: 1.1,
		// Slightly larger for more detail
		tint: 0xffffff,
		// White color for eye
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 2,
			// Shadow blur radius
			offsetX: 1,
			// Horizontal shadow offset
			offsetY: 1 // Vertical shadow offset
		}
	});
	// Pupil
	var pupil = self.attachAsset('pupil', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: body.width / 4 + 2,
		y: -body.height / 4,
		scaleX: 1.1,
		// Slightly larger for more detail
		scaleY: 1.1,
		// Slightly larger for more detail
		tint: 0x000000,
		// Black color for pupil
		shadow: {
			color: 0x000000,
			// Shadow color
			blur: 1,
			// Shadow blur radius
			offsetX: 0.5,
			// Horizontal shadow offset
			offsetY: 0.5 // Vertical shadow offset
		}
	});
	// Size/Level properties
	self.level = 1;
	self.initialWidth = body.width;
	self.initialHeight = body.height;
	// Movement target
	self.targetX = null;
	self.targetY = null;
	self.speed = 5;
	// Animation
	var _animateTail2 = function animateTail() {
		tween(tail, {
			scaleX: 0.7
		}, {
			duration: 300,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(tail, {
					scaleX: 1
				}, {
					duration: 300,
					easing: tween.easeInOut,
					onFinish: _animateTail2
				});
			}
		});
	};
	_animateTail2();
	// Growth function
	self.grow = function () {
		self.level++;
		// Calculate new size based on level (capped at level 5)
		var growthFactor = Math.min(self.level * 0.25 + 1, 2.5);
		tween(body, {
			width: self.initialWidth * growthFactor,
			height: self.initialHeight * growthFactor
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		tween(tail, {
			width: tail.width * 1.2,
			height: tail.height * 1.2,
			x: -(self.initialWidth * growthFactor) / 2
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		tween(eye, {
			x: self.initialWidth * growthFactor / 4,
			y: -(self.initialHeight * growthFactor) / 4
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		tween(pupil, {
			x: self.initialWidth * growthFactor / 4 + 2,
			y: -(self.initialHeight * growthFactor) / 4
		}, {
			duration: 500,
			easing: tween.easeOut
		});
		// Play level up sound
		LK.getSound('levelUp').play();
	};
	// Update method called automatically by LK engine
	self.update = function () {
		if (self.targetX !== null && self.targetY !== null) {
			// Calculate direction vector
			var dx = self.targetX - self.x;
			var dy = self.targetY - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			// If we're close enough to the target, stop moving
			if (distance < self.speed) {
				self.x = self.targetX;
				self.y = self.targetY;
				self.targetX = null;
				self.targetY = null;
			} else {
				// Move towards target
				self.x += dx / distance * self.speed;
				self.y += dy / distance * self.speed;
				// Rotate fish in the direction of movement
				var angle = Math.atan2(dy, dx);
				// Only flip horizontally, no rotation
				if (dx < 0) {
					self.scale.x = -1;
				} else {
					self.scale.x = 1;
				}
			}
		}
		// Keep fish in bounds
		if (self.x < 0) {
			self.x = 0;
		}
		if (self.y < 0) {
			self.y = 0;
		}
		if (self.x > 2048) {
			self.x = 2048;
		}
		if (self.y > 2732) {
			self.y = 2732;
			self.shield.x = self.x;
			self.shield.y = self.y;
		}
	};
	// Health bar
	self.health = 3; // Initialize health
	self.healthBar = new Container();
	self.healthBar.x = -body.width / 2;
	self.healthBar.y = -body.height / 2 - 30; // Move health bar higher above the player fish 
	self.addChild(self.healthBar);
	for (var i = 0; i < self.health; i++) {
		var healthSegment = LK.getAsset('playerFish', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: i * 20,
			y: 0,
			scaleX: 0.2,
			scaleY: 0.2,
			tint: 0x00FF00 // Green color for health
		});
		self.healthBar.addChild(healthSegment);
	}
	// Shield
	var shield = self.attachAsset('playerFish', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 3.0,
		// Larger size 
		scaleY: 3.0,
		// Larger size 
		tint: 0x0000FF,
		// Blue color
		alpha: 0.5 // Semi-transparent
	});
	self.shield = shield;
	self.shieldActive = true; // Initialize shield as active
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x202438 // Dark blue cave background
});
/**** 
* Game Code
****/ 
var clam = null;
var player = null;
var fishes = [];
var mines = [];
var score = 0;
var gameActive = true;
// Background cave elements
var cave = game.addChild(LK.getAsset('cave', {
	anchorX: 0,
	anchorY: 0,
	x: 0,
	y: 0
}));
// Initialize player fish
player = new PlayerFish();
player.x = 2048 / 2;
player.y = 2732 / 2;
game.addChild(player);
// UI
var scoreTxt = new Text2('Score: 0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(scoreTxt);
scoreTxt.x = -20;
scoreTxt.y = 20;
var timerTxt = new Text2('Time: 60', {
	size: 60,
	fill: 0xFFFFFF
});
timerTxt.anchor.set(0, 0);
LK.gui.topLeft.addChild(timerTxt);
timerTxt.x = 20;
timerTxt.y = 20;
var remainingTime = 60;
var timerInterval = LK.setInterval(function () {
	if (remainingTime > 0) {
		remainingTime--;
		timerTxt.setText('Time: ' + remainingTime);
	}
}, 1000);
var levelTxt = new Text2('Level: 1', {
	size: 60,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(levelTxt);
levelTxt.x = -levelTxt.width - 20;
levelTxt.y = 90;
var highScoreTxt = new Text2('High Score: ' + storage.highScore, {
	size: 50,
	fill: 0xFFDD00
});
highScoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(highScoreTxt);
highScoreTxt.x = -highScoreTxt.width - 20;
highScoreTxt.y = 160;
// Initialize game elements
function initializeGame() {
	// Add a clam to the game
	clam = new Clam();
	clam.x = 2048 / 2;
	clam.y = 2732 - 50; // Position clam closer to the bottom
	clam.visible = true; // Make the clam visible
	game.addChild(clam);
	// Initialize player score and level
	score = 0;
	scoreTxt.setText('Score: ' + score);
	player.level = 1;
	player.shieldActive = true;
	// Set a 60-second timer to end the game
	LK.setTimeout(function () {
		gameActive = false;
		if (score >= 1000) {
			LK.showYouWin(); // Player wins if score is 1000 or more
		} else {
			LK.showGameOver(); // Player loses if score is less than 1000
		}
		LK.clearInterval(timerInterval); // Clear the timer interval when the game ends
	}, 60000); // 60 seconds
	LK.setTimeout(function () {
		player.shieldActive = false;
		player.shield.visible = false; // Hide the shield after 3 seconds
	}, 3000); // Shield lasts for 3 seconds
	// Start game at stage 1
	spawnFish(5, 'smallFish', 1);
	spawnFish(10, 'mediumFish', 2);
	spawnMines(10);
	// Start playing ambient cave sound
	LK.playMusic('caveAmbience', {
		loop: true,
		fade: {
			start: 0,
			end: 0.4,
			duration: 1000
		}
	});
}
// Spawn fish
function spawnFish(count, type, level) {
	for (var i = 0; i < count; i++) {
		var fish = new Fish(type, level);
		fish.x = Math.random() * 2048;
		fish.y = Math.random() * 2732;
		fishes.push(fish);
		game.addChild(fish);
	}
}
// Spawn mines
function spawnMines(count) {
	for (var i = 0; i < count; i++) {
		var mine = new Mine();
		mine.x = Math.random() * 2048;
		mine.y = Math.random() * 2732;
		mines.push(mine);
		game.addChild(mine);
	}
}
// Handle player movement
function handleMove(x, y, obj) {
	if (gameActive) {
		player.targetX = x;
		player.targetY = y;
		// Ensure player moves towards the target
		player.update();
	}
}
// Mouse/touch events
game.down = function (x, y, obj) {
	handleMove(x, y, obj);
	player.speed = 10; // Increase speed when mouse is held down
};
game.move = handleMove;
game.up = function (x, y, obj) {
	player.speed = 5; // Reset speed when mouse is released
};
// Main game loop
game.update = function () {
	if (!gameActive) {
		return;
	}
	// Update player movement
	player.update();
	// Check if player intersects with the pearl and can eat it
	if (clam.pearl.visible && player.intersects(clam.pearl)) {
		clam.pearl.visible = false; // Hide the pearl after eating
		LK.getSound('eat').play();
		score += 50; // Add score for eating the pearl
		scoreTxt.setText('Score: ' + score);
		// Check if player should level up
		if (score >= player.level * 50) {
			player.grow();
			levelTxt.setText('Level: ' + player.level);
		}
	}
	for (var i = fishes.length - 1; i >= 0; i--) {
		var fish = fishes[i];
		if (player.intersects(fish) && !fish.eaten) {
			fish.eaten = true;
			fish.destroy();
			fishes.splice(i, 1);
			// Determine if player can eat the fish
			if (player.level >= fish.fishLevel) {
				// Player eats fish
				fish.eaten = true;
				LK.getSound('eat').play();
				// Add score based on fish type
				var points = fish.fishLevel * 10;
				score += points;
				scoreTxt.setText('Score: ' + score);
				// Check if player should level up
				if (score >= player.level * 50) {
					player.grow();
					levelTxt.setText('Level: ' + player.level);
					// Spawn a replacement fish
					if (fish.fishLevel === 1) {
						spawnFish(1, 'smallFish', 1);
					} else if (fish.fishLevel === 2) {
						spawnFish(1, 'mediumFish', 2);
					} else {
						spawnFish(1, 'largeFish', 3);
					}
				}
				// Update high score if needed
				if (score > storage.highScore) {
					storage.highScore = score;
					highScoreTxt.setText('High Score: ' + storage.highScore);
				}
			} else if (!(fish instanceof GoldFish) && fish.fishLevel > player.level) {
				if (!player.shieldActive) {
					// Fish eats player - game over
					gameActive = false;
					LK.getSound('explode').play();
					// Flash player
					LK.effects.flashObject(player, 0xFF0000, 1000);
					// Show game over
					LK.setTimeout(function () {
						LK.showGameOver();
					}, 1500);
				}
			}
		}
	}
	// Check for collisions with mines
	for (var j = mines.length - 1; j >= 0; j--) {
		var mine = mines[j];
		if (player.intersects(mine)) {
			if (!player.shieldActive) {
				// Mine explodes - reduce health by 1/3
				player.health -= 1 / 3;
				LK.getSound('explode').play();
				LK.effects.flashObject(player, 0xFF0000, 500);
				// Update health bar
				var segmentsToRemove = Math.ceil(player.healthBar.children.length / 3);
				for (var k = 0; k < segmentsToRemove; k++) {
					if (player.healthBar.children.length > 0) {
						player.healthBar.removeChildAt(player.healthBar.children.length - 1);
					}
				}
				// Check if health is depleted
				if (player.health <= 0) {
					gameActive = false;
					// Flash screen
					LK.effects.flashScreen(0xFF0000, 1000);
					// Show game over 
					LK.setTimeout(function () {
						LK.showGameOver();
					}, 1500);
				}
				// Remove mine after explosion
				mine.destroy();
				mines.splice(j, 1);
			}
		}
	}
	// Occasionally spawn a new small fish
	if (Math.random() < 0.02 && fishes.length < 30) {
		// Increased spawn rate and max count
		spawnFish(1, 'smallFish', 1);
	}
};
// Initialize the game
initializeGame();