/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Animal = Container.expand(function (type) {
	var self = Container.call(this);
	self.type = type;
	self.speed = 3;
	self.isSelected = false;
	var graphics = self.attachAsset(type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.setSelected = function (selected) {
		self.isSelected = selected;
		if (selected) {
			graphics.tint = 0xFFFFFF;
			graphics.alpha = 1.0;
		} else {
			graphics.tint = 0x888888;
			graphics.alpha = 0.7;
		}
	};
	self.getAbility = function () {
		switch (self.type) {
			case 'nemo':
				return 'jump';
			case 'macchiato':
				return 'squeeze';
			case 'carrot':
				return 'dig';
			default:
				return 'none';
		}
	};
	return self;
});
var Passage = Container.expand(function () {
	var self = Container.call(this);
	self.isOpen = false;
	self.requiredAbility = 'none';
	var graphics = self.attachAsset('passage', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.setRequirement = function (ability) {
		self.requiredAbility = ability;
		switch (ability) {
			case 'jump':
				graphics.tint = 0xD2691E; // Nemo's color
				break;
			case 'squeeze':
				graphics.tint = 0x8B4513; // Macchiato's color
				break;
			case 'dig':
				graphics.tint = 0xFFFFFF; // Carrot's color
				break;
		}
	};
	self.open = function () {
		if (!self.isOpen) {
			self.isOpen = true;
			graphics.alpha = 0.3;
		}
	};
	return self;
});
var Switch = Container.expand(function () {
	var self = Container.call(this);
	self.activated = false;
	var graphics = self.attachAsset('switch', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.activate = function () {
		if (!self.activated) {
			self.activated = true;
			graphics.tint = 0x00FF00;
			LK.getSound('switch_activate').play();
			return true;
		}
		return false;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2F4F4F
});
/**** 
* Game Code
****/ 
// Game state
var gameTime = 0;
var acidLevel = 2732 - 150;
var acidRiseSpeed = 0.5;
var currentAnimal = 0;
var switchesActivated = 0;
var totalSwitches = 3;
var gameWon = false;
// Create animals
var animals = [];
var nemo = new Animal('nemo');
var macchiato = new Animal('macchiato');
var carrot = new Animal('carrot');
animals.push(nemo, macchiato, carrot);
// Position animals
nemo.x = 200;
nemo.y = 2200;
macchiato.x = 300;
macchiato.y = 2200;
carrot.x = 400;
carrot.y = 2200;
// Add animals to game
game.addChild(nemo);
game.addChild(macchiato);
game.addChild(carrot);
// Set initial selection
animals[currentAnimal].setSelected(true);
// Create acid
var acid = game.addChild(LK.getAsset('acid', {
	anchorX: 0,
	anchorY: 0,
	x: 0,
	y: acidLevel
}));
// Create walls and obstacles
var walls = [];
for (var i = 0; i < 8; i++) {
	var wall = new Container();
	wall.addChild(LK.getAsset('wall', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	wall.x = Math.random() * 1800 + 200;
	wall.y = Math.random() * 1800 + 400;
	walls.push(wall);
	game.addChild(wall);
}
// Create switches
var switches = [];
for (var i = 0; i < totalSwitches; i++) {
	var switchObj = new Switch();
	switchObj.x = 300 + i * 600;
	switchObj.y = 1000 + Math.random() * 800;
	switches.push(switchObj);
	game.addChild(switchObj);
}
// Create passages
var passages = [];
var abilities = ['jump', 'squeeze', 'dig'];
for (var i = 0; i < 3; i++) {
	var passage = new Passage();
	passage.setRequirement(abilities[i]);
	passage.x = 500 + i * 500;
	passage.y = 800;
	passages.push(passage);
	game.addChild(passage);
}
// Create exit
var exit = game.addChild(LK.getAsset('exit', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 200
}));
// UI
var timeText = new Text2('Time: 0', {
	size: 60,
	fill: 0xFFFFFF
});
timeText.anchor.set(0, 0);
LK.gui.topRight.addChild(timeText);
var instructionText = new Text2('Tap to switch animals', {
	size: 40,
	fill: 0xFFFF00
});
instructionText.anchor.set(0.5, 0);
LK.gui.top.addChild(instructionText);
var animalText = new Text2('Nemo (Jump)', {
	size: 50,
	fill: 0xFFFFFF
});
animalText.anchor.set(0, 0);
LK.gui.topLeft.addChild(animalText);
animalText.x = 120; // Avoid platform menu
// Touch controls
var dragAnimal = null;
game.down = function (x, y, obj) {
	// Check if touching current animal
	var currentAnimalObj = animals[currentAnimal];
	var globalPos = currentAnimalObj.toGlobal({
		x: 0,
		y: 0
	});
	var distance = Math.sqrt(Math.pow(x - globalPos.x, 2) + Math.pow(y - globalPos.y, 2));
	if (distance < 100) {
		dragAnimal = currentAnimalObj;
	} else {
		// Switch animal
		animals[currentAnimal].setSelected(false);
		currentAnimal = (currentAnimal + 1) % 3;
		animals[currentAnimal].setSelected(true);
		// Update UI
		var animalNames = ['Nemo (Jump)', 'Macchiato (Squeeze)', 'Carrot (Dig)'];
		animalText.setText(animalNames[currentAnimal]);
	}
};
game.move = function (x, y, obj) {
	if (dragAnimal) {
		var newX = Math.max(50, Math.min(1998, x));
		var newY = Math.max(50, Math.min(acidLevel - 50, y));
		dragAnimal.x = newX;
		dragAnimal.y = newY;
	}
};
game.up = function (x, y, obj) {
	if (dragAnimal) {
		// Check interactions
		checkSwitchInteraction(dragAnimal);
		checkPassageInteraction(dragAnimal);
		checkExitInteraction(dragAnimal);
		dragAnimal = null;
	}
};
// Interaction functions
function checkSwitchInteraction(animal) {
	for (var i = 0; i < switches.length; i++) {
		var switchObj = switches[i];
		if (!switchObj.activated && animal.intersects(switchObj)) {
			if (switchObj.activate()) {
				switchesActivated++;
				// Open corresponding passage
				if (passages[i]) {
					passages[i].open();
				}
				if (switchesActivated >= totalSwitches) {
					instructionText.setText('All switches activated! Reach the exit!');
				}
			}
		}
	}
}
function checkPassageInteraction(animal) {
	for (var i = 0; i < passages.length; i++) {
		var passage = passages[i];
		if (animal.intersects(passage)) {
			if (passage.requiredAbility === animal.getAbility() && !passage.isOpen) {
				passage.open();
				instructionText.setText('Passage opened by ' + animal.type + '!');
			}
		}
	}
}
function checkExitInteraction(animal) {
	if (animal.intersects(exit) && switchesActivated >= totalSwitches) {
		// Check if all animals are near exit
		var allNearExit = true;
		for (var i = 0; i < animals.length; i++) {
			var dist = Math.sqrt(Math.pow(animals[i].x - exit.x, 2) + Math.pow(animals[i].y - exit.y, 2));
			if (dist > 200) {
				allNearExit = false;
				break;
			}
		}
		if (allNearExit && !gameWon) {
			gameWon = true;
			LK.getSound('escape_success').play();
			LK.showYouWin();
		} else if (!allNearExit) {
			instructionText.setText('Get all animals to the exit!');
		}
	}
}
// Main update loop
game.update = function () {
	gameTime++;
	if (gameWon) return;
	// Update time display
	var seconds = Math.floor(gameTime / 60);
	timeText.setText('Time: ' + seconds + 's');
	// Raise acid level
	acidLevel -= acidRiseSpeed;
	acid.y = acidLevel;
	// Check if any animal touches acid
	for (var i = 0; i < animals.length; i++) {
		if (animals[i].y + 40 > acidLevel) {
			LK.getSound('acid_warning').play();
			LK.effects.flashScreen(0x32CD32, 500);
			LK.showGameOver();
			return;
		}
	}
	// Increase difficulty over time
	if (gameTime % 600 === 0) {
		// Every 10 seconds
		acidRiseSpeed += 0.1;
	}
	// Warning when acid gets close
	if (acidLevel < 2000 && gameTime % 120 === 0) {
		LK.effects.flashObject(acid, 0xFF0000, 300);
	}
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Animal = Container.expand(function (type) {
	var self = Container.call(this);
	self.type = type;
	self.speed = 3;
	self.isSelected = false;
	var graphics = self.attachAsset(type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.setSelected = function (selected) {
		self.isSelected = selected;
		if (selected) {
			graphics.tint = 0xFFFFFF;
			graphics.alpha = 1.0;
		} else {
			graphics.tint = 0x888888;
			graphics.alpha = 0.7;
		}
	};
	self.getAbility = function () {
		switch (self.type) {
			case 'nemo':
				return 'jump';
			case 'macchiato':
				return 'squeeze';
			case 'carrot':
				return 'dig';
			default:
				return 'none';
		}
	};
	return self;
});
var Passage = Container.expand(function () {
	var self = Container.call(this);
	self.isOpen = false;
	self.requiredAbility = 'none';
	var graphics = self.attachAsset('passage', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.setRequirement = function (ability) {
		self.requiredAbility = ability;
		switch (ability) {
			case 'jump':
				graphics.tint = 0xD2691E; // Nemo's color
				break;
			case 'squeeze':
				graphics.tint = 0x8B4513; // Macchiato's color
				break;
			case 'dig':
				graphics.tint = 0xFFFFFF; // Carrot's color
				break;
		}
	};
	self.open = function () {
		if (!self.isOpen) {
			self.isOpen = true;
			graphics.alpha = 0.3;
		}
	};
	return self;
});
var Switch = Container.expand(function () {
	var self = Container.call(this);
	self.activated = false;
	var graphics = self.attachAsset('switch', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.activate = function () {
		if (!self.activated) {
			self.activated = true;
			graphics.tint = 0x00FF00;
			LK.getSound('switch_activate').play();
			return true;
		}
		return false;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2F4F4F
});
/**** 
* Game Code
****/ 
// Game state
var gameTime = 0;
var acidLevel = 2732 - 150;
var acidRiseSpeed = 0.5;
var currentAnimal = 0;
var switchesActivated = 0;
var totalSwitches = 3;
var gameWon = false;
// Create animals
var animals = [];
var nemo = new Animal('nemo');
var macchiato = new Animal('macchiato');
var carrot = new Animal('carrot');
animals.push(nemo, macchiato, carrot);
// Position animals
nemo.x = 200;
nemo.y = 2200;
macchiato.x = 300;
macchiato.y = 2200;
carrot.x = 400;
carrot.y = 2200;
// Add animals to game
game.addChild(nemo);
game.addChild(macchiato);
game.addChild(carrot);
// Set initial selection
animals[currentAnimal].setSelected(true);
// Create acid
var acid = game.addChild(LK.getAsset('acid', {
	anchorX: 0,
	anchorY: 0,
	x: 0,
	y: acidLevel
}));
// Create walls and obstacles
var walls = [];
for (var i = 0; i < 8; i++) {
	var wall = new Container();
	wall.addChild(LK.getAsset('wall', {
		anchorX: 0.5,
		anchorY: 0.5
	}));
	wall.x = Math.random() * 1800 + 200;
	wall.y = Math.random() * 1800 + 400;
	walls.push(wall);
	game.addChild(wall);
}
// Create switches
var switches = [];
for (var i = 0; i < totalSwitches; i++) {
	var switchObj = new Switch();
	switchObj.x = 300 + i * 600;
	switchObj.y = 1000 + Math.random() * 800;
	switches.push(switchObj);
	game.addChild(switchObj);
}
// Create passages
var passages = [];
var abilities = ['jump', 'squeeze', 'dig'];
for (var i = 0; i < 3; i++) {
	var passage = new Passage();
	passage.setRequirement(abilities[i]);
	passage.x = 500 + i * 500;
	passage.y = 800;
	passages.push(passage);
	game.addChild(passage);
}
// Create exit
var exit = game.addChild(LK.getAsset('exit', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 200
}));
// UI
var timeText = new Text2('Time: 0', {
	size: 60,
	fill: 0xFFFFFF
});
timeText.anchor.set(0, 0);
LK.gui.topRight.addChild(timeText);
var instructionText = new Text2('Tap to switch animals', {
	size: 40,
	fill: 0xFFFF00
});
instructionText.anchor.set(0.5, 0);
LK.gui.top.addChild(instructionText);
var animalText = new Text2('Nemo (Jump)', {
	size: 50,
	fill: 0xFFFFFF
});
animalText.anchor.set(0, 0);
LK.gui.topLeft.addChild(animalText);
animalText.x = 120; // Avoid platform menu
// Touch controls
var dragAnimal = null;
game.down = function (x, y, obj) {
	// Check if touching current animal
	var currentAnimalObj = animals[currentAnimal];
	var globalPos = currentAnimalObj.toGlobal({
		x: 0,
		y: 0
	});
	var distance = Math.sqrt(Math.pow(x - globalPos.x, 2) + Math.pow(y - globalPos.y, 2));
	if (distance < 100) {
		dragAnimal = currentAnimalObj;
	} else {
		// Switch animal
		animals[currentAnimal].setSelected(false);
		currentAnimal = (currentAnimal + 1) % 3;
		animals[currentAnimal].setSelected(true);
		// Update UI
		var animalNames = ['Nemo (Jump)', 'Macchiato (Squeeze)', 'Carrot (Dig)'];
		animalText.setText(animalNames[currentAnimal]);
	}
};
game.move = function (x, y, obj) {
	if (dragAnimal) {
		var newX = Math.max(50, Math.min(1998, x));
		var newY = Math.max(50, Math.min(acidLevel - 50, y));
		dragAnimal.x = newX;
		dragAnimal.y = newY;
	}
};
game.up = function (x, y, obj) {
	if (dragAnimal) {
		// Check interactions
		checkSwitchInteraction(dragAnimal);
		checkPassageInteraction(dragAnimal);
		checkExitInteraction(dragAnimal);
		dragAnimal = null;
	}
};
// Interaction functions
function checkSwitchInteraction(animal) {
	for (var i = 0; i < switches.length; i++) {
		var switchObj = switches[i];
		if (!switchObj.activated && animal.intersects(switchObj)) {
			if (switchObj.activate()) {
				switchesActivated++;
				// Open corresponding passage
				if (passages[i]) {
					passages[i].open();
				}
				if (switchesActivated >= totalSwitches) {
					instructionText.setText('All switches activated! Reach the exit!');
				}
			}
		}
	}
}
function checkPassageInteraction(animal) {
	for (var i = 0; i < passages.length; i++) {
		var passage = passages[i];
		if (animal.intersects(passage)) {
			if (passage.requiredAbility === animal.getAbility() && !passage.isOpen) {
				passage.open();
				instructionText.setText('Passage opened by ' + animal.type + '!');
			}
		}
	}
}
function checkExitInteraction(animal) {
	if (animal.intersects(exit) && switchesActivated >= totalSwitches) {
		// Check if all animals are near exit
		var allNearExit = true;
		for (var i = 0; i < animals.length; i++) {
			var dist = Math.sqrt(Math.pow(animals[i].x - exit.x, 2) + Math.pow(animals[i].y - exit.y, 2));
			if (dist > 200) {
				allNearExit = false;
				break;
			}
		}
		if (allNearExit && !gameWon) {
			gameWon = true;
			LK.getSound('escape_success').play();
			LK.showYouWin();
		} else if (!allNearExit) {
			instructionText.setText('Get all animals to the exit!');
		}
	}
}
// Main update loop
game.update = function () {
	gameTime++;
	if (gameWon) return;
	// Update time display
	var seconds = Math.floor(gameTime / 60);
	timeText.setText('Time: ' + seconds + 's');
	// Raise acid level
	acidLevel -= acidRiseSpeed;
	acid.y = acidLevel;
	// Check if any animal touches acid
	for (var i = 0; i < animals.length; i++) {
		if (animals[i].y + 40 > acidLevel) {
			LK.getSound('acid_warning').play();
			LK.effects.flashScreen(0x32CD32, 500);
			LK.showGameOver();
			return;
		}
	}
	// Increase difficulty over time
	if (gameTime % 600 === 0) {
		// Every 10 seconds
		acidRiseSpeed += 0.1;
	}
	// Warning when acid gets close
	if (acidLevel < 2000 && gameTime % 120 === 0) {
		LK.effects.flashObject(acid, 0xFF0000, 300);
	}
};