/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0,
	level: 1
});
/**** 
* Classes
****/ 
var Banana = Container.expand(function () {
	var self = Container.call(this);
	var bananaGraphics = self.attachAsset('banana', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collected = false;
	self.value = 10;
	// Animation
	self.update = function () {
		self.rotation += 0.02;
		// Create scaling effect based on vertical position to simulate depth
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	// Collect this banana
	self.collect = function () {
		if (self.collected) {
			return;
		}
		self.collected = true;
		LK.effects.flashObject(self, 0xFFFFFF, 500);
		// Tween to disappear
		tween(self, {
			alpha: 0,
			scaleX: 2,
			scaleY: 2
		}, {
			duration: 500,
			onFinish: function onFinish() {
				self.destroy();
			}
		});
		// Play collect sound
		LK.getSound('collect').play();
		return self.value;
	};
	return self;
});
var Cloud = Container.expand(function () {
	var self = Container.call(this);
	var cloudGraphics = self.attachAsset('cloud', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.8
	});
	self.speed = 0.2 + Math.random() * 0.3;
	self.update = function () {
		self.x -= self.speed;
		// Loop back to right side when off screen
		if (self.x < -cloudGraphics.width) {
			self.x = 2048 + cloudGraphics.width;
			self.y = Math.random() * 500;
		}
	};
	return self;
});
var Monkey = Container.expand(function () {
	var self = Container.call(this);
	// Monkey states
	self.isSwinging = false;
	self.canSwing = true;
	self.swingSpeed = 0;
	self.swingAngle = 0;
	self.targetVine = null;
	self.currentVine = null;
	self.gravity = 1.2;
	self.velocity = {
		x: 0,
		y: 0
	};
	self.onGround = false;
	self.stunned = false;
	// Monkey graphics
	var monkeyGraphics = self.attachAsset('monkey', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Swing line (only visible when swinging)
	self.swingLine = self.addChild(LK.getAsset('swingLine', {
		anchorX: 0.5,
		anchorY: 0,
		alpha: 0
	}));
	// Update monkey position and state
	self.update = function () {
		if (self.stunned) {
			return;
		}
		if (self.isSwinging) {
			// Update swing angle
			self.swingAngle += self.swingSpeed;
			// Apply some drag to the swing speed
			self.swingSpeed *= 0.99;
			// Calculate position based on swing angle
			if (self.currentVine) {
				var swingRadius = self.currentVine.height * 0.8;
				self.x = self.currentVine.x + Math.sin(self.swingAngle) * swingRadius;
				self.y = self.currentVine.y + self.currentVine.height * 0.5 + Math.cos(self.swingAngle) * swingRadius;
				// Update swing line
				self.swingLine.x = 0;
				self.swingLine.y = 0;
				self.swingLine.rotation = Math.atan2(self.currentVine.x - self.x, self.currentVine.y + self.currentVine.height * 0.5 - self.y);
				self.swingLine.height = Math.sqrt(Math.pow(self.currentVine.x - self.x, 2) + Math.pow(self.currentVine.y + self.currentVine.height * 0.5 - self.y, 2));
				self.swingLine.alpha = 1;
			}
		} else {
			// Apply gravity and move monkey
			self.velocity.y += self.gravity;
			self.x += self.velocity.x;
			self.y += self.velocity.y;
			self.swingLine.alpha = 0;
			// Check if on ground
			if (self.y >= groundY - monkeyGraphics.height / 2) {
				self.y = groundY - monkeyGraphics.height / 2;
				self.velocity.y = 0;
				self.velocity.x *= 0.9; // Friction
				self.onGround = true;
			} else {
				self.onGround = false;
			}
		}
		// Create scaling effect based on vertical position to simulate depth
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	// Start swinging from a vine
	self.startSwinging = function (vine) {
		if (!self.canSwing || self.stunned) {
			return;
		}
		self.isSwinging = true;
		self.currentVine = vine;
		// Calculate the initial angle
		var dx = self.x - vine.x;
		var dy = self.y - (vine.y + vine.height * 0.5);
		self.swingAngle = Math.atan2(dx, dy);
		// Set an initial swing speed based on current velocity
		self.swingSpeed = self.velocity.x * 0.01 || 0.05;
		// Play swing sound
		LK.getSound('swing').play();
	};
	// Release from the current swing
	self.releaseSwing = function () {
		if (!self.isSwinging || self.stunned) {
			return;
		}
		// Calculate release velocity based on current swing
		var releaseSpeed = 15 * Math.abs(self.swingSpeed);
		self.velocity.x = Math.sin(self.swingAngle) * releaseSpeed;
		self.velocity.y = Math.cos(self.swingAngle) * releaseSpeed;
		// Reset swing state
		self.isSwinging = false;
		self.currentVine = null;
		self.canSwing = false;
		// Allow swinging again after a short delay
		LK.setTimeout(function () {
			self.canSwing = true;
		}, 300);
		// Play jump sound
		LK.getSound('jump').play();
	};
	// Stun the monkey (when hitting obstacles)
	self.getStunned = function () {
		if (self.stunned) {
			return;
		}
		self.stunned = true;
		self.isSwinging = false;
		self.swingLine.alpha = 0;
		// Flash monkey red
		LK.effects.flashObject(self, 0xFF0000, 1000);
		// Recover after 1.5 seconds
		LK.setTimeout(function () {
			self.stunned = false;
		}, 1500);
		// Play crash sound
		LK.getSound('crash').play();
	};
	return self;
});
var Predator = Container.expand(function () {
	var self = Container.call(this);
	var predatorGraphics = self.attachAsset('predator', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = {
		x: 0,
		y: 0
	};
	self.speed = 2 + Math.random() * 2;
	self.direction = Math.random() > 0.5 ? 1 : -1;
	// Movement pattern
	self.update = function () {
		self.x += self.speed * self.direction;
		// Bounce off screen edges
		if (self.x < 100 || self.x > 2048 - 100) {
			self.direction *= -1;
			predatorGraphics.scaleX = -predatorGraphics.scaleX; // Flip sprite
		}
		// Create scaling effect based on vertical position to simulate depth
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	return self;
});
var Tree = Container.expand(function () {
	var self = Container.call(this);
	var treeGraphics = self.attachAsset('tree', {
		anchorX: 0.5,
		anchorY: 1
	});
	// Add some leaves to the top
	var leaves = self.addChild(LK.getAsset('leaves', {
		anchorX: 0.5,
		anchorY: 1,
		y: -treeGraphics.height,
		scaleX: 1.5,
		scaleY: 1.2
	}));
	// Create scaling effect based on vertical position to simulate depth
	self.updateDepth = function () {
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	return self;
});
var Vine = Container.expand(function () {
	var self = Container.call(this);
	var vineGraphics = self.attachAsset('vine', {
		anchorX: 0.5,
		anchorY: 0
	});
	// Randomize height a bit
	vineGraphics.height = 300 + Math.random() * 200;
	// Add some leaves to the top
	var leaves = self.addChild(LK.getAsset('leaves', {
		anchorX: 0.5,
		anchorY: 1,
		y: 0,
		scaleX: 0.7,
		scaleY: 0.7
	}));
	// Create scaling effect based on vertical position to simulate depth
	self.updateDepth = function () {
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Sky blue background
});
/**** 
* Game Code
****/ 
// Game variables
var score = 0;
var highScore = storage.highScore || 0;
var level = storage.level || 1;
var gameSpeed = 1 + level * 0.1;
var groundY = 2400;
var nextVineX = 400;
var distance = 0;
// Game elements
var monkey;
var vines = [];
var bananas = [];
var predators = [];
var clouds = [];
var trees = [];
var ground;
// Create ground
ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0.5,
	anchorY: 0,
	x: 2048 / 2,
	y: groundY
}));
// Create clouds
for (var i = 0; i < 6; i++) {
	var cloud = new Cloud();
	cloud.x = Math.random() * 2048;
	cloud.y = 100 + Math.random() * 400;
	clouds.push(cloud);
	game.addChild(cloud);
}
// Create trees for the background
for (var i = 0; i < 10; i++) {
	var tree = new Tree();
	tree.x = Math.random() * 2048;
	tree.y = groundY;
	trees.push(tree);
	game.addChild(tree);
	tree.updateDepth();
}
// Create initial vines
for (var i = 0; i < 8; i++) {
	var vine = new Vine();
	vine.x = nextVineX;
	vine.y = 200 + Math.random() * 300;
	vines.push(vine);
	game.addChild(vine);
	vine.updateDepth();
	// Place a banana near some vines
	if (Math.random() < 0.7) {
		var banana = new Banana();
		banana.x = vine.x + (Math.random() * 200 - 100);
		banana.y = vine.y + 200 + Math.random() * 200;
		bananas.push(banana);
		game.addChild(banana);
	}
	nextVineX += 250 + Math.random() * 150;
}
// Create player
monkey = new Monkey();
monkey.x = 200;
monkey.y = 500;
game.addChild(monkey);
// Create predators
function spawnPredator() {
	var predator = new Predator();
	predator.x = Math.random() * 2048;
	predator.y = groundY - 120;
	predators.push(predator);
	game.addChild(predator);
	// Schedule next predator
	LK.setTimeout(spawnPredator, 10000 / gameSpeed);
}
// Start spawning predators after a delay
LK.setTimeout(spawnPredator, 5000);
// Create UI elements
var scoreTxt = new Text2('Score: 0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
var levelTxt = new Text2('Level: ' + level, {
	size: 80,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(levelTxt);
levelTxt.y = 90;
var highScoreTxt = new Text2('High Score: ' + highScore, {
	size: 60,
	fill: 0xFFFF00
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 10;
// Drag handling
var dragActive = false;
// Input handling
game.down = function (x, y, obj) {
	dragActive = true;
	handleInput(x, y);
};
game.up = function (x, y, obj) {
	dragActive = false;
	if (monkey.isSwinging) {
		monkey.releaseSwing();
	}
};
game.move = function (x, y, obj) {
	if (dragActive) {
		handleInput(x, y);
	}
};
function handleInput(x, y) {
	if (!monkey.isSwinging) {
		// Find closest vine to swing from
		var closestVine = null;
		var closestDist = 300; // Maximum swinging distance
		for (var i = 0; i < vines.length; i++) {
			var vine = vines[i];
			var dist = Math.sqrt(Math.pow(monkey.x - vine.x, 2) + Math.pow(monkey.y - (vine.y + vine.height * 0.5), 2));
			if (dist < closestDist) {
				closestDist = dist;
				closestVine = vine;
			}
		}
		if (closestVine) {
			monkey.startSwinging(closestVine);
		}
	}
}
// Main game update
game.update = function () {
	// Update game elements
	distance += gameSpeed;
	// Check if we need to increase level
	if (Math.floor(distance / 5000) + 1 > level) {
		level = Math.floor(distance / 5000) + 1;
		gameSpeed = 1 + level * 0.1;
		levelTxt.setText('Level: ' + level);
		storage.level = level;
	}
	// Camera follows monkey with some lag
	var targetX = Math.max(2048 / 2, monkey.x);
	game.x = Math.lerp(game.x, 2048 / 2 - targetX, 0.05);
	// Check if monkey is off screen
	if (monkey.y > 2732 + 100 || monkey.x < -500) {
		// Game over
		LK.showGameOver();
		return;
	}
	// Generate new vines as we move
	if (nextVineX + game.x < 2048) {
		var vine = new Vine();
		vine.x = nextVineX;
		vine.y = 200 + Math.random() * 300;
		vines.push(vine);
		game.addChild(vine);
		vine.updateDepth();
		// Place a banana near some vines
		if (Math.random() < 0.7) {
			var banana = new Banana();
			banana.x = vine.x + (Math.random() * 200 - 100);
			banana.y = vine.y + 200 + Math.random() * 200;
			bananas.push(banana);
			game.addChild(banana);
		}
		nextVineX += 250 + Math.random() * 150;
	}
	// Update all game elements
	for (var i = clouds.length - 1; i >= 0; i--) {
		clouds[i].update();
	}
	for (var i = bananas.length - 1; i >= 0; i--) {
		var banana = bananas[i];
		banana.update();
		// Check collision with monkey
		if (!banana.collected && monkey.intersects(banana)) {
			var points = banana.collect();
			score += points;
			scoreTxt.setText('Score: ' + score);
			// Update high score
			if (score > highScore) {
				highScore = score;
				highScoreTxt.setText('High Score: ' + highScore);
				storage.highScore = highScore;
			}
			// Remove from array
			bananas.splice(i, 1);
		}
	}
	for (var i = predators.length - 1; i >= 0; i--) {
		var predator = predators[i];
		predator.update();
		// Check collision with monkey
		if (!monkey.stunned && monkey.intersects(predator)) {
			monkey.getStunned();
			// Reduce score
			score = Math.max(0, score - 50);
			scoreTxt.setText('Score: ' + score);
		}
		// Remove predators that are far behind
		if (predator.x < monkey.x - 2048) {
			predator.destroy();
			predators.splice(i, 1);
		}
	}
	// Check for win condition (placeholder - reach level 10)
	if (level >= 10 && score >= 1000) {
		LK.showYouWin();
	}
};
// Helper function for lerping (not available in standard JS)
Math.lerp = function (a, b, t) {
	return a + (b - a) * t;
};
// Play background music
LK.playMusic('jungleMusic'); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highScore: 0,
	level: 1
});
/**** 
* Classes
****/ 
var Banana = Container.expand(function () {
	var self = Container.call(this);
	var bananaGraphics = self.attachAsset('banana', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.collected = false;
	self.value = 10;
	// Animation
	self.update = function () {
		self.rotation += 0.02;
		// Create scaling effect based on vertical position to simulate depth
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	// Collect this banana
	self.collect = function () {
		if (self.collected) {
			return;
		}
		self.collected = true;
		LK.effects.flashObject(self, 0xFFFFFF, 500);
		// Tween to disappear
		tween(self, {
			alpha: 0,
			scaleX: 2,
			scaleY: 2
		}, {
			duration: 500,
			onFinish: function onFinish() {
				self.destroy();
			}
		});
		// Play collect sound
		LK.getSound('collect').play();
		return self.value;
	};
	return self;
});
var Cloud = Container.expand(function () {
	var self = Container.call(this);
	var cloudGraphics = self.attachAsset('cloud', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.8
	});
	self.speed = 0.2 + Math.random() * 0.3;
	self.update = function () {
		self.x -= self.speed;
		// Loop back to right side when off screen
		if (self.x < -cloudGraphics.width) {
			self.x = 2048 + cloudGraphics.width;
			self.y = Math.random() * 500;
		}
	};
	return self;
});
var Monkey = Container.expand(function () {
	var self = Container.call(this);
	// Monkey states
	self.isSwinging = false;
	self.canSwing = true;
	self.swingSpeed = 0;
	self.swingAngle = 0;
	self.targetVine = null;
	self.currentVine = null;
	self.gravity = 1.2;
	self.velocity = {
		x: 0,
		y: 0
	};
	self.onGround = false;
	self.stunned = false;
	// Monkey graphics
	var monkeyGraphics = self.attachAsset('monkey', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Swing line (only visible when swinging)
	self.swingLine = self.addChild(LK.getAsset('swingLine', {
		anchorX: 0.5,
		anchorY: 0,
		alpha: 0
	}));
	// Update monkey position and state
	self.update = function () {
		if (self.stunned) {
			return;
		}
		if (self.isSwinging) {
			// Update swing angle
			self.swingAngle += self.swingSpeed;
			// Apply some drag to the swing speed
			self.swingSpeed *= 0.99;
			// Calculate position based on swing angle
			if (self.currentVine) {
				var swingRadius = self.currentVine.height * 0.8;
				self.x = self.currentVine.x + Math.sin(self.swingAngle) * swingRadius;
				self.y = self.currentVine.y + self.currentVine.height * 0.5 + Math.cos(self.swingAngle) * swingRadius;
				// Update swing line
				self.swingLine.x = 0;
				self.swingLine.y = 0;
				self.swingLine.rotation = Math.atan2(self.currentVine.x - self.x, self.currentVine.y + self.currentVine.height * 0.5 - self.y);
				self.swingLine.height = Math.sqrt(Math.pow(self.currentVine.x - self.x, 2) + Math.pow(self.currentVine.y + self.currentVine.height * 0.5 - self.y, 2));
				self.swingLine.alpha = 1;
			}
		} else {
			// Apply gravity and move monkey
			self.velocity.y += self.gravity;
			self.x += self.velocity.x;
			self.y += self.velocity.y;
			self.swingLine.alpha = 0;
			// Check if on ground
			if (self.y >= groundY - monkeyGraphics.height / 2) {
				self.y = groundY - monkeyGraphics.height / 2;
				self.velocity.y = 0;
				self.velocity.x *= 0.9; // Friction
				self.onGround = true;
			} else {
				self.onGround = false;
			}
		}
		// Create scaling effect based on vertical position to simulate depth
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	// Start swinging from a vine
	self.startSwinging = function (vine) {
		if (!self.canSwing || self.stunned) {
			return;
		}
		self.isSwinging = true;
		self.currentVine = vine;
		// Calculate the initial angle
		var dx = self.x - vine.x;
		var dy = self.y - (vine.y + vine.height * 0.5);
		self.swingAngle = Math.atan2(dx, dy);
		// Set an initial swing speed based on current velocity
		self.swingSpeed = self.velocity.x * 0.01 || 0.05;
		// Play swing sound
		LK.getSound('swing').play();
	};
	// Release from the current swing
	self.releaseSwing = function () {
		if (!self.isSwinging || self.stunned) {
			return;
		}
		// Calculate release velocity based on current swing
		var releaseSpeed = 15 * Math.abs(self.swingSpeed);
		self.velocity.x = Math.sin(self.swingAngle) * releaseSpeed;
		self.velocity.y = Math.cos(self.swingAngle) * releaseSpeed;
		// Reset swing state
		self.isSwinging = false;
		self.currentVine = null;
		self.canSwing = false;
		// Allow swinging again after a short delay
		LK.setTimeout(function () {
			self.canSwing = true;
		}, 300);
		// Play jump sound
		LK.getSound('jump').play();
	};
	// Stun the monkey (when hitting obstacles)
	self.getStunned = function () {
		if (self.stunned) {
			return;
		}
		self.stunned = true;
		self.isSwinging = false;
		self.swingLine.alpha = 0;
		// Flash monkey red
		LK.effects.flashObject(self, 0xFF0000, 1000);
		// Recover after 1.5 seconds
		LK.setTimeout(function () {
			self.stunned = false;
		}, 1500);
		// Play crash sound
		LK.getSound('crash').play();
	};
	return self;
});
var Predator = Container.expand(function () {
	var self = Container.call(this);
	var predatorGraphics = self.attachAsset('predator', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocity = {
		x: 0,
		y: 0
	};
	self.speed = 2 + Math.random() * 2;
	self.direction = Math.random() > 0.5 ? 1 : -1;
	// Movement pattern
	self.update = function () {
		self.x += self.speed * self.direction;
		// Bounce off screen edges
		if (self.x < 100 || self.x > 2048 - 100) {
			self.direction *= -1;
			predatorGraphics.scaleX = -predatorGraphics.scaleX; // Flip sprite
		}
		// Create scaling effect based on vertical position to simulate depth
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	return self;
});
var Tree = Container.expand(function () {
	var self = Container.call(this);
	var treeGraphics = self.attachAsset('tree', {
		anchorX: 0.5,
		anchorY: 1
	});
	// Add some leaves to the top
	var leaves = self.addChild(LK.getAsset('leaves', {
		anchorX: 0.5,
		anchorY: 1,
		y: -treeGraphics.height,
		scaleX: 1.5,
		scaleY: 1.2
	}));
	// Create scaling effect based on vertical position to simulate depth
	self.updateDepth = function () {
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	return self;
});
var Vine = Container.expand(function () {
	var self = Container.call(this);
	var vineGraphics = self.attachAsset('vine', {
		anchorX: 0.5,
		anchorY: 0
	});
	// Randomize height a bit
	vineGraphics.height = 300 + Math.random() * 200;
	// Add some leaves to the top
	var leaves = self.addChild(LK.getAsset('leaves', {
		anchorX: 0.5,
		anchorY: 1,
		y: 0,
		scaleX: 0.7,
		scaleY: 0.7
	}));
	// Create scaling effect based on vertical position to simulate depth
	self.updateDepth = function () {
		var scale = 0.5 + self.y / 2732 * 0.5;
		self.scale.set(scale, scale);
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Sky blue background
});
/**** 
* Game Code
****/ 
// Game variables
var score = 0;
var highScore = storage.highScore || 0;
var level = storage.level || 1;
var gameSpeed = 1 + level * 0.1;
var groundY = 2400;
var nextVineX = 400;
var distance = 0;
// Game elements
var monkey;
var vines = [];
var bananas = [];
var predators = [];
var clouds = [];
var trees = [];
var ground;
// Create ground
ground = game.addChild(LK.getAsset('ground', {
	anchorX: 0.5,
	anchorY: 0,
	x: 2048 / 2,
	y: groundY
}));
// Create clouds
for (var i = 0; i < 6; i++) {
	var cloud = new Cloud();
	cloud.x = Math.random() * 2048;
	cloud.y = 100 + Math.random() * 400;
	clouds.push(cloud);
	game.addChild(cloud);
}
// Create trees for the background
for (var i = 0; i < 10; i++) {
	var tree = new Tree();
	tree.x = Math.random() * 2048;
	tree.y = groundY;
	trees.push(tree);
	game.addChild(tree);
	tree.updateDepth();
}
// Create initial vines
for (var i = 0; i < 8; i++) {
	var vine = new Vine();
	vine.x = nextVineX;
	vine.y = 200 + Math.random() * 300;
	vines.push(vine);
	game.addChild(vine);
	vine.updateDepth();
	// Place a banana near some vines
	if (Math.random() < 0.7) {
		var banana = new Banana();
		banana.x = vine.x + (Math.random() * 200 - 100);
		banana.y = vine.y + 200 + Math.random() * 200;
		bananas.push(banana);
		game.addChild(banana);
	}
	nextVineX += 250 + Math.random() * 150;
}
// Create player
monkey = new Monkey();
monkey.x = 200;
monkey.y = 500;
game.addChild(monkey);
// Create predators
function spawnPredator() {
	var predator = new Predator();
	predator.x = Math.random() * 2048;
	predator.y = groundY - 120;
	predators.push(predator);
	game.addChild(predator);
	// Schedule next predator
	LK.setTimeout(spawnPredator, 10000 / gameSpeed);
}
// Start spawning predators after a delay
LK.setTimeout(spawnPredator, 5000);
// Create UI elements
var scoreTxt = new Text2('Score: 0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
var levelTxt = new Text2('Level: ' + level, {
	size: 80,
	fill: 0xFFFFFF
});
levelTxt.anchor.set(1, 0);
LK.gui.topRight.addChild(levelTxt);
levelTxt.y = 90;
var highScoreTxt = new Text2('High Score: ' + highScore, {
	size: 60,
	fill: 0xFFFF00
});
highScoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(highScoreTxt);
highScoreTxt.y = 10;
// Drag handling
var dragActive = false;
// Input handling
game.down = function (x, y, obj) {
	dragActive = true;
	handleInput(x, y);
};
game.up = function (x, y, obj) {
	dragActive = false;
	if (monkey.isSwinging) {
		monkey.releaseSwing();
	}
};
game.move = function (x, y, obj) {
	if (dragActive) {
		handleInput(x, y);
	}
};
function handleInput(x, y) {
	if (!monkey.isSwinging) {
		// Find closest vine to swing from
		var closestVine = null;
		var closestDist = 300; // Maximum swinging distance
		for (var i = 0; i < vines.length; i++) {
			var vine = vines[i];
			var dist = Math.sqrt(Math.pow(monkey.x - vine.x, 2) + Math.pow(monkey.y - (vine.y + vine.height * 0.5), 2));
			if (dist < closestDist) {
				closestDist = dist;
				closestVine = vine;
			}
		}
		if (closestVine) {
			monkey.startSwinging(closestVine);
		}
	}
}
// Main game update
game.update = function () {
	// Update game elements
	distance += gameSpeed;
	// Check if we need to increase level
	if (Math.floor(distance / 5000) + 1 > level) {
		level = Math.floor(distance / 5000) + 1;
		gameSpeed = 1 + level * 0.1;
		levelTxt.setText('Level: ' + level);
		storage.level = level;
	}
	// Camera follows monkey with some lag
	var targetX = Math.max(2048 / 2, monkey.x);
	game.x = Math.lerp(game.x, 2048 / 2 - targetX, 0.05);
	// Check if monkey is off screen
	if (monkey.y > 2732 + 100 || monkey.x < -500) {
		// Game over
		LK.showGameOver();
		return;
	}
	// Generate new vines as we move
	if (nextVineX + game.x < 2048) {
		var vine = new Vine();
		vine.x = nextVineX;
		vine.y = 200 + Math.random() * 300;
		vines.push(vine);
		game.addChild(vine);
		vine.updateDepth();
		// Place a banana near some vines
		if (Math.random() < 0.7) {
			var banana = new Banana();
			banana.x = vine.x + (Math.random() * 200 - 100);
			banana.y = vine.y + 200 + Math.random() * 200;
			bananas.push(banana);
			game.addChild(banana);
		}
		nextVineX += 250 + Math.random() * 150;
	}
	// Update all game elements
	for (var i = clouds.length - 1; i >= 0; i--) {
		clouds[i].update();
	}
	for (var i = bananas.length - 1; i >= 0; i--) {
		var banana = bananas[i];
		banana.update();
		// Check collision with monkey
		if (!banana.collected && monkey.intersects(banana)) {
			var points = banana.collect();
			score += points;
			scoreTxt.setText('Score: ' + score);
			// Update high score
			if (score > highScore) {
				highScore = score;
				highScoreTxt.setText('High Score: ' + highScore);
				storage.highScore = highScore;
			}
			// Remove from array
			bananas.splice(i, 1);
		}
	}
	for (var i = predators.length - 1; i >= 0; i--) {
		var predator = predators[i];
		predator.update();
		// Check collision with monkey
		if (!monkey.stunned && monkey.intersects(predator)) {
			monkey.getStunned();
			// Reduce score
			score = Math.max(0, score - 50);
			scoreTxt.setText('Score: ' + score);
		}
		// Remove predators that are far behind
		if (predator.x < monkey.x - 2048) {
			predator.destroy();
			predators.splice(i, 1);
		}
	}
	// Check for win condition (placeholder - reach level 10)
	if (level >= 10 && score >= 1000) {
		LK.showYouWin();
	}
};
// Helper function for lerping (not available in standard JS)
Math.lerp = function (a, b, t) {
	return a + (b - a) * t;
};
// Play background music
LK.playMusic('jungleMusic');