/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highestNumber: 0,
	highScore: 0,
	invincibilityHighScore: 0
});
/**** 
* Classes
****/ 
var GenerateButton = Container.expand(function () {
	var self = Container.call(this);
	// Create button background
	var buttonBox = self.attachAsset('generateButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create button text
	self.buttonText = new Text2("TAP TO GENERATE", {
		size: 70,
		fill: 0xFFFFFF
	});
	self.buttonText.anchor.set(0.5, 0.5);
	self.addChild(self.buttonText);
	// Interactive properties
	self.interactive = true;
	// Handle button press
	self.down = function () {
		buttonBox.alpha = 0.7;
		tween(buttonBox, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100,
			easing: tween.easeOut
		});
	};
	// Handle button release
	self.up = function () {
		buttonBox.alpha = 1.0;
		tween(buttonBox, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100,
			easing: tween.easeOut
		});
	};
	return self;
});
var InvincibilityButton = Container.expand(function () {
	var self = Container.call(this);
	// Create button background
	var buttonBox = self.attachAsset('invincibilityButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create button text
	self.buttonText = new Text2("INVINCIBILITY: OFF", {
		size: 40,
		fill: 0xFFFFFF
	});
	self.buttonText.anchor.set(0.5, 0.5);
	self.addChild(self.buttonText);
	// Interactive properties
	self.interactive = true;
	// Handle button press
	self.down = function () {
		buttonBox.alpha = 0.7;
		tween(buttonBox, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100,
			easing: tween.easeOut
		});
	};
	// Handle button release
	self.up = function () {
		buttonBox.alpha = 1.0;
		tween(buttonBox, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100,
			easing: tween.easeOut
		});
		// Toggle invincibility mode
		game.invincibilityMode = !game.invincibilityMode;
		self.buttonText.setText("INVINCIBILITY: " + (game.invincibilityMode ? "ON" : "OFF"));
		game.setBackgroundColor(game.invincibilityMode ? 0x006400 : 0x2C3E50);
		// Reset number progress
		initGame();
	};
	return self;
});
var NumberDisplay = Container.expand(function () {
	var self = Container.call(this);
	// Create the background box
	var box = self.attachAsset('numberBox', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create text display
	self.valueText = new Text2("0", {
		size: 120,
		fill: 0x333333
	});
	self.valueText.anchor.set(0.5, 0.5);
	self.addChild(self.valueText);
	// Create probability display
	self.probabilityText = new Text2("100%", {
		size: 40,
		fill: 0x666666
	});
	self.probabilityText.anchor.set(0.5, 0.5);
	self.probabilityText.y = 80;
	self.addChild(self.probabilityText);
	// Set the current value and update display
	self.currentValue = 0;
	// Method to update the display
	self.setValue = function (value) {
		self.currentValue = value;
		self.valueText.setText(value.toString());
		// Calculate and display probability of next number
		var nextProbability = Math.pow(0.99, value) * 100;
		var probabilityDisplay = nextProbability < 0.01 ? "<0.01%" : nextProbability.toFixed(2) + "%";
		self.probabilityText.setText(probabilityDisplay);
		// Flash effect for new number
		LK.effects.flashObject(box, 0x00FF00, 300);
	};
	return self;
});
var ScoreDisplay = Container.expand(function () {
	var self = Container.call(this);
	// Create score text
	self.scoreText = new Text2("Score: 0", {
		size: 60,
		fill: 0xFFFFFF
	});
	self.scoreText.anchor.set(0, 0);
	self.addChild(self.scoreText);
	// Create high score text
	self.highScoreText = new Text2("High Score: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.highScoreText.anchor.set(0, 0);
	self.highScoreText.y = 70;
	self.addChild(self.highScoreText);
	// Create highest number text
	self.highestNumberText = new Text2("Highest Number: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.highestNumberText.anchor.set(0, 0);
	self.highestNumberText.y = 120;
	self.addChild(self.highestNumberText);
	// Create invincibility high score text
	self.invincibilityHighScoreText = new Text2("Invincibility High Score: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.invincibilityHighScoreText.anchor.set(0, 0);
	self.invincibilityHighScoreText.y = 170;
	self.addChild(self.invincibilityHighScoreText);
	// Update methods
	self.updateScore = function (score) {
		self.scoreText.setText("Score: " + score);
	};
	self.updateHighScore = function (highScore) {
		self.highScoreText.setText("High Score: " + highScore);
	};
	self.updateHighestNumber = function (highestNumber) {
		self.highestNumberText.setText("Highest Number: " + highestNumber);
	};
	// Create invincibility highest number text
	self.invincibilityHighestNumberText = new Text2("Invincibility Highest Number: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.invincibilityHighestNumberText.anchor.set(0, 0);
	self.invincibilityHighestNumberText.y = 220;
	self.addChild(self.invincibilityHighestNumberText);
	// Update method for invincibility highest number
	self.updateInvincibilityHighestNumber = function (invincibilityHighestNumber) {
		self.invincibilityHighestNumberText.setText("Invincibility Highest Number: " + invincibilityHighestNumber);
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2C3E50
});
/**** 
* Game Code
****/ 
// Game state variables
var currentNumber = 0;
var currentScore = 0;
var gameActive = true;
var highestReached = 0;
// Create main number display
var numberDisplay = new NumberDisplay();
numberDisplay.x = 2048 / 2;
numberDisplay.y = 2732 / 2 - 300;
game.addChild(numberDisplay);
// Create generate button
var generateButton = new GenerateButton();
generateButton.x = 2048 / 2;
generateButton.y = 2732 / 2 + 300;
game.addChild(generateButton);
// Create score display
var scoreDisplay = new ScoreDisplay();
scoreDisplay.x = 150;
scoreDisplay.y = 150;
game.addChild(scoreDisplay);
// Initialize invincibility mode
game.invincibilityMode = false;
// Create invincibility button
var invincibilityButton = new InvincibilityButton();
invincibilityButton.x = 2048 / 2;
invincibilityButton.y = 2732 / 2 + 500;
game.addChild(invincibilityButton);
// Initialize stored data
scoreDisplay.updateHighScore(storage.highScore || 0);
scoreDisplay.updateHighestNumber(storage.highestNumber || 0);
// Function to try generating the next number
function tryGenerateNext() {
	if (!gameActive) {
		return;
	}
	// Play sound
	LK.getSound('generate').play();
	// Calculate probability for next number (0.99^currentNumber)
	var probability = Math.pow(0.99, currentNumber);
	// Generate random number and check if we succeed
	if (Math.random() < probability) {
		// Success - increment number
		currentNumber++;
		// Update highest reached if needed
		if (currentNumber > highestReached) {
			highestReached = currentNumber;
			// Play success sound
			LK.getSound('success').play();
			// Add points to score (higher numbers give more points)
			var pointsEarned = Math.pow(2, currentNumber);
			currentScore += pointsEarned;
			// Show points earned with animation
			var pointsText = new Text2("+" + pointsEarned, {
				size: 80,
				fill: 0xFFFF00
			});
			pointsText.anchor.set(0.5, 0.5);
			pointsText.x = numberDisplay.x;
			pointsText.y = numberDisplay.y - 300;
			game.addChild(pointsText);
			// Animate points text
			tween(pointsText, {
				y: pointsText.y - 200,
				alpha: 0
			}, {
				duration: 1000,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					pointsText.destroy();
				}
			});
			// Update displays
			scoreDisplay.updateScore(currentScore);
			LK.setScore(currentScore);
			// Check if new high score or highest number
			if (currentNumber > (storage.highestNumber || 0)) {
				storage.highestNumber = currentNumber;
				scoreDisplay.updateHighestNumber(currentNumber);
			}
			if (game.invincibilityMode) {
				if (currentScore > (storage.invincibilityHighScore || 0)) {
					storage.invincibilityHighScore = currentScore;
					scoreDisplay.invincibilityHighScoreText.setText("Invincibility High Score: " + currentScore);
				}
				if (currentNumber > (storage.invincibilityHighestNumber || 0)) {
					storage.invincibilityHighestNumber = currentNumber;
					scoreDisplay.updateInvincibilityHighestNumber(currentNumber);
				}
			} else {
				if (currentScore > (storage.highScore || 0)) {
					storage.highScore = currentScore;
					scoreDisplay.updateHighScore(currentScore);
				}
			}
		}
		// Update number display
		numberDisplay.setValue(currentNumber);
	} else {
		if (!game.invincibilityMode) {
			// Failed - game over
			gameActive = false;
			// Play game over sound
			LK.getSound('gameover').play();
			// Flash effect for game over
			LK.effects.flashScreen(0xFF0000, 500);
			// Show game over after a delay
			LK.setTimeout(function () {
				LK.showGameOver();
			}, 800);
		} else {
			// In invincibility mode, do not flash the screen
		}
	}
}
// Handle button press to generate number
generateButton.down = function () {
	// Original button press animation
	var buttonBox = generateButton.getChildAt(0);
	buttonBox.alpha = 0.7;
	tween(buttonBox, {
		scaleX: 0.95,
		scaleY: 0.95
	}, {
		duration: 100,
		easing: tween.easeOut
	});
};
generateButton.up = function () {
	// Original button release animation
	var buttonBox = generateButton.getChildAt(0);
	buttonBox.alpha = 1.0;
	tween(buttonBox, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 100,
		easing: tween.easeOut
	});
	// Try generating next number
	tryGenerateNext();
};
// Game update function
game.update = function () {
	// Game logic that runs every frame
	// (Currently empty as our game is event-driven)
};
// Initialize the game
function initGame() {
	// Reset game state
	currentNumber = 0;
	currentScore = 0;
	gameActive = true;
	highestReached = 0;
	// Reset displays
	numberDisplay.setValue(0);
	scoreDisplay.updateScore(0);
	LK.setScore(0);
	// Play background music
	LK.playMusic('bgMusic');
}
// Call init on game start
initGame(); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1", {
	highestNumber: 0,
	highScore: 0,
	invincibilityHighScore: 0
});
/**** 
* Classes
****/ 
var GenerateButton = Container.expand(function () {
	var self = Container.call(this);
	// Create button background
	var buttonBox = self.attachAsset('generateButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create button text
	self.buttonText = new Text2("TAP TO GENERATE", {
		size: 70,
		fill: 0xFFFFFF
	});
	self.buttonText.anchor.set(0.5, 0.5);
	self.addChild(self.buttonText);
	// Interactive properties
	self.interactive = true;
	// Handle button press
	self.down = function () {
		buttonBox.alpha = 0.7;
		tween(buttonBox, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100,
			easing: tween.easeOut
		});
	};
	// Handle button release
	self.up = function () {
		buttonBox.alpha = 1.0;
		tween(buttonBox, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100,
			easing: tween.easeOut
		});
	};
	return self;
});
var InvincibilityButton = Container.expand(function () {
	var self = Container.call(this);
	// Create button background
	var buttonBox = self.attachAsset('invincibilityButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create button text
	self.buttonText = new Text2("INVINCIBILITY: OFF", {
		size: 40,
		fill: 0xFFFFFF
	});
	self.buttonText.anchor.set(0.5, 0.5);
	self.addChild(self.buttonText);
	// Interactive properties
	self.interactive = true;
	// Handle button press
	self.down = function () {
		buttonBox.alpha = 0.7;
		tween(buttonBox, {
			scaleX: 0.95,
			scaleY: 0.95
		}, {
			duration: 100,
			easing: tween.easeOut
		});
	};
	// Handle button release
	self.up = function () {
		buttonBox.alpha = 1.0;
		tween(buttonBox, {
			scaleX: 1,
			scaleY: 1
		}, {
			duration: 100,
			easing: tween.easeOut
		});
		// Toggle invincibility mode
		game.invincibilityMode = !game.invincibilityMode;
		self.buttonText.setText("INVINCIBILITY: " + (game.invincibilityMode ? "ON" : "OFF"));
		game.setBackgroundColor(game.invincibilityMode ? 0x006400 : 0x2C3E50);
		// Reset number progress
		initGame();
	};
	return self;
});
var NumberDisplay = Container.expand(function () {
	var self = Container.call(this);
	// Create the background box
	var box = self.attachAsset('numberBox', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Create text display
	self.valueText = new Text2("0", {
		size: 120,
		fill: 0x333333
	});
	self.valueText.anchor.set(0.5, 0.5);
	self.addChild(self.valueText);
	// Create probability display
	self.probabilityText = new Text2("100%", {
		size: 40,
		fill: 0x666666
	});
	self.probabilityText.anchor.set(0.5, 0.5);
	self.probabilityText.y = 80;
	self.addChild(self.probabilityText);
	// Set the current value and update display
	self.currentValue = 0;
	// Method to update the display
	self.setValue = function (value) {
		self.currentValue = value;
		self.valueText.setText(value.toString());
		// Calculate and display probability of next number
		var nextProbability = Math.pow(0.99, value) * 100;
		var probabilityDisplay = nextProbability < 0.01 ? "<0.01%" : nextProbability.toFixed(2) + "%";
		self.probabilityText.setText(probabilityDisplay);
		// Flash effect for new number
		LK.effects.flashObject(box, 0x00FF00, 300);
	};
	return self;
});
var ScoreDisplay = Container.expand(function () {
	var self = Container.call(this);
	// Create score text
	self.scoreText = new Text2("Score: 0", {
		size: 60,
		fill: 0xFFFFFF
	});
	self.scoreText.anchor.set(0, 0);
	self.addChild(self.scoreText);
	// Create high score text
	self.highScoreText = new Text2("High Score: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.highScoreText.anchor.set(0, 0);
	self.highScoreText.y = 70;
	self.addChild(self.highScoreText);
	// Create highest number text
	self.highestNumberText = new Text2("Highest Number: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.highestNumberText.anchor.set(0, 0);
	self.highestNumberText.y = 120;
	self.addChild(self.highestNumberText);
	// Create invincibility high score text
	self.invincibilityHighScoreText = new Text2("Invincibility High Score: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.invincibilityHighScoreText.anchor.set(0, 0);
	self.invincibilityHighScoreText.y = 170;
	self.addChild(self.invincibilityHighScoreText);
	// Update methods
	self.updateScore = function (score) {
		self.scoreText.setText("Score: " + score);
	};
	self.updateHighScore = function (highScore) {
		self.highScoreText.setText("High Score: " + highScore);
	};
	self.updateHighestNumber = function (highestNumber) {
		self.highestNumberText.setText("Highest Number: " + highestNumber);
	};
	// Create invincibility highest number text
	self.invincibilityHighestNumberText = new Text2("Invincibility Highest Number: 0", {
		size: 40,
		fill: 0xCCCCCC
	});
	self.invincibilityHighestNumberText.anchor.set(0, 0);
	self.invincibilityHighestNumberText.y = 220;
	self.addChild(self.invincibilityHighestNumberText);
	// Update method for invincibility highest number
	self.updateInvincibilityHighestNumber = function (invincibilityHighestNumber) {
		self.invincibilityHighestNumberText.setText("Invincibility Highest Number: " + invincibilityHighestNumber);
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x2C3E50
});
/**** 
* Game Code
****/ 
// Game state variables
var currentNumber = 0;
var currentScore = 0;
var gameActive = true;
var highestReached = 0;
// Create main number display
var numberDisplay = new NumberDisplay();
numberDisplay.x = 2048 / 2;
numberDisplay.y = 2732 / 2 - 300;
game.addChild(numberDisplay);
// Create generate button
var generateButton = new GenerateButton();
generateButton.x = 2048 / 2;
generateButton.y = 2732 / 2 + 300;
game.addChild(generateButton);
// Create score display
var scoreDisplay = new ScoreDisplay();
scoreDisplay.x = 150;
scoreDisplay.y = 150;
game.addChild(scoreDisplay);
// Initialize invincibility mode
game.invincibilityMode = false;
// Create invincibility button
var invincibilityButton = new InvincibilityButton();
invincibilityButton.x = 2048 / 2;
invincibilityButton.y = 2732 / 2 + 500;
game.addChild(invincibilityButton);
// Initialize stored data
scoreDisplay.updateHighScore(storage.highScore || 0);
scoreDisplay.updateHighestNumber(storage.highestNumber || 0);
// Function to try generating the next number
function tryGenerateNext() {
	if (!gameActive) {
		return;
	}
	// Play sound
	LK.getSound('generate').play();
	// Calculate probability for next number (0.99^currentNumber)
	var probability = Math.pow(0.99, currentNumber);
	// Generate random number and check if we succeed
	if (Math.random() < probability) {
		// Success - increment number
		currentNumber++;
		// Update highest reached if needed
		if (currentNumber > highestReached) {
			highestReached = currentNumber;
			// Play success sound
			LK.getSound('success').play();
			// Add points to score (higher numbers give more points)
			var pointsEarned = Math.pow(2, currentNumber);
			currentScore += pointsEarned;
			// Show points earned with animation
			var pointsText = new Text2("+" + pointsEarned, {
				size: 80,
				fill: 0xFFFF00
			});
			pointsText.anchor.set(0.5, 0.5);
			pointsText.x = numberDisplay.x;
			pointsText.y = numberDisplay.y - 300;
			game.addChild(pointsText);
			// Animate points text
			tween(pointsText, {
				y: pointsText.y - 200,
				alpha: 0
			}, {
				duration: 1000,
				easing: tween.easeOut,
				onFinish: function onFinish() {
					pointsText.destroy();
				}
			});
			// Update displays
			scoreDisplay.updateScore(currentScore);
			LK.setScore(currentScore);
			// Check if new high score or highest number
			if (currentNumber > (storage.highestNumber || 0)) {
				storage.highestNumber = currentNumber;
				scoreDisplay.updateHighestNumber(currentNumber);
			}
			if (game.invincibilityMode) {
				if (currentScore > (storage.invincibilityHighScore || 0)) {
					storage.invincibilityHighScore = currentScore;
					scoreDisplay.invincibilityHighScoreText.setText("Invincibility High Score: " + currentScore);
				}
				if (currentNumber > (storage.invincibilityHighestNumber || 0)) {
					storage.invincibilityHighestNumber = currentNumber;
					scoreDisplay.updateInvincibilityHighestNumber(currentNumber);
				}
			} else {
				if (currentScore > (storage.highScore || 0)) {
					storage.highScore = currentScore;
					scoreDisplay.updateHighScore(currentScore);
				}
			}
		}
		// Update number display
		numberDisplay.setValue(currentNumber);
	} else {
		if (!game.invincibilityMode) {
			// Failed - game over
			gameActive = false;
			// Play game over sound
			LK.getSound('gameover').play();
			// Flash effect for game over
			LK.effects.flashScreen(0xFF0000, 500);
			// Show game over after a delay
			LK.setTimeout(function () {
				LK.showGameOver();
			}, 800);
		} else {
			// In invincibility mode, do not flash the screen
		}
	}
}
// Handle button press to generate number
generateButton.down = function () {
	// Original button press animation
	var buttonBox = generateButton.getChildAt(0);
	buttonBox.alpha = 0.7;
	tween(buttonBox, {
		scaleX: 0.95,
		scaleY: 0.95
	}, {
		duration: 100,
		easing: tween.easeOut
	});
};
generateButton.up = function () {
	// Original button release animation
	var buttonBox = generateButton.getChildAt(0);
	buttonBox.alpha = 1.0;
	tween(buttonBox, {
		scaleX: 1,
		scaleY: 1
	}, {
		duration: 100,
		easing: tween.easeOut
	});
	// Try generating next number
	tryGenerateNext();
};
// Game update function
game.update = function () {
	// Game logic that runs every frame
	// (Currently empty as our game is event-driven)
};
// Initialize the game
function initGame() {
	// Reset game state
	currentNumber = 0;
	currentScore = 0;
	gameActive = true;
	highestReached = 0;
	// Reset displays
	numberDisplay.setValue(0);
	scoreDisplay.updateScore(0);
	LK.setScore(0);
	// Play background music
	LK.playMusic('bgMusic');
}
// Call init on game start
initGame();