/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
// Animal class represents the characters crawling on the belly.
var Animal = Container.expand(function (assetId) {
	var self = Container.call(this);
	// Public properties for velocity
	self.vx = 0;
	self.vy = 0;
	// Private properties for movement behavior
	var crawlSpeed = 1.5; // Constant speed at which animals crawl outwards
	var friction = 0.95; // Damping factor to slow down after a push
	// Attach the animal's graphic, centered.
	var graphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Pushes the animal away from a tap point.
	self.push = function (tapX, tapY) {
		var dx = self.x - tapX;
		var dy = self.y - tapY;
		var distance = Math.sqrt(dx * dx + dy * dy);
		// The push is stronger if the tap is closer, up to a max radius.
		var maxPushRadius = 500;
		if (distance < maxPushRadius) {
			var pushStrength = 60; // Base strength of the push
			var force = (1 - distance / maxPushRadius) * pushStrength;
			// Apply force only if not directly on the tap point
			if (distance > 0) {
				self.vx += dx / distance * force;
				self.vy += dy / distance * force;
			}
		}
	};
	// Called every frame by the LK engine.
	self.update = function () {
		// Calculate vector to crawl outwards from the screen center.
		var centerX = 2048 / 2;
		var centerY = 2732 / 2;
		var dx_center = self.x - centerX;
		var dy_center = self.y - centerY;
		var dist_center = Math.sqrt(dx_center * dx_center + dy_center * dy_center);
		if (dist_center > 0) {
			// Add a constant outward crawl velocity.
			self.vx += dx_center / dist_center * crawlSpeed;
			self.vy += dy_center / dist_center * crawlSpeed;
		}
		// Apply friction to gradually reduce velocity.
		self.vx *= friction;
		self.vy *= friction;
		// Update the animal's position based on its velocity.
		self.x += self.vx;
		self.y += self.vy;
		// Rotate the animal to face the direction it's crawling.
		self.rotation = Math.atan2(self.y - centerY, self.x - centerX) + Math.PI / 2;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xF5DDC5 // A skin-tone color in case the belly image fails to load.
});
/**** 
* Game Code
****/ 
// Tween library for animations like the ripple effect.
// Playful background music.
// Sound effect for the belly jiggle.
// A shape for the visual ripple effect on tap.
// The four animal friends trying to escape.
// Shin's belly, serving as the game's background.
// Global variables
var animals = [];
var scoreTxt;
// Add the belly background, filling the screen.
var belly = game.addChild(LK.getAsset('bellyBg', {
	x: 2048 / 2,
	y: 2732 / 2,
	anchorX: 0.5,
	anchorY: 0.5
}));
// Set up the score display (based on time survived).
scoreTxt = new Text2('0', {
	size: 120,
	fill: 0x333333
});
scoreTxt.anchor.set(0.5, 0);
// Position the score at the top-center, with a small margin.
scoreTxt.y = 50;
LK.gui.top.addChild(scoreTxt);
// Define the animals and their starting positions around the center.
var animalAssets = ['corgi', 'cat', 'bunny', 'tiger'];
var startPositions = [{
	x: 2048 / 2 - 350,
	y: 2732 / 2 - 400
}, {
	x: 2048 / 2 + 350,
	y: 2732 / 2 - 400
}, {
	x: 2048 / 2 - 350,
	y: 2732 / 2 + 400
}, {
	x: 2048 / 2 + 350,
	y: 2732 / 2 + 400
}];
// Create and place the animals on the belly.
for (var i = 0; i < animalAssets.length; i++) {
	var animal = new Animal(animalAssets[i]);
	animal.x = startPositions[i].x;
	animal.y = startPositions[i].y;
	game.addChild(animal);
	animals.push(animal);
}
// Handle player taps on the screen.
game.down = function (x, y, obj) {
	// Play a jiggle sound.
	LK.getSound('jiggleSound').play();
	// Create a visual ripple effect at the tap location.
	var ripple = game.addChild(LK.getAsset('ripple', {
		x: x,
		y: y,
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.8
	}));
	// Animate the ripple to expand and fade out.
	tween(ripple, {
		scaleX: 5,
		scaleY: 5,
		alpha: 0
	}, {
		duration: 400,
		easing: tween.easeOut,
		onFinish: function onFinish() {
			ripple.destroy();
		}
	});
	// Push all animals away from the tap.
	for (var i = 0; i < animals.length; i++) {
		animals[i].push(x, y);
	}
};
// The main game loop, called every frame.
game.update = function () {
	// The score is the number of seconds survived.
	var timeSurvived = Math.floor(LK.ticks / 60);
	LK.setScore(timeSurvived);
	scoreTxt.setText(String(timeSurvived));
	// Check if any animal has reached the edge of the screen.
	for (var i = 0; i < animals.length; i++) {
		var animal = animals[i];
		var halfWidth = animal.width / 2;
		var halfHeight = animal.height / 2;
		if (animal.x - halfWidth < 0 || animal.x + halfWidth > 2048 || animal.y - halfHeight < 0 || animal.y + halfHeight > 2732) {
			LK.showGameOver();
			// Stop the update loop to prevent further actions after game over.
			game.update = function () {};
			return;
		}
	}
};
// Start the background music.
LK.playMusic('bgMusic'); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
// Animal class represents the characters crawling on the belly.
var Animal = Container.expand(function (assetId) {
	var self = Container.call(this);
	// Public properties for velocity
	self.vx = 0;
	self.vy = 0;
	// Private properties for movement behavior
	var crawlSpeed = 1.5; // Constant speed at which animals crawl outwards
	var friction = 0.95; // Damping factor to slow down after a push
	// Attach the animal's graphic, centered.
	var graphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Pushes the animal away from a tap point.
	self.push = function (tapX, tapY) {
		var dx = self.x - tapX;
		var dy = self.y - tapY;
		var distance = Math.sqrt(dx * dx + dy * dy);
		// The push is stronger if the tap is closer, up to a max radius.
		var maxPushRadius = 500;
		if (distance < maxPushRadius) {
			var pushStrength = 60; // Base strength of the push
			var force = (1 - distance / maxPushRadius) * pushStrength;
			// Apply force only if not directly on the tap point
			if (distance > 0) {
				self.vx += dx / distance * force;
				self.vy += dy / distance * force;
			}
		}
	};
	// Called every frame by the LK engine.
	self.update = function () {
		// Calculate vector to crawl outwards from the screen center.
		var centerX = 2048 / 2;
		var centerY = 2732 / 2;
		var dx_center = self.x - centerX;
		var dy_center = self.y - centerY;
		var dist_center = Math.sqrt(dx_center * dx_center + dy_center * dy_center);
		if (dist_center > 0) {
			// Add a constant outward crawl velocity.
			self.vx += dx_center / dist_center * crawlSpeed;
			self.vy += dy_center / dist_center * crawlSpeed;
		}
		// Apply friction to gradually reduce velocity.
		self.vx *= friction;
		self.vy *= friction;
		// Update the animal's position based on its velocity.
		self.x += self.vx;
		self.y += self.vy;
		// Rotate the animal to face the direction it's crawling.
		self.rotation = Math.atan2(self.y - centerY, self.x - centerX) + Math.PI / 2;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xF5DDC5 // A skin-tone color in case the belly image fails to load.
});
/**** 
* Game Code
****/ 
// Tween library for animations like the ripple effect.
// Playful background music.
// Sound effect for the belly jiggle.
// A shape for the visual ripple effect on tap.
// The four animal friends trying to escape.
// Shin's belly, serving as the game's background.
// Global variables
var animals = [];
var scoreTxt;
// Add the belly background, filling the screen.
var belly = game.addChild(LK.getAsset('bellyBg', {
	x: 2048 / 2,
	y: 2732 / 2,
	anchorX: 0.5,
	anchorY: 0.5
}));
// Set up the score display (based on time survived).
scoreTxt = new Text2('0', {
	size: 120,
	fill: 0x333333
});
scoreTxt.anchor.set(0.5, 0);
// Position the score at the top-center, with a small margin.
scoreTxt.y = 50;
LK.gui.top.addChild(scoreTxt);
// Define the animals and their starting positions around the center.
var animalAssets = ['corgi', 'cat', 'bunny', 'tiger'];
var startPositions = [{
	x: 2048 / 2 - 350,
	y: 2732 / 2 - 400
}, {
	x: 2048 / 2 + 350,
	y: 2732 / 2 - 400
}, {
	x: 2048 / 2 - 350,
	y: 2732 / 2 + 400
}, {
	x: 2048 / 2 + 350,
	y: 2732 / 2 + 400
}];
// Create and place the animals on the belly.
for (var i = 0; i < animalAssets.length; i++) {
	var animal = new Animal(animalAssets[i]);
	animal.x = startPositions[i].x;
	animal.y = startPositions[i].y;
	game.addChild(animal);
	animals.push(animal);
}
// Handle player taps on the screen.
game.down = function (x, y, obj) {
	// Play a jiggle sound.
	LK.getSound('jiggleSound').play();
	// Create a visual ripple effect at the tap location.
	var ripple = game.addChild(LK.getAsset('ripple', {
		x: x,
		y: y,
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.8
	}));
	// Animate the ripple to expand and fade out.
	tween(ripple, {
		scaleX: 5,
		scaleY: 5,
		alpha: 0
	}, {
		duration: 400,
		easing: tween.easeOut,
		onFinish: function onFinish() {
			ripple.destroy();
		}
	});
	// Push all animals away from the tap.
	for (var i = 0; i < animals.length; i++) {
		animals[i].push(x, y);
	}
};
// The main game loop, called every frame.
game.update = function () {
	// The score is the number of seconds survived.
	var timeSurvived = Math.floor(LK.ticks / 60);
	LK.setScore(timeSurvived);
	scoreTxt.setText(String(timeSurvived));
	// Check if any animal has reached the edge of the screen.
	for (var i = 0; i < animals.length; i++) {
		var animal = animals[i];
		var halfWidth = animal.width / 2;
		var halfHeight = animal.height / 2;
		if (animal.x - halfWidth < 0 || animal.x + halfWidth > 2048 || animal.y - halfHeight < 0 || animal.y + halfHeight > 2732) {
			LK.showGameOver();
			// Stop the update loop to prevent further actions after game over.
			game.update = function () {};
			return;
		}
	}
};
// Start the background music.
LK.playMusic('bgMusic');