/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var facekit = LK.import("@upit/facekit.v1");
var storage = LK.import("@upit/storage.v1", {
	gameScale: 1
});
/**** 
* Classes
****/ 
var Asteroid = Container.expand(function () {
	var self = Container.call(this);
	self.points = 20;
	var asteroidGraphics = self.attachAsset('asteroidMedium', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 3 + 1;
	self.rotationSpeed = (Math.random() - 0.5) * 0.1;
	self.update = function () {
		self.y += self.speed;
		asteroidGraphics.rotation += self.rotationSpeed;
	};
	return self;
});
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = -8;
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var Ship = Container.expand(function () {
	var self = Container.call(this);
	var shipGraphics = self.attachAsset('ship', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000022
});
/**** 
* Game Code
****/ 
var gameScale = storage.gameScale || 1.0;
var bullets = [];
var asteroids = [];
var bulletTimer = 0;
var asteroidTimer = 0;
var asteroidSpawnRate = 90;
var dragNode = null;
var lastMouthOpen = false;
var ship = game.addChild(new Ship());
ship.x = 2048 / 2;
ship.y = 2732 - 150;
ship.scaleX = gameScale;
ship.scaleY = gameScale;
var scoreTxt = new Text2('0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var highScoreTxt = new Text2('High Score: ' + (storage.highScore || 0), {
	size: 40,
	fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0.5, 0);
highScoreTxt.y = 100;
LK.gui.top.addChild(highScoreTxt);
function spawnAsteroid() {
	var size = 'medium';
	var asteroid = new Asteroid();
	asteroid.x = Math.random() * (2048 - 100) + 50;
	asteroid.y = -60;
	asteroid.scaleX = gameScale;
	asteroid.scaleY = gameScale;
	asteroids.push(asteroid);
	game.addChild(asteroid);
}
function fireBullet() {
	var bullet = new Bullet();
	bullet.x = ship.x;
	bullet.y = ship.y - 50;
	bullet.scaleX = gameScale;
	bullet.scaleY = gameScale;
	bullets.push(bullet);
	game.addChild(bullet);
	LK.getSound('shoot').play();
}
game.move = function (x, y, obj) {
	if (dragNode) {
		dragNode.x = Math.max(40, Math.min(2048 - 40, x));
		// Allow vertical movement - move forward when dragging up
		dragNode.y = Math.max(150, Math.min(2732 - 150, y));
	}
};
game.down = function (x, y, obj) {
	dragNode = ship;
	ship.x = Math.max(40, Math.min(2048 - 40, x));
	// Allow initial vertical positioning when touching down
	ship.y = Math.max(150, Math.min(2732 - 150, y));
	// Fire bullet on tap
	fireBullet();
};
game.up = function (x, y, obj) {
	dragNode = null;
};
game.update = function () {
	bulletTimer++;
	asteroidTimer++;
	// Handle mouth shooting
	if (!lastMouthOpen && facekit.mouthOpen) {
		fireBullet();
	}
	lastMouthOpen = facekit.mouthOpen;
	// Adjust game scale based on mouth opening width (simulate size setting)
	if (facekit.mouthOpen) {
		var newScale = Math.max(0.5, Math.min(2.0, 1.0 + facekit.volume * 2));
		if (Math.abs(newScale - gameScale) > 0.1) {
			gameScale = newScale;
			storage.gameScale = gameScale;
			// Apply new scale to existing objects
			ship.scaleX = gameScale;
			ship.scaleY = gameScale;
			for (var s = 0; s < asteroids.length; s++) {
				asteroids[s].scaleX = gameScale;
				asteroids[s].scaleY = gameScale;
			}
			for (var t = 0; t < bullets.length; t++) {
				bullets[t].scaleX = gameScale;
				bullets[t].scaleY = gameScale;
			}
		}
	}
	// Spawn asteroids
	if (asteroidTimer >= asteroidSpawnRate) {
		spawnAsteroid();
		asteroidTimer = 0;
		// Gradually increase difficulty
		if (asteroidSpawnRate > 30) {
			asteroidSpawnRate -= 0.5;
		}
	}
	// Update and check bullets
	for (var i = bullets.length - 1; i >= 0; i--) {
		var bullet = bullets[i];
		if (bullet.y < -50) {
			bullet.destroy();
			bullets.splice(i, 1);
			continue;
		}
		// Check bullet-asteroid collisions
		for (var j = asteroids.length - 1; j >= 0; j--) {
			var asteroid = asteroids[j];
			if (bullet.intersects(asteroid)) {
				LK.setScore(LK.getScore() + asteroid.points);
				scoreTxt.setText(LK.getScore());
				LK.getSound('explosion').play();
				// Flash effect on hit
				LK.effects.flashObject(asteroid, 0xffffff, 200);
				bullet.destroy();
				bullets.splice(i, 1);
				asteroid.destroy();
				asteroids.splice(j, 1);
				break;
			}
		}
	}
	// Update and check asteroids
	for (var k = asteroids.length - 1; k >= 0; k--) {
		var asteroid = asteroids[k];
		if (asteroid.y > 2732 + 100) {
			// Game over when asteroid reaches bottom
			LK.effects.flashScreen(0xff0000, 1000);
			// Save high score before showing game over
			var currentScore = LK.getScore();
			var highScore = storage.highScore || 0;
			if (currentScore > highScore) {
				storage.highScore = currentScore;
			}
			LK.showGameOver();
			return;
		}
		// Check ship-asteroid collision
		if (ship.intersects(asteroid)) {
			LK.effects.flashScreen(0xff0000, 1000);
			// Save high score before showing game over
			var currentScore = LK.getScore();
			var highScore = storage.highScore || 0;
			if (currentScore > highScore) {
				storage.highScore = currentScore;
			}
			LK.showGameOver();
			return;
		}
	}
}; /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
var facekit = LK.import("@upit/facekit.v1");
var storage = LK.import("@upit/storage.v1", {
	gameScale: 1
});
/**** 
* Classes
****/ 
var Asteroid = Container.expand(function () {
	var self = Container.call(this);
	self.points = 20;
	var asteroidGraphics = self.attachAsset('asteroidMedium', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = Math.random() * 3 + 1;
	self.rotationSpeed = (Math.random() - 0.5) * 0.1;
	self.update = function () {
		self.y += self.speed;
		asteroidGraphics.rotation += self.rotationSpeed;
	};
	return self;
});
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = -8;
	self.update = function () {
		self.y += self.speed;
	};
	return self;
});
var Ship = Container.expand(function () {
	var self = Container.call(this);
	var shipGraphics = self.attachAsset('ship', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000022
});
/**** 
* Game Code
****/ 
var gameScale = storage.gameScale || 1.0;
var bullets = [];
var asteroids = [];
var bulletTimer = 0;
var asteroidTimer = 0;
var asteroidSpawnRate = 90;
var dragNode = null;
var lastMouthOpen = false;
var ship = game.addChild(new Ship());
ship.x = 2048 / 2;
ship.y = 2732 - 150;
ship.scaleX = gameScale;
ship.scaleY = gameScale;
var scoreTxt = new Text2('0', {
	size: 80,
	fill: 0xFFFFFF
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
var highScoreTxt = new Text2('High Score: ' + (storage.highScore || 0), {
	size: 40,
	fill: 0xFFFFFF
});
highScoreTxt.anchor.set(0.5, 0);
highScoreTxt.y = 100;
LK.gui.top.addChild(highScoreTxt);
function spawnAsteroid() {
	var size = 'medium';
	var asteroid = new Asteroid();
	asteroid.x = Math.random() * (2048 - 100) + 50;
	asteroid.y = -60;
	asteroid.scaleX = gameScale;
	asteroid.scaleY = gameScale;
	asteroids.push(asteroid);
	game.addChild(asteroid);
}
function fireBullet() {
	var bullet = new Bullet();
	bullet.x = ship.x;
	bullet.y = ship.y - 50;
	bullet.scaleX = gameScale;
	bullet.scaleY = gameScale;
	bullets.push(bullet);
	game.addChild(bullet);
	LK.getSound('shoot').play();
}
game.move = function (x, y, obj) {
	if (dragNode) {
		dragNode.x = Math.max(40, Math.min(2048 - 40, x));
		// Allow vertical movement - move forward when dragging up
		dragNode.y = Math.max(150, Math.min(2732 - 150, y));
	}
};
game.down = function (x, y, obj) {
	dragNode = ship;
	ship.x = Math.max(40, Math.min(2048 - 40, x));
	// Allow initial vertical positioning when touching down
	ship.y = Math.max(150, Math.min(2732 - 150, y));
	// Fire bullet on tap
	fireBullet();
};
game.up = function (x, y, obj) {
	dragNode = null;
};
game.update = function () {
	bulletTimer++;
	asteroidTimer++;
	// Handle mouth shooting
	if (!lastMouthOpen && facekit.mouthOpen) {
		fireBullet();
	}
	lastMouthOpen = facekit.mouthOpen;
	// Adjust game scale based on mouth opening width (simulate size setting)
	if (facekit.mouthOpen) {
		var newScale = Math.max(0.5, Math.min(2.0, 1.0 + facekit.volume * 2));
		if (Math.abs(newScale - gameScale) > 0.1) {
			gameScale = newScale;
			storage.gameScale = gameScale;
			// Apply new scale to existing objects
			ship.scaleX = gameScale;
			ship.scaleY = gameScale;
			for (var s = 0; s < asteroids.length; s++) {
				asteroids[s].scaleX = gameScale;
				asteroids[s].scaleY = gameScale;
			}
			for (var t = 0; t < bullets.length; t++) {
				bullets[t].scaleX = gameScale;
				bullets[t].scaleY = gameScale;
			}
		}
	}
	// Spawn asteroids
	if (asteroidTimer >= asteroidSpawnRate) {
		spawnAsteroid();
		asteroidTimer = 0;
		// Gradually increase difficulty
		if (asteroidSpawnRate > 30) {
			asteroidSpawnRate -= 0.5;
		}
	}
	// Update and check bullets
	for (var i = bullets.length - 1; i >= 0; i--) {
		var bullet = bullets[i];
		if (bullet.y < -50) {
			bullet.destroy();
			bullets.splice(i, 1);
			continue;
		}
		// Check bullet-asteroid collisions
		for (var j = asteroids.length - 1; j >= 0; j--) {
			var asteroid = asteroids[j];
			if (bullet.intersects(asteroid)) {
				LK.setScore(LK.getScore() + asteroid.points);
				scoreTxt.setText(LK.getScore());
				LK.getSound('explosion').play();
				// Flash effect on hit
				LK.effects.flashObject(asteroid, 0xffffff, 200);
				bullet.destroy();
				bullets.splice(i, 1);
				asteroid.destroy();
				asteroids.splice(j, 1);
				break;
			}
		}
	}
	// Update and check asteroids
	for (var k = asteroids.length - 1; k >= 0; k--) {
		var asteroid = asteroids[k];
		if (asteroid.y > 2732 + 100) {
			// Game over when asteroid reaches bottom
			LK.effects.flashScreen(0xff0000, 1000);
			// Save high score before showing game over
			var currentScore = LK.getScore();
			var highScore = storage.highScore || 0;
			if (currentScore > highScore) {
				storage.highScore = currentScore;
			}
			LK.showGameOver();
			return;
		}
		// Check ship-asteroid collision
		if (ship.intersects(asteroid)) {
			LK.effects.flashScreen(0xff0000, 1000);
			// Save high score before showing game over
			var currentScore = LK.getScore();
			var highScore = storage.highScore || 0;
			if (currentScore > highScore) {
				storage.highScore = currentScore;
			}
			LK.showGameOver();
			return;
		}
	}
};