/**** 
* Plugins
****/ 
var facekit = LK.import("@upit/facekit.v1");
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var Chicken = Container.expand(function () {
	var self = Container.call(this);
	self.failed = false;
	self.mouthClosedFlag = true;
	self.tail = self.attachAsset('tail', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1,
		scaleY: -1
	});
	self.tail.x = -210;
	self.tail.y = -200;
	var paws = self.attachAsset('nyanCatPaws', {
		anchorX: 0.5,
		anchorY: 1,
		scaleX: 0.9
	});
	paws.x = 30;
	paws.y = 20;
	self.body = self.attachAsset('nyanCatBody', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 1
	});
	var headIdle = self.attachAsset('nyanCatHeadIdle', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 1
	});
	var headOh = self.attachAsset('nyanCatHeadAngry', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 0
	});
	var headAngry = self.attachAsset('nyanCatHeadOh', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 0
	});
	var headSmile = self.attachAsset('nyanCatHeadSmile', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 0
	});
	var collisionElement = self.attachAsset('collisionElement', {
		anchorX: 0.5,
		anchorY: 0.5,
		tint: 0xFF0000,
		width: self.body.width * 0.4,
		height: self.body.height * 0.2
	});
	headIdle.x = 180;
	headIdle.y = -100;
	headOh.x = 180;
	headOh.y = -100;
	headAngry.x = 180;
	headAngry.y = -100;
	headSmile.x = 180;
	headSmile.y = -100;
	collisionElement.alpha = 0;
	collisionElement.y = -30;
	self.body.y = 40;
	self.speed = 8;
	self.initialGlobalSpeed = typeof globalSpeed !== 'undefined' ? Math.abs(globalSpeed) : 10;
	self.baseJumpVelocity = -30;
	self.baseJumpHeight = 300;
	self.jumpHeight = self.baseJumpHeight;
	self.isJumping = true;
	self.jumpVelocity = 0;
	self.isOnPlatform = true;
	self.lastPlatform = undefined;
	self.update = function () {
		self.y += self.jumpVelocity;
		if (self.failed) {
			return;
		}
		var gravityFactor = Math.abs(globalSpeed) / 8;
		self.jumpVelocity += 1 * gravityFactor;
		var maxFallSpeed = 30 * gravityFactor;
		if (self.jumpVelocity > maxFallSpeed) {
			self.jumpVelocity = maxFallSpeed;
		}
		if (!self.isHit) {
			if (self.isJumping) {
				// && self.jumpVelocity < 0
				headIdle.alpha = 0;
				headOh.alpha = 0;
				headAngry.alpha = 0;
				headSmile.alpha = 1;
			} else if (targetPlatform && self.y > targetPlatform.y) {
				headIdle.alpha = 0;
				headOh.alpha = 0;
				headAngry.alpha = 1;
				headSmile.alpha = 0;
			} else {
				headIdle.alpha = 1;
				headOh.alpha = 0;
				headAngry.alpha = 0;
				headSmile.alpha = 0;
			}
		}
		var targetPlatform = undefined;
		self.isOnPlatform = platforms.some(function (platform) {
			if (collisionElement.intersects(platform)) {
				if (self.y - platform.y < 100) {
					targetPlatform = platform;
					return true;
				}
			}
		});
		if (targetPlatform) {
			//pitchLabel.setText('Platform #' + targetPlatform.index);
			jumpCounter = Math.max(0, targetPlatform.index - 2);
			if (countdown) {
				countdown.updateCount(jumpCounter);
			}
			if (jumpCounter > highscore) {
				highscore = jumpCounter;
				storage.highscore = highscore;
				highscoreText.setText('Best: ' + highscore);
			}
		}
		if (!self.failed && self.y > 2400) {
			self.failed = true;
			xspeed = 0;
			LK.effects.flashScreen(0xFFFFFF, 100);
			headIdle.alpha = 0;
			headOh.alpha = 0;
			headAngry.alpha = 1;
			headSmile.alpha = 0;
			self.jumpVelocity = 0;
			LK.getSound('failed').play();
			// Update highscore before failure animation
			// if (jumpCounter > highscore) {
			// 	highscore = jumpCounter;
			// 	storage.highscore = highscore;
			// 	highscoreText.setText('Best: ' + highscore);
			// }
			tween(headAngry, {
				scaleY: 3,
				scaleX: 3
			}, {
				duration: 500,
				easing: tween.easeOut
			});
			tween(self, {
				y: 3000
			}, {
				duration: 1600,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					self.jumpVelocity = 20;
				}
			});
		}
		if (self.isOnPlatform && self.jumpVelocity > 0) {
			if (self.lastPlatform !== targetPlatform) {
				animatePaws();
				self.lastPlatform = targetPlatform;
			}
			self.y = targetPlatform.y + 70;
			if (self.isJumping) {
				self.body.scale.y = .8;
				self.body.scale.x = 1.2;
				tween(self.body, {
					scaleY: 1,
					scaleX: 1
				}, {
					duration: 1000,
					easing: tween.elasticOut
				});
				tween.stop(paws, {
					x: true
				});
				paws.x = 0;
				animatePaws();
			}
			self.isJumping = false;
			self.lastPlatform = targetPlatform;
			self.jumpVelocity = 0;
		}
		self.x = 2048 / 2;
		var speedScaleFactor = Math.min(Math.abs(globalSpeed / 8), 2.0);
		self.jumpHeight = self.baseJumpHeight * speedScaleFactor;
	};
	self.animateHit = function () {
		if (self.failed) {
			return;
		}
		self.isHit = true;
		headIdle.alpha = 0;
		headOh.alpha = 1;
		headAngry.alpha = 0;
		headSmile.alpha = 0;
		LK.effects.flashScreen(0xFFF0000, 300);
		// Play hit sound
		LK.getSound('hit').play();
		// After 1 second, reset hit state
		LK.setTimeout(function () {
			self.isHit = false;
			if (self.failed) {
				return;
			}
			if (!self.isJumping) {
				headIdle.alpha = 1;
				headSmile.alpha = 0;
			}
			if (self.isJumping) {
				headIdle.alpha = 0;
				headSmile.alpha = 1;
			}
			headOh.alpha = 0;
			headAngry.alpha = 0;
		}, 1000);
	};
	function groove() {
		tween(self, {
			scaleX: 1.03,
			scaleY: 1.03
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(self, {
					scaleX: 1,
					scaleY: 1
				}, {
					onFinish: groove,
					duration: 300
				});
			}
		});
	}
	groove();
	self.jump = function (strength) {
		if (platforms[0].x > -1500) {
			return;
		}
		if (!self.isJumping && self.isOnPlatform && self.mouthClosedFlag) {
			self.isJumping = true;
			self.mouthClosedFlag = false;
			LK.getSound('jump').play();
			var currentSpeed = typeof globalSpeed !== 'undefined' ? Math.abs(globalSpeed) : self.initialGlobalSpeed;
			var jumpScale = Math.sqrt(currentSpeed / self.initialGlobalSpeed);
			chicken.jumpVelocity = self.baseJumpVelocity * jumpScale;
			tween.stop(paws, {
				x: true
			});
			tween(paws, {
				x: 0
			}, {
				duration: 100
			});
			self.update();
		}
	};
	function animatePaws() {
		if (!self.isOnPlatform) {
			return;
		}
		tween(paws, {
			x: -10
		}, {
			duration: 160,
			easing: tween.linear,
			onFinish: function onFinish() {
				tween(paws, {
					x: 30
				}, {
					duration: 160,
					easing: tween.linear,
					onFinish: animatePaws
				});
			}
		});
	}
	function animateTail() {
		self.tail.rotation = 0;
		tween(self.tail, {
			rotation: 0.3
		}, {
			duration: 700,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				tween(self.tail, {
					rotation: 0
				}, {
					duration: 700,
					easing: tween.easeOut,
					onFinish: animateTail
				});
			}
		});
	}
	// function animateTail() {
	// 	self.tail.scale.x = 1;
	// 	self.tail.scale.y = 1;
	// 	tween(self.tail, {
	// 		scaleX: 1.1,
	// 		scaleY: -1
	// 	}, {
	// 		duration: 600,
	// 		easing: tween.easeOut,
	// 		onFinish: function onFinish() {
	// 			tween(self.tail, {
	// 				scaleX: 1,
	// 				scaleY: 1
	// 			}, {
	// 				duration: 600,
	// 				easing: tween.easeOut,
	// 				onFinish: animateTail
	// 			});
	// 		}
	// 	});
	// }
	animateTail();
	return self;
});
var Countdown = Container.expand(function () {
	var self = Container.call(this);
	var jumpCount = 0;
	var countdownTextShadow = new Text2("0", {
		size: 500,
		fill: 0x000000,
		weight: 800
	});
	var countdownText = new Text2("0", {
		size: 500,
		fill: 0xFFFFFF,
		weight: 800
	});
	countdownText.anchor.set(0.5, 0.5);
	countdownTextShadow.anchor.set(0.5, 0.5);
	self.addChild(countdownTextShadow);
	self.addChild(countdownText);
	self.x = 2048 / 2;
	self.y = 400;
	countdownTextShadow.x = 15;
	countdownTextShadow.y = 15;
	function tweenIt() {
		self.scale.set(1, 1);
		tween(self, {
			scaleX: .6,
			scaleY: .6
		}, {
			duration: 300,
			easing: tween.bounceOut
		});
	}
	self.updateCount = function (count) {
		if (jumpCount !== count) {
			jumpCount = count;
			countdownText.setText(jumpCount.toString());
			countdownTextShadow.setText(jumpCount.toString());
			// Update LK score to match jumpCounter
			LK.setScore(count);
			tweenIt();
		}
	};
	tweenIt();
});
var DebugMarker = Container.expand(function () {
	var self = Container.call(this);
	var debugGraphics = self.attachAsset('debugMark', {
		anchorX: 0,
		anchorY: 0.5
	});
	debugGraphics.width = 1;
	self.update = function () {
		debugGraphics.width = facekit.mouthOpen ? 1000 : 100;
		debugGraphics.tint = facekit.mouthOpen ? 0x00ff00 : 0xff0000;
	};
	return self;
});
var DirtParticle = Container.expand(function () {
	var self = Container.call(this);
	var dirtGraphics = self.attachAsset('trail', {
		anchorX: 0.5,
		anchorY: 1
	});
	var speed = 2;
	var angle = 0;
	var scale = 1;
	self.livetime = 110;
	self.update = function () {
		self.livetime--;
		if (self.livetime < 0) {
			self.destroy();
			return;
		}
		if (chicken.failed) {
			return;
		}
		if (self.x < -50) {
			self.destroy();
			return;
		}
		self.x += Math.cos(angle) * speed;
		self.y += Math.sin(angle) * speed;
	};
});
var LargeBork = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	var largeBorkGraphicsChicken = self.attachAsset('NyanCat', {
		anchorX: 0.5,
		anchorY: 1,
		scaleX: -1
	});
	largeBorkGraphicsChicken.y = -300;
	var largeBorkGraphics = self.attachAsset('large_bork', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
	self.bork = function () {
		var speechBubbleLarge = new SpeechBubbleLarge();
		var offset = Math.PI / 4 * Math.random() - Math.PI / 2 - Math.PI / 4;
		speechBubbleLarge.x = -360 + Math.cos(offset) * 300;
		speechBubbleLarge.y = -300 + Math.sin(offset) * 350;
		speechBubbleLarge.rotation = offset + Math.PI * 0.5 + .3;
		self.addChild(speechBubbleLarge);
		largeBorkGraphicsChicken.scale.y = 1.05;
		largeBorkGraphicsChicken.scale.x = -1.05;
		largeBorkGraphicsChicken.rotation = .2;
		tween(largeBorkGraphicsChicken, {
			scaleY: 1,
			scaleX: -1,
			rotation: 0
		}, {
			duration: 1000,
			easing: tween.elasticOut
		});
	};
});
var Particles = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	self.addDirtParticle = function (x, y) {
		var dirtParticle = new DirtParticle();
		dirtParticle.x = x - self.x + 100;
		dirtParticle.y = y - 100;
		self.addChild(dirtParticle);
	};
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 1;
	self.index = getNextPlatformIndex();
	var groundAsset = 'ground';
	if (self.index < 5) {
		groundAsset = 'ground';
	} else if (self.index < 15) {
		groundAsset = 'ground0';
	} else if (self.index < 30) {
		groundAsset = 'ground1';
	} else if (self.index < 40) {
		groundAsset = 'ground2';
	} else if (self.index < 50) {
		groundAsset = 'ground3';
	} else if (self.index < 60) {
		groundAsset = 'ground4';
	} else if (self.index < 70) {
		groundAsset = 'ground5';
	} else if (self.index < 80) {
		groundAsset = 'ground6';
	} else {
		groundAsset = 'ground7';
	}
	var groundGraphics = self.attachAsset(groundAsset, {
		anchorX: 0,
		anchorY: 0
	});
	self.width = groundGraphics.width;
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
});
var PlatformManager = Container.expand(function () {
	var self = Container.call(this);
	self.lastX = 0;
	self.platformGap = 1500;
	self.platformCount = 0;
	self.maxPlatforms = 10;
	self.activePlatforms = [];
	self.trackLastPlatform = function () {
		var lastX = 0;
		platforms.forEach(function (platform) {
			if (platform.x + platform.width > lastX) {
				lastX = platform.x + platform.width;
			}
		});
		self.lastX = lastX;
	};
	self.generatePlatform = function (x, y) {
		var platform = new Platform();
		var offset = 450;
		// Adjust offset based on platform width to maintain consistent gaps
		// if (platform.width < 1500) {
		// 	offset = Math.max(100, offset - (1500 - platform.width));
		// }
		//pitchLabel.setText('Next gap #' + platform.index + ' w=' + platform.width + ' x=' + platform.x + ' : ' + offset);
		platform.x = x + offset;
		//self.lastX = platform.x + platform.width;
		platform.y = y;
		backgroundContainer.addChild(platform);
		platforms.push(platform);
		self.activePlatforms.push(platform);
		return platform;
	};
	self.getRandomHeight = function () {
		var heights = [1600, 1800, 2000, 2200];
		return heights[Math.floor(Math.random() * heights.length)];
	};
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		self.trackLastPlatform();
		if (self.activePlatforms.length < self.maxPlatforms) {
			var newPlatform = self.generatePlatform(self.lastX, self.getRandomHeight());
			self.platformCount++;
		}
		for (var i = self.activePlatforms.length - 1; i >= 0; i--) {
			var platform = self.activePlatforms[i];
			if (platform.x + platform.width < -2000) {
				if (platforms.indexOf(platform) >= 5) {
					platforms.splice(platforms.indexOf(platform), 1);
					self.activePlatforms.splice(i, 1);
					platform.destroy();
				}
			}
		}
	};
	return self;
});
var Projectile = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 1;
	// Use cucumber asset with proper anchoring
	var cucumberGraphics = self.attachAsset('cucumber', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Initial rotation and setup
	self.rotationSpeed = Math.random() * 0.05 - 0.10; // Random rotation speed
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		// Move like platforms
		self.moveMultiplier = xspeed * 10;
		self.x += globalSpeed * self.moveMultiplier;
		// Apply rotation
		self.rotation += self.rotationSpeed;
		// Check for collision with chicken
		if (chicken && chicken.body && !chicken.failed) {
			if (chicken.body.intersects(self)) {
				chicken.animateHit();
				// Find this projectile in the manager's array and remove it
				var index = projectileManager.activeProjectiles.indexOf(self);
				if (index !== -1) {
					projectileManager.activeProjectiles.splice(index, 1);
				}
				self.destroy();
			}
		}
	};
	return self;
});
var ProjectileManager = Container.expand(function () {
	var self = Container.call(this);
	self.maxProjectiles = 5;
	self.activeProjectiles = [];
	self.spawnDelay = 1000; // Frames between spawns
	self.timer = 0;
	self.spawnProjectile = function () {
		var projectile = new Projectile();
		var heights = [500, 700, 1000, 1300];
		var randomHeight = heights[Math.floor(Math.random() * heights.length)];
		projectile.x = 2048 + 320; // Start off-screen to the right
		projectile.y = randomHeight;
		backgroundContainer.addChild(projectile);
		self.activeProjectiles.push(projectile);
		return projectile;
	};
	self.update = function () {
		if (!projectilesStarted || chicken.failed) {
			return;
		}
		// Spawn new projectiles on timer
		self.timer++;
		if (self.timer >= self.spawnDelay && self.activeProjectiles.length < self.maxProjectiles) {
			self.spawnProjectile();
			self.timer = 0;
		}
		// Clean up projectiles that are off-screen
		for (var i = self.activeProjectiles.length - 1; i >= 0; i--) {
			var projectile = self.activeProjectiles[i];
			if (projectile.x < -300) {
				self.activeProjectiles.splice(i, 1);
				projectile.destroy();
			}
		}
	};
	return self;
});
var SpeechBubble = Container.expand(function () {
	var self = Container.call(this);
	var bubbleGraphics = self.attachAsset('small_bork_speach', {
		anchorX: 0.8,
		anchorY: 1
	});
	self.y -= 50;
	self.alpha = 1;
	self.update = function () {
		self.y -= 1;
		self.alpha -= 0.01;
		if (self.alpha <= 0) {
			self.destroy();
		}
	};
	tween(self, {
		alpha: 0
	}, {
		duration: 2000,
		easing: tween.linear,
		onFinish: function onFinish() {
			self.destroy();
		}
	});
});
var SpeechBubbleLarge = Container.expand(function () {
	var self = Container.call(this);
	var bubbleGraphics = self.attachAsset('large_bork_speach', {
		anchorX: 0.2,
		anchorY: 1
	});
	self.y -= 50;
	self.alpha = 1;
	self.scale.set(0, 0);
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 600,
		easing: tween.elasticOut
	});
	LK.setTimeout(function () {
		tween(self, {
			alpha: 0,
			y: self.y - 100
		}, {
			duration: 100,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.destroy();
			}
		});
	}, 800);
});
var Spikes = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	var spikesGraphics = self.attachAsset('spikes', {
		anchorX: 0,
		anchorY: 0
	});
	self.update = function () {
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
});
var StartFlag = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	var flagGraphics = self.attachAsset('start', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
		if (self.x < -500) {
			self.destroy();
			startFlag = null;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
var globalNextPlatformIndex = 0;
var jumpCounter = 0;
function getNextPlatformIndex() {
	globalNextPlatformIndex++;
	return globalNextPlatformIndex;
}
// var volumeLabel = new Text2('Volume: 0', {
// 	size: 50,
// 	fill: 0xFFFFFF
// });
// volumeLabel.anchor.set(1, 1);
// volumeLabel.alpha = .3;
// LK.gui.bottomRight.addChild(volumeLabel);
var pitchLabel = new Text2('', {
	size: 50,
	fill: 0xFFFFFF
});
pitchLabel.anchor.set(0, 1);
pitchLabel.alpha = .5;
LK.gui.bottomLeft.addChild(pitchLabel);
// Create highscore display
var highscoreText = new Text2('', {
	size: 100,
	fill: 0xFFFFFF,
	dropShadow: true,
	fontWeight: 900
});
highscoreText.anchor.set(0.5, -3.5);
highscoreText.alpha = 0;
LK.gui.top.addChild(highscoreText);
// Initialize highscore from storage
var highscore = storage.highscore || 0;
highscoreText.setText('Best: ' + highscore);
function addPlatformAt(x, y) {
	var platform = backgroundContainer.addChild(new Platform());
	platform.x = x;
	platform.y = y;
	platforms.push(platform);
	return platform;
}
var backgroundContainer = new Container();
var foregroundContainer = new Container();
game.addChild(backgroundContainer);
game.addChild(foregroundContainer);
var xspeed = 0;
var globalSpeed = -5;
var platforms = [];
var platform0 = addPlatformAt(1000, 2200);
var platform1 = addPlatformAt(2450, 2200);
var platform2 = addPlatformAt(3000 + 1450, 2200);
var platform3 = addPlatformAt(5000 + 1450, 1800);
var platform4 = addPlatformAt(7000 + 1450, 2200);
var platformRef = platforms[0];
var chickenBork = platformRef.attachAsset('NyanCat', {
	anchorX: 0.5,
	anchorY: 1,
	x: platformRef.width / 2,
	y: -1300
});
var smallBork = platformRef.attachAsset('small_bork', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: platformRef.width / 2,
	y: -1024
});
var platform2 = platforms[1];
var largeBork = backgroundContainer.addChild(new LargeBork());
largeBork.x = platform2.x + platform2.width + 250;
largeBork.y = platform2.y - 1024;
var lastPlatform = addPlatformAt(9000 + 1500, 1600);
var trophy = lastPlatform.attachAsset('trophy', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: lastPlatform.width / 2,
	y: -100
});
trophy.visible = false;
var boffset = 0;
var projectilesStarted = false;
var trailStarted = false;
var trailWave = false;
function doSmallBork() {
	var speechBubble = new SpeechBubble();
	var offset = Math.PI / 2 * Math.random() - Math.PI / 2 - Math.PI / 4;
	speechBubble.x = smallBork.x + 400 + Math.cos(offset) * 300;
	speechBubble.y = smallBork.y - 400 + Math.sin(offset) * 300;
	speechBubble.rotation = offset + Math.PI * 0.5 - .1;
	platformRef.addChildAt(speechBubble, 0);
	chickenBork.scale.y = 1.05;
	chickenBork.scale.x = 1.05;
	tween(chickenBork, {
		scaleY: 1,
		scaleX: 1
	}, {
		duration: 800,
		easing: tween.elasticOut
	});
	largeBork.bork();
	if (platform2.x > -1024) {
		LK.setTimeout(doSmallBork, 1200 + Math.random() * 300);
	}
}
doSmallBork();
var particles = backgroundContainer.addChild(new Particles());
var chicken = foregroundContainer.addChild(new Chicken());
Platform.prototype.chicken = chicken;
chicken.x = 2048 / 2;
chicken.y = 2200;
var startFlag = foregroundContainer.addChild(new StartFlag());
startFlag.x = platform2.x + 130;
startFlag.y = platform2.y - 140;
var platformManager = backgroundContainer.addChild(new PlatformManager());
platformManager.platformCount = 0;
platformManager.activePlatforms.push(platform0, platform1, platform2, platform3, platform4);
var projectileManager = backgroundContainer.addChild(new ProjectileManager());
// var pitchDebugMarker = backgroundContainer.addChild(new DebugMarker());
// pitchDebugMarker.x = 50;
// pitchDebugMarker.y = 2600;
// pitchDebugMarker.alpha = 0.7;
var countdown = foregroundContainer.addChild(new Countdown());
countdown.alpha = 0;
countdown.updateCount(0);
game.update = function () {
	if (chicken.y > 5000) {
		LK.effects.flashScreen(0xff0000, 3000);
		// Update highscore before game over
		// if (jumpCounter > highscore) {
		// 	highscore = jumpCounter;
		// 	storage.highscore = highscore;
		// 	highscoreText.setText('Best: ' + highscore);
		// }
		highscoreText.alpha = 1;
		if (jumpCounter == highscore) {
			LK.showYouWin();
		} else {
			LK.showGameOver();
		}
	}
	if (chicken.failed) {
		return;
	}
	// Collision detection is now handled in Projectile class
	if (!chicken.isJumping) {
		xspeed += .5;
		xspeed *= .4;
		if (facekit.mouthOpen) {
			chicken.jump(1.0);
		} else if (!facekit.mouthOpen && !chicken.mouthClosedFlag) {
			chicken.mouthClosedFlag = true;
		}
	}
	if (trailStarted) {
		particles.addDirtParticle(chicken.x - 200 + 20 * (Math.abs(globalSpeed) / 5), chicken.y + (trailWave ? -10 : 0));
	}
};
LK.playMusic('nyanLikeMusic');
LK.setTimeout(function () {
	trailStarted = true;
}, 300);
LK.setTimeout(function () {
	projectilesStarted = true;
}, 3000);
LK.setTimeout(function () {
	tween(countdown, {
		alpha: 1
	}, {
		duration: 400
	});
}, 6000);
LK.setInterval(function () {
	trailWave = !trailWave;
}, 300);
LK.setInterval(function () {
	xspeed *= 1.08;
	globalSpeed *= 1.08;
}, 3000); /**** 
* Plugins
****/ 
var facekit = LK.import("@upit/facekit.v1");
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var Chicken = Container.expand(function () {
	var self = Container.call(this);
	self.failed = false;
	self.mouthClosedFlag = true;
	self.tail = self.attachAsset('tail', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1,
		scaleY: -1
	});
	self.tail.x = -210;
	self.tail.y = -200;
	var paws = self.attachAsset('nyanCatPaws', {
		anchorX: 0.5,
		anchorY: 1,
		scaleX: 0.9
	});
	paws.x = 30;
	paws.y = 20;
	self.body = self.attachAsset('nyanCatBody', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 1
	});
	var headIdle = self.attachAsset('nyanCatHeadIdle', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 1
	});
	var headOh = self.attachAsset('nyanCatHeadAngry', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 0
	});
	var headAngry = self.attachAsset('nyanCatHeadOh', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 0
	});
	var headSmile = self.attachAsset('nyanCatHeadSmile', {
		anchorX: 0.5,
		anchorY: 1,
		alpha: 0
	});
	var collisionElement = self.attachAsset('collisionElement', {
		anchorX: 0.5,
		anchorY: 0.5,
		tint: 0xFF0000,
		width: self.body.width * 0.4,
		height: self.body.height * 0.2
	});
	headIdle.x = 180;
	headIdle.y = -100;
	headOh.x = 180;
	headOh.y = -100;
	headAngry.x = 180;
	headAngry.y = -100;
	headSmile.x = 180;
	headSmile.y = -100;
	collisionElement.alpha = 0;
	collisionElement.y = -30;
	self.body.y = 40;
	self.speed = 8;
	self.initialGlobalSpeed = typeof globalSpeed !== 'undefined' ? Math.abs(globalSpeed) : 10;
	self.baseJumpVelocity = -30;
	self.baseJumpHeight = 300;
	self.jumpHeight = self.baseJumpHeight;
	self.isJumping = true;
	self.jumpVelocity = 0;
	self.isOnPlatform = true;
	self.lastPlatform = undefined;
	self.update = function () {
		self.y += self.jumpVelocity;
		if (self.failed) {
			return;
		}
		var gravityFactor = Math.abs(globalSpeed) / 8;
		self.jumpVelocity += 1 * gravityFactor;
		var maxFallSpeed = 30 * gravityFactor;
		if (self.jumpVelocity > maxFallSpeed) {
			self.jumpVelocity = maxFallSpeed;
		}
		if (!self.isHit) {
			if (self.isJumping) {
				// && self.jumpVelocity < 0
				headIdle.alpha = 0;
				headOh.alpha = 0;
				headAngry.alpha = 0;
				headSmile.alpha = 1;
			} else if (targetPlatform && self.y > targetPlatform.y) {
				headIdle.alpha = 0;
				headOh.alpha = 0;
				headAngry.alpha = 1;
				headSmile.alpha = 0;
			} else {
				headIdle.alpha = 1;
				headOh.alpha = 0;
				headAngry.alpha = 0;
				headSmile.alpha = 0;
			}
		}
		var targetPlatform = undefined;
		self.isOnPlatform = platforms.some(function (platform) {
			if (collisionElement.intersects(platform)) {
				if (self.y - platform.y < 100) {
					targetPlatform = platform;
					return true;
				}
			}
		});
		if (targetPlatform) {
			//pitchLabel.setText('Platform #' + targetPlatform.index);
			jumpCounter = Math.max(0, targetPlatform.index - 2);
			if (countdown) {
				countdown.updateCount(jumpCounter);
			}
			if (jumpCounter > highscore) {
				highscore = jumpCounter;
				storage.highscore = highscore;
				highscoreText.setText('Best: ' + highscore);
			}
		}
		if (!self.failed && self.y > 2400) {
			self.failed = true;
			xspeed = 0;
			LK.effects.flashScreen(0xFFFFFF, 100);
			headIdle.alpha = 0;
			headOh.alpha = 0;
			headAngry.alpha = 1;
			headSmile.alpha = 0;
			self.jumpVelocity = 0;
			LK.getSound('failed').play();
			// Update highscore before failure animation
			// if (jumpCounter > highscore) {
			// 	highscore = jumpCounter;
			// 	storage.highscore = highscore;
			// 	highscoreText.setText('Best: ' + highscore);
			// }
			tween(headAngry, {
				scaleY: 3,
				scaleX: 3
			}, {
				duration: 500,
				easing: tween.easeOut
			});
			tween(self, {
				y: 3000
			}, {
				duration: 1600,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					self.jumpVelocity = 20;
				}
			});
		}
		if (self.isOnPlatform && self.jumpVelocity > 0) {
			if (self.lastPlatform !== targetPlatform) {
				animatePaws();
				self.lastPlatform = targetPlatform;
			}
			self.y = targetPlatform.y + 70;
			if (self.isJumping) {
				self.body.scale.y = .8;
				self.body.scale.x = 1.2;
				tween(self.body, {
					scaleY: 1,
					scaleX: 1
				}, {
					duration: 1000,
					easing: tween.elasticOut
				});
				tween.stop(paws, {
					x: true
				});
				paws.x = 0;
				animatePaws();
			}
			self.isJumping = false;
			self.lastPlatform = targetPlatform;
			self.jumpVelocity = 0;
		}
		self.x = 2048 / 2;
		var speedScaleFactor = Math.min(Math.abs(globalSpeed / 8), 2.0);
		self.jumpHeight = self.baseJumpHeight * speedScaleFactor;
	};
	self.animateHit = function () {
		if (self.failed) {
			return;
		}
		self.isHit = true;
		headIdle.alpha = 0;
		headOh.alpha = 1;
		headAngry.alpha = 0;
		headSmile.alpha = 0;
		LK.effects.flashScreen(0xFFF0000, 300);
		// Play hit sound
		LK.getSound('hit').play();
		// After 1 second, reset hit state
		LK.setTimeout(function () {
			self.isHit = false;
			if (self.failed) {
				return;
			}
			if (!self.isJumping) {
				headIdle.alpha = 1;
				headSmile.alpha = 0;
			}
			if (self.isJumping) {
				headIdle.alpha = 0;
				headSmile.alpha = 1;
			}
			headOh.alpha = 0;
			headAngry.alpha = 0;
		}, 1000);
	};
	function groove() {
		tween(self, {
			scaleX: 1.03,
			scaleY: 1.03
		}, {
			duration: 300,
			onFinish: function onFinish() {
				tween(self, {
					scaleX: 1,
					scaleY: 1
				}, {
					onFinish: groove,
					duration: 300
				});
			}
		});
	}
	groove();
	self.jump = function (strength) {
		if (platforms[0].x > -1500) {
			return;
		}
		if (!self.isJumping && self.isOnPlatform && self.mouthClosedFlag) {
			self.isJumping = true;
			self.mouthClosedFlag = false;
			LK.getSound('jump').play();
			var currentSpeed = typeof globalSpeed !== 'undefined' ? Math.abs(globalSpeed) : self.initialGlobalSpeed;
			var jumpScale = Math.sqrt(currentSpeed / self.initialGlobalSpeed);
			chicken.jumpVelocity = self.baseJumpVelocity * jumpScale;
			tween.stop(paws, {
				x: true
			});
			tween(paws, {
				x: 0
			}, {
				duration: 100
			});
			self.update();
		}
	};
	function animatePaws() {
		if (!self.isOnPlatform) {
			return;
		}
		tween(paws, {
			x: -10
		}, {
			duration: 160,
			easing: tween.linear,
			onFinish: function onFinish() {
				tween(paws, {
					x: 30
				}, {
					duration: 160,
					easing: tween.linear,
					onFinish: animatePaws
				});
			}
		});
	}
	function animateTail() {
		self.tail.rotation = 0;
		tween(self.tail, {
			rotation: 0.3
		}, {
			duration: 700,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				tween(self.tail, {
					rotation: 0
				}, {
					duration: 700,
					easing: tween.easeOut,
					onFinish: animateTail
				});
			}
		});
	}
	// function animateTail() {
	// 	self.tail.scale.x = 1;
	// 	self.tail.scale.y = 1;
	// 	tween(self.tail, {
	// 		scaleX: 1.1,
	// 		scaleY: -1
	// 	}, {
	// 		duration: 600,
	// 		easing: tween.easeOut,
	// 		onFinish: function onFinish() {
	// 			tween(self.tail, {
	// 				scaleX: 1,
	// 				scaleY: 1
	// 			}, {
	// 				duration: 600,
	// 				easing: tween.easeOut,
	// 				onFinish: animateTail
	// 			});
	// 		}
	// 	});
	// }
	animateTail();
	return self;
});
var Countdown = Container.expand(function () {
	var self = Container.call(this);
	var jumpCount = 0;
	var countdownTextShadow = new Text2("0", {
		size: 500,
		fill: 0x000000,
		weight: 800
	});
	var countdownText = new Text2("0", {
		size: 500,
		fill: 0xFFFFFF,
		weight: 800
	});
	countdownText.anchor.set(0.5, 0.5);
	countdownTextShadow.anchor.set(0.5, 0.5);
	self.addChild(countdownTextShadow);
	self.addChild(countdownText);
	self.x = 2048 / 2;
	self.y = 400;
	countdownTextShadow.x = 15;
	countdownTextShadow.y = 15;
	function tweenIt() {
		self.scale.set(1, 1);
		tween(self, {
			scaleX: .6,
			scaleY: .6
		}, {
			duration: 300,
			easing: tween.bounceOut
		});
	}
	self.updateCount = function (count) {
		if (jumpCount !== count) {
			jumpCount = count;
			countdownText.setText(jumpCount.toString());
			countdownTextShadow.setText(jumpCount.toString());
			// Update LK score to match jumpCounter
			LK.setScore(count);
			tweenIt();
		}
	};
	tweenIt();
});
var DebugMarker = Container.expand(function () {
	var self = Container.call(this);
	var debugGraphics = self.attachAsset('debugMark', {
		anchorX: 0,
		anchorY: 0.5
	});
	debugGraphics.width = 1;
	self.update = function () {
		debugGraphics.width = facekit.mouthOpen ? 1000 : 100;
		debugGraphics.tint = facekit.mouthOpen ? 0x00ff00 : 0xff0000;
	};
	return self;
});
var DirtParticle = Container.expand(function () {
	var self = Container.call(this);
	var dirtGraphics = self.attachAsset('trail', {
		anchorX: 0.5,
		anchorY: 1
	});
	var speed = 2;
	var angle = 0;
	var scale = 1;
	self.livetime = 110;
	self.update = function () {
		self.livetime--;
		if (self.livetime < 0) {
			self.destroy();
			return;
		}
		if (chicken.failed) {
			return;
		}
		if (self.x < -50) {
			self.destroy();
			return;
		}
		self.x += Math.cos(angle) * speed;
		self.y += Math.sin(angle) * speed;
	};
});
var LargeBork = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	var largeBorkGraphicsChicken = self.attachAsset('NyanCat', {
		anchorX: 0.5,
		anchorY: 1,
		scaleX: -1
	});
	largeBorkGraphicsChicken.y = -300;
	var largeBorkGraphics = self.attachAsset('large_bork', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
	self.bork = function () {
		var speechBubbleLarge = new SpeechBubbleLarge();
		var offset = Math.PI / 4 * Math.random() - Math.PI / 2 - Math.PI / 4;
		speechBubbleLarge.x = -360 + Math.cos(offset) * 300;
		speechBubbleLarge.y = -300 + Math.sin(offset) * 350;
		speechBubbleLarge.rotation = offset + Math.PI * 0.5 + .3;
		self.addChild(speechBubbleLarge);
		largeBorkGraphicsChicken.scale.y = 1.05;
		largeBorkGraphicsChicken.scale.x = -1.05;
		largeBorkGraphicsChicken.rotation = .2;
		tween(largeBorkGraphicsChicken, {
			scaleY: 1,
			scaleX: -1,
			rotation: 0
		}, {
			duration: 1000,
			easing: tween.elasticOut
		});
	};
});
var Particles = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	self.addDirtParticle = function (x, y) {
		var dirtParticle = new DirtParticle();
		dirtParticle.x = x - self.x + 100;
		dirtParticle.y = y - 100;
		self.addChild(dirtParticle);
	};
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 1;
	self.index = getNextPlatformIndex();
	var groundAsset = 'ground';
	if (self.index < 5) {
		groundAsset = 'ground';
	} else if (self.index < 15) {
		groundAsset = 'ground0';
	} else if (self.index < 30) {
		groundAsset = 'ground1';
	} else if (self.index < 40) {
		groundAsset = 'ground2';
	} else if (self.index < 50) {
		groundAsset = 'ground3';
	} else if (self.index < 60) {
		groundAsset = 'ground4';
	} else if (self.index < 70) {
		groundAsset = 'ground5';
	} else if (self.index < 80) {
		groundAsset = 'ground6';
	} else {
		groundAsset = 'ground7';
	}
	var groundGraphics = self.attachAsset(groundAsset, {
		anchorX: 0,
		anchorY: 0
	});
	self.width = groundGraphics.width;
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
});
var PlatformManager = Container.expand(function () {
	var self = Container.call(this);
	self.lastX = 0;
	self.platformGap = 1500;
	self.platformCount = 0;
	self.maxPlatforms = 10;
	self.activePlatforms = [];
	self.trackLastPlatform = function () {
		var lastX = 0;
		platforms.forEach(function (platform) {
			if (platform.x + platform.width > lastX) {
				lastX = platform.x + platform.width;
			}
		});
		self.lastX = lastX;
	};
	self.generatePlatform = function (x, y) {
		var platform = new Platform();
		var offset = 450;
		// Adjust offset based on platform width to maintain consistent gaps
		// if (platform.width < 1500) {
		// 	offset = Math.max(100, offset - (1500 - platform.width));
		// }
		//pitchLabel.setText('Next gap #' + platform.index + ' w=' + platform.width + ' x=' + platform.x + ' : ' + offset);
		platform.x = x + offset;
		//self.lastX = platform.x + platform.width;
		platform.y = y;
		backgroundContainer.addChild(platform);
		platforms.push(platform);
		self.activePlatforms.push(platform);
		return platform;
	};
	self.getRandomHeight = function () {
		var heights = [1600, 1800, 2000, 2200];
		return heights[Math.floor(Math.random() * heights.length)];
	};
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		self.trackLastPlatform();
		if (self.activePlatforms.length < self.maxPlatforms) {
			var newPlatform = self.generatePlatform(self.lastX, self.getRandomHeight());
			self.platformCount++;
		}
		for (var i = self.activePlatforms.length - 1; i >= 0; i--) {
			var platform = self.activePlatforms[i];
			if (platform.x + platform.width < -2000) {
				if (platforms.indexOf(platform) >= 5) {
					platforms.splice(platforms.indexOf(platform), 1);
					self.activePlatforms.splice(i, 1);
					platform.destroy();
				}
			}
		}
	};
	return self;
});
var Projectile = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 1;
	// Use cucumber asset with proper anchoring
	var cucumberGraphics = self.attachAsset('cucumber', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Initial rotation and setup
	self.rotationSpeed = Math.random() * 0.05 - 0.10; // Random rotation speed
	self.update = function () {
		if (chicken.failed) {
			return;
		}
		// Move like platforms
		self.moveMultiplier = xspeed * 10;
		self.x += globalSpeed * self.moveMultiplier;
		// Apply rotation
		self.rotation += self.rotationSpeed;
		// Check for collision with chicken
		if (chicken && chicken.body && !chicken.failed) {
			if (chicken.body.intersects(self)) {
				chicken.animateHit();
				// Find this projectile in the manager's array and remove it
				var index = projectileManager.activeProjectiles.indexOf(self);
				if (index !== -1) {
					projectileManager.activeProjectiles.splice(index, 1);
				}
				self.destroy();
			}
		}
	};
	return self;
});
var ProjectileManager = Container.expand(function () {
	var self = Container.call(this);
	self.maxProjectiles = 5;
	self.activeProjectiles = [];
	self.spawnDelay = 1000; // Frames between spawns
	self.timer = 0;
	self.spawnProjectile = function () {
		var projectile = new Projectile();
		var heights = [500, 700, 1000, 1300];
		var randomHeight = heights[Math.floor(Math.random() * heights.length)];
		projectile.x = 2048 + 320; // Start off-screen to the right
		projectile.y = randomHeight;
		backgroundContainer.addChild(projectile);
		self.activeProjectiles.push(projectile);
		return projectile;
	};
	self.update = function () {
		if (!projectilesStarted || chicken.failed) {
			return;
		}
		// Spawn new projectiles on timer
		self.timer++;
		if (self.timer >= self.spawnDelay && self.activeProjectiles.length < self.maxProjectiles) {
			self.spawnProjectile();
			self.timer = 0;
		}
		// Clean up projectiles that are off-screen
		for (var i = self.activeProjectiles.length - 1; i >= 0; i--) {
			var projectile = self.activeProjectiles[i];
			if (projectile.x < -300) {
				self.activeProjectiles.splice(i, 1);
				projectile.destroy();
			}
		}
	};
	return self;
});
var SpeechBubble = Container.expand(function () {
	var self = Container.call(this);
	var bubbleGraphics = self.attachAsset('small_bork_speach', {
		anchorX: 0.8,
		anchorY: 1
	});
	self.y -= 50;
	self.alpha = 1;
	self.update = function () {
		self.y -= 1;
		self.alpha -= 0.01;
		if (self.alpha <= 0) {
			self.destroy();
		}
	};
	tween(self, {
		alpha: 0
	}, {
		duration: 2000,
		easing: tween.linear,
		onFinish: function onFinish() {
			self.destroy();
		}
	});
});
var SpeechBubbleLarge = Container.expand(function () {
	var self = Container.call(this);
	var bubbleGraphics = self.attachAsset('large_bork_speach', {
		anchorX: 0.2,
		anchorY: 1
	});
	self.y -= 50;
	self.alpha = 1;
	self.scale.set(0, 0);
	tween(self, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 600,
		easing: tween.elasticOut
	});
	LK.setTimeout(function () {
		tween(self, {
			alpha: 0,
			y: self.y - 100
		}, {
			duration: 100,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.destroy();
			}
		});
	}, 800);
});
var Spikes = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	var spikesGraphics = self.attachAsset('spikes', {
		anchorX: 0,
		anchorY: 0
	});
	self.update = function () {
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
	};
});
var StartFlag = Container.expand(function () {
	var self = Container.call(this);
	self.moveMultiplier = 0;
	var flagGraphics = self.attachAsset('start', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.moveMultiplier = xspeed * 6;
		self.x += globalSpeed * self.moveMultiplier;
		if (self.x < -500) {
			self.destroy();
			startFlag = null;
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
var globalNextPlatformIndex = 0;
var jumpCounter = 0;
function getNextPlatformIndex() {
	globalNextPlatformIndex++;
	return globalNextPlatformIndex;
}
// var volumeLabel = new Text2('Volume: 0', {
// 	size: 50,
// 	fill: 0xFFFFFF
// });
// volumeLabel.anchor.set(1, 1);
// volumeLabel.alpha = .3;
// LK.gui.bottomRight.addChild(volumeLabel);
var pitchLabel = new Text2('', {
	size: 50,
	fill: 0xFFFFFF
});
pitchLabel.anchor.set(0, 1);
pitchLabel.alpha = .5;
LK.gui.bottomLeft.addChild(pitchLabel);
// Create highscore display
var highscoreText = new Text2('', {
	size: 100,
	fill: 0xFFFFFF,
	dropShadow: true,
	fontWeight: 900
});
highscoreText.anchor.set(0.5, -3.5);
highscoreText.alpha = 0;
LK.gui.top.addChild(highscoreText);
// Initialize highscore from storage
var highscore = storage.highscore || 0;
highscoreText.setText('Best: ' + highscore);
function addPlatformAt(x, y) {
	var platform = backgroundContainer.addChild(new Platform());
	platform.x = x;
	platform.y = y;
	platforms.push(platform);
	return platform;
}
var backgroundContainer = new Container();
var foregroundContainer = new Container();
game.addChild(backgroundContainer);
game.addChild(foregroundContainer);
var xspeed = 0;
var globalSpeed = -5;
var platforms = [];
var platform0 = addPlatformAt(1000, 2200);
var platform1 = addPlatformAt(2450, 2200);
var platform2 = addPlatformAt(3000 + 1450, 2200);
var platform3 = addPlatformAt(5000 + 1450, 1800);
var platform4 = addPlatformAt(7000 + 1450, 2200);
var platformRef = platforms[0];
var chickenBork = platformRef.attachAsset('NyanCat', {
	anchorX: 0.5,
	anchorY: 1,
	x: platformRef.width / 2,
	y: -1300
});
var smallBork = platformRef.attachAsset('small_bork', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: platformRef.width / 2,
	y: -1024
});
var platform2 = platforms[1];
var largeBork = backgroundContainer.addChild(new LargeBork());
largeBork.x = platform2.x + platform2.width + 250;
largeBork.y = platform2.y - 1024;
var lastPlatform = addPlatformAt(9000 + 1500, 1600);
var trophy = lastPlatform.attachAsset('trophy', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: lastPlatform.width / 2,
	y: -100
});
trophy.visible = false;
var boffset = 0;
var projectilesStarted = false;
var trailStarted = false;
var trailWave = false;
function doSmallBork() {
	var speechBubble = new SpeechBubble();
	var offset = Math.PI / 2 * Math.random() - Math.PI / 2 - Math.PI / 4;
	speechBubble.x = smallBork.x + 400 + Math.cos(offset) * 300;
	speechBubble.y = smallBork.y - 400 + Math.sin(offset) * 300;
	speechBubble.rotation = offset + Math.PI * 0.5 - .1;
	platformRef.addChildAt(speechBubble, 0);
	chickenBork.scale.y = 1.05;
	chickenBork.scale.x = 1.05;
	tween(chickenBork, {
		scaleY: 1,
		scaleX: 1
	}, {
		duration: 800,
		easing: tween.elasticOut
	});
	largeBork.bork();
	if (platform2.x > -1024) {
		LK.setTimeout(doSmallBork, 1200 + Math.random() * 300);
	}
}
doSmallBork();
var particles = backgroundContainer.addChild(new Particles());
var chicken = foregroundContainer.addChild(new Chicken());
Platform.prototype.chicken = chicken;
chicken.x = 2048 / 2;
chicken.y = 2200;
var startFlag = foregroundContainer.addChild(new StartFlag());
startFlag.x = platform2.x + 130;
startFlag.y = platform2.y - 140;
var platformManager = backgroundContainer.addChild(new PlatformManager());
platformManager.platformCount = 0;
platformManager.activePlatforms.push(platform0, platform1, platform2, platform3, platform4);
var projectileManager = backgroundContainer.addChild(new ProjectileManager());
// var pitchDebugMarker = backgroundContainer.addChild(new DebugMarker());
// pitchDebugMarker.x = 50;
// pitchDebugMarker.y = 2600;
// pitchDebugMarker.alpha = 0.7;
var countdown = foregroundContainer.addChild(new Countdown());
countdown.alpha = 0;
countdown.updateCount(0);
game.update = function () {
	if (chicken.y > 5000) {
		LK.effects.flashScreen(0xff0000, 3000);
		// Update highscore before game over
		// if (jumpCounter > highscore) {
		// 	highscore = jumpCounter;
		// 	storage.highscore = highscore;
		// 	highscoreText.setText('Best: ' + highscore);
		// }
		highscoreText.alpha = 1;
		if (jumpCounter == highscore) {
			LK.showYouWin();
		} else {
			LK.showGameOver();
		}
	}
	if (chicken.failed) {
		return;
	}
	// Collision detection is now handled in Projectile class
	if (!chicken.isJumping) {
		xspeed += .5;
		xspeed *= .4;
		if (facekit.mouthOpen) {
			chicken.jump(1.0);
		} else if (!facekit.mouthOpen && !chicken.mouthClosedFlag) {
			chicken.mouthClosedFlag = true;
		}
	}
	if (trailStarted) {
		particles.addDirtParticle(chicken.x - 200 + 20 * (Math.abs(globalSpeed) / 5), chicken.y + (trailWave ? -10 : 0));
	}
};
LK.playMusic('nyanLikeMusic');
LK.setTimeout(function () {
	trailStarted = true;
}, 300);
LK.setTimeout(function () {
	projectilesStarted = true;
}, 3000);
LK.setTimeout(function () {
	tween(countdown, {
		alpha: 1
	}, {
		duration: 400
	});
}, 6000);
LK.setInterval(function () {
	trailWave = !trailWave;
}, 300);
LK.setInterval(function () {
	xspeed *= 1.08;
	globalSpeed *= 1.08;
}, 3000);
:quality(85)/https://cdn.frvr.ai/679d0450200702e4a761011c.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/679d0ecef1bec1b24bcb550c.png%3F3) 
 Row of Spikes. Computer Game Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/679d2b901a7b86672c796707.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/679d36bf1a7b86672c79687f.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/679d3e0d1a7b86672c7968d6.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6814e5f70e23fbc7292fa21c.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/681db074365c33de7dbef344.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/681dc4db749684c992474252.png%3F3) 
 photo-realistic
:quality(85)/https://cdn.frvr.ai/681df13e8ac3f0ba61c4b278.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/681df6585294a37c3f44b74a.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/681df94a5294a37c3f44b779.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/681dff335294a37c3f44b7a9.png%3F3) 
 a smartphone (black screen)
:quality(85)/https://cdn.frvr.ai/68207b21cca9b85b814edd80.png%3F3) 
 A simple wide hand-drawn symmetrical ribbon banners. The banner text reads “Open WIDE to jump!” in playful, cartoonish black lettering. The ribbon is warm beige parchment. Each side ends with simple curved, scroll-like ribbon. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/68207b8bcca9b85b814edd93.png%3F3) 
 A simple wide hand-drawn symmetrical ribbon banners. The banner text reads “Keep mouth closed” in playful, cartoonish black lettering. The ribbon is warm beige parchment. Each side ends with simple curved, scroll-like ribbon. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/68208c33f603b8fc5c6591bd.png%3F3) 
 Simple cartoon speech bubble with closed lips icon and lowercase lettering 'mmm...'. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/68208cd9f603b8fc5c6591d1.png%3F3) 
 Simple cartoon speech bubble with big open mouth icon and uppercase lettering 'MIAW!'. Bubble tail on the right. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6820a67af603b8fc5c659357.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6820ebad0929ccfb11cca8e1.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6820ed120929ccfb11cca8ef.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6820f8360929ccfb11cca95e.png%3F3) 
 lateral view of a flat cake in rectangular platform shape for a platformer game. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6820fdca0929ccfb11cca97b.png%3F3) 
 lateral view of a flat cake in rectangular platform shape for a platformer game. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/682112edaeb083630b98f52c.png%3F3) 
 lateral view of a flat rainbow cake in rectangular platform shape for a platformer game. In-Game asset. 2d. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/68216ff63af03528036d3e0a.png%3F3) 
 photo of a cucumber meme
:quality(85)/https://cdn.frvr.ai/682173d1fba23b35754f0f0b.png%3F3) 
 open cardboard box. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows
:quality(85)/https://cdn.frvr.ai/6821846a35e4f156d7ebb6a5.png%3F3)