/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Logo = Container.expand(function (logoType) {
	var self = Container.call(this);
	logoType = logoType || 'small';
	var assetName = logoType + 'Logo';
	if (logoType === 'viacom') assetName = 'viacomLogo';
	var logoGraphics = self.attachAsset(assetName, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.logoType = logoType;
	self.points = logoType === 'small' ? 10 : logoType === 'medium' ? 25 : logoType === 'large' ? 50 : 100;
	self.minPlayerSize = logoType === 'small' ? 1 : logoType === 'medium' ? 1.5 : logoType === 'large' ? 2.5 : 4;
	// Random drift movement
	self.driftX = (Math.random() - 0.5) * 2;
	self.driftY = (Math.random() - 0.5) * 2;
	self.update = function () {
		self.x += self.driftX;
		self.y += self.driftY;
		// Bounce off edges
		if (self.x < 100 || self.x > 1948) self.driftX *= -1;
		if (self.y < 100 || self.y > 2632) self.driftY *= -1;
		// Keep in bounds
		self.x = Math.max(100, Math.min(1948, self.x));
		self.y = Math.max(100, Math.min(2632, self.y));
	};
	return self;
});
var PHead = Container.expand(function () {
	var self = Container.call(this);
	var pheadGraphics = self.attachAsset('phead', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.size = 1;
	self.maxSize = 5;
	self.speed = 3;
	self.logosEaten = 0;
	self.grow = function () {
		if (self.size < self.maxSize) {
			self.size += 0.3;
			pheadGraphics.scaleX = self.size;
			pheadGraphics.scaleY = self.size;
			// Eating animation
			tween(pheadGraphics, {
				scaleX: self.size * 1.2,
				scaleY: self.size * 1.2
			}, {
				duration: 150,
				onFinish: function onFinish() {
					tween(pheadGraphics, {
						scaleX: self.size,
						scaleY: self.size
					}, {
						duration: 150
					});
				}
			});
		}
	};
	self.hurt = function () {
		// Flash red when hurt
		tween(pheadGraphics, {
			tint: 0xff0000
		}, {
			duration: 200,
			onFinish: function onFinish() {
				tween(pheadGraphics, {
					tint: 0xffffff
				}, {
					duration: 200
				});
			}
		});
		LK.getSound('hurt').play();
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x001122
});
/**** 
* Game Code
****/ 
var phead = game.addChild(new PHead());
phead.x = 1024;
phead.y = 1366;
var logos = [];
var gameLevel = 1;
var logosToWin = 20;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
var sizeTxt = new Text2('Size: 1.0', {
	size: 60,
	fill: 0x00FF00
});
sizeTxt.anchor.set(1, 0);
sizeTxt.x = -20;
sizeTxt.y = 100;
LK.gui.topRight.addChild(sizeTxt);
var levelTxt = new Text2('Level: 1', {
	size: 60,
	fill: 0xFFFF00
});
levelTxt.anchor.set(0, 1);
levelTxt.x = 20;
levelTxt.y = -20;
LK.gui.bottomLeft.addChild(levelTxt);
// Spawn initial logos
function spawnLogo() {
	var logoTypes = ['small', 'small', 'small', 'medium', 'medium'];
	if (gameLevel >= 2) logoTypes.push('large');
	if (gameLevel >= 3) logoTypes.push('viacom');
	var randomType = logoTypes[Math.floor(Math.random() * logoTypes.length)];
	var newLogo = new Logo(randomType);
	// Random spawn position away from player
	do {
		newLogo.x = Math.random() * 1848 + 100;
		newLogo.y = Math.random() * 2532 + 100;
	} while (Math.abs(newLogo.x - phead.x) < 300 && Math.abs(newLogo.y - phead.y) < 300);
	logos.push(newLogo);
	game.addChild(newLogo);
}
// Initial logo spawn
for (var i = 0; i < 8; i++) {
	spawnLogo();
}
var dragActive = false;
game.down = function (x, y, obj) {
	dragActive = true;
	phead.x = x;
	phead.y = y;
};
game.move = function (x, y, obj) {
	if (dragActive) {
		phead.x = x;
		phead.y = y;
	}
};
game.up = function (x, y, obj) {
	dragActive = false;
};
// Game update loop
game.update = function () {
	// Update UI
	scoreTxt.setText('Score: ' + LK.getScore());
	sizeTxt.setText('Size: ' + phead.size.toFixed(1));
	levelTxt.setText('Level: ' + gameLevel);
	// Check logo interactions
	for (var i = logos.length - 1; i >= 0; i--) {
		var logo = logos[i];
		if (phead.intersects(logo)) {
			if (phead.size >= logo.minPlayerSize) {
				// Can eat this logo
				LK.setScore(LK.getScore() + logo.points);
				phead.grow();
				phead.logosEaten++;
				// Play eat sound and burp occasionally
				LK.getSound('eat').play();
				if (Math.random() < 0.3) {
					LK.setTimeout(function () {
						LK.getSound('burp').play();
					}, 500);
				}
				// Remove logo
				logo.destroy();
				logos.splice(i, 1);
				// Check for level progression
				if (phead.logosEaten >= logosToWin) {
					gameLevel++;
					phead.logosEaten = 0;
					logosToWin += 5;
					if (gameLevel >= 4) {
						// Victory condition - ate Viacom
						LK.showYouWin();
						return;
					}
				}
				// Spawn new logo
				spawnLogo();
			} else {
				// Logo is too big, player gets hurt
				phead.hurt();
				// Push player away
				var dx = phead.x - logo.x;
				var dy = phead.y - logo.y;
				var distance = Math.sqrt(dx * dx + dy * dy);
				if (distance > 0) {
					phead.x += dx / distance * 100;
					phead.y += dy / distance * 100;
				}
				// Keep player in bounds
				phead.x = Math.max(100, Math.min(1948, phead.x));
				phead.y = Math.max(100, Math.min(2632, phead.y));
			}
		}
	}
	// Maintain logo count
	while (logos.length < 8) {
		spawnLogo();
	}
	// Game over if player gets too small (from too many hits)
	if (LK.getScore() < -50) {
		LK.showGameOver();
	}
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var Logo = Container.expand(function (logoType) {
	var self = Container.call(this);
	logoType = logoType || 'small';
	var assetName = logoType + 'Logo';
	if (logoType === 'viacom') assetName = 'viacomLogo';
	var logoGraphics = self.attachAsset(assetName, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.logoType = logoType;
	self.points = logoType === 'small' ? 10 : logoType === 'medium' ? 25 : logoType === 'large' ? 50 : 100;
	self.minPlayerSize = logoType === 'small' ? 1 : logoType === 'medium' ? 1.5 : logoType === 'large' ? 2.5 : 4;
	// Random drift movement
	self.driftX = (Math.random() - 0.5) * 2;
	self.driftY = (Math.random() - 0.5) * 2;
	self.update = function () {
		self.x += self.driftX;
		self.y += self.driftY;
		// Bounce off edges
		if (self.x < 100 || self.x > 1948) self.driftX *= -1;
		if (self.y < 100 || self.y > 2632) self.driftY *= -1;
		// Keep in bounds
		self.x = Math.max(100, Math.min(1948, self.x));
		self.y = Math.max(100, Math.min(2632, self.y));
	};
	return self;
});
var PHead = Container.expand(function () {
	var self = Container.call(this);
	var pheadGraphics = self.attachAsset('phead', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.size = 1;
	self.maxSize = 5;
	self.speed = 3;
	self.logosEaten = 0;
	self.grow = function () {
		if (self.size < self.maxSize) {
			self.size += 0.3;
			pheadGraphics.scaleX = self.size;
			pheadGraphics.scaleY = self.size;
			// Eating animation
			tween(pheadGraphics, {
				scaleX: self.size * 1.2,
				scaleY: self.size * 1.2
			}, {
				duration: 150,
				onFinish: function onFinish() {
					tween(pheadGraphics, {
						scaleX: self.size,
						scaleY: self.size
					}, {
						duration: 150
					});
				}
			});
		}
	};
	self.hurt = function () {
		// Flash red when hurt
		tween(pheadGraphics, {
			tint: 0xff0000
		}, {
			duration: 200,
			onFinish: function onFinish() {
				tween(pheadGraphics, {
					tint: 0xffffff
				}, {
					duration: 200
				});
			}
		});
		LK.getSound('hurt').play();
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x001122
});
/**** 
* Game Code
****/ 
var phead = game.addChild(new PHead());
phead.x = 1024;
phead.y = 1366;
var logos = [];
var gameLevel = 1;
var logosToWin = 20;
// UI Elements
var scoreTxt = new Text2('Score: 0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0, 0);
LK.gui.topRight.addChild(scoreTxt);
var sizeTxt = new Text2('Size: 1.0', {
	size: 60,
	fill: 0x00FF00
});
sizeTxt.anchor.set(1, 0);
sizeTxt.x = -20;
sizeTxt.y = 100;
LK.gui.topRight.addChild(sizeTxt);
var levelTxt = new Text2('Level: 1', {
	size: 60,
	fill: 0xFFFF00
});
levelTxt.anchor.set(0, 1);
levelTxt.x = 20;
levelTxt.y = -20;
LK.gui.bottomLeft.addChild(levelTxt);
// Spawn initial logos
function spawnLogo() {
	var logoTypes = ['small', 'small', 'small', 'medium', 'medium'];
	if (gameLevel >= 2) logoTypes.push('large');
	if (gameLevel >= 3) logoTypes.push('viacom');
	var randomType = logoTypes[Math.floor(Math.random() * logoTypes.length)];
	var newLogo = new Logo(randomType);
	// Random spawn position away from player
	do {
		newLogo.x = Math.random() * 1848 + 100;
		newLogo.y = Math.random() * 2532 + 100;
	} while (Math.abs(newLogo.x - phead.x) < 300 && Math.abs(newLogo.y - phead.y) < 300);
	logos.push(newLogo);
	game.addChild(newLogo);
}
// Initial logo spawn
for (var i = 0; i < 8; i++) {
	spawnLogo();
}
var dragActive = false;
game.down = function (x, y, obj) {
	dragActive = true;
	phead.x = x;
	phead.y = y;
};
game.move = function (x, y, obj) {
	if (dragActive) {
		phead.x = x;
		phead.y = y;
	}
};
game.up = function (x, y, obj) {
	dragActive = false;
};
// Game update loop
game.update = function () {
	// Update UI
	scoreTxt.setText('Score: ' + LK.getScore());
	sizeTxt.setText('Size: ' + phead.size.toFixed(1));
	levelTxt.setText('Level: ' + gameLevel);
	// Check logo interactions
	for (var i = logos.length - 1; i >= 0; i--) {
		var logo = logos[i];
		if (phead.intersects(logo)) {
			if (phead.size >= logo.minPlayerSize) {
				// Can eat this logo
				LK.setScore(LK.getScore() + logo.points);
				phead.grow();
				phead.logosEaten++;
				// Play eat sound and burp occasionally
				LK.getSound('eat').play();
				if (Math.random() < 0.3) {
					LK.setTimeout(function () {
						LK.getSound('burp').play();
					}, 500);
				}
				// Remove logo
				logo.destroy();
				logos.splice(i, 1);
				// Check for level progression
				if (phead.logosEaten >= logosToWin) {
					gameLevel++;
					phead.logosEaten = 0;
					logosToWin += 5;
					if (gameLevel >= 4) {
						// Victory condition - ate Viacom
						LK.showYouWin();
						return;
					}
				}
				// Spawn new logo
				spawnLogo();
			} else {
				// Logo is too big, player gets hurt
				phead.hurt();
				// Push player away
				var dx = phead.x - logo.x;
				var dy = phead.y - logo.y;
				var distance = Math.sqrt(dx * dx + dy * dy);
				if (distance > 0) {
					phead.x += dx / distance * 100;
					phead.y += dy / distance * 100;
				}
				// Keep player in bounds
				phead.x = Math.max(100, Math.min(1948, phead.x));
				phead.y = Math.max(100, Math.min(2632, phead.y));
			}
		}
	}
	// Maintain logo count
	while (logos.length < 8) {
		spawnLogo();
	}
	// Game over if player gets too small (from too many hits)
	if (LK.getScore() < -50) {
		LK.showGameOver();
	}
};