/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Animal = Container.expand(function (type) {
	var self = Container.call(this);
	self.animalType = type;
	var graphics = self.attachAsset(type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.moveSpeed = 2;
	self.directionX = Math.random() > 0.5 ? 1 : -1;
	self.directionY = Math.random() > 0.5 ? 1 : -1;
	self.changeDirectionTimer = 0;
	self.update = function () {
		self.changeDirectionTimer++;
		if (self.changeDirectionTimer > 120) {
			self.directionX = Math.random() > 0.5 ? 1 : -1;
			self.directionY = Math.random() > 0.5 ? 1 : -1;
			self.changeDirectionTimer = 0;
		}
		self.x += self.directionX * self.moveSpeed;
		self.y += self.directionY * self.moveSpeed;
		if (self.x < 50) self.directionX = 1;
		if (self.x > 1998) self.directionX = -1;
		if (self.y < 50) self.directionY = 1;
		if (self.y > 2682) self.directionY = -1;
	};
	return self;
});
var Peacock = Container.expand(function () {
	var self = Container.call(this);
	self.body = self.attachAsset('peacock', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.beak = self.attachAsset('peacockBeak', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 60,
		y: -20
	});
	self.belly = self.attachAsset('peacockBelly', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.7,
		y: 30
	});
	self.fullness = 0;
	self.maxFullness = 20;
	self.baseSpeed = 4;
	self.currentSpeed = self.baseSpeed;
	self.isBeakOpen = false;
	self.blinkTimer = 0;
	self.isSleeping = false;
	self.update = function () {
		var drowsinessLevel = self.fullness / self.maxFullness;
		self.currentSpeed = self.baseSpeed * (1 - drowsinessLevel * 0.7);
		self.blinkTimer++;
		var blinkFrequency = 180 - drowsinessLevel * 120;
		if (self.blinkTimer > blinkFrequency) {
			self.blink();
			self.blinkTimer = 0;
		}
		if (self.fullness >= self.maxFullness && !self.isSleeping) {
			self.fallAsleep();
		}
	};
	self.blink = function () {
		self.body.scaleY = 0.8;
		tween(self.body, {
			scaleY: 1
		}, {
			duration: 200
		});
	};
	self.openBeak = function () {
		if (!self.isBeakOpen) {
			self.isBeakOpen = true;
			tween(self.beak, {
				scaleY: 1.5
			}, {
				duration: 100
			});
		}
	};
	self.closeBeak = function () {
		if (self.isBeakOpen) {
			self.isBeakOpen = false;
			tween(self.beak, {
				scaleY: 1
			}, {
				duration: 100
			});
		}
	};
	self.eatAnimal = function (animal) {
		self.fullness++;
		var bellySizeIncrease = 1 + self.fullness * 0.1;
		tween(self.belly, {
			scaleX: bellySizeIncrease,
			scaleY: bellySizeIncrease
		}, {
			duration: 300
		});
		var swallowedAnimal = new SwallowedAnimal(animal.animalType);
		swallowedAnimal.x = Math.random() * 80 - 40;
		swallowedAnimal.y = Math.random() * 60 - 30;
		self.belly.addChild(swallowedAnimal);
		LK.getSound('chomp').play();
		var points = 10;
		if (animal.animalType === 'lion') points = 20;
		if (animal.animalType === 'shark') points = 15;
		LK.setScore(LK.getScore() + points);
	};
	self.fallAsleep = function () {
		self.isSleeping = true;
		tween(self, {
			alpha: 0.8
		}, {
			duration: 1000
		});
		LK.setTimeout(function () {
			LK.showGameOver();
		}, 2000);
	};
	return self;
});
var SwallowedAnimal = Container.expand(function (type) {
	var self = Container.call(this);
	var graphics = self.attachAsset(type, {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.6
	});
	self.complaintTimer = 0;
	self.complaintText = null;
	var complaints = ["Let me out!", "This is awful!", "Help me!", "I'm trapped!", "So dark in here!"];
	self.update = function () {
		self.complaintTimer++;
		if (self.complaintTimer > 180 && !self.complaintText) {
			var complaint = complaints[Math.floor(Math.random() * complaints.length)];
			self.complaintText = new Text2(complaint, {
				size: 30,
				fill: 0xFFFFFF
			});
			self.complaintText.anchor.set(0.5, 0.5);
			self.complaintText.x = self.x + Math.random() * 60 - 30;
			self.complaintText.y = self.y - 40;
			self.parent.addChild(self.complaintText);
			LK.getSound('complaint').play();
			tween(self.complaintText, {
				alpha: 0
			}, {
				duration: 2000,
				onFinish: function onFinish() {
					if (self.complaintText && self.complaintText.parent) {
						self.complaintText.parent.removeChild(self.complaintText);
					}
					self.complaintText = null;
					self.complaintTimer = 0;
				}
			});
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var peacock = game.addChild(new Peacock());
peacock.x = 1024;
peacock.y = 1366;
var animals = [];
var animalTypes = ['lion', 'snake', 'shark', 'cow'];
var spawnTimer = 0;
var bed = LK.getAsset('bed', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 2600
});
game.addChild(bed);
var scoreText = new Text2('Score: 0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var fullnessText = new Text2('Fullness: 0/20', {
	size: 50,
	fill: 0xFFFFFF
});
fullnessText.anchor.set(0, 0);
fullnessText.x = 50;
fullnessText.y = 100;
LK.gui.topLeft.addChild(fullnessText);
var dragNode = null;
game.down = function (x, y, obj) {
	if (!peacock.isSleeping) {
		dragNode = peacock;
		peacock.openBeak();
	}
};
game.move = function (x, y, obj) {
	if (dragNode && !peacock.isSleeping) {
		dragNode.x = x;
		dragNode.y = y;
	}
};
game.up = function (x, y, obj) {
	if (dragNode) {
		peacock.closeBeak();
		dragNode = null;
	}
};
game.update = function () {
	if (peacock.isSleeping) return;
	spawnTimer++;
	if (spawnTimer > 120 && animals.length < 8) {
		var animalType = animalTypes[Math.floor(Math.random() * animalTypes.length)];
		var animal = new Animal(animalType);
		animal.x = Math.random() * 1800 + 100;
		animal.y = Math.random() * 2400 + 100;
		animals.push(animal);
		game.addChild(animal);
		spawnTimer = 0;
	}
	for (var i = animals.length - 1; i >= 0; i--) {
		var animal = animals[i];
		if (peacock.isBeakOpen && peacock.intersects(animal)) {
			peacock.eatAnimal(animal);
			animal.destroy();
			animals.splice(i, 1);
			continue;
		}
	}
	scoreText.setText('Score: ' + LK.getScore());
	fullnessText.setText('Fullness: ' + peacock.fullness + '/' + peacock.maxFullness);
	if (peacock.fullness >= peacock.maxFullness && !peacock.isSleeping) {
		peacock.y = Math.min(peacock.y + 2, 2600);
	}
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Animal = Container.expand(function (type) {
	var self = Container.call(this);
	self.animalType = type;
	var graphics = self.attachAsset(type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.moveSpeed = 2;
	self.directionX = Math.random() > 0.5 ? 1 : -1;
	self.directionY = Math.random() > 0.5 ? 1 : -1;
	self.changeDirectionTimer = 0;
	self.update = function () {
		self.changeDirectionTimer++;
		if (self.changeDirectionTimer > 120) {
			self.directionX = Math.random() > 0.5 ? 1 : -1;
			self.directionY = Math.random() > 0.5 ? 1 : -1;
			self.changeDirectionTimer = 0;
		}
		self.x += self.directionX * self.moveSpeed;
		self.y += self.directionY * self.moveSpeed;
		if (self.x < 50) self.directionX = 1;
		if (self.x > 1998) self.directionX = -1;
		if (self.y < 50) self.directionY = 1;
		if (self.y > 2682) self.directionY = -1;
	};
	return self;
});
var Peacock = Container.expand(function () {
	var self = Container.call(this);
	self.body = self.attachAsset('peacock', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.beak = self.attachAsset('peacockBeak', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 60,
		y: -20
	});
	self.belly = self.attachAsset('peacockBelly', {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.7,
		y: 30
	});
	self.fullness = 0;
	self.maxFullness = 20;
	self.baseSpeed = 4;
	self.currentSpeed = self.baseSpeed;
	self.isBeakOpen = false;
	self.blinkTimer = 0;
	self.isSleeping = false;
	self.update = function () {
		var drowsinessLevel = self.fullness / self.maxFullness;
		self.currentSpeed = self.baseSpeed * (1 - drowsinessLevel * 0.7);
		self.blinkTimer++;
		var blinkFrequency = 180 - drowsinessLevel * 120;
		if (self.blinkTimer > blinkFrequency) {
			self.blink();
			self.blinkTimer = 0;
		}
		if (self.fullness >= self.maxFullness && !self.isSleeping) {
			self.fallAsleep();
		}
	};
	self.blink = function () {
		self.body.scaleY = 0.8;
		tween(self.body, {
			scaleY: 1
		}, {
			duration: 200
		});
	};
	self.openBeak = function () {
		if (!self.isBeakOpen) {
			self.isBeakOpen = true;
			tween(self.beak, {
				scaleY: 1.5
			}, {
				duration: 100
			});
		}
	};
	self.closeBeak = function () {
		if (self.isBeakOpen) {
			self.isBeakOpen = false;
			tween(self.beak, {
				scaleY: 1
			}, {
				duration: 100
			});
		}
	};
	self.eatAnimal = function (animal) {
		self.fullness++;
		var bellySizeIncrease = 1 + self.fullness * 0.1;
		tween(self.belly, {
			scaleX: bellySizeIncrease,
			scaleY: bellySizeIncrease
		}, {
			duration: 300
		});
		var swallowedAnimal = new SwallowedAnimal(animal.animalType);
		swallowedAnimal.x = Math.random() * 80 - 40;
		swallowedAnimal.y = Math.random() * 60 - 30;
		self.belly.addChild(swallowedAnimal);
		LK.getSound('chomp').play();
		var points = 10;
		if (animal.animalType === 'lion') points = 20;
		if (animal.animalType === 'shark') points = 15;
		LK.setScore(LK.getScore() + points);
	};
	self.fallAsleep = function () {
		self.isSleeping = true;
		tween(self, {
			alpha: 0.8
		}, {
			duration: 1000
		});
		LK.setTimeout(function () {
			LK.showGameOver();
		}, 2000);
	};
	return self;
});
var SwallowedAnimal = Container.expand(function (type) {
	var self = Container.call(this);
	var graphics = self.attachAsset(type, {
		anchorX: 0.5,
		anchorY: 0.5,
		alpha: 0.6
	});
	self.complaintTimer = 0;
	self.complaintText = null;
	var complaints = ["Let me out!", "This is awful!", "Help me!", "I'm trapped!", "So dark in here!"];
	self.update = function () {
		self.complaintTimer++;
		if (self.complaintTimer > 180 && !self.complaintText) {
			var complaint = complaints[Math.floor(Math.random() * complaints.length)];
			self.complaintText = new Text2(complaint, {
				size: 30,
				fill: 0xFFFFFF
			});
			self.complaintText.anchor.set(0.5, 0.5);
			self.complaintText.x = self.x + Math.random() * 60 - 30;
			self.complaintText.y = self.y - 40;
			self.parent.addChild(self.complaintText);
			LK.getSound('complaint').play();
			tween(self.complaintText, {
				alpha: 0
			}, {
				duration: 2000,
				onFinish: function onFinish() {
					if (self.complaintText && self.complaintText.parent) {
						self.complaintText.parent.removeChild(self.complaintText);
					}
					self.complaintText = null;
					self.complaintTimer = 0;
				}
			});
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var peacock = game.addChild(new Peacock());
peacock.x = 1024;
peacock.y = 1366;
var animals = [];
var animalTypes = ['lion', 'snake', 'shark', 'cow'];
var spawnTimer = 0;
var bed = LK.getAsset('bed', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 1024,
	y: 2600
});
game.addChild(bed);
var scoreText = new Text2('Score: 0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreText.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreText);
var fullnessText = new Text2('Fullness: 0/20', {
	size: 50,
	fill: 0xFFFFFF
});
fullnessText.anchor.set(0, 0);
fullnessText.x = 50;
fullnessText.y = 100;
LK.gui.topLeft.addChild(fullnessText);
var dragNode = null;
game.down = function (x, y, obj) {
	if (!peacock.isSleeping) {
		dragNode = peacock;
		peacock.openBeak();
	}
};
game.move = function (x, y, obj) {
	if (dragNode && !peacock.isSleeping) {
		dragNode.x = x;
		dragNode.y = y;
	}
};
game.up = function (x, y, obj) {
	if (dragNode) {
		peacock.closeBeak();
		dragNode = null;
	}
};
game.update = function () {
	if (peacock.isSleeping) return;
	spawnTimer++;
	if (spawnTimer > 120 && animals.length < 8) {
		var animalType = animalTypes[Math.floor(Math.random() * animalTypes.length)];
		var animal = new Animal(animalType);
		animal.x = Math.random() * 1800 + 100;
		animal.y = Math.random() * 2400 + 100;
		animals.push(animal);
		game.addChild(animal);
		spawnTimer = 0;
	}
	for (var i = animals.length - 1; i >= 0; i--) {
		var animal = animals[i];
		if (peacock.isBeakOpen && peacock.intersects(animal)) {
			peacock.eatAnimal(animal);
			animal.destroy();
			animals.splice(i, 1);
			continue;
		}
	}
	scoreText.setText('Score: ' + LK.getScore());
	fullnessText.setText('Fullness: ' + peacock.fullness + '/' + peacock.maxFullness);
	if (peacock.fullness >= peacock.maxFullness && !peacock.isSleeping) {
		peacock.y = Math.min(peacock.y + 2, 2600);
	}
};