/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
// Bee class (bees to be eaten)
var Bee = Container.expand(function () {
	var self = Container.call(this);
	// Attach Bee asset (smaller yellow ellipse)
	var bee = self.attachAsset('bee', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Randomize bee size a bit
	var scale = 0.5 + Math.random() * 0.2;
	bee.scaleX = scale;
	bee.scaleY = scale;
	// Bee speed (for chaos mode)
	self.vx = 0;
	self.vy = 0;
	self.escaping = false;
	// For chaos mode: set bee to escape in a random direction
	self.startEscape = function () {
		self.escaping = true;
		// Random direction and speed
		var angle = Math.random() * Math.PI * 2;
		var speed = 8 + Math.random() * 6;
		self.vx = Math.cos(angle) * speed;
		self.vy = Math.sin(angle) * speed;
	};
	// Update method for bee
	self.update = function () {
		if (self.escaping) {
			self.x += self.vx;
			self.y += self.vy;
		}
	};
	return self;
});
// KingBee class (the player)
var KingBee = Container.expand(function () {
	var self = Container.call(this);
	// Attach King Bee asset (big yellow ellipse)
	var kingBee = self.attachAsset('kingBee', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Set initial scale for King Bee
	kingBee.scaleX = 1.2;
	kingBee.scaleY = 1.2;
	// For collision, use self as the hitbox
	// Dragging state
	self.isDragging = false;
	// Event handlers for touch
	self.down = function (x, y, obj) {
		self.isDragging = true;
	};
	self.up = function (x, y, obj) {
		self.isDragging = false;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xffe066 // Honey yellow background
});
/**** 
* Game Code
****/ 
// Tween plugin for animations
// Game constants
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var BEE_LIMIT = 60;
var INITIAL_BEE_COUNT = 20;
var CHAOS_BEE_COUNT = 30;
// Game state
var kingBee;
var bees = [];
var chaosMode = false;
var beesEaten = 0;
var dragNode = null;
var lastIntersectingBee = null;
var scoreTxt;
var infoTxt;
var chaosTxt;
var chaosTimeout = null;
// --- Asset Initialization (shapes) ---
// For dead bees
// --- GUI Elements ---
scoreTxt = new Text2('0', {
	size: 120,
	fill: 0x222222
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
infoTxt = new Text2('Eat bees! Don\'t exceed 60.', {
	size: 70,
	fill: 0x222222
});
infoTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(infoTxt);
infoTxt.y = 130;
chaosTxt = new Text2('CHAOS! Eliminate survivors!', {
	size: 120,
	fill: 0xFF2222
});
chaosTxt.anchor.set(0.5, 0.5);
chaosTxt.visible = false;
LK.gui.center.addChild(chaosTxt);
// --- King Bee ---
kingBee = new KingBee();
game.addChild(kingBee);
// Start in the center of the hive
kingBee.x = GAME_WIDTH / 2;
kingBee.y = GAME_HEIGHT / 2 + 200;
// --- Bee Spawning ---
function spawnBee(x, y) {
	var bee = new Bee();
	bee.x = x;
	bee.y = y;
	bees.push(bee);
	game.addChild(bee);
	return bee;
}
// Place bees randomly in the hive (not too close to King Bee)
function spawnInitialBees(count) {
	for (var i = 0; i < count; i++) {
		var placed = false;
		var tries = 0;
		while (!placed && tries < 20) {
			var bx = 200 + Math.random() * (GAME_WIDTH - 400);
			var by = 400 + Math.random() * (GAME_HEIGHT - 800);
			// Don't spawn too close to King Bee
			var dx = bx - kingBee.x;
			var dy = by - kingBee.y;
			if (dx * dx + dy * dy > 400 * 400) {
				spawnBee(bx, by);
				placed = true;
			}
			tries++;
		}
	}
}
spawnInitialBees(INITIAL_BEE_COUNT);
// --- Dragging King Bee ---
function handleMove(x, y, obj) {
	if (dragNode) {
		// Clamp to game area, keep King Bee inside
		var kbw = kingBee.width / 2;
		var kbh = kingBee.height / 2;
		kingBee.x = Math.max(kbw, Math.min(GAME_WIDTH - kbw, x));
		kingBee.y = Math.max(kbh, Math.min(GAME_HEIGHT - kbh, y));
	}
}
game.move = handleMove;
game.down = function (x, y, obj) {
	// Only start drag if touch is on King Bee
	var local = kingBee.toLocal(game.toGlobal({
		x: x,
		y: y
	}));
	if (Math.abs(local.x) < kingBee.width / 2 && Math.abs(local.y) < kingBee.height / 2) {
		dragNode = kingBee;
	}
};
game.up = function (x, y, obj) {
	dragNode = null;
};
// --- Eating Bees ---
function eatBee(bee) {
	// Animate bee shrinking and fading out
	tween(bee, {
		scaleX: 0,
		scaleY: 0,
		alpha: 0
	}, {
		duration: 250,
		easing: tween.easeIn,
		onFinish: function onFinish() {
			bee.destroy();
		}
	});
	// Remove from bees array
	for (var i = 0; i < bees.length; i++) {
		if (bees[i] === bee) {
			bees.splice(i, 1);
			break;
		}
	}
	beesEaten++;
	scoreTxt.setText(beesEaten);
	// King Bee grows slightly
	tween(kingBee, {
		scaleX: 1.2 + beesEaten * 0.01,
		scaleY: 1.2 + beesEaten * 0.01
	}, {
		duration: 200,
		easing: tween.easeOut
	});
	// Check for chaos trigger
	if (!chaosMode && beesEaten > BEE_LIMIT) {
		triggerChaos();
	}
}
// --- Chaos Mode ---
function triggerChaos() {
	chaosMode = true;
	chaosTxt.visible = true;
	infoTxt.setText('Too many bees eaten!');
	// All remaining bees start escaping
	for (var i = 0; i < bees.length; i++) {
		bees[i].startEscape();
	}
	// Spawn extra survivor bees
	for (var j = 0; j < CHAOS_BEE_COUNT; j++) {
		var bx = 200 + Math.random() * (GAME_WIDTH - 400);
		var by = 400 + Math.random() * (GAME_HEIGHT - 800);
		var bee = spawnBee(bx, by);
		bee.startEscape();
	}
	// King Bee shakes (flash red)
	LK.effects.flashObject(kingBee, 0xff2222, 800);
	// Show chaos message for 2 seconds
	if (chaosTimeout) LK.clearTimeout(chaosTimeout);
	chaosTimeout = LK.setTimeout(function () {
		chaosTxt.visible = false;
	}, 2000);
}
// --- Eliminating Survivor Bees in Chaos Mode ---
function eliminateBee(bee) {
	// Animate bee turning gray and shrinking
	var deadBee = LK.getAsset('beeDead', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: bee.x,
		y: bee.y,
		scaleX: bee.scaleX,
		scaleY: bee.scaleY
	});
	game.addChild(deadBee);
	tween(deadBee, {
		scaleX: 0,
		scaleY: 0,
		alpha: 0
	}, {
		duration: 300,
		easing: tween.easeIn,
		onFinish: function onFinish() {
			deadBee.destroy();
		}
	});
	bee.destroy();
	for (var i = 0; i < bees.length; i++) {
		if (bees[i] === bee) {
			bees.splice(i, 1);
			break;
		}
	}
	// If all bees eliminated, restore order (win)
	if (bees.length === 0) {
		LK.setScore(beesEaten);
		LK.showYouWin();
	}
}
// --- Main Game Update ---
game.update = function () {
	// Update all bees
	for (var i = bees.length - 1; i >= 0; i--) {
		var bee = bees[i];
		if (bee.update) bee.update();
		// If bee is escaping and out of bounds, remove it
		if (bee.escaping) {
			if (bee.x < -100 || bee.x > GAME_WIDTH + 100 || bee.y < -100 || bee.y > GAME_HEIGHT + 100) {
				bee.destroy();
				bees.splice(i, 1);
				continue;
			}
		}
		// If not in chaos mode, check for eating
		if (!chaosMode) {
			if (kingBee.intersects(bee)) {
				eatBee(bee);
			}
		}
	}
};
// --- Touch to eliminate bees in chaos mode ---
game.down = function (x, y, obj) {
	// Drag King Bee if not in chaos mode
	if (!chaosMode) {
		var local = kingBee.toLocal(game.toGlobal({
			x: x,
			y: y
		}));
		if (Math.abs(local.x) < kingBee.width / 2 && Math.abs(local.y) < kingBee.height / 2) {
			dragNode = kingBee;
		}
	} else {
		// In chaos mode, tap bees to eliminate
		for (var i = bees.length - 1; i >= 0; i--) {
			var bee = bees[i];
			var local = bee.toLocal(game.toGlobal({
				x: x,
				y: y
			}));
			if (Math.abs(local.x) < bee.width / 2 && Math.abs(local.y) < bee.height / 2) {
				eliminateBee(bee);
				break;
			}
		}
	}
};
game.up = function (x, y, obj) {
	dragNode = null;
};
// --- Reset on Game Over ---
game.onGameOver = function () {
	// Not needed, LK handles reset
};
// --- Prevent elements in top left 100x100 ---
/* All GUI elements are centered or top center, and bees/king bee are not spawned in top left. */
/* End of game code */ /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
// Bee class (bees to be eaten)
var Bee = Container.expand(function () {
	var self = Container.call(this);
	// Attach Bee asset (smaller yellow ellipse)
	var bee = self.attachAsset('bee', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Randomize bee size a bit
	var scale = 0.5 + Math.random() * 0.2;
	bee.scaleX = scale;
	bee.scaleY = scale;
	// Bee speed (for chaos mode)
	self.vx = 0;
	self.vy = 0;
	self.escaping = false;
	// For chaos mode: set bee to escape in a random direction
	self.startEscape = function () {
		self.escaping = true;
		// Random direction and speed
		var angle = Math.random() * Math.PI * 2;
		var speed = 8 + Math.random() * 6;
		self.vx = Math.cos(angle) * speed;
		self.vy = Math.sin(angle) * speed;
	};
	// Update method for bee
	self.update = function () {
		if (self.escaping) {
			self.x += self.vx;
			self.y += self.vy;
		}
	};
	return self;
});
// KingBee class (the player)
var KingBee = Container.expand(function () {
	var self = Container.call(this);
	// Attach King Bee asset (big yellow ellipse)
	var kingBee = self.attachAsset('kingBee', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Set initial scale for King Bee
	kingBee.scaleX = 1.2;
	kingBee.scaleY = 1.2;
	// For collision, use self as the hitbox
	// Dragging state
	self.isDragging = false;
	// Event handlers for touch
	self.down = function (x, y, obj) {
		self.isDragging = true;
	};
	self.up = function (x, y, obj) {
		self.isDragging = false;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xffe066 // Honey yellow background
});
/**** 
* Game Code
****/ 
// Tween plugin for animations
// Game constants
var GAME_WIDTH = 2048;
var GAME_HEIGHT = 2732;
var BEE_LIMIT = 60;
var INITIAL_BEE_COUNT = 20;
var CHAOS_BEE_COUNT = 30;
// Game state
var kingBee;
var bees = [];
var chaosMode = false;
var beesEaten = 0;
var dragNode = null;
var lastIntersectingBee = null;
var scoreTxt;
var infoTxt;
var chaosTxt;
var chaosTimeout = null;
// --- Asset Initialization (shapes) ---
// For dead bees
// --- GUI Elements ---
scoreTxt = new Text2('0', {
	size: 120,
	fill: 0x222222
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
infoTxt = new Text2('Eat bees! Don\'t exceed 60.', {
	size: 70,
	fill: 0x222222
});
infoTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(infoTxt);
infoTxt.y = 130;
chaosTxt = new Text2('CHAOS! Eliminate survivors!', {
	size: 120,
	fill: 0xFF2222
});
chaosTxt.anchor.set(0.5, 0.5);
chaosTxt.visible = false;
LK.gui.center.addChild(chaosTxt);
// --- King Bee ---
kingBee = new KingBee();
game.addChild(kingBee);
// Start in the center of the hive
kingBee.x = GAME_WIDTH / 2;
kingBee.y = GAME_HEIGHT / 2 + 200;
// --- Bee Spawning ---
function spawnBee(x, y) {
	var bee = new Bee();
	bee.x = x;
	bee.y = y;
	bees.push(bee);
	game.addChild(bee);
	return bee;
}
// Place bees randomly in the hive (not too close to King Bee)
function spawnInitialBees(count) {
	for (var i = 0; i < count; i++) {
		var placed = false;
		var tries = 0;
		while (!placed && tries < 20) {
			var bx = 200 + Math.random() * (GAME_WIDTH - 400);
			var by = 400 + Math.random() * (GAME_HEIGHT - 800);
			// Don't spawn too close to King Bee
			var dx = bx - kingBee.x;
			var dy = by - kingBee.y;
			if (dx * dx + dy * dy > 400 * 400) {
				spawnBee(bx, by);
				placed = true;
			}
			tries++;
		}
	}
}
spawnInitialBees(INITIAL_BEE_COUNT);
// --- Dragging King Bee ---
function handleMove(x, y, obj) {
	if (dragNode) {
		// Clamp to game area, keep King Bee inside
		var kbw = kingBee.width / 2;
		var kbh = kingBee.height / 2;
		kingBee.x = Math.max(kbw, Math.min(GAME_WIDTH - kbw, x));
		kingBee.y = Math.max(kbh, Math.min(GAME_HEIGHT - kbh, y));
	}
}
game.move = handleMove;
game.down = function (x, y, obj) {
	// Only start drag if touch is on King Bee
	var local = kingBee.toLocal(game.toGlobal({
		x: x,
		y: y
	}));
	if (Math.abs(local.x) < kingBee.width / 2 && Math.abs(local.y) < kingBee.height / 2) {
		dragNode = kingBee;
	}
};
game.up = function (x, y, obj) {
	dragNode = null;
};
// --- Eating Bees ---
function eatBee(bee) {
	// Animate bee shrinking and fading out
	tween(bee, {
		scaleX: 0,
		scaleY: 0,
		alpha: 0
	}, {
		duration: 250,
		easing: tween.easeIn,
		onFinish: function onFinish() {
			bee.destroy();
		}
	});
	// Remove from bees array
	for (var i = 0; i < bees.length; i++) {
		if (bees[i] === bee) {
			bees.splice(i, 1);
			break;
		}
	}
	beesEaten++;
	scoreTxt.setText(beesEaten);
	// King Bee grows slightly
	tween(kingBee, {
		scaleX: 1.2 + beesEaten * 0.01,
		scaleY: 1.2 + beesEaten * 0.01
	}, {
		duration: 200,
		easing: tween.easeOut
	});
	// Check for chaos trigger
	if (!chaosMode && beesEaten > BEE_LIMIT) {
		triggerChaos();
	}
}
// --- Chaos Mode ---
function triggerChaos() {
	chaosMode = true;
	chaosTxt.visible = true;
	infoTxt.setText('Too many bees eaten!');
	// All remaining bees start escaping
	for (var i = 0; i < bees.length; i++) {
		bees[i].startEscape();
	}
	// Spawn extra survivor bees
	for (var j = 0; j < CHAOS_BEE_COUNT; j++) {
		var bx = 200 + Math.random() * (GAME_WIDTH - 400);
		var by = 400 + Math.random() * (GAME_HEIGHT - 800);
		var bee = spawnBee(bx, by);
		bee.startEscape();
	}
	// King Bee shakes (flash red)
	LK.effects.flashObject(kingBee, 0xff2222, 800);
	// Show chaos message for 2 seconds
	if (chaosTimeout) LK.clearTimeout(chaosTimeout);
	chaosTimeout = LK.setTimeout(function () {
		chaosTxt.visible = false;
	}, 2000);
}
// --- Eliminating Survivor Bees in Chaos Mode ---
function eliminateBee(bee) {
	// Animate bee turning gray and shrinking
	var deadBee = LK.getAsset('beeDead', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: bee.x,
		y: bee.y,
		scaleX: bee.scaleX,
		scaleY: bee.scaleY
	});
	game.addChild(deadBee);
	tween(deadBee, {
		scaleX: 0,
		scaleY: 0,
		alpha: 0
	}, {
		duration: 300,
		easing: tween.easeIn,
		onFinish: function onFinish() {
			deadBee.destroy();
		}
	});
	bee.destroy();
	for (var i = 0; i < bees.length; i++) {
		if (bees[i] === bee) {
			bees.splice(i, 1);
			break;
		}
	}
	// If all bees eliminated, restore order (win)
	if (bees.length === 0) {
		LK.setScore(beesEaten);
		LK.showYouWin();
	}
}
// --- Main Game Update ---
game.update = function () {
	// Update all bees
	for (var i = bees.length - 1; i >= 0; i--) {
		var bee = bees[i];
		if (bee.update) bee.update();
		// If bee is escaping and out of bounds, remove it
		if (bee.escaping) {
			if (bee.x < -100 || bee.x > GAME_WIDTH + 100 || bee.y < -100 || bee.y > GAME_HEIGHT + 100) {
				bee.destroy();
				bees.splice(i, 1);
				continue;
			}
		}
		// If not in chaos mode, check for eating
		if (!chaosMode) {
			if (kingBee.intersects(bee)) {
				eatBee(bee);
			}
		}
	}
};
// --- Touch to eliminate bees in chaos mode ---
game.down = function (x, y, obj) {
	// Drag King Bee if not in chaos mode
	if (!chaosMode) {
		var local = kingBee.toLocal(game.toGlobal({
			x: x,
			y: y
		}));
		if (Math.abs(local.x) < kingBee.width / 2 && Math.abs(local.y) < kingBee.height / 2) {
			dragNode = kingBee;
		}
	} else {
		// In chaos mode, tap bees to eliminate
		for (var i = bees.length - 1; i >= 0; i--) {
			var bee = bees[i];
			var local = bee.toLocal(game.toGlobal({
				x: x,
				y: y
			}));
			if (Math.abs(local.x) < bee.width / 2 && Math.abs(local.y) < bee.height / 2) {
				eliminateBee(bee);
				break;
			}
		}
	}
};
game.up = function (x, y, obj) {
	dragNode = null;
};
// --- Reset on Game Over ---
game.onGameOver = function () {
	// Not needed, LK handles reset
};
// --- Prevent elements in top left 100x100 ---
/* All GUI elements are centered or top center, and bees/king bee are not spawned in top left. */
/* End of game code */