/**** 
* Classes
****/ 
//<Assets used in the game will automatically appear here>
// Class for the main character (Jumping Jack)
var JumpingJack = Container.expand(function () {
	var self = Container.call(this);
	var jackGraphics = self.attachAsset('jack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedY = 0;
	self.gravity = 1.0;
	self.jumpStrength = -30;
	self.isJumping = false;
	self.isInvincible = false;
	self.invincibilityHitsLeft = 0;
	self.invincibilityDuration = 5000; // Duration of invincibility in milliseconds
	self.isScoreMultiplier = false;
	self.invincibilityStartTime = 0; // Track when invincibility starts
	self.obstaclesPassThrough = false; // Track if obstacles should pass through player
	self.update = function () {
		if (self.isJumping) {
			self.speedY += self.gravity * 1.5; // Increase fall speed
			self.y += self.speedY;
			if (self.y >= 2000) {
				// Ground level
				self.y = 2000;
				self.isJumping = false;
				self.speedY = 0;
			} else {
				// Check for platform collisions
				var onPlatform = false;
				for (var i = platforms.length - 1; i >= 0; i--) {
					if (platforms[i] && self.intersects(platforms[i])) {
						if (self.speedY > 0) {
							// Only land on platform if falling
							self.y = platforms[i].y - self.height / 2;
							self.isJumping = false;
							self.speedY = 0;
							onPlatform = true;
						} else if (self.speedY === 0) {
							// Temporarily suspend player on platform
							self.y = platforms[i].y - self.height / 2;
							onPlatform = true;
						}
					}
				}
				if (!onPlatform) {
					self.isJumping = true;
					self.speedY += self.gravity * 1.5; // Ensure player falls if not touching platform
				} else {
					self.isJumping = false; // Ensure player stops falling if touching platform
					self.speedY = 0;
					LK.setTimeout(function () {
						self.isJumping = true;
					}, 1000); // Delay before falling off platform
				}
			}
		}
	};
	self.jump = function () {
		if (!self.isJumping) {
			self.isJumping = true;
			self.speedY = self.jumpStrength * 2.0; // Increase jump speed more drastically
		}
	};
	return self;
});
// Class for obstacles
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedX = -5;
	self.update = function () {
		self.x += self.speedX;
		self.y = 2000; // Ensure obstacles stay at ground level
		self.y = 2000; // Ensure obstacles stay at ground level
		self.y = 2000; // Ensure obstacles stay at ground level
		// Ensure obstacles stay on the ground level
		if (self.x < -100) {
			// Off-screen
			self.destroy();
		}
	};
	return self;
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('platform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.x -= 5; // Move platform from right to left
		if (self.x < -self.width) {
			self.destroy(); // Destroy platform if it goes off-screen
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Sky blue background
});
/**** 
* Game Code
****/ 
var score = 0;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var jack = game.addChild(new JumpingJack());
jack.jumpStrength = -30; // Initialize player jump height
jack.speedY = 0; // Initialize player jump speed
jack.gravity = 1.0; // Initialize player fall speed
jack.x = 300;
jack.y = 2000;
var obstacles = [];
var platforms = [];
var platformInterval = Math.floor(Math.random() * 200) + 100; // Random interval between 100 and 300 for platform generation
var obstacleInterval = Math.floor(Math.random() * 150) + 100; // Random interval between 100 and 250 for obstacle generation
game.down = function (x, y, obj) {
	jack.jump();
};
game.update = function () {
	// Update score
	if (jack.isInvincible && Date.now() - jack.invincibilityStartTime > jack.invincibilityDuration) {
		jack.isInvincible = false;
		jack.obstaclesPassThrough = false; // Disable obstacles passing through player
	}
	if (jack.isScoreMultiplier) {
		score += 3; // Triple score
	} else {
		score += 1;
	}
	// Increase jump height, jump speed, and fall speed at every 100 score milestone
	if (score % 100 === 0 && score !== 0) {
		jack.jumpStrength -= 5; // Increase jump height
		jack.speedY -= 1; // Increase jump speed
		jack.gravity += 0.1; // Increase fall speed
	}
	scoreTxt.setText(score);
	// Update obstacles
	for (var i = obstacles.length - 1; i >= 0; i--) {
		if (obstacles[i]) {
			obstacles[i].update();
		}
		if (obstacles[i]) {
			obstacles[i].x += obstacles[i].speedX;
		}
		// Movement handled in obstacle class
		if (obstacles[i] && obstacles[i].intersects(jack)) {
			if (jack.isInvincible || jack.obstaclesPassThrough) {
				jack.invincibilityHitsLeft--;
				if (jack.invincibilityHitsLeft <= 0) {
					jack.isInvincible = false;
					jack.obstaclesPassThrough = false; // Disable obstacles passing through player
				}
			} else if (!jack.obstaclesPassThrough) {
				LK.effects.flashScreen(0xff0000, 1000);
				// Reset player position to start
				jack.x = 300;
				jack.y = 2000;
				jack.isJumping = false;
				jack.speedY = 0;
				// Reset obstacle positions
				for (var j = obstacles.length - 1; j >= 0; j--) {
					obstacles[j].x = 2048;
					obstacles[j].y = 2000; // Ground level
				}
				// Update platforms
				for (var i = platforms.length - 1; i >= 0; i--) {
					if (platforms[i]) {
						platforms[i].update();
						platforms[i].x -= 5; // Move platform from right to left
						if (platforms[i].x < -platforms[i].width) {
							platforms[i].destroy();
							platforms.splice(i, 1);
						}
					}
					if (platforms[i] && platforms[i].intersects(jack)) {
						if (jack.speedY > 0) {
							// Only land on platform if falling
							jack.y = platforms[i].y - jack.height / 2;
							jack.isJumping = false;
							jack.speedY = 0;
						}
					} else {
						jack.isJumping = true;
						jack.speedY += jack.gravity * 1.5; // Ensure player falls if not touching platform
					}
					if (platforms[i] && platforms[i].x < -100) {
						platforms[i].destroy();
						platforms.splice(i, 1);
					}
				}
				// Reset platform positions
				for (var k = platforms.length - 1; k >= 0; k--) {
					platforms[k].destroy();
					platforms.splice(k, 1);
				}
				// Reset score
				score = 0;
				scoreTxt.setText(score);
				// Reset jump height, jump speed, and fall speed
				jack.jumpStrength = -30;
				jack.speedY = 0;
				jack.gravity = 1.0;
				// Reset obstacle generation interval to a random value
				obstacleInterval = Math.floor(Math.random() * 100) + 50;
				// Reset fall speed
				jack.gravity = 1.0;
				// Reset obstacles
				for (var j = obstacles.length - 1; j >= 0; j--) {
					obstacles[j].destroy();
					obstacles.splice(j, 1);
				}
			}
		}
		if (obstacles[i] && obstacles[i].x < -100) {
			obstacles[i].destroy();
			obstacles.splice(i, 1);
		}
	}
	// Generate new obstacles
	if (LK.ticks % obstacleInterval == 0) {
		obstacleInterval = Math.floor(Math.random() * 150) + 100; // Set next interval to a new random value
		var newObstacle = new Obstacle();
		newObstacle.x = 2048;
		newObstacle.y = 2000; // Ground level
		newObstacle.y = 2000; // Ground level
		obstacles.push(newObstacle);
		game.addChild(newObstacle);
	}
	// Generate new platforms
	if (LK.ticks % platformInterval == 0) {
		platformInterval = Math.floor(Math.random() * 200) + 100; // Set next interval to a new random value
		var newPlatform = new Platform();
		newPlatform.x = 2048;
		newPlatform.y = Math.floor(Math.random() * 1500) + 500; // Random height between 500 and 2000
		platforms.push(newPlatform);
		game.addChild(newPlatform);
	}
};
// Play background music
LK.playMusic('bgmusic', {
	loop: true
}); /**** 
* Classes
****/ 
//<Assets used in the game will automatically appear here>
// Class for the main character (Jumping Jack)
var JumpingJack = Container.expand(function () {
	var self = Container.call(this);
	var jackGraphics = self.attachAsset('jack', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedY = 0;
	self.gravity = 1.0;
	self.jumpStrength = -30;
	self.isJumping = false;
	self.isInvincible = false;
	self.invincibilityHitsLeft = 0;
	self.invincibilityDuration = 5000; // Duration of invincibility in milliseconds
	self.isScoreMultiplier = false;
	self.invincibilityStartTime = 0; // Track when invincibility starts
	self.obstaclesPassThrough = false; // Track if obstacles should pass through player
	self.update = function () {
		if (self.isJumping) {
			self.speedY += self.gravity * 1.5; // Increase fall speed
			self.y += self.speedY;
			if (self.y >= 2000) {
				// Ground level
				self.y = 2000;
				self.isJumping = false;
				self.speedY = 0;
			} else {
				// Check for platform collisions
				var onPlatform = false;
				for (var i = platforms.length - 1; i >= 0; i--) {
					if (platforms[i] && self.intersects(platforms[i])) {
						if (self.speedY > 0) {
							// Only land on platform if falling
							self.y = platforms[i].y - self.height / 2;
							self.isJumping = false;
							self.speedY = 0;
							onPlatform = true;
						} else if (self.speedY === 0) {
							// Temporarily suspend player on platform
							self.y = platforms[i].y - self.height / 2;
							onPlatform = true;
						}
					}
				}
				if (!onPlatform) {
					self.isJumping = true;
					self.speedY += self.gravity * 1.5; // Ensure player falls if not touching platform
				} else {
					self.isJumping = false; // Ensure player stops falling if touching platform
					self.speedY = 0;
					LK.setTimeout(function () {
						self.isJumping = true;
					}, 1000); // Delay before falling off platform
				}
			}
		}
	};
	self.jump = function () {
		if (!self.isJumping) {
			self.isJumping = true;
			self.speedY = self.jumpStrength * 2.0; // Increase jump speed more drastically
		}
	};
	return self;
});
// Class for obstacles
var Obstacle = Container.expand(function () {
	var self = Container.call(this);
	var obstacleGraphics = self.attachAsset('obstacle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speedX = -5;
	self.update = function () {
		self.x += self.speedX;
		self.y = 2000; // Ensure obstacles stay at ground level
		self.y = 2000; // Ensure obstacles stay at ground level
		self.y = 2000; // Ensure obstacles stay at ground level
		// Ensure obstacles stay on the ground level
		if (self.x < -100) {
			// Off-screen
			self.destroy();
		}
	};
	return self;
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('platform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		self.x -= 5; // Move platform from right to left
		if (self.x < -self.width) {
			self.destroy(); // Destroy platform if it goes off-screen
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB // Sky blue background
});
/**** 
* Game Code
****/ 
var score = 0;
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var jack = game.addChild(new JumpingJack());
jack.jumpStrength = -30; // Initialize player jump height
jack.speedY = 0; // Initialize player jump speed
jack.gravity = 1.0; // Initialize player fall speed
jack.x = 300;
jack.y = 2000;
var obstacles = [];
var platforms = [];
var platformInterval = Math.floor(Math.random() * 200) + 100; // Random interval between 100 and 300 for platform generation
var obstacleInterval = Math.floor(Math.random() * 150) + 100; // Random interval between 100 and 250 for obstacle generation
game.down = function (x, y, obj) {
	jack.jump();
};
game.update = function () {
	// Update score
	if (jack.isInvincible && Date.now() - jack.invincibilityStartTime > jack.invincibilityDuration) {
		jack.isInvincible = false;
		jack.obstaclesPassThrough = false; // Disable obstacles passing through player
	}
	if (jack.isScoreMultiplier) {
		score += 3; // Triple score
	} else {
		score += 1;
	}
	// Increase jump height, jump speed, and fall speed at every 100 score milestone
	if (score % 100 === 0 && score !== 0) {
		jack.jumpStrength -= 5; // Increase jump height
		jack.speedY -= 1; // Increase jump speed
		jack.gravity += 0.1; // Increase fall speed
	}
	scoreTxt.setText(score);
	// Update obstacles
	for (var i = obstacles.length - 1; i >= 0; i--) {
		if (obstacles[i]) {
			obstacles[i].update();
		}
		if (obstacles[i]) {
			obstacles[i].x += obstacles[i].speedX;
		}
		// Movement handled in obstacle class
		if (obstacles[i] && obstacles[i].intersects(jack)) {
			if (jack.isInvincible || jack.obstaclesPassThrough) {
				jack.invincibilityHitsLeft--;
				if (jack.invincibilityHitsLeft <= 0) {
					jack.isInvincible = false;
					jack.obstaclesPassThrough = false; // Disable obstacles passing through player
				}
			} else if (!jack.obstaclesPassThrough) {
				LK.effects.flashScreen(0xff0000, 1000);
				// Reset player position to start
				jack.x = 300;
				jack.y = 2000;
				jack.isJumping = false;
				jack.speedY = 0;
				// Reset obstacle positions
				for (var j = obstacles.length - 1; j >= 0; j--) {
					obstacles[j].x = 2048;
					obstacles[j].y = 2000; // Ground level
				}
				// Update platforms
				for (var i = platforms.length - 1; i >= 0; i--) {
					if (platforms[i]) {
						platforms[i].update();
						platforms[i].x -= 5; // Move platform from right to left
						if (platforms[i].x < -platforms[i].width) {
							platforms[i].destroy();
							platforms.splice(i, 1);
						}
					}
					if (platforms[i] && platforms[i].intersects(jack)) {
						if (jack.speedY > 0) {
							// Only land on platform if falling
							jack.y = platforms[i].y - jack.height / 2;
							jack.isJumping = false;
							jack.speedY = 0;
						}
					} else {
						jack.isJumping = true;
						jack.speedY += jack.gravity * 1.5; // Ensure player falls if not touching platform
					}
					if (platforms[i] && platforms[i].x < -100) {
						platforms[i].destroy();
						platforms.splice(i, 1);
					}
				}
				// Reset platform positions
				for (var k = platforms.length - 1; k >= 0; k--) {
					platforms[k].destroy();
					platforms.splice(k, 1);
				}
				// Reset score
				score = 0;
				scoreTxt.setText(score);
				// Reset jump height, jump speed, and fall speed
				jack.jumpStrength = -30;
				jack.speedY = 0;
				jack.gravity = 1.0;
				// Reset obstacle generation interval to a random value
				obstacleInterval = Math.floor(Math.random() * 100) + 50;
				// Reset fall speed
				jack.gravity = 1.0;
				// Reset obstacles
				for (var j = obstacles.length - 1; j >= 0; j--) {
					obstacles[j].destroy();
					obstacles.splice(j, 1);
				}
			}
		}
		if (obstacles[i] && obstacles[i].x < -100) {
			obstacles[i].destroy();
			obstacles.splice(i, 1);
		}
	}
	// Generate new obstacles
	if (LK.ticks % obstacleInterval == 0) {
		obstacleInterval = Math.floor(Math.random() * 150) + 100; // Set next interval to a new random value
		var newObstacle = new Obstacle();
		newObstacle.x = 2048;
		newObstacle.y = 2000; // Ground level
		newObstacle.y = 2000; // Ground level
		obstacles.push(newObstacle);
		game.addChild(newObstacle);
	}
	// Generate new platforms
	if (LK.ticks % platformInterval == 0) {
		platformInterval = Math.floor(Math.random() * 200) + 100; // Set next interval to a new random value
		var newPlatform = new Platform();
		newPlatform.x = 2048;
		newPlatform.y = Math.floor(Math.random() * 1500) + 500; // Random height between 500 and 2000
		platforms.push(newPlatform);
		game.addChild(newPlatform);
	}
};
// Play background music
LK.playMusic('bgmusic', {
	loop: true
});