/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var DoodleCharacter = Container.expand(function () {
	var self = Container.call(this);
	var characterGraphics = self.attachAsset('doodleCharacter', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocityY = 0;
	self.velocityX = 0;
	self.gravity = 0.4;
	self.jumpPower = -22;
	self.isJumping = true;
	self.maxVelocityX = 8;
	self.friction = 0.95;
	self.jump = function () {
		self.velocityY = self.jumpPower;
		self.isJumping = true;
		LK.getSound('jump').play();
	};
	self.update = function () {
		// Apply gravity
		self.velocityY += self.gravity;
		// Apply velocities
		self.y += self.velocityY;
		self.x += self.velocityX;
		// Apply friction to horizontal movement
		self.velocityX *= self.friction;
		// Wrap around screen horizontally
		if (self.x < -40) {
			self.x = 2048 + 40;
		} else if (self.x > 2048 + 40) {
			self.x = -40;
		}
		// Update jumping state
		if (self.velocityY > 0) {
			self.isJumping = true;
		}
	};
	return self;
});
var Platform = Container.expand(function (type) {
	var self = Container.call(this);
	self.platformType = type || 'normal';
	self.used = false;
	self.direction = 1;
	self.speed = 1;
	var assetId = 'platform';
	if (self.platformType === 'breakable') {
		assetId = 'breakablePlatform';
	} else if (self.platformType === 'moving') {
		assetId = 'movingPlatform';
	}
	var platformGraphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		if (self.platformType === 'moving') {
			self.x += self.direction * self.speed;
			if (self.x <= 60 || self.x >= 2048 - 60) {
				self.direction *= -1;
			}
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var doodleCharacter = game.addChild(new DoodleCharacter());
var platforms = [];
var cameraY = 0;
var highestY = 2732;
var platformSpacing = 200;
var lastPlatformY = 2732 - 100;
// Initialize starting platforms
for (var i = 0; i < 15; i++) {
	var platformY = 2732 - i * platformSpacing;
	var platformX = Math.random() * (2048 - 120) + 60;
	var platformType = 'normal';
	if (i > 5) {
		var rand = Math.random();
		if (rand < 0.15) {
			platformType = 'breakable';
		} else if (rand < 0.25) {
			platformType = 'moving';
		}
	}
	var platform = new Platform(platformType);
	platform.x = platformX;
	platform.y = platformY;
	platforms.push(platform);
	game.addChild(platform);
}
// Position character on first platform
doodleCharacter.x = platforms[platforms.length - 1].x;
doodleCharacter.y = platforms[platforms.length - 1].y - 50;
// Score display
var scoreTxt = new Text2('0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Control variables
var isPressingLeft = false;
var isPressingRight = false;
// Touch controls
game.down = function (x, y, obj) {
	if (x < 2048 / 2) {
		isPressingLeft = true;
	} else {
		isPressingRight = true;
	}
};
game.up = function (x, y, obj) {
	isPressingLeft = false;
	isPressingRight = false;
};
game.update = function () {
	// Handle character input
	if (isPressingLeft) {
		doodleCharacter.velocityX -= 0.8;
		if (doodleCharacter.velocityX < -doodleCharacter.maxVelocityX) {
			doodleCharacter.velocityX = -doodleCharacter.maxVelocityX;
		}
	}
	if (isPressingRight) {
		doodleCharacter.velocityX += 0.8;
		if (doodleCharacter.velocityX > doodleCharacter.maxVelocityX) {
			doodleCharacter.velocityX = doodleCharacter.maxVelocityX;
		}
	}
	// Check platform collisions
	for (var i = platforms.length - 1; i >= 0; i--) {
		var platform = platforms[i];
		// Only check collision when character is falling and above platform
		if (doodleCharacter.velocityY > 0 && doodleCharacter.y < platform.y && doodleCharacter.y + 40 > platform.y - 10 && doodleCharacter.x > platform.x - 60 && doodleCharacter.x < platform.x + 60) {
			if (platform.platformType === 'breakable' && !platform.used) {
				platform.used = true;
				tween(platform, {
					alpha: 0,
					scaleY: 0.1
				}, {
					duration: 300,
					onFinish: function onFinish() {
						platform.destroy();
						platforms.splice(platforms.indexOf(platform), 1);
					}
				});
				LK.getSound('break').play();
			}
			doodleCharacter.jump();
			// Add extra upward boost with tween animation
			tween(doodleCharacter, {
				scaleX: 1.2,
				scaleY: 0.8
			}, {
				duration: 100,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					tween(doodleCharacter, {
						scaleX: 1.0,
						scaleY: 1.0
					}, {
						duration: 100,
						easing: tween.bounceOut
					});
				}
			});
			doodleCharacter.velocityY -= 3; // Extra velocity boost
			break;
		}
	}
	// Update camera to follow character
	if (doodleCharacter.y < highestY) {
		highestY = doodleCharacter.y;
		var targetCameraY = highestY - 1500;
		cameraY = targetCameraY;
		// Move all game objects down relative to camera
		game.y = -cameraY;
		// Update score based on height
		var score = Math.floor((2732 - highestY) / 10);
		LK.setScore(score);
		scoreTxt.setText(LK.getScore());
	}
	// Generate new platforms as we go higher
	while (lastPlatformY > highestY - 2000) {
		lastPlatformY -= platformSpacing;
		var platformX = Math.random() * (2048 - 120) + 60;
		var platformType = 'normal';
		var rand = Math.random();
		if (rand < 0.2) {
			platformType = 'breakable';
		} else if (rand < 0.3) {
			platformType = 'moving';
		}
		var platform = new Platform(platformType);
		platform.x = platformX;
		platform.y = lastPlatformY;
		platforms.push(platform);
		game.addChild(platform);
	}
	// Remove platforms that are too far down
	for (var j = platforms.length - 1; j >= 0; j--) {
		if (platforms[j].y > cameraY + 3000) {
			platforms[j].destroy();
			platforms.splice(j, 1);
		}
	}
	// Check game over (character fell below screen)
	if (doodleCharacter.y > cameraY + 2732 + 200) {
		LK.showGameOver();
	}
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
var DoodleCharacter = Container.expand(function () {
	var self = Container.call(this);
	var characterGraphics = self.attachAsset('doodleCharacter', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.velocityY = 0;
	self.velocityX = 0;
	self.gravity = 0.4;
	self.jumpPower = -22;
	self.isJumping = true;
	self.maxVelocityX = 8;
	self.friction = 0.95;
	self.jump = function () {
		self.velocityY = self.jumpPower;
		self.isJumping = true;
		LK.getSound('jump').play();
	};
	self.update = function () {
		// Apply gravity
		self.velocityY += self.gravity;
		// Apply velocities
		self.y += self.velocityY;
		self.x += self.velocityX;
		// Apply friction to horizontal movement
		self.velocityX *= self.friction;
		// Wrap around screen horizontally
		if (self.x < -40) {
			self.x = 2048 + 40;
		} else if (self.x > 2048 + 40) {
			self.x = -40;
		}
		// Update jumping state
		if (self.velocityY > 0) {
			self.isJumping = true;
		}
	};
	return self;
});
var Platform = Container.expand(function (type) {
	var self = Container.call(this);
	self.platformType = type || 'normal';
	self.used = false;
	self.direction = 1;
	self.speed = 1;
	var assetId = 'platform';
	if (self.platformType === 'breakable') {
		assetId = 'breakablePlatform';
	} else if (self.platformType === 'moving') {
		assetId = 'movingPlatform';
	}
	var platformGraphics = self.attachAsset(assetId, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.update = function () {
		if (self.platformType === 'moving') {
			self.x += self.direction * self.speed;
			if (self.x <= 60 || self.x >= 2048 - 60) {
				self.direction *= -1;
			}
		}
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x87CEEB
});
/**** 
* Game Code
****/ 
var doodleCharacter = game.addChild(new DoodleCharacter());
var platforms = [];
var cameraY = 0;
var highestY = 2732;
var platformSpacing = 200;
var lastPlatformY = 2732 - 100;
// Initialize starting platforms
for (var i = 0; i < 15; i++) {
	var platformY = 2732 - i * platformSpacing;
	var platformX = Math.random() * (2048 - 120) + 60;
	var platformType = 'normal';
	if (i > 5) {
		var rand = Math.random();
		if (rand < 0.15) {
			platformType = 'breakable';
		} else if (rand < 0.25) {
			platformType = 'moving';
		}
	}
	var platform = new Platform(platformType);
	platform.x = platformX;
	platform.y = platformY;
	platforms.push(platform);
	game.addChild(platform);
}
// Position character on first platform
doodleCharacter.x = platforms[platforms.length - 1].x;
doodleCharacter.y = platforms[platforms.length - 1].y - 50;
// Score display
var scoreTxt = new Text2('0', {
	size: 60,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
// Control variables
var isPressingLeft = false;
var isPressingRight = false;
// Touch controls
game.down = function (x, y, obj) {
	if (x < 2048 / 2) {
		isPressingLeft = true;
	} else {
		isPressingRight = true;
	}
};
game.up = function (x, y, obj) {
	isPressingLeft = false;
	isPressingRight = false;
};
game.update = function () {
	// Handle character input
	if (isPressingLeft) {
		doodleCharacter.velocityX -= 0.8;
		if (doodleCharacter.velocityX < -doodleCharacter.maxVelocityX) {
			doodleCharacter.velocityX = -doodleCharacter.maxVelocityX;
		}
	}
	if (isPressingRight) {
		doodleCharacter.velocityX += 0.8;
		if (doodleCharacter.velocityX > doodleCharacter.maxVelocityX) {
			doodleCharacter.velocityX = doodleCharacter.maxVelocityX;
		}
	}
	// Check platform collisions
	for (var i = platforms.length - 1; i >= 0; i--) {
		var platform = platforms[i];
		// Only check collision when character is falling and above platform
		if (doodleCharacter.velocityY > 0 && doodleCharacter.y < platform.y && doodleCharacter.y + 40 > platform.y - 10 && doodleCharacter.x > platform.x - 60 && doodleCharacter.x < platform.x + 60) {
			if (platform.platformType === 'breakable' && !platform.used) {
				platform.used = true;
				tween(platform, {
					alpha: 0,
					scaleY: 0.1
				}, {
					duration: 300,
					onFinish: function onFinish() {
						platform.destroy();
						platforms.splice(platforms.indexOf(platform), 1);
					}
				});
				LK.getSound('break').play();
			}
			doodleCharacter.jump();
			// Add extra upward boost with tween animation
			tween(doodleCharacter, {
				scaleX: 1.2,
				scaleY: 0.8
			}, {
				duration: 100,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					tween(doodleCharacter, {
						scaleX: 1.0,
						scaleY: 1.0
					}, {
						duration: 100,
						easing: tween.bounceOut
					});
				}
			});
			doodleCharacter.velocityY -= 3; // Extra velocity boost
			break;
		}
	}
	// Update camera to follow character
	if (doodleCharacter.y < highestY) {
		highestY = doodleCharacter.y;
		var targetCameraY = highestY - 1500;
		cameraY = targetCameraY;
		// Move all game objects down relative to camera
		game.y = -cameraY;
		// Update score based on height
		var score = Math.floor((2732 - highestY) / 10);
		LK.setScore(score);
		scoreTxt.setText(LK.getScore());
	}
	// Generate new platforms as we go higher
	while (lastPlatformY > highestY - 2000) {
		lastPlatformY -= platformSpacing;
		var platformX = Math.random() * (2048 - 120) + 60;
		var platformType = 'normal';
		var rand = Math.random();
		if (rand < 0.2) {
			platformType = 'breakable';
		} else if (rand < 0.3) {
			platformType = 'moving';
		}
		var platform = new Platform(platformType);
		platform.x = platformX;
		platform.y = lastPlatformY;
		platforms.push(platform);
		game.addChild(platform);
	}
	// Remove platforms that are too far down
	for (var j = platforms.length - 1; j >= 0; j--) {
		if (platforms[j].y > cameraY + 3000) {
			platforms[j].destroy();
			platforms.splice(j, 1);
		}
	}
	// Check game over (character fell below screen)
	if (doodleCharacter.y > cameraY + 2732 + 200) {
		LK.showGameOver();
	}
};