/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Pulse animation
	self.pulseAnimation = function () {
		tween(coinGraphics, {
			scaleX: 1.2,
			scaleY: 1.2
		}, {
			duration: 500,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(coinGraphics, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 500,
					easing: tween.easeInOut,
					onFinish: self.pulseAnimation
				});
			}
		});
	};
	self.pulseAnimation();
	self.collect = function () {
		LK.getSound('coinCollect').play();
		// Animation for collection
		tween(self, {
			alpha: 0,
			scaleX: 1.5,
			scaleY: 1.5
		}, {
			duration: 300,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.destroy();
			}
		});
	};
	return self;
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Player properties
	self.direction = "right"; // Initial direction
	self.speed = 5; // Initial speed
	self.alive = true;
	self.update = function () {
		if (!self.alive) {
			return;
		}
		// Move the player based on current direction
		switch (self.direction) {
			case "up":
				self.y -= self.speed;
				break;
			case "down":
				self.y += self.speed;
				break;
			case "left":
				self.x -= self.speed;
				break;
			case "right":
				self.x += self.speed;
				break;
		}
		// Check if player is out of bounds
		if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
			self.die();
		}
	};
	// Change direction by 90 degrees
	self.turn = function () {
		if (!self.alive) {
			return;
		}
		LK.getSound('turn').play();
		// Determine new direction (90-degree turn)
		switch (self.direction) {
			case "up":
			case "down":
				self.direction = Math.random() < 0.5 ? "left" : "right";
				break;
			case "left":
			case "right":
				self.direction = Math.random() < 0.5 ? "up" : "down";
				break;
		}
	};
	self.die = function () {
		if (!self.alive) {
			return;
		}
		self.alive = false;
		LK.getSound('death').play();
		LK.effects.flashObject(self, 0xff0000, 500);
		LK.setTimeout(function () {
			LK.showGameOver();
		}, 1000);
	};
	return self;
});
var Wall = Container.expand(function () {
	var self = Container.call(this);
	var wallGraphics = self.attachAsset('wall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xecf0f1
});
/**** 
* Game Code
****/ 
// Game variables
var MazeGenerator = function MazeGenerator() {
	// Generate a maze configuration for the current level
	this.generateMaze = function (level) {
		var walls = [];
		var coins = [];
		// Base the number of walls and coins on the level
		var numWalls = Math.min(5 + level * 2, 30);
		var numCoins = Math.min(3 + level, 15);
		// Create a safe zone at the start
		var safeZoneRadius = 150;
		// Generate walls avoiding the safe zone
		for (var i = 0; i < numWalls; i++) {
			var x, y;
			var validPosition = false;
			// Try to find a valid position for the wall
			var attempts = 0;
			while (!validPosition && attempts < 50) {
				x = 100 + Math.random() * (2048 - 200);
				y = 100 + Math.random() * (2732 - 200);
				// Check if wall is away from the safe zone
				var distFromStart = Math.sqrt(Math.pow(x - player.x, 2) + Math.pow(y - player.y, 2));
				// Wall must be away from the safe zone
				validPosition = distFromStart > safeZoneRadius;
				// Check it's not too close to other walls
				if (validPosition) {
					for (var j = 0; j < walls.length; j++) {
						var existingWall = walls[j];
						var distBetweenWalls = Math.sqrt(Math.pow(x - existingWall.x, 2) + Math.pow(y - existingWall.y, 2));
						if (distBetweenWalls < 100) {
							validPosition = false;
							break;
						}
					}
				}
				attempts++;
			}
			if (validPosition) {
				var wall = new Wall();
				wall.x = x;
				wall.y = y;
				walls.push(wall);
			}
		}
		// Generate coins avoiding walls and the safe zone
		for (var i = 0; i < numCoins; i++) {
			var x, y;
			var validPosition = false;
			var attempts = 0;
			while (!validPosition && attempts < 50) {
				x = 100 + Math.random() * (2048 - 200);
				y = 100 + Math.random() * (2732 - 200);
				// Check if coin is away from the safe zone
				var distFromStart = Math.sqrt(Math.pow(x - player.x, 2) + Math.pow(y - player.y, 2));
				validPosition = distFromStart > safeZoneRadius;
				// Check it's not too close to walls
				if (validPosition) {
					for (var j = 0; j < walls.length; j++) {
						var wall = walls[j];
						var distFromWall = Math.sqrt(Math.pow(x - wall.x, 2) + Math.pow(y - wall.y, 2));
						if (distFromWall < 75) {
							validPosition = false;
							break;
						}
					}
				}
				// Check it's not too close to other coins
				if (validPosition) {
					for (var j = 0; j < coins.length; j++) {
						var existingCoin = coins[j];
						var distFromCoin = Math.sqrt(Math.pow(x - existingCoin.x, 2) + Math.pow(y - existingCoin.y, 2));
						if (distFromCoin < 100) {
							validPosition = false;
							break;
						}
					}
				}
				attempts++;
			}
			if (validPosition) {
				var coin = new Coin();
				coin.x = x;
				coin.y = y;
				coins.push(coin);
			}
		}
		return {
			walls: walls,
			coins: coins
		};
	};
};
var player;
var walls = [];
var coins = [];
var level = 1;
var mazeGenerator = new MazeGenerator();
var levelUpThreshold = 5; // Coins needed to level up
var coinsCollected = 0;
var speedIncreasePerLevel = 1;
// Setup UI
var scoreTxt = new Text2('0', {
	size: 80,
	fill: 0x2C3E50
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var levelTxt = new Text2('Level 1', {
	size: 60,
	fill: 0x2C3E50
});
levelTxt.anchor.set(0.5, 0);
levelTxt.y = 90;
LK.gui.top.addChild(levelTxt);
// Instructions text
var instructionsTxt = new Text2('Tap to change direction', {
	size: 50,
	fill: 0x7F8C8D
});
instructionsTxt.anchor.set(0.5, 0);
instructionsTxt.y = 170;
LK.gui.top.addChild(instructionsTxt);
// Hide instructions after a few seconds
LK.setTimeout(function () {
	tween(instructionsTxt, {
		alpha: 0
	}, {
		duration: 1000,
		easing: tween.easeOut
	});
}, 5000);
// Initialize player
function initializePlayer() {
	player = new Player();
	player.x = 2048 / 2;
	player.y = 2732 / 2;
	game.addChild(player);
}
// Load level
function loadLevel(level) {
	// Clear previous level elements
	clearLevel();
	// Update level text
	levelTxt.setText('Level ' + level);
	// Increase player speed with each level
	player.speed = 5 + (level - 1) * speedIncreasePerLevel;
	// Generate maze elements
	var maze = mazeGenerator.generateMaze(level);
	// Add walls
	for (var i = 0; i < maze.walls.length; i++) {
		var wall = maze.walls[i];
		game.addChild(wall);
		walls.push(wall);
	}
	// Add coins
	for (var i = 0; i < maze.coins.length; i++) {
		var coin = maze.coins[i];
		game.addChild(coin);
		coins.push(coin);
	}
}
// Clear level elements
function clearLevel() {
	// Remove all walls
	for (var i = 0; i < walls.length; i++) {
		walls[i].destroy();
	}
	walls = [];
	// Remove all coins
	for (var i = 0; i < coins.length; i++) {
		coins[i].destroy();
	}
	coins = [];
}
// Start the game
function startGame() {
	// Initialize player
	initializePlayer();
	// Load first level
	loadLevel(level);
	// Play background music
	LK.playMusic('bgMusic', {
		fade: {
			start: 0,
			end: 0.5,
			duration: 1000
		}
	});
}
// Handle game tap
game.down = function (x, y, obj) {
	// Change player direction on tap
	if (player && player.alive) {
		player.turn();
	}
};
// Main game update loop
game.update = function () {
	if (!player || !player.alive) {
		return;
	}
	// Check for coin collection
	for (var i = coins.length - 1; i >= 0; i--) {
		var coin = coins[i];
		if (player.intersects(coin)) {
			// Collect coin
			coin.collect();
			coins.splice(i, 1);
			// Update score
			coinsCollected++;
			LK.setScore(coinsCollected);
			scoreTxt.setText(coinsCollected);
			// Check for level up
			if (coinsCollected % levelUpThreshold === 0) {
				level++;
				// Flash screen green for level up
				LK.effects.flashScreen(0x2ecc71, 500);
				// Load next level
				loadLevel(level);
			}
		}
	}
	// Check for wall collisions
	for (var i = 0; i < walls.length; i++) {
		var wall = walls[i];
		if (player.intersects(wall)) {
			player.die();
			break;
		}
	}
	// Check if all coins are collected
	if (coins.length === 0) {
		level++;
		// Flash screen
		LK.effects.flashScreen(0x2ecc71, 500);
		// Load next level
		loadLevel(level);
	}
};
// Start the game
startGame(); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var storage = LK.import("@upit/storage.v1");
/**** 
* Classes
****/ 
var Coin = Container.expand(function () {
	var self = Container.call(this);
	var coinGraphics = self.attachAsset('coin', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Pulse animation
	self.pulseAnimation = function () {
		tween(coinGraphics, {
			scaleX: 1.2,
			scaleY: 1.2
		}, {
			duration: 500,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(coinGraphics, {
					scaleX: 1,
					scaleY: 1
				}, {
					duration: 500,
					easing: tween.easeInOut,
					onFinish: self.pulseAnimation
				});
			}
		});
	};
	self.pulseAnimation();
	self.collect = function () {
		LK.getSound('coinCollect').play();
		// Animation for collection
		tween(self, {
			alpha: 0,
			scaleX: 1.5,
			scaleY: 1.5
		}, {
			duration: 300,
			easing: tween.easeOut,
			onFinish: function onFinish() {
				self.destroy();
			}
		});
	};
	return self;
});
var Player = Container.expand(function () {
	var self = Container.call(this);
	var playerGraphics = self.attachAsset('player', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Player properties
	self.direction = "right"; // Initial direction
	self.speed = 5; // Initial speed
	self.alive = true;
	self.update = function () {
		if (!self.alive) {
			return;
		}
		// Move the player based on current direction
		switch (self.direction) {
			case "up":
				self.y -= self.speed;
				break;
			case "down":
				self.y += self.speed;
				break;
			case "left":
				self.x -= self.speed;
				break;
			case "right":
				self.x += self.speed;
				break;
		}
		// Check if player is out of bounds
		if (self.x < 0 || self.x > 2048 || self.y < 0 || self.y > 2732) {
			self.die();
		}
	};
	// Change direction by 90 degrees
	self.turn = function () {
		if (!self.alive) {
			return;
		}
		LK.getSound('turn').play();
		// Determine new direction (90-degree turn)
		switch (self.direction) {
			case "up":
			case "down":
				self.direction = Math.random() < 0.5 ? "left" : "right";
				break;
			case "left":
			case "right":
				self.direction = Math.random() < 0.5 ? "up" : "down";
				break;
		}
	};
	self.die = function () {
		if (!self.alive) {
			return;
		}
		self.alive = false;
		LK.getSound('death').play();
		LK.effects.flashObject(self, 0xff0000, 500);
		LK.setTimeout(function () {
			LK.showGameOver();
		}, 1000);
	};
	return self;
});
var Wall = Container.expand(function () {
	var self = Container.call(this);
	var wallGraphics = self.attachAsset('wall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0xecf0f1
});
/**** 
* Game Code
****/ 
// Game variables
var MazeGenerator = function MazeGenerator() {
	// Generate a maze configuration for the current level
	this.generateMaze = function (level) {
		var walls = [];
		var coins = [];
		// Base the number of walls and coins on the level
		var numWalls = Math.min(5 + level * 2, 30);
		var numCoins = Math.min(3 + level, 15);
		// Create a safe zone at the start
		var safeZoneRadius = 150;
		// Generate walls avoiding the safe zone
		for (var i = 0; i < numWalls; i++) {
			var x, y;
			var validPosition = false;
			// Try to find a valid position for the wall
			var attempts = 0;
			while (!validPosition && attempts < 50) {
				x = 100 + Math.random() * (2048 - 200);
				y = 100 + Math.random() * (2732 - 200);
				// Check if wall is away from the safe zone
				var distFromStart = Math.sqrt(Math.pow(x - player.x, 2) + Math.pow(y - player.y, 2));
				// Wall must be away from the safe zone
				validPosition = distFromStart > safeZoneRadius;
				// Check it's not too close to other walls
				if (validPosition) {
					for (var j = 0; j < walls.length; j++) {
						var existingWall = walls[j];
						var distBetweenWalls = Math.sqrt(Math.pow(x - existingWall.x, 2) + Math.pow(y - existingWall.y, 2));
						if (distBetweenWalls < 100) {
							validPosition = false;
							break;
						}
					}
				}
				attempts++;
			}
			if (validPosition) {
				var wall = new Wall();
				wall.x = x;
				wall.y = y;
				walls.push(wall);
			}
		}
		// Generate coins avoiding walls and the safe zone
		for (var i = 0; i < numCoins; i++) {
			var x, y;
			var validPosition = false;
			var attempts = 0;
			while (!validPosition && attempts < 50) {
				x = 100 + Math.random() * (2048 - 200);
				y = 100 + Math.random() * (2732 - 200);
				// Check if coin is away from the safe zone
				var distFromStart = Math.sqrt(Math.pow(x - player.x, 2) + Math.pow(y - player.y, 2));
				validPosition = distFromStart > safeZoneRadius;
				// Check it's not too close to walls
				if (validPosition) {
					for (var j = 0; j < walls.length; j++) {
						var wall = walls[j];
						var distFromWall = Math.sqrt(Math.pow(x - wall.x, 2) + Math.pow(y - wall.y, 2));
						if (distFromWall < 75) {
							validPosition = false;
							break;
						}
					}
				}
				// Check it's not too close to other coins
				if (validPosition) {
					for (var j = 0; j < coins.length; j++) {
						var existingCoin = coins[j];
						var distFromCoin = Math.sqrt(Math.pow(x - existingCoin.x, 2) + Math.pow(y - existingCoin.y, 2));
						if (distFromCoin < 100) {
							validPosition = false;
							break;
						}
					}
				}
				attempts++;
			}
			if (validPosition) {
				var coin = new Coin();
				coin.x = x;
				coin.y = y;
				coins.push(coin);
			}
		}
		return {
			walls: walls,
			coins: coins
		};
	};
};
var player;
var walls = [];
var coins = [];
var level = 1;
var mazeGenerator = new MazeGenerator();
var levelUpThreshold = 5; // Coins needed to level up
var coinsCollected = 0;
var speedIncreasePerLevel = 1;
// Setup UI
var scoreTxt = new Text2('0', {
	size: 80,
	fill: 0x2C3E50
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var levelTxt = new Text2('Level 1', {
	size: 60,
	fill: 0x2C3E50
});
levelTxt.anchor.set(0.5, 0);
levelTxt.y = 90;
LK.gui.top.addChild(levelTxt);
// Instructions text
var instructionsTxt = new Text2('Tap to change direction', {
	size: 50,
	fill: 0x7F8C8D
});
instructionsTxt.anchor.set(0.5, 0);
instructionsTxt.y = 170;
LK.gui.top.addChild(instructionsTxt);
// Hide instructions after a few seconds
LK.setTimeout(function () {
	tween(instructionsTxt, {
		alpha: 0
	}, {
		duration: 1000,
		easing: tween.easeOut
	});
}, 5000);
// Initialize player
function initializePlayer() {
	player = new Player();
	player.x = 2048 / 2;
	player.y = 2732 / 2;
	game.addChild(player);
}
// Load level
function loadLevel(level) {
	// Clear previous level elements
	clearLevel();
	// Update level text
	levelTxt.setText('Level ' + level);
	// Increase player speed with each level
	player.speed = 5 + (level - 1) * speedIncreasePerLevel;
	// Generate maze elements
	var maze = mazeGenerator.generateMaze(level);
	// Add walls
	for (var i = 0; i < maze.walls.length; i++) {
		var wall = maze.walls[i];
		game.addChild(wall);
		walls.push(wall);
	}
	// Add coins
	for (var i = 0; i < maze.coins.length; i++) {
		var coin = maze.coins[i];
		game.addChild(coin);
		coins.push(coin);
	}
}
// Clear level elements
function clearLevel() {
	// Remove all walls
	for (var i = 0; i < walls.length; i++) {
		walls[i].destroy();
	}
	walls = [];
	// Remove all coins
	for (var i = 0; i < coins.length; i++) {
		coins[i].destroy();
	}
	coins = [];
}
// Start the game
function startGame() {
	// Initialize player
	initializePlayer();
	// Load first level
	loadLevel(level);
	// Play background music
	LK.playMusic('bgMusic', {
		fade: {
			start: 0,
			end: 0.5,
			duration: 1000
		}
	});
}
// Handle game tap
game.down = function (x, y, obj) {
	// Change player direction on tap
	if (player && player.alive) {
		player.turn();
	}
};
// Main game update loop
game.update = function () {
	if (!player || !player.alive) {
		return;
	}
	// Check for coin collection
	for (var i = coins.length - 1; i >= 0; i--) {
		var coin = coins[i];
		if (player.intersects(coin)) {
			// Collect coin
			coin.collect();
			coins.splice(i, 1);
			// Update score
			coinsCollected++;
			LK.setScore(coinsCollected);
			scoreTxt.setText(coinsCollected);
			// Check for level up
			if (coinsCollected % levelUpThreshold === 0) {
				level++;
				// Flash screen green for level up
				LK.effects.flashScreen(0x2ecc71, 500);
				// Load next level
				loadLevel(level);
			}
		}
	}
	// Check for wall collisions
	for (var i = 0; i < walls.length; i++) {
		var wall = walls[i];
		if (player.intersects(wall)) {
			player.die();
			break;
		}
	}
	// Check if all coins are collected
	if (coins.length === 0) {
		level++;
		// Flash screen
		LK.effects.flashScreen(0x2ecc71, 500);
		// Load next level
		loadLevel(level);
	}
};
// Start the game
startGame();