/****
* Classes
****/
// Fish class: represents a single fish swimming in the ocean
var Fish = Container.expand(function () {
var self = Container.call(this);
// Create a simple ellipse as the fish
var fishAsset = self.attachAsset('fish', {
width: 120,
height: 60,
color: 0xffcc66,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
// Randomize initial position, speed, and direction
self.direction = Math.random() < 0.5 ? 1 : -1; // 1: right, -1: left
if (self.direction === 1) {
self.x = Math.random() * 1024; // left half
} else {
self.x = 1024 + Math.random() * 1024; // right half
}
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
// Track lastX for edge detection
self.lastX = self.x;
// Fish swims in both directions, loops back when off screen
self.update = function () {
self.lastX = self.x;
self.x += self.speed;
// Flip fish asset horizontally if swimming left, normal if right
if (self.direction === 1) {
if (typeof fishAsset !== "undefined") fishAsset.scaleX = 1;
} else {
if (typeof fishAsset !== "undefined") fishAsset.scaleX = -1;
}
if (self.direction === 1) {
// rightward
if (self.lastX <= 2048 && self.x > 2048) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
} else {
// leftward
if (self.lastX >= -100 && self.x < -100) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
}
};
return self;
});
var Ocean = Container.expand(function () {
var self = Container.call(this);
// Create a large blue rectangle as the ocean background
var oceanAsset = self.attachAsset('ocean', {
width: 2048,
height: 2732,
color: 0x3399ff,
shape: 'box',
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
return self;
});
// RareFish class: represents the rare black fish that appears every 10 seconds
var RareFish = Container.expand(function () {
var self = Container.call(this);
// Attach rare fish asset (black)
var rareFishAsset = self.attachAsset('rarefish', {
width: 240,
height: 120,
color: 0x000000,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
// Randomize initial position, speed, and direction
self.direction = Math.random() < 0.5 ? 1 : -1; // 1: right, -1: left
if (self.direction === 1) {
self.x = Math.random() * 1024;
} else {
self.x = 1024 + Math.random() * 1024;
}
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
self.lastX = self.x;
// Rare fish swims in both directions, loops back when off screen
self.update = function () {
self.lastX = self.x;
self.x += self.speed;
// Flip rare fish asset horizontally if swimming left, normal if right
if (self.direction === 1) {
if (typeof rareFishAsset !== "undefined") rareFishAsset.scaleX = 1;
} else {
if (typeof rareFishAsset !== "undefined") rareFishAsset.scaleX = -1;
}
if (self.direction === 1) {
// rightward
if (self.lastX <= 2048 && self.x > 2048) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
} else {
// leftward
if (self.lastX >= -100 && self.x < -100) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
}
};
return self;
});
// Function to spawn rare fish
// RedFish class: represents a red fish that shrinks the shark
var RedFish = Container.expand(function () {
var self = Container.call(this);
// Attach red fish asset (nadir balığı is black)
var redFishAsset = self.attachAsset('redfish', {
width: 120,
height: 60,
color: 0x000000,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
// Randomize initial position, speed, and direction
self.direction = Math.random() < 0.5 ? 1 : -1; // 1: right, -1: left
if (self.direction === 1) {
self.x = Math.random() * 1024;
} else {
self.x = 1024 + Math.random() * 1024;
}
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
// Track lastX for edge detection
self.lastX = self.x;
// Red fish swims in both directions, loops back when off screen
self.update = function () {
self.lastX = self.x;
self.x += self.speed;
// Flip red fish asset horizontally if swimming left, normal if right
if (self.direction === 1) {
if (typeof redFishAsset !== "undefined") redFishAsset.scaleX = 1;
} else {
if (typeof redFishAsset !== "undefined") redFishAsset.scaleX = -1;
}
if (self.direction === 1) {
// rightward
if (self.lastX <= 2048 && self.x > 2048) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
} else {
// leftward
if (self.lastX >= -100 && self.x < -100) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
}
};
return self;
});
// Shark class: represents the main character (shark) swimming in the ocean
var Shark = Container.expand(function () {
var self = Container.call(this);
// Attach a shark asset (box for now, can be replaced with a shark image asset)
var sharkAsset = self.attachAsset('shark', {
width: 220,
height: 100,
color: 0x444444,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
// Start in the center of the ocean
self.x = 1024;
self.y = 1366;
// Track lastX and lastY for possible future movement logic
self.lastX = self.x;
self.lastY = self.y;
// Shark update: just track lastX/lastY for drag movement
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Hide the mouse cursor during gameplay
if (typeof LK.setCursor === "function") {
LK.setCursor("none");
} else if (typeof stage !== "undefined" && stage.view && stage.view.style) {
stage.view.style.cursor = "none";
}
// Add ocean background
var ocean = new Ocean();
game.addChild(ocean);
// Play water sound as looping background music after first user interaction
var waterMusicStarted = false;
function startWaterMusic() {
if (!waterMusicStarted) {
LK.playMusic('water');
waterMusicStarted = true;
}
}
game.down = function (origDown) {
return function (x, y, obj) {
startWaterMusic();
if (typeof origDown === "function") origDown(x, y, obj);
};
}(game.down);
// Score system
var score = 0;
var scoreTxt = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.x = LK.gui.top.width / 2;
scoreTxt.y = 10;
// Timer system
var secondsLeft = 60;
var targetScore = 30;
var currentLevel = 1;
// Check if player is on level 2 (after winning level 1)
if (typeof storage !== "undefined" && typeof storage.getItem === "function") {
var storedLevel = parseInt(storage.getItem("level"));
if (!isNaN(storedLevel) && storedLevel === 2) {
secondsLeft = 60;
targetScore = 120;
currentLevel = 2;
}
}
var timerTxt = new Text2(secondsLeft + '', {
size: 90,
fill: 0xFFFFFF
});
timerTxt.anchor.set(0.5, 1);
LK.gui.bottom.addChild(timerTxt);
// Place timer at bottom center, but a bit higher
timerTxt.x = LK.gui.bottom.width / 2;
timerTxt.y = LK.gui.bottom.height - 120;
// Timer interval
var timerInterval = LK.setInterval(function () {
if (secondsLeft > 0) {
secondsLeft--;
timerTxt.setText(secondsLeft + "");
if (secondsLeft === 0 && score < targetScore) {
LK.showGameOver();
}
}
}, 1000);
// Add several fish swimming in the ocean
var fishArray = [];
for (var i = 0; i < 7; i++) {
var fish = new Fish();
fishArray.push(fish);
game.addChild(fish);
}
// Rare fish (nadir balığı) logic
var rareFish = null;
var rareFishTimer = null;
// Function to spawn rare fish
function spawnRareFish() {
// Remove previous rare fish if exists
if (rareFish && typeof rareFish.destroy === "function") {
rareFish.destroy();
rareFish = null;
}
rareFish = new RareFish();
// Place rare fish at a random position, but ensure it's visible
rareFish.direction = Math.random() < 0.5 ? 1 : -1;
if (rareFish.direction === 1) {
rareFish.x = Math.random() * 1024;
} else {
rareFish.x = 1024 + Math.random() * 1024;
}
rareFish.y = 400 + Math.random() * 1800;
rareFish.speed = (2 + Math.random() * 3) * rareFish.direction;
game.addChild(rareFish);
}
// Start timer to spawn rare fish every 30 seconds
rareFishTimer = LK.setInterval(function () {
spawnRareFish();
}, 30000);
// Spawn the first rare fish immediately
spawnRareFish();
// Add the shark character to the game
var shark = new Shark();
game.addChild(shark);
// Add several red fish swimming in the ocean
var redFishArray = [];
for (var i = 0; i < 3; i++) {
var redFish = new RedFish();
redFishArray.push(redFish);
game.addChild(redFish);
}
// Mouse/touch drag control for shark
var draggingShark = false;
// Handle drag start
game.down = function (x, y, obj) {
// Check if the press is on the shark (simple bounding box check)
var dx = x - shark.x;
var dy = y - shark.y;
// Use shark asset size for hit area
var halfW = 0.5 * shark.width;
var halfH = 0.5 * shark.height;
if (dx >= -halfW && dx <= halfW && dy >= -halfH && dy <= halfH) {
draggingShark = true;
// Move shark immediately to touch point
shark.x = x;
shark.y = y;
}
};
// Handle drag move
game.move = function (x, y, obj) {
if (draggingShark) {
shark.x = x;
shark.y = y;
}
};
// Handle drag end
game.up = function (x, y, obj) {
draggingShark = false;
};
// Update fish and shark positions every frame
game.update = function () {
// Blue fish logic (grow)
for (var i = 0; i < fishArray.length; i++) {
var fish = fishArray[i];
fish.update();
// Track lastWasIntersecting for each fish
if (typeof fish.lastWasIntersecting === "undefined") {
fish.lastWasIntersecting = false;
}
var isIntersecting = shark.intersects(fish);
// If shark just started intersecting with this fish, "eat" it
if (!fish.lastWasIntersecting && isIntersecting) {
// Move fish to a new random position and speed
fish.x = -100;
fish.y = 400 + Math.random() * 1800;
fish.speed = 2 + Math.random() * 3;
// Increment score and update display
score++;
scoreTxt.setText(score + "");
// Grow the shark a bit each time it eats a fish
if (typeof shark._scaleX === "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
}
// Increase scale by 5% per fish eaten
shark._scaleX *= 1.05;
shark._scaleY *= 1.05;
// Clamp max scale to avoid infinite growth
if (shark._scaleX > 3) shark._scaleX = 3;
if (shark._scaleY > 3) shark._scaleY = 3;
shark.scaleX = shark._scaleX;
shark.scaleY = shark._scaleY;
}
fish.lastWasIntersecting = isIntersecting;
}
// Increase blue fish speed every 10 score
if (score > 0 && score % 10 === 0 && typeof game.lastSpeedUpScore !== "number") {
for (var s = 0; s < fishArray.length; s++) {
fishArray[s].speed += 1;
}
game.lastSpeedUpScore = score;
}
if (score % 10 !== 0) {
game.lastSpeedUpScore = undefined;
}
// Red fish logic (shrink)
for (var j = 0; j < redFishArray.length; j++) {
var redFish = redFishArray[j];
redFish.update();
if (typeof redFish.lastWasIntersecting === "undefined") {
redFish.lastWasIntersecting = false;
}
var isRedIntersecting = shark.intersects(redFish);
// If shark just started intersecting with this red fish, "eat" it and shrink
if (!redFish.lastWasIntersecting && isRedIntersecting) {
// Move red fish to a new random position and speed
redFish.x = -100;
redFish.y = 400 + Math.random() * 1800;
redFish.speed = 2 + Math.random() * 3;
// Shrink the shark a bit each time it eats a red fish
if (typeof shark._scaleX === "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
}
// Decrease scale by 10% per red fish eaten
shark._scaleX *= 0.90;
shark._scaleY *= 0.90;
// Clamp min scale to avoid disappearing
if (shark._scaleX < 0.4) shark._scaleX = 0.4;
if (shark._scaleY < 0.4) shark._scaleY = 0.4;
shark.scaleX = shark._scaleX;
shark.scaleY = shark._scaleY;
}
redFish.lastWasIntersecting = isRedIntersecting;
}
// After score reaches 100, increase the number of red fish if not already done
if (score >= 100 && !game.redFishIncreased) {
// Add 5 more red fish
for (var add = 0; add < 5; add++) {
var extraRedFish = new RedFish();
redFishArray.push(extraRedFish);
game.addChild(extraRedFish);
}
game.redFishIncreased = true;
}
// End game if score reaches targetScore before timer runs out (win condition)
if (score >= targetScore && secondsLeft > 0) {
// If currently on level 1, immediately start level 2 instead of showing win screen
if (currentLevel === 1) {
// Set persistent storage for level 2
if (typeof storage !== "undefined" && typeof storage.setItem === "function") {
storage.setItem("level", 2);
}
// Reset level 2 parameters
currentLevel = 2;
secondsLeft = 60;
targetScore = 30; // CHANGED: Level 2 target is 30 for transition to level 3
score = 0;
scoreTxt.setText(score + "");
timerTxt.setText(secondsLeft + "");
// Clear and restart timer
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
timerInterval = LK.setInterval(function () {
if (secondsLeft > 0) {
secondsLeft--;
timerTxt.setText(secondsLeft + "");
if (secondsLeft === 0 && score < targetScore) {
LK.showGameOver();
}
}
}, 1000);
// Optionally reset shark size for new level
if (typeof shark._scaleX !== "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
shark.scaleX = 1;
shark.scaleY = 1;
}
// Optionally reset red fish count for level 2
if (game.redFishIncreased) {
// Remove extra red fish from game
for (var i = redFishArray.length - 1; i >= 3; i--) {
if (typeof redFishArray[i].destroy === "function") {
redFishArray[i].destroy();
}
redFishArray.splice(i, 1);
}
game.redFishIncreased = false;
}
// Move all fish and red fish to new random positions
for (var i = 0; i < fishArray.length; i++) {
fishArray[i].x = Math.random() < 0.5 ? -100 : 2048 + 100;
fishArray[i].y = 400 + Math.random() * 1800;
fishArray[i].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
for (var j = 0; j < redFishArray.length; j++) {
redFishArray[j].x = Math.random() < 0.5 ? -100 : 2048 + 100;
redFishArray[j].y = 400 + Math.random() * 1800;
redFishArray[j].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
// Move shark to center
shark.x = 1024;
shark.y = 1366;
// Return so win screen is not shown
return;
}
// If currently on level 2, immediately start level 3 instead of showing win screen
if (currentLevel === 2) {
// Set persistent storage for level 3
if (typeof storage !== "undefined" && typeof storage.setItem === "function") {
storage.setItem("level", 3);
}
// Reset level 3 parameters
currentLevel = 3;
secondsLeft = 60;
targetScore = 30; // Level 3 target is now 30
score = 0;
scoreTxt.setText(score + "");
timerTxt.setText(secondsLeft + "");
// Clear and restart timer
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
timerInterval = LK.setInterval(function () {
if (secondsLeft > 0) {
secondsLeft--;
timerTxt.setText(secondsLeft + "");
if (secondsLeft === 0 && score < targetScore) {
LK.showGameOver();
}
}
}, 1000);
// Optionally reset shark size for new level
if (typeof shark._scaleX !== "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
shark.scaleX = 1;
shark.scaleY = 1;
}
// Optionally reset red fish count for level 3
if (game.redFishIncreased) {
// Remove extra red fish from game
for (var i = redFishArray.length - 1; i >= 3; i--) {
if (typeof redFishArray[i].destroy === "function") {
redFishArray[i].destroy();
}
redFishArray.splice(i, 1);
}
game.redFishIncreased = false;
}
// Move all fish and red fish to new random positions
for (var i = 0; i < fishArray.length; i++) {
fishArray[i].x = Math.random() < 0.5 ? -100 : 2048 + 100;
fishArray[i].y = 400 + Math.random() * 1800;
fishArray[i].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
for (var j = 0; j < redFishArray.length; j++) {
redFishArray[j].x = Math.random() < 0.5 ? -100 : 2048 + 100;
redFishArray[j].y = 400 + Math.random() * 1800;
redFishArray[j].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
// Move shark to center
shark.x = 1024;
shark.y = 1366;
// Return so win screen is not shown
return;
}
// End game if score reaches targetScore before timer runs out (win condition) in level 3
if (currentLevel === 3 && score >= targetScore && secondsLeft > 0) {
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
if (typeof rareFishTimer !== "undefined") {
LK.clearInterval(rareFishTimer);
rareFishTimer = undefined;
}
LK.showYouWin();
return;
}
// Store level info in persistent storage for next game
if (typeof storage !== "undefined") {
var nextLevel = currentLevel;
if (typeof storage.getItem === "function") {
var storedLevel = parseInt(storage.getItem("level"));
if (!isNaN(storedLevel)) {
nextLevel = storedLevel;
}
}
// If player just won level 1, set next level to 2
if (nextLevel === 1) {
storage.setItem("level", 2);
}
}
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
LK.showYouWin();
}
// Always update shark to track lastX/lastY for movement
shark.update();
// Rare fish (nadir balığı) collision and cleanup
if (rareFish) {
rareFish.update && rareFish.update();
if (typeof rareFish.lastWasIntersecting === "undefined") {
rareFish.lastWasIntersecting = false;
}
var isRareIntersecting = shark.intersects(rareFish);
if (!rareFish.lastWasIntersecting && isRareIntersecting) {
// Play bomb sound
if (typeof LK.getSound === "function") {
var bombSound = LK.getSound('bomb');
if (bombSound && typeof bombSound.play === "function") {
bombSound.play();
}
}
// Remove rare fish from game
if (typeof rareFish.destroy === "function") {
rareFish.destroy();
}
rareFish = null;
// End the game immediately when rare fish is eaten
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
if (typeof rareFishTimer !== "undefined") {
LK.clearInterval(rareFishTimer);
rareFishTimer = undefined;
}
LK.showGameOver();
return;
}
if (rareFish) {
rareFish.lastWasIntersecting = isRareIntersecting;
}
}
// Clear timer if game is over (defensive, in case LK.showGameOver is called elsewhere)
if (typeof timerInterval !== "undefined" && (secondsLeft === 0 || typeof LK._gameOver !== "undefined" && LK._gameOver)) {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
// Also clear rare fish timer if game is over
if (typeof rareFishTimer !== "undefined" && (secondsLeft === 0 || typeof LK._gameOver !== "undefined" && LK._gameOver)) {
LK.clearInterval(rareFishTimer);
rareFishTimer = undefined;
if (rareFish && typeof rareFish.destroy === "function") {
rareFish.destroy();
rareFish = null;
}
}
}; /****
* Classes
****/
// Fish class: represents a single fish swimming in the ocean
var Fish = Container.expand(function () {
var self = Container.call(this);
// Create a simple ellipse as the fish
var fishAsset = self.attachAsset('fish', {
width: 120,
height: 60,
color: 0xffcc66,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
// Randomize initial position, speed, and direction
self.direction = Math.random() < 0.5 ? 1 : -1; // 1: right, -1: left
if (self.direction === 1) {
self.x = Math.random() * 1024; // left half
} else {
self.x = 1024 + Math.random() * 1024; // right half
}
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
// Track lastX for edge detection
self.lastX = self.x;
// Fish swims in both directions, loops back when off screen
self.update = function () {
self.lastX = self.x;
self.x += self.speed;
// Flip fish asset horizontally if swimming left, normal if right
if (self.direction === 1) {
if (typeof fishAsset !== "undefined") fishAsset.scaleX = 1;
} else {
if (typeof fishAsset !== "undefined") fishAsset.scaleX = -1;
}
if (self.direction === 1) {
// rightward
if (self.lastX <= 2048 && self.x > 2048) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
} else {
// leftward
if (self.lastX >= -100 && self.x < -100) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
}
};
return self;
});
var Ocean = Container.expand(function () {
var self = Container.call(this);
// Create a large blue rectangle as the ocean background
var oceanAsset = self.attachAsset('ocean', {
width: 2048,
height: 2732,
color: 0x3399ff,
shape: 'box',
anchorX: 0,
anchorY: 0,
x: 0,
y: 0
});
return self;
});
// RareFish class: represents the rare black fish that appears every 10 seconds
var RareFish = Container.expand(function () {
var self = Container.call(this);
// Attach rare fish asset (black)
var rareFishAsset = self.attachAsset('rarefish', {
width: 240,
height: 120,
color: 0x000000,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
// Randomize initial position, speed, and direction
self.direction = Math.random() < 0.5 ? 1 : -1; // 1: right, -1: left
if (self.direction === 1) {
self.x = Math.random() * 1024;
} else {
self.x = 1024 + Math.random() * 1024;
}
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
self.lastX = self.x;
// Rare fish swims in both directions, loops back when off screen
self.update = function () {
self.lastX = self.x;
self.x += self.speed;
// Flip rare fish asset horizontally if swimming left, normal if right
if (self.direction === 1) {
if (typeof rareFishAsset !== "undefined") rareFishAsset.scaleX = 1;
} else {
if (typeof rareFishAsset !== "undefined") rareFishAsset.scaleX = -1;
}
if (self.direction === 1) {
// rightward
if (self.lastX <= 2048 && self.x > 2048) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
} else {
// leftward
if (self.lastX >= -100 && self.x < -100) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
}
};
return self;
});
// Function to spawn rare fish
// RedFish class: represents a red fish that shrinks the shark
var RedFish = Container.expand(function () {
var self = Container.call(this);
// Attach red fish asset (nadir balığı is black)
var redFishAsset = self.attachAsset('redfish', {
width: 120,
height: 60,
color: 0x000000,
shape: 'ellipse',
anchorX: 0.5,
anchorY: 0.5
});
// Randomize initial position, speed, and direction
self.direction = Math.random() < 0.5 ? 1 : -1; // 1: right, -1: left
if (self.direction === 1) {
self.x = Math.random() * 1024;
} else {
self.x = 1024 + Math.random() * 1024;
}
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
// Track lastX for edge detection
self.lastX = self.x;
// Red fish swims in both directions, loops back when off screen
self.update = function () {
self.lastX = self.x;
self.x += self.speed;
// Flip red fish asset horizontally if swimming left, normal if right
if (self.direction === 1) {
if (typeof redFishAsset !== "undefined") redFishAsset.scaleX = 1;
} else {
if (typeof redFishAsset !== "undefined") redFishAsset.scaleX = -1;
}
if (self.direction === 1) {
// rightward
if (self.lastX <= 2048 && self.x > 2048) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
} else {
// leftward
if (self.lastX >= -100 && self.x < -100) {
self.direction = Math.random() < 0.5 ? 1 : -1;
self.x = self.direction === 1 ? -100 : 2048 + 100;
self.y = 400 + Math.random() * 1800;
self.speed = (2 + Math.random() * 3) * self.direction;
}
}
};
return self;
});
// Shark class: represents the main character (shark) swimming in the ocean
var Shark = Container.expand(function () {
var self = Container.call(this);
// Attach a shark asset (box for now, can be replaced with a shark image asset)
var sharkAsset = self.attachAsset('shark', {
width: 220,
height: 100,
color: 0x444444,
shape: 'box',
anchorX: 0.5,
anchorY: 0.5
});
// Start in the center of the ocean
self.x = 1024;
self.y = 1366;
// Track lastX and lastY for possible future movement logic
self.lastX = self.x;
self.lastY = self.y;
// Shark update: just track lastX/lastY for drag movement
self.update = function () {
self.lastX = self.x;
self.lastY = self.y;
};
return self;
});
/****
* Initialize Game
****/
var game = new LK.Game({
backgroundColor: 0x000000
});
/****
* Game Code
****/
// Hide the mouse cursor during gameplay
if (typeof LK.setCursor === "function") {
LK.setCursor("none");
} else if (typeof stage !== "undefined" && stage.view && stage.view.style) {
stage.view.style.cursor = "none";
}
// Add ocean background
var ocean = new Ocean();
game.addChild(ocean);
// Play water sound as looping background music after first user interaction
var waterMusicStarted = false;
function startWaterMusic() {
if (!waterMusicStarted) {
LK.playMusic('water');
waterMusicStarted = true;
}
}
game.down = function (origDown) {
return function (x, y, obj) {
startWaterMusic();
if (typeof origDown === "function") origDown(x, y, obj);
};
}(game.down);
// Score system
var score = 0;
var scoreTxt = new Text2('0', {
size: 120,
fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
scoreTxt.x = LK.gui.top.width / 2;
scoreTxt.y = 10;
// Timer system
var secondsLeft = 60;
var targetScore = 30;
var currentLevel = 1;
// Check if player is on level 2 (after winning level 1)
if (typeof storage !== "undefined" && typeof storage.getItem === "function") {
var storedLevel = parseInt(storage.getItem("level"));
if (!isNaN(storedLevel) && storedLevel === 2) {
secondsLeft = 60;
targetScore = 120;
currentLevel = 2;
}
}
var timerTxt = new Text2(secondsLeft + '', {
size: 90,
fill: 0xFFFFFF
});
timerTxt.anchor.set(0.5, 1);
LK.gui.bottom.addChild(timerTxt);
// Place timer at bottom center, but a bit higher
timerTxt.x = LK.gui.bottom.width / 2;
timerTxt.y = LK.gui.bottom.height - 120;
// Timer interval
var timerInterval = LK.setInterval(function () {
if (secondsLeft > 0) {
secondsLeft--;
timerTxt.setText(secondsLeft + "");
if (secondsLeft === 0 && score < targetScore) {
LK.showGameOver();
}
}
}, 1000);
// Add several fish swimming in the ocean
var fishArray = [];
for (var i = 0; i < 7; i++) {
var fish = new Fish();
fishArray.push(fish);
game.addChild(fish);
}
// Rare fish (nadir balığı) logic
var rareFish = null;
var rareFishTimer = null;
// Function to spawn rare fish
function spawnRareFish() {
// Remove previous rare fish if exists
if (rareFish && typeof rareFish.destroy === "function") {
rareFish.destroy();
rareFish = null;
}
rareFish = new RareFish();
// Place rare fish at a random position, but ensure it's visible
rareFish.direction = Math.random() < 0.5 ? 1 : -1;
if (rareFish.direction === 1) {
rareFish.x = Math.random() * 1024;
} else {
rareFish.x = 1024 + Math.random() * 1024;
}
rareFish.y = 400 + Math.random() * 1800;
rareFish.speed = (2 + Math.random() * 3) * rareFish.direction;
game.addChild(rareFish);
}
// Start timer to spawn rare fish every 30 seconds
rareFishTimer = LK.setInterval(function () {
spawnRareFish();
}, 30000);
// Spawn the first rare fish immediately
spawnRareFish();
// Add the shark character to the game
var shark = new Shark();
game.addChild(shark);
// Add several red fish swimming in the ocean
var redFishArray = [];
for (var i = 0; i < 3; i++) {
var redFish = new RedFish();
redFishArray.push(redFish);
game.addChild(redFish);
}
// Mouse/touch drag control for shark
var draggingShark = false;
// Handle drag start
game.down = function (x, y, obj) {
// Check if the press is on the shark (simple bounding box check)
var dx = x - shark.x;
var dy = y - shark.y;
// Use shark asset size for hit area
var halfW = 0.5 * shark.width;
var halfH = 0.5 * shark.height;
if (dx >= -halfW && dx <= halfW && dy >= -halfH && dy <= halfH) {
draggingShark = true;
// Move shark immediately to touch point
shark.x = x;
shark.y = y;
}
};
// Handle drag move
game.move = function (x, y, obj) {
if (draggingShark) {
shark.x = x;
shark.y = y;
}
};
// Handle drag end
game.up = function (x, y, obj) {
draggingShark = false;
};
// Update fish and shark positions every frame
game.update = function () {
// Blue fish logic (grow)
for (var i = 0; i < fishArray.length; i++) {
var fish = fishArray[i];
fish.update();
// Track lastWasIntersecting for each fish
if (typeof fish.lastWasIntersecting === "undefined") {
fish.lastWasIntersecting = false;
}
var isIntersecting = shark.intersects(fish);
// If shark just started intersecting with this fish, "eat" it
if (!fish.lastWasIntersecting && isIntersecting) {
// Move fish to a new random position and speed
fish.x = -100;
fish.y = 400 + Math.random() * 1800;
fish.speed = 2 + Math.random() * 3;
// Increment score and update display
score++;
scoreTxt.setText(score + "");
// Grow the shark a bit each time it eats a fish
if (typeof shark._scaleX === "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
}
// Increase scale by 5% per fish eaten
shark._scaleX *= 1.05;
shark._scaleY *= 1.05;
// Clamp max scale to avoid infinite growth
if (shark._scaleX > 3) shark._scaleX = 3;
if (shark._scaleY > 3) shark._scaleY = 3;
shark.scaleX = shark._scaleX;
shark.scaleY = shark._scaleY;
}
fish.lastWasIntersecting = isIntersecting;
}
// Increase blue fish speed every 10 score
if (score > 0 && score % 10 === 0 && typeof game.lastSpeedUpScore !== "number") {
for (var s = 0; s < fishArray.length; s++) {
fishArray[s].speed += 1;
}
game.lastSpeedUpScore = score;
}
if (score % 10 !== 0) {
game.lastSpeedUpScore = undefined;
}
// Red fish logic (shrink)
for (var j = 0; j < redFishArray.length; j++) {
var redFish = redFishArray[j];
redFish.update();
if (typeof redFish.lastWasIntersecting === "undefined") {
redFish.lastWasIntersecting = false;
}
var isRedIntersecting = shark.intersects(redFish);
// If shark just started intersecting with this red fish, "eat" it and shrink
if (!redFish.lastWasIntersecting && isRedIntersecting) {
// Move red fish to a new random position and speed
redFish.x = -100;
redFish.y = 400 + Math.random() * 1800;
redFish.speed = 2 + Math.random() * 3;
// Shrink the shark a bit each time it eats a red fish
if (typeof shark._scaleX === "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
}
// Decrease scale by 10% per red fish eaten
shark._scaleX *= 0.90;
shark._scaleY *= 0.90;
// Clamp min scale to avoid disappearing
if (shark._scaleX < 0.4) shark._scaleX = 0.4;
if (shark._scaleY < 0.4) shark._scaleY = 0.4;
shark.scaleX = shark._scaleX;
shark.scaleY = shark._scaleY;
}
redFish.lastWasIntersecting = isRedIntersecting;
}
// After score reaches 100, increase the number of red fish if not already done
if (score >= 100 && !game.redFishIncreased) {
// Add 5 more red fish
for (var add = 0; add < 5; add++) {
var extraRedFish = new RedFish();
redFishArray.push(extraRedFish);
game.addChild(extraRedFish);
}
game.redFishIncreased = true;
}
// End game if score reaches targetScore before timer runs out (win condition)
if (score >= targetScore && secondsLeft > 0) {
// If currently on level 1, immediately start level 2 instead of showing win screen
if (currentLevel === 1) {
// Set persistent storage for level 2
if (typeof storage !== "undefined" && typeof storage.setItem === "function") {
storage.setItem("level", 2);
}
// Reset level 2 parameters
currentLevel = 2;
secondsLeft = 60;
targetScore = 30; // CHANGED: Level 2 target is 30 for transition to level 3
score = 0;
scoreTxt.setText(score + "");
timerTxt.setText(secondsLeft + "");
// Clear and restart timer
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
timerInterval = LK.setInterval(function () {
if (secondsLeft > 0) {
secondsLeft--;
timerTxt.setText(secondsLeft + "");
if (secondsLeft === 0 && score < targetScore) {
LK.showGameOver();
}
}
}, 1000);
// Optionally reset shark size for new level
if (typeof shark._scaleX !== "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
shark.scaleX = 1;
shark.scaleY = 1;
}
// Optionally reset red fish count for level 2
if (game.redFishIncreased) {
// Remove extra red fish from game
for (var i = redFishArray.length - 1; i >= 3; i--) {
if (typeof redFishArray[i].destroy === "function") {
redFishArray[i].destroy();
}
redFishArray.splice(i, 1);
}
game.redFishIncreased = false;
}
// Move all fish and red fish to new random positions
for (var i = 0; i < fishArray.length; i++) {
fishArray[i].x = Math.random() < 0.5 ? -100 : 2048 + 100;
fishArray[i].y = 400 + Math.random() * 1800;
fishArray[i].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
for (var j = 0; j < redFishArray.length; j++) {
redFishArray[j].x = Math.random() < 0.5 ? -100 : 2048 + 100;
redFishArray[j].y = 400 + Math.random() * 1800;
redFishArray[j].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
// Move shark to center
shark.x = 1024;
shark.y = 1366;
// Return so win screen is not shown
return;
}
// If currently on level 2, immediately start level 3 instead of showing win screen
if (currentLevel === 2) {
// Set persistent storage for level 3
if (typeof storage !== "undefined" && typeof storage.setItem === "function") {
storage.setItem("level", 3);
}
// Reset level 3 parameters
currentLevel = 3;
secondsLeft = 60;
targetScore = 30; // Level 3 target is now 30
score = 0;
scoreTxt.setText(score + "");
timerTxt.setText(secondsLeft + "");
// Clear and restart timer
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
timerInterval = LK.setInterval(function () {
if (secondsLeft > 0) {
secondsLeft--;
timerTxt.setText(secondsLeft + "");
if (secondsLeft === 0 && score < targetScore) {
LK.showGameOver();
}
}
}, 1000);
// Optionally reset shark size for new level
if (typeof shark._scaleX !== "undefined") {
shark._scaleX = 1;
shark._scaleY = 1;
shark.scaleX = 1;
shark.scaleY = 1;
}
// Optionally reset red fish count for level 3
if (game.redFishIncreased) {
// Remove extra red fish from game
for (var i = redFishArray.length - 1; i >= 3; i--) {
if (typeof redFishArray[i].destroy === "function") {
redFishArray[i].destroy();
}
redFishArray.splice(i, 1);
}
game.redFishIncreased = false;
}
// Move all fish and red fish to new random positions
for (var i = 0; i < fishArray.length; i++) {
fishArray[i].x = Math.random() < 0.5 ? -100 : 2048 + 100;
fishArray[i].y = 400 + Math.random() * 1800;
fishArray[i].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
for (var j = 0; j < redFishArray.length; j++) {
redFishArray[j].x = Math.random() < 0.5 ? -100 : 2048 + 100;
redFishArray[j].y = 400 + Math.random() * 1800;
redFishArray[j].speed = (2 + Math.random() * 3) * (Math.random() < 0.5 ? 1 : -1);
}
// Move shark to center
shark.x = 1024;
shark.y = 1366;
// Return so win screen is not shown
return;
}
// End game if score reaches targetScore before timer runs out (win condition) in level 3
if (currentLevel === 3 && score >= targetScore && secondsLeft > 0) {
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
if (typeof rareFishTimer !== "undefined") {
LK.clearInterval(rareFishTimer);
rareFishTimer = undefined;
}
LK.showYouWin();
return;
}
// Store level info in persistent storage for next game
if (typeof storage !== "undefined") {
var nextLevel = currentLevel;
if (typeof storage.getItem === "function") {
var storedLevel = parseInt(storage.getItem("level"));
if (!isNaN(storedLevel)) {
nextLevel = storedLevel;
}
}
// If player just won level 1, set next level to 2
if (nextLevel === 1) {
storage.setItem("level", 2);
}
}
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
LK.showYouWin();
}
// Always update shark to track lastX/lastY for movement
shark.update();
// Rare fish (nadir balığı) collision and cleanup
if (rareFish) {
rareFish.update && rareFish.update();
if (typeof rareFish.lastWasIntersecting === "undefined") {
rareFish.lastWasIntersecting = false;
}
var isRareIntersecting = shark.intersects(rareFish);
if (!rareFish.lastWasIntersecting && isRareIntersecting) {
// Play bomb sound
if (typeof LK.getSound === "function") {
var bombSound = LK.getSound('bomb');
if (bombSound && typeof bombSound.play === "function") {
bombSound.play();
}
}
// Remove rare fish from game
if (typeof rareFish.destroy === "function") {
rareFish.destroy();
}
rareFish = null;
// End the game immediately when rare fish is eaten
if (typeof timerInterval !== "undefined") {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
if (typeof rareFishTimer !== "undefined") {
LK.clearInterval(rareFishTimer);
rareFishTimer = undefined;
}
LK.showGameOver();
return;
}
if (rareFish) {
rareFish.lastWasIntersecting = isRareIntersecting;
}
}
// Clear timer if game is over (defensive, in case LK.showGameOver is called elsewhere)
if (typeof timerInterval !== "undefined" && (secondsLeft === 0 || typeof LK._gameOver !== "undefined" && LK._gameOver)) {
LK.clearInterval(timerInterval);
timerInterval = undefined;
}
// Also clear rare fish timer if game is over
if (typeof rareFishTimer !== "undefined" && (secondsLeft === 0 || typeof LK._gameOver !== "undefined" && LK._gameOver)) {
LK.clearInterval(rareFishTimer);
rareFishTimer = undefined;
if (rareFish && typeof rareFish.destroy === "function") {
rareFish.destroy();
rareFish = null;
}
}
};