/**** 
* Classes
****/ 
var Character = Container.expand(function () {
	var self = Container.call(this);
	var characterGraphics = self.attachAsset('character', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocityY = 0;
	self.gravity = 0.8;
	self.jumpPower = -25.5;
	self.horizontalSpeed = 0;
	self.maxHorizontalSpeed = 12;
	self.onGround = false;
	self.jump = function () {
		self.velocityY = self.jumpPower;
		self.onGround = false;
	};
	self.moveLeft = function () {
		self.horizontalSpeed = Math.max(self.horizontalSpeed - 2, -self.maxHorizontalSpeed);
	};
	self.moveRight = function () {
		self.horizontalSpeed = Math.min(self.horizontalSpeed + 2, self.maxHorizontalSpeed);
	};
	self.update = function () {
		// Only apply gravity if not on ground
		if (!self.onGround) {
			self.velocityY += self.gravity;
		}
		// Update position
		self.y += self.velocityY;
		self.x += self.horizontalSpeed;
		// Apply horizontal friction
		self.horizontalSpeed *= 0.85;
		// Screen wrapping
		if (self.x < 0) {
			self.x = 2048;
		} else if (self.x > 2048) {
			self.x = 0;
		}
	};
	return self;
});
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.hasBeenCollected = false;
	// Add rotation animation
	self.update = function () {
		coinGraphics.rotation += 0.1;
	};
	return self;
});
var ControlButton = Container.expand(function (direction) {
	var self = Container.call(this);
	var buttonAsset = direction === 'left' ? 'leftButton' : 'rightButton';
	var buttonGraphics = self.attachAsset(buttonAsset, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.direction = direction;
	self.alpha = 0.7;
	self.down = function (x, y, obj) {
		self.alpha = 1.0;
		if (self.direction === 'left') {
			isLeftPressed = true;
		} else {
			isRightPressed = true;
		}
	};
	self.up = function (x, y, obj) {
		self.alpha = 0.7;
		if (self.direction === 'left') {
			isLeftPressed = false;
		} else {
			isRightPressed = false;
		}
	};
	return self;
});
var FragilePlatform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('fragilePlatform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.hasBeenLandedOn = false;
	self.isBroken = false;
	self.breakPlatform = function () {
		self.isBroken = true;
		platformGraphics.alpha = 0.3;
		// Platform will be removed after a short delay
		LK.setTimeout(function () {
			if (self.parent) {
				self.destroy();
			}
		}, 200);
	};
	return self;
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('platform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.hasBeenLandedOn = false;
	return self;
});
var SpawnPlatform = Container.expand(function () {
	var self = Container.call(this);
	var spawnPlatformGraphics = self.attachAsset('spawnPlatform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var character;
var platforms = [];
var coins = [];
var spawnPlatform;
var leftButton;
var rightButton;
var cameraOffsetY = 0;
var isLeftPressed = false;
var isRightPressed = false;
var gameSpeed = 1;
// Create score display
var scoreTxt = new Text2('0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create spawn platform
spawnPlatform = game.addChild(new SpawnPlatform());
spawnPlatform.x = 1024;
spawnPlatform.y = 2500;
// Create character
character = game.addChild(new Character());
character.x = 1024;
character.y = 2400;
// Generate initial platforms
function generatePlatform(y) {
	var platform;
	// 30% chance to create fragile platform
	if (Math.random() < 0.3) {
		platform = new FragilePlatform();
	} else {
		platform = new Platform();
	}
	platform.x = Math.random() * 1500 + 124; // Keep platforms within screen bounds
	platform.y = y;
	platforms.push(platform);
	game.addChild(platform);
}
function generateCoin(y) {
	var coin = new Coin();
	coin.x = Math.random() * 1500 + 124; // Keep coins within screen bounds
	coin.y = y - 100; // Position coins above platforms
	coins.push(coin);
	game.addChild(coin);
}
// Generate initial platforms with closer spacing
for (var i = 0; i < 20; i++) {
	generatePlatform(2200 - i * 80); // Reduced to 80 for much closer spacing
	// 15% chance to generate a coin near this platform
	if (Math.random() < 0.15) {
		generateCoin(2200 - i * 80);
	}
}
// Create control buttons
leftButton = new ControlButton('left');
leftButton.x = 200;
leftButton.y = 2600;
LK.gui.bottomLeft.addChild(leftButton);
rightButton = new ControlButton('right');
rightButton.x = 200;
rightButton.y = 2600;
LK.gui.bottomRight.addChild(rightButton);
// Position buttons correctly
leftButton.x = 150;
leftButton.y = -100;
rightButton.x = -150;
rightButton.y = -100;
function checkPlatformCollisions() {
	var wasOnGround = character.onGround;
	character.onGround = false;
	// Check collision with spawn platform first
	if (character.velocityY >= 0 && character.y + 40 >= spawnPlatform.y - 15 && character.y - 40 <= spawnPlatform.y + 15 && character.x >= spawnPlatform.x - 150 && character.x <= spawnPlatform.x + 150) {
		character.y = spawnPlatform.y - 40;
		character.velocityY = 0;
		character.onGround = true;
		// Only jump if we just landed (not if we were already on ground)
		if (!wasOnGround) {
			character.jump();
		}
		return;
	}
	// Check regular platforms
	for (var i = platforms.length - 1; i >= 0; i--) {
		var platform = platforms[i];
		// Skip broken fragile platforms
		if (platform.isBroken) {
			continue;
		}
		if (character.velocityY >= 0 && character.y + 40 >= platform.y - 10 && character.y - 40 <= platform.y + 10 && character.x >= platform.x - 100 && character.x <= platform.x + 100) {
			character.y = platform.y - 40;
			character.velocityY = 0;
			character.onGround = true;
			// Only jump and score if we just landed (not if we were already on ground)
			if (!wasOnGround) {
				character.jump();
				if (!platform.hasBeenLandedOn) {
					platform.hasBeenLandedOn = true;
					LK.setScore(LK.getScore() + 1);
					scoreTxt.setText(LK.getScore());
					// Increase game speed gradually
					gameSpeed = 1 + LK.getScore() * 0.02;
					// Break fragile platforms after landing
					if (platform.breakPlatform) {
						platform.breakPlatform();
						// Remove from platforms array
						platforms.splice(i, 1);
					}
				}
			}
			break;
		}
	}
}
function updateCamera() {
	var targetY = character.y - 1366; // Keep character in middle-upper area
	var newCameraOffsetY = targetY;
	var cameraDiff = newCameraOffsetY - cameraOffsetY;
	cameraOffsetY = newCameraOffsetY;
	// Apply camera offset to all game objects
	game.y -= cameraDiff;
}
function checkCoinCollisions() {
	for (var i = coins.length - 1; i >= 0; i--) {
		var coin = coins[i];
		if (!coin.hasBeenCollected && character.x >= coin.x - 50 && character.x <= coin.x + 50 && character.y >= coin.y - 50 && character.y <= coin.y + 50) {
			coin.hasBeenCollected = true;
			LK.setScore(LK.getScore() + 3);
			scoreTxt.setText(LK.getScore());
			// Remove coin
			coin.destroy();
			coins.splice(i, 1);
		}
	}
}
function managePlatforms() {
	// Remove platforms that are too far below
	for (var i = platforms.length - 1; i >= 0; i--) {
		if (platforms[i].y > character.y + 1500) {
			platforms[i].destroy();
			platforms.splice(i, 1);
		}
	}
	// Remove coins that are too far below
	for (var i = coins.length - 1; i >= 0; i--) {
		if (coins[i].y > character.y + 1500) {
			coins[i].destroy();
			coins.splice(i, 1);
		}
	}
	// Add new platforms above with better spacing
	var highestPlatform = Math.min.apply(Math, platforms.map(function (p) {
		return p.y;
	}));
	while (highestPlatform > character.y - 1500) {
		highestPlatform -= 60 + Math.random() * 20; // Much closer spacing: 60-80 pixels
		generatePlatform(highestPlatform);
		// 15% chance to generate a coin near new platform
		if (Math.random() < 0.15) {
			generateCoin(highestPlatform);
		}
	}
}
game.update = function () {
	// Handle input
	if (isLeftPressed) {
		character.moveLeft();
	}
	if (isRightPressed) {
		character.moveRight();
	}
	// Check platform collisions
	checkPlatformCollisions();
	// Check coin collisions
	checkCoinCollisions();
	// Update camera
	updateCamera();
	// Manage platforms
	managePlatforms();
	// Check game over - character falls below spawn platform
	if (character.y > spawnPlatform.y + 500) {
		LK.showGameOver();
	}
};
// Handle global touch controls for mobile
game.down = function (x, y, obj) {
	if (x < 1024) {
		isLeftPressed = true;
		leftButton.alpha = 1.0;
	} else {
		isRightPressed = true;
		rightButton.alpha = 1.0;
	}
};
game.up = function (x, y, obj) {
	isLeftPressed = false;
	isRightPressed = false;
	leftButton.alpha = 0.7;
	rightButton.alpha = 0.7;
}; /**** 
* Classes
****/ 
var Character = Container.expand(function () {
	var self = Container.call(this);
	var characterGraphics = self.attachAsset('character', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocityY = 0;
	self.gravity = 0.8;
	self.jumpPower = -25.5;
	self.horizontalSpeed = 0;
	self.maxHorizontalSpeed = 12;
	self.onGround = false;
	self.jump = function () {
		self.velocityY = self.jumpPower;
		self.onGround = false;
	};
	self.moveLeft = function () {
		self.horizontalSpeed = Math.max(self.horizontalSpeed - 2, -self.maxHorizontalSpeed);
	};
	self.moveRight = function () {
		self.horizontalSpeed = Math.min(self.horizontalSpeed + 2, self.maxHorizontalSpeed);
	};
	self.update = function () {
		// Only apply gravity if not on ground
		if (!self.onGround) {
			self.velocityY += self.gravity;
		}
		// Update position
		self.y += self.velocityY;
		self.x += self.horizontalSpeed;
		// Apply horizontal friction
		self.horizontalSpeed *= 0.85;
		// Screen wrapping
		if (self.x < 0) {
			self.x = 2048;
		} else if (self.x > 2048) {
			self.x = 0;
		}
	};
	return self;
});
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.hasBeenCollected = false;
	// Add rotation animation
	self.update = function () {
		coinGraphics.rotation += 0.1;
	};
	return self;
});
var ControlButton = Container.expand(function (direction) {
	var self = Container.call(this);
	var buttonAsset = direction === 'left' ? 'leftButton' : 'rightButton';
	var buttonGraphics = self.attachAsset(buttonAsset, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.direction = direction;
	self.alpha = 0.7;
	self.down = function (x, y, obj) {
		self.alpha = 1.0;
		if (self.direction === 'left') {
			isLeftPressed = true;
		} else {
			isRightPressed = true;
		}
	};
	self.up = function (x, y, obj) {
		self.alpha = 0.7;
		if (self.direction === 'left') {
			isLeftPressed = false;
		} else {
			isRightPressed = false;
		}
	};
	return self;
});
var FragilePlatform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('fragilePlatform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.hasBeenLandedOn = false;
	self.isBroken = false;
	self.breakPlatform = function () {
		self.isBroken = true;
		platformGraphics.alpha = 0.3;
		// Platform will be removed after a short delay
		LK.setTimeout(function () {
			if (self.parent) {
				self.destroy();
			}
		}, 200);
	};
	return self;
});
var Platform = Container.expand(function () {
	var self = Container.call(this);
	var platformGraphics = self.attachAsset('platform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.hasBeenLandedOn = false;
	return self;
});
var SpawnPlatform = Container.expand(function () {
	var self = Container.call(this);
	var spawnPlatformGraphics = self.attachAsset('spawnPlatform', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var character;
var platforms = [];
var coins = [];
var spawnPlatform;
var leftButton;
var rightButton;
var cameraOffsetY = 0;
var isLeftPressed = false;
var isRightPressed = false;
var gameSpeed = 1;
// Create score display
var scoreTxt = new Text2('0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Create spawn platform
spawnPlatform = game.addChild(new SpawnPlatform());
spawnPlatform.x = 1024;
spawnPlatform.y = 2500;
// Create character
character = game.addChild(new Character());
character.x = 1024;
character.y = 2400;
// Generate initial platforms
function generatePlatform(y) {
	var platform;
	// 30% chance to create fragile platform
	if (Math.random() < 0.3) {
		platform = new FragilePlatform();
	} else {
		platform = new Platform();
	}
	platform.x = Math.random() * 1500 + 124; // Keep platforms within screen bounds
	platform.y = y;
	platforms.push(platform);
	game.addChild(platform);
}
function generateCoin(y) {
	var coin = new Coin();
	coin.x = Math.random() * 1500 + 124; // Keep coins within screen bounds
	coin.y = y - 100; // Position coins above platforms
	coins.push(coin);
	game.addChild(coin);
}
// Generate initial platforms with closer spacing
for (var i = 0; i < 20; i++) {
	generatePlatform(2200 - i * 80); // Reduced to 80 for much closer spacing
	// 15% chance to generate a coin near this platform
	if (Math.random() < 0.15) {
		generateCoin(2200 - i * 80);
	}
}
// Create control buttons
leftButton = new ControlButton('left');
leftButton.x = 200;
leftButton.y = 2600;
LK.gui.bottomLeft.addChild(leftButton);
rightButton = new ControlButton('right');
rightButton.x = 200;
rightButton.y = 2600;
LK.gui.bottomRight.addChild(rightButton);
// Position buttons correctly
leftButton.x = 150;
leftButton.y = -100;
rightButton.x = -150;
rightButton.y = -100;
function checkPlatformCollisions() {
	var wasOnGround = character.onGround;
	character.onGround = false;
	// Check collision with spawn platform first
	if (character.velocityY >= 0 && character.y + 40 >= spawnPlatform.y - 15 && character.y - 40 <= spawnPlatform.y + 15 && character.x >= spawnPlatform.x - 150 && character.x <= spawnPlatform.x + 150) {
		character.y = spawnPlatform.y - 40;
		character.velocityY = 0;
		character.onGround = true;
		// Only jump if we just landed (not if we were already on ground)
		if (!wasOnGround) {
			character.jump();
		}
		return;
	}
	// Check regular platforms
	for (var i = platforms.length - 1; i >= 0; i--) {
		var platform = platforms[i];
		// Skip broken fragile platforms
		if (platform.isBroken) {
			continue;
		}
		if (character.velocityY >= 0 && character.y + 40 >= platform.y - 10 && character.y - 40 <= platform.y + 10 && character.x >= platform.x - 100 && character.x <= platform.x + 100) {
			character.y = platform.y - 40;
			character.velocityY = 0;
			character.onGround = true;
			// Only jump and score if we just landed (not if we were already on ground)
			if (!wasOnGround) {
				character.jump();
				if (!platform.hasBeenLandedOn) {
					platform.hasBeenLandedOn = true;
					LK.setScore(LK.getScore() + 1);
					scoreTxt.setText(LK.getScore());
					// Increase game speed gradually
					gameSpeed = 1 + LK.getScore() * 0.02;
					// Break fragile platforms after landing
					if (platform.breakPlatform) {
						platform.breakPlatform();
						// Remove from platforms array
						platforms.splice(i, 1);
					}
				}
			}
			break;
		}
	}
}
function updateCamera() {
	var targetY = character.y - 1366; // Keep character in middle-upper area
	var newCameraOffsetY = targetY;
	var cameraDiff = newCameraOffsetY - cameraOffsetY;
	cameraOffsetY = newCameraOffsetY;
	// Apply camera offset to all game objects
	game.y -= cameraDiff;
}
function checkCoinCollisions() {
	for (var i = coins.length - 1; i >= 0; i--) {
		var coin = coins[i];
		if (!coin.hasBeenCollected && character.x >= coin.x - 50 && character.x <= coin.x + 50 && character.y >= coin.y - 50 && character.y <= coin.y + 50) {
			coin.hasBeenCollected = true;
			LK.setScore(LK.getScore() + 3);
			scoreTxt.setText(LK.getScore());
			// Remove coin
			coin.destroy();
			coins.splice(i, 1);
		}
	}
}
function managePlatforms() {
	// Remove platforms that are too far below
	for (var i = platforms.length - 1; i >= 0; i--) {
		if (platforms[i].y > character.y + 1500) {
			platforms[i].destroy();
			platforms.splice(i, 1);
		}
	}
	// Remove coins that are too far below
	for (var i = coins.length - 1; i >= 0; i--) {
		if (coins[i].y > character.y + 1500) {
			coins[i].destroy();
			coins.splice(i, 1);
		}
	}
	// Add new platforms above with better spacing
	var highestPlatform = Math.min.apply(Math, platforms.map(function (p) {
		return p.y;
	}));
	while (highestPlatform > character.y - 1500) {
		highestPlatform -= 60 + Math.random() * 20; // Much closer spacing: 60-80 pixels
		generatePlatform(highestPlatform);
		// 15% chance to generate a coin near new platform
		if (Math.random() < 0.15) {
			generateCoin(highestPlatform);
		}
	}
}
game.update = function () {
	// Handle input
	if (isLeftPressed) {
		character.moveLeft();
	}
	if (isRightPressed) {
		character.moveRight();
	}
	// Check platform collisions
	checkPlatformCollisions();
	// Check coin collisions
	checkCoinCollisions();
	// Update camera
	updateCamera();
	// Manage platforms
	managePlatforms();
	// Check game over - character falls below spawn platform
	if (character.y > spawnPlatform.y + 500) {
		LK.showGameOver();
	}
};
// Handle global touch controls for mobile
game.down = function (x, y, obj) {
	if (x < 1024) {
		isLeftPressed = true;
		leftButton.alpha = 1.0;
	} else {
		isRightPressed = true;
		rightButton.alpha = 1.0;
	}
};
game.up = function (x, y, obj) {
	isLeftPressed = false;
	isRightPressed = false;
	leftButton.alpha = 0.7;
	rightButton.alpha = 0.7;
};