/**** 
* Classes
****/ 
// Bullet class
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = -5;
	self.update = function () {
		self.y += self.speed;
	};
});
// DOTMissile class
var DOTMissile = Container.expand(function () {
	var self = Container.call(this);
	var missileGraphics = self.attachAsset('DOTmissile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 8;
	self.update = function () {
		self.x += self.vx;
		self.y += self.vy;
		// Calculate the angle of movement
		var angle = Math.atan2(self.vy, self.vx);
		// Rotate the missile to point in the direction of movement
		missileGraphics.rotation = angle;
	};
});
// Explosion class
var Explosion = Container.expand(function () {
	var self = Container.call(this);
	var explosionGraphics = self.attachAsset('explosion', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.lifetime = 30; // Duration of the explosion effect
	self.update = function () {
		self.lifetime--;
		if (self.lifetime <= 0) {
			self.destroy();
		}
	};
});
// LifePod class
var LifePod = Container.expand(function () {
	var self = Container.call(this);
	var podGraphics = self.attachAsset('cargo', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2; // Falling speed of the life pod
	self.update = function () {
		self.y += self.speed;
		// Check if the life pod goes off screen
		if (self.y > 2732) {
			self.destroy();
		}
	};
});
// Missile class
var Missile = Container.expand(function () {
	var self = Container.call(this);
	var missileGraphics = self.attachAsset('missile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 8;
	self.update = function () {
		self.x += self.vx;
		self.y += self.vy;
		// Calculate the angle of movement
		var angle = Math.atan2(self.vy, self.vx);
		// Rotate the missile to point in the direction of movement
		missileGraphics.rotation = angle;
	};
});
// Mothership class
var Mothership = Container.expand(function () {
	var self = Container.call(this);
	var mothershipGraphics = self.attachAsset('spacePirateMotherShip', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.health = 10;
	self.update = function () {
		// Mothership logic can be added here
	};
});
// PowerBoost class
var PowerBoost = Container.expand(function () {
	var self = Container.call(this);
	var boostGraphics = self.attachAsset('yellowStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2; // Falling speed of the power boost
	self.update = function () {
		self.y += self.speed;
		// Check if the power boost goes off screen
		if (self.y > 2732) {
			self.destroy();
		}
	};
});
// Red Star class
var RedStar = Container.expand(function () {
	var self = Container.call(this);
	var starGraphics = self.attachAsset('redStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 1; // Set a consistent falling speed for stars
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height; // Reset position to top when it goes off screen
		}
	};
});
// Space DOT class
var SpaceDOT = Container.expand(function () {
	var self = Container.call(this);
	var dotGraphics = self.attachAsset('spaceDOT', {
		anchorX: 0.0,
		anchorY: 0.5
	});
	self.speed = 5;
	self.health = 3;
	self.update = function () {
		// Space DOTs fire missiles every 4 seconds
		if (LK.ticks % 240 == 0) {
			var newMissile = new DOTMissile();
			newMissile.x = self.x;
			newMissile.y = self.y;
			var dx = truck.x - self.x;
			var dy = truck.y - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			newMissile.vx = dx / distance * newMissile.speed;
			newMissile.vy = dy / distance * newMissile.speed;
			missiles.push(newMissile);
			game.addChild(newMissile);
		}
		// Calculate angle towards the truck
		var angle = Math.atan2(truck.y - self.y, truck.x - self.x);
		// Rotate the SpaceDOT to point towards the truck
		dotGraphics.rotation = angle;
		// Calculate direction towards the truck
		var dx = truck.x - self.x;
		var dy = truck.y - self.y;
		var distance = Math.sqrt(dx * dx + dy * dy);
		if (distance > 0.1) {
			// Prevent division by zero
			// Calculate movement towards the truck
			self.x += dx / distance * self.speed;
			self.y += dy / distance * self.speed;
		}
		if (self.y > 2732) {
			self.y = -self.height;
		}
	};
});
// Space Pirate class
var SpacePirate = Container.expand(function () {
	var self = Container.call(this);
	var pirateGraphics = self.attachAsset('spacePirate', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 3;
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height;
		}
		// Space pirates fire missiles
		if (LK.ticks % 240 == 0) {
			var newMissile = new Missile();
			newMissile.x = self.x;
			newMissile.y = self.y;
			// Calculate direction towards the truck
			var dx = truck.x - self.x;
			var dy = truck.y - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			newMissile.vx = dx / distance * newMissile.speed;
			newMissile.vy = dy / distance * newMissile.speed;
			missiles.push(newMissile);
			game.addChild(newMissile);
		}
	};
});
// Star class
var Star = Container.expand(function () {
	var self = Container.call(this);
	var starGraphics = self.attachAsset('blueStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 1; // Set a consistent falling speed for stars
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height; // Reset position to top when it goes off screen
		}
	};
});
//<Assets used in the game will automatically appear here>
// Truck class
var Truck = Container.expand(function () {
	var self = Container.call(this);
	self.lives = 5; // Initialize truck with 5 lives
	var truckGraphics = self.attachAsset('truck', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.update = function () {
		// Truck movement logic
		// Touch-based controls for truck movement
		game.move = function (x, y, obj) {
			self.x = x;
			self.y = y;
		};
		if (LK.ticks % 30 == 0) {
			var newBullet = new Bullet();
			newBullet.x = self.x;
			newBullet.y = self.y;
			bullets.push(newBullet);
			game.addChild(newBullet);
		}
	};
	self.move = function (x, y, obj) {
		self.x = x;
		self.y = y;
	};
});
// Yellow Star class
var YellowStar = Container.expand(function () {
	var self = Container.call(this);
	var starGraphics = self.attachAsset('yellowStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 1; // Set a consistent falling speed for stars
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height; // Reset position to top when it goes off screen
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background
});
/**** 
* Game Code
****/ 
// Load and play background music at the beginning of the game
LK.playMusic('fspaceadventure');
var stars = [];
for (var i = 0; i < 100; i++) {
	var star;
	var randomType = Math.random();
	if (randomType < 0.33) {
		star = new Star();
	} else if (randomType < 0.66) {
		star = new RedStar();
	} else {
		star = new YellowStar();
	}
	star.x = Math.random() * 2048;
	star.y = Math.random() * 2732;
	stars.push(star);
	game.addChild(star);
}
// Update stars
for (var i = 0; i < stars.length; i++) {
	stars[i].update();
}
// Initialize score to 0 points
LK.setScore(0);
// Initialize bullets
var bullets = [];
// Initialize missiles
var missiles = [];
// Initialize truck
var truck = game.addChild(new Truck());
truck.x = 2048 / 2;
truck.y = 2732 - 200;
// Initialize space pirates
var spacePirates = [];
var pirateSpawnInterval = LK.setInterval(function () {
	var pirate = new SpacePirate();
	pirate.x = Math.random() * 2048;
	pirate.y = Math.random() * -2732; // Ensure pirates spawn off-screen initially
	spacePirates.push(pirate);
	game.addChild(pirate);
}, 5000); // Spawn a pirate every 5 seconds
// Initialize space DOTs
var spaceDOTs = [];
var dotSpawnInterval = LK.setInterval(function () {
	var dot = new SpaceDOT();
	dot.x = Math.random() * 2048;
	dot.y = Math.random() * -500;
	spaceDOTs.push(dot);
	game.addChild(dot);
}, 10000); // Spawn a DOT every 10 seconds
// Handle truck movement
game.move = function (x, y, obj) {
	truck.x = x;
	truck.y = y;
};
// Display current points at the top of the screen in white letters and numbers, in a retro font
var scoreDisplay = new Text2('Score: 0', {
	size: 100,
	// Increased size for the score banner
	fill: "#ffffff",
	font: "'Press Start 2P', cursive" // Retro font
});
scoreDisplay.anchor.set(0.5, 0); // Center the text horizontally
LK.gui.top.addChild(scoreDisplay);
// Display truck lives below the score banner
var livesDisplay = new Text2('Lives: 3', {
	size: 80,
	fill: "#ffffff",
	font: "'Press Start 2P', cursive"
});
livesDisplay.anchor.set(0.5, 0);
livesDisplay.y = scoreDisplay.height; // Position below the score banner
LK.gui.top.addChild(livesDisplay);
// Update game logic
if (LK.getScore() >= 500 && !mothershipSpawned) {
	var mothership = new Mothership();
	mothership.x = 2048 / 2;
	mothership.y = -mothership.height; // Start off-screen
	game.addChild(mothership);
	mothershipSpawned = true;
}
game.update = function () {
	// Increase missile speed after reaching 300 points
	if (LK.getScore() >= 300) {
		for (var i = 0; i < missiles.length; i++) {
			missiles[i].speed = 12; // Increase speed
		}
	}
	// Check if score is 100 or more to increase difficulty
	if (LK.getScore() >= 100) {
		// Increase pirate fire rate
		for (var i = 0; i < spacePirates.length; i++) {
			if (LK.ticks % 60 == 0) {
				// Fire every second instead of every two seconds
				var newMissile = new Missile();
				newMissile.x = spacePirates[i].x;
				newMissile.y = spacePirates[i].y;
				var dx = truck.x - spacePirates[i].x;
				var dy = truck.y - spacePirates[i].y;
				var distance = Math.sqrt(dx * dx + dy * dy);
				newMissile.vx = dx / distance * newMissile.speed;
				newMissile.vy = dy / distance * newMissile.speed;
				missiles.push(newMissile);
				game.addChild(newMissile);
			}
		}
		// Increase pirate spawn rate
		if (LK.getScore() >= 250) {
			if (LK.ticks % 150 == 0) {
				// Spawn a pirate every 2.5 seconds
				var pirate = new SpacePirate();
				pirate.x = Math.random() * 2048;
				pirate.y = Math.random() * -2732;
				spacePirates.push(pirate);
				game.addChild(pirate);
			}
		} else {
			if (LK.ticks % 300 == 0) {
				// Spawn a pirate every 5 seconds
				var pirate = new SpacePirate();
				pirate.x = Math.random() * 2048;
				pirate.y = Math.random() * -2732;
				spacePirates.push(pirate);
				game.addChild(pirate);
			}
		}
	}
	// Update lives display
	livesDisplay.setText('Lives: ' + truck.lives);
	// Update score display
	scoreDisplay.setText('Score: ' + LK.getScore());
	// Update game logic
	// Update bullets
	for (var i = bullets.length - 1; i >= 0; i--) {
		bullets[i].update();
		// Remove bullets that go off screen
		if (bullets[i].y < -50) {
			game.removeChild(bullets[i]);
			bullets.splice(i, 1);
			continue;
		}
		// Check collision with space pirates
		for (var j = spacePirates.length - 1; j >= 0; j--) {
			if (bullets[i].intersects(spacePirates[j])) {
				LK.getSound('Explosion').play();
				game.removeChild(bullets[i]);
				var explosion = new Explosion();
				explosion.x = spacePirates[j].x;
				explosion.y = spacePirates[j].y;
				game.addChild(explosion);
				game.removeChild(spacePirates[j]);
				// 10% chance to spawn a LifePod
				if (Math.random() < 0.1) {
					var lifePod = new LifePod();
					lifePod.x = spacePirates[j].x;
					lifePod.y = spacePirates[j].y;
					game.addChild(lifePod);
				}
				bullets.splice(i, 1);
				spacePirates.splice(j, 1);
				LK.setScore(LK.getScore() + 5); // Add 5 points for destroying a space pirate
				scoreDisplay.setStyle({
					fill: "#00ff00"
				}); // Change text color to green
				LK.setTimeout(function () {
					scoreDisplay.setStyle({
						fill: "#ffffff"
					}); // Change text color back to white
				}, 200);
				break;
			}
		}
		// Check collision with spaceDOTs
		for (var j = spaceDOTs.length - 1; j >= 0; j--) {
			if (bullets[i] && bullets[i].intersects(spaceDOTs[j])) {
				game.removeChild(bullets[i]);
				bullets.splice(i, 1);
				spaceDOTs[j].health -= 1;
				if (spaceDOTs[j].health <= 0) {
					var explosion = new Explosion();
					explosion.x = spaceDOTs[j].x;
					explosion.y = spaceDOTs[j].y;
					game.addChild(explosion);
					LK.getSound('Explosion2').play();
					game.removeChild(spaceDOTs[j]);
					spaceDOTs.splice(j, 1);
					LK.setScore(LK.getScore() + 15); // Add 15 points for destroying a DOT
					// 100% chance to spawn a PowerBoost
					if (spaceDOTs[j]) {
						var powerBoost = new PowerBoost();
						powerBoost.x = spaceDOTs[j].x;
						powerBoost.y = spaceDOTs[j].y;
						game.addChild(powerBoost);
					}
					scoreDisplay.setStyle({
						fill: "#00ff00"
					}); // Change text color to green
					LK.setTimeout(function () {
						scoreDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				}
				break;
			}
		}
	}
	for (var i = 0; i < spacePirates.length; i++) {
		spacePirates[i].update();
		// If a space pirate is destroyed, spawn a new one
		if (spacePirates.length == 0) {
			var pirate = new SpacePirate();
			pirate.x = Math.random() * 2048;
			pirate.y = Math.random() * -2732;
			spacePirates.push(pirate);
			game.addChild(pirate);
		}
		// Check collision with truck
		if (spacePirates[i].intersects(truck)) {
			game.removeChild(spacePirates[i]);
			spacePirates.splice(i, 1);
			truck.lives -= 1; // Decrease truck lives by 1
			livesDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				livesDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
				LK.setTimeout(function () {
					livesDisplay.setStyle({
						fill: "#ff0000"
					}); // Change text color to red again
					LK.setTimeout(function () {
						livesDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				}, 200);
			}, 200);
			if (truck.lives <= 0) {
				LK.effects.flashScreen(0xff0000, 1000);
				LK.showGameOver();
			}
		}
	}
	// Update space DOTs
	for (var i = 0; i < spaceDOTs.length; i++) {
		spaceDOTs[i].update();
		// Check collision with truck
		if (spaceDOTs[i].intersects(truck)) {
			var explosion = new Explosion();
			explosion.x = spaceDOTs[i].x;
			explosion.y = spaceDOTs[i].y;
			game.addChild(explosion);
			game.removeChild(spaceDOTs[i]);
			spaceDOTs.splice(i, 1);
			LK.getSound('Hit2').play(); // Play Hit2 sound when DOT collides with truck
			LK.setScore(LK.getScore() - 15); // Subtract 15 points for collision with the truck
			scoreDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				scoreDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
			}, 200);
		}
	}
	// Update life pods
	for (var i = game.children.length - 1; i >= 0; i--) {
		var child = game.children[i];
		if (child instanceof LifePod) {
			child.update();
			if (child.intersects(truck)) {
				game.removeChild(child);
				if (child instanceof LifePod) {
					truck.lives += 1; // Increase truck lives by 1
					LK.getSound('Hit3').play(); // Play Hit3 sound when truck picks up extra life
					livesDisplay.setStyle({
						fill: "#00ff00"
					}); // Change text color to green
					LK.setTimeout(function () {
						livesDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				} else if (child instanceof PowerBoost) {
					// Fire 3 bullets: 1 straight, 2 at 30-degree angles
					var angles = [0, Math.PI / 6, -Math.PI / 6];
					for (var _i = 0, _angles = angles; _i < _angles.length; _i++) {
						var angle = _angles[_i];
						var newBullet = new Bullet();
						newBullet.x = truck.x;
						newBullet.y = truck.y;
						newBullet.speed = -5;
						newBullet.update = function () {
							this.y += this.speed * Math.cos(angle);
							this.x += this.speed * Math.sin(angle);
						};
						bullets.push(newBullet);
						game.addChild(newBullet);
					}
				}
			}
		}
	}
	for (var i = missiles.length - 1; i >= 0; i--) {
		missiles[i].update();
		// Remove missiles that go off screen
		if (missiles[i].y > 2732 || missiles[i].x < -50 || missiles[i].x > 2098) {
			game.removeChild(missiles[i]);
			missiles.splice(i, 1);
			continue;
		}
		// Check collision with truck
		if (missiles[i].intersects(truck) && !truck.invincible) {
			game.removeChild(missiles[i]);
			missiles.splice(i, 1);
			if (missiles[i] instanceof DOTMissile) {
				LK.getSound('Hit2').play(); // Play Hit2 sound when DOTmissile hits truck
				LK.setScore(LK.getScore() - 5); // Subtract 5 points for DOTmissile collision
			} else {
				truck.lives -= 1;
				LK.getSound('Hit1').play(); // Play Hit1 sound when other missile hits truck
			}
			scoreDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				scoreDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
			}, 200);
			truck.invincible = true; // Make truck invincible
			LK.setTimeout(function () {
				truck.invincible = false; // Remove invincibility after 0.5 seconds
			}, 500);
			livesDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				livesDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
				LK.setTimeout(function () {
					livesDisplay.setStyle({
						fill: "#ff0000"
					}); // Change text color to red again
					LK.setTimeout(function () {
						livesDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				}, 200);
			}, 200);
			if (truck.lives <= 0) {
				LK.effects.flashScreen(0xff0000, 1000);
				LK.showGameOver();
			}
			continue; // Ensure the missile is immediately removed
		}
		// Check collision with missile and truck
		if (missiles[i].intersects(truck)) {
			game.removeChild(missiles[i]);
			missiles.splice(i, 1);
		}
	}
}; /**** 
* Classes
****/ 
// Bullet class
var Bullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('bullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = -5;
	self.update = function () {
		self.y += self.speed;
	};
});
// DOTMissile class
var DOTMissile = Container.expand(function () {
	var self = Container.call(this);
	var missileGraphics = self.attachAsset('DOTmissile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 8;
	self.update = function () {
		self.x += self.vx;
		self.y += self.vy;
		// Calculate the angle of movement
		var angle = Math.atan2(self.vy, self.vx);
		// Rotate the missile to point in the direction of movement
		missileGraphics.rotation = angle;
	};
});
// Explosion class
var Explosion = Container.expand(function () {
	var self = Container.call(this);
	var explosionGraphics = self.attachAsset('explosion', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.lifetime = 30; // Duration of the explosion effect
	self.update = function () {
		self.lifetime--;
		if (self.lifetime <= 0) {
			self.destroy();
		}
	};
});
// LifePod class
var LifePod = Container.expand(function () {
	var self = Container.call(this);
	var podGraphics = self.attachAsset('cargo', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2; // Falling speed of the life pod
	self.update = function () {
		self.y += self.speed;
		// Check if the life pod goes off screen
		if (self.y > 2732) {
			self.destroy();
		}
	};
});
// Missile class
var Missile = Container.expand(function () {
	var self = Container.call(this);
	var missileGraphics = self.attachAsset('missile', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 8;
	self.update = function () {
		self.x += self.vx;
		self.y += self.vy;
		// Calculate the angle of movement
		var angle = Math.atan2(self.vy, self.vx);
		// Rotate the missile to point in the direction of movement
		missileGraphics.rotation = angle;
	};
});
// Mothership class
var Mothership = Container.expand(function () {
	var self = Container.call(this);
	var mothershipGraphics = self.attachAsset('spacePirateMotherShip', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.health = 10;
	self.update = function () {
		// Mothership logic can be added here
	};
});
// PowerBoost class
var PowerBoost = Container.expand(function () {
	var self = Container.call(this);
	var boostGraphics = self.attachAsset('yellowStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 2; // Falling speed of the power boost
	self.update = function () {
		self.y += self.speed;
		// Check if the power boost goes off screen
		if (self.y > 2732) {
			self.destroy();
		}
	};
});
// Red Star class
var RedStar = Container.expand(function () {
	var self = Container.call(this);
	var starGraphics = self.attachAsset('redStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 1; // Set a consistent falling speed for stars
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height; // Reset position to top when it goes off screen
		}
	};
});
// Space DOT class
var SpaceDOT = Container.expand(function () {
	var self = Container.call(this);
	var dotGraphics = self.attachAsset('spaceDOT', {
		anchorX: 0.0,
		anchorY: 0.5
	});
	self.speed = 5;
	self.health = 3;
	self.update = function () {
		// Space DOTs fire missiles every 4 seconds
		if (LK.ticks % 240 == 0) {
			var newMissile = new DOTMissile();
			newMissile.x = self.x;
			newMissile.y = self.y;
			var dx = truck.x - self.x;
			var dy = truck.y - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			newMissile.vx = dx / distance * newMissile.speed;
			newMissile.vy = dy / distance * newMissile.speed;
			missiles.push(newMissile);
			game.addChild(newMissile);
		}
		// Calculate angle towards the truck
		var angle = Math.atan2(truck.y - self.y, truck.x - self.x);
		// Rotate the SpaceDOT to point towards the truck
		dotGraphics.rotation = angle;
		// Calculate direction towards the truck
		var dx = truck.x - self.x;
		var dy = truck.y - self.y;
		var distance = Math.sqrt(dx * dx + dy * dy);
		if (distance > 0.1) {
			// Prevent division by zero
			// Calculate movement towards the truck
			self.x += dx / distance * self.speed;
			self.y += dy / distance * self.speed;
		}
		if (self.y > 2732) {
			self.y = -self.height;
		}
	};
});
// Space Pirate class
var SpacePirate = Container.expand(function () {
	var self = Container.call(this);
	var pirateGraphics = self.attachAsset('spacePirate', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 3;
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height;
		}
		// Space pirates fire missiles
		if (LK.ticks % 240 == 0) {
			var newMissile = new Missile();
			newMissile.x = self.x;
			newMissile.y = self.y;
			// Calculate direction towards the truck
			var dx = truck.x - self.x;
			var dy = truck.y - self.y;
			var distance = Math.sqrt(dx * dx + dy * dy);
			newMissile.vx = dx / distance * newMissile.speed;
			newMissile.vy = dy / distance * newMissile.speed;
			missiles.push(newMissile);
			game.addChild(newMissile);
		}
	};
});
// Star class
var Star = Container.expand(function () {
	var self = Container.call(this);
	var starGraphics = self.attachAsset('blueStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 1; // Set a consistent falling speed for stars
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height; // Reset position to top when it goes off screen
		}
	};
});
//<Assets used in the game will automatically appear here>
// Truck class
var Truck = Container.expand(function () {
	var self = Container.call(this);
	self.lives = 5; // Initialize truck with 5 lives
	var truckGraphics = self.attachAsset('truck', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.update = function () {
		// Truck movement logic
		// Touch-based controls for truck movement
		game.move = function (x, y, obj) {
			self.x = x;
			self.y = y;
		};
		if (LK.ticks % 30 == 0) {
			var newBullet = new Bullet();
			newBullet.x = self.x;
			newBullet.y = self.y;
			bullets.push(newBullet);
			game.addChild(newBullet);
		}
	};
	self.move = function (x, y, obj) {
		self.x = x;
		self.y = y;
	};
});
// Yellow Star class
var YellowStar = Container.expand(function () {
	var self = Container.call(this);
	var starGraphics = self.attachAsset('yellowStar', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 1; // Set a consistent falling speed for stars
	self.update = function () {
		self.y += self.speed;
		if (self.y > 2732) {
			self.y = -self.height; // Reset position to top when it goes off screen
		}
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 //Init game with black background
});
/**** 
* Game Code
****/ 
// Load and play background music at the beginning of the game
LK.playMusic('fspaceadventure');
var stars = [];
for (var i = 0; i < 100; i++) {
	var star;
	var randomType = Math.random();
	if (randomType < 0.33) {
		star = new Star();
	} else if (randomType < 0.66) {
		star = new RedStar();
	} else {
		star = new YellowStar();
	}
	star.x = Math.random() * 2048;
	star.y = Math.random() * 2732;
	stars.push(star);
	game.addChild(star);
}
// Update stars
for (var i = 0; i < stars.length; i++) {
	stars[i].update();
}
// Initialize score to 0 points
LK.setScore(0);
// Initialize bullets
var bullets = [];
// Initialize missiles
var missiles = [];
// Initialize truck
var truck = game.addChild(new Truck());
truck.x = 2048 / 2;
truck.y = 2732 - 200;
// Initialize space pirates
var spacePirates = [];
var pirateSpawnInterval = LK.setInterval(function () {
	var pirate = new SpacePirate();
	pirate.x = Math.random() * 2048;
	pirate.y = Math.random() * -2732; // Ensure pirates spawn off-screen initially
	spacePirates.push(pirate);
	game.addChild(pirate);
}, 5000); // Spawn a pirate every 5 seconds
// Initialize space DOTs
var spaceDOTs = [];
var dotSpawnInterval = LK.setInterval(function () {
	var dot = new SpaceDOT();
	dot.x = Math.random() * 2048;
	dot.y = Math.random() * -500;
	spaceDOTs.push(dot);
	game.addChild(dot);
}, 10000); // Spawn a DOT every 10 seconds
// Handle truck movement
game.move = function (x, y, obj) {
	truck.x = x;
	truck.y = y;
};
// Display current points at the top of the screen in white letters and numbers, in a retro font
var scoreDisplay = new Text2('Score: 0', {
	size: 100,
	// Increased size for the score banner
	fill: "#ffffff",
	font: "'Press Start 2P', cursive" // Retro font
});
scoreDisplay.anchor.set(0.5, 0); // Center the text horizontally
LK.gui.top.addChild(scoreDisplay);
// Display truck lives below the score banner
var livesDisplay = new Text2('Lives: 3', {
	size: 80,
	fill: "#ffffff",
	font: "'Press Start 2P', cursive"
});
livesDisplay.anchor.set(0.5, 0);
livesDisplay.y = scoreDisplay.height; // Position below the score banner
LK.gui.top.addChild(livesDisplay);
// Update game logic
if (LK.getScore() >= 500 && !mothershipSpawned) {
	var mothership = new Mothership();
	mothership.x = 2048 / 2;
	mothership.y = -mothership.height; // Start off-screen
	game.addChild(mothership);
	mothershipSpawned = true;
}
game.update = function () {
	// Increase missile speed after reaching 300 points
	if (LK.getScore() >= 300) {
		for (var i = 0; i < missiles.length; i++) {
			missiles[i].speed = 12; // Increase speed
		}
	}
	// Check if score is 100 or more to increase difficulty
	if (LK.getScore() >= 100) {
		// Increase pirate fire rate
		for (var i = 0; i < spacePirates.length; i++) {
			if (LK.ticks % 60 == 0) {
				// Fire every second instead of every two seconds
				var newMissile = new Missile();
				newMissile.x = spacePirates[i].x;
				newMissile.y = spacePirates[i].y;
				var dx = truck.x - spacePirates[i].x;
				var dy = truck.y - spacePirates[i].y;
				var distance = Math.sqrt(dx * dx + dy * dy);
				newMissile.vx = dx / distance * newMissile.speed;
				newMissile.vy = dy / distance * newMissile.speed;
				missiles.push(newMissile);
				game.addChild(newMissile);
			}
		}
		// Increase pirate spawn rate
		if (LK.getScore() >= 250) {
			if (LK.ticks % 150 == 0) {
				// Spawn a pirate every 2.5 seconds
				var pirate = new SpacePirate();
				pirate.x = Math.random() * 2048;
				pirate.y = Math.random() * -2732;
				spacePirates.push(pirate);
				game.addChild(pirate);
			}
		} else {
			if (LK.ticks % 300 == 0) {
				// Spawn a pirate every 5 seconds
				var pirate = new SpacePirate();
				pirate.x = Math.random() * 2048;
				pirate.y = Math.random() * -2732;
				spacePirates.push(pirate);
				game.addChild(pirate);
			}
		}
	}
	// Update lives display
	livesDisplay.setText('Lives: ' + truck.lives);
	// Update score display
	scoreDisplay.setText('Score: ' + LK.getScore());
	// Update game logic
	// Update bullets
	for (var i = bullets.length - 1; i >= 0; i--) {
		bullets[i].update();
		// Remove bullets that go off screen
		if (bullets[i].y < -50) {
			game.removeChild(bullets[i]);
			bullets.splice(i, 1);
			continue;
		}
		// Check collision with space pirates
		for (var j = spacePirates.length - 1; j >= 0; j--) {
			if (bullets[i].intersects(spacePirates[j])) {
				LK.getSound('Explosion').play();
				game.removeChild(bullets[i]);
				var explosion = new Explosion();
				explosion.x = spacePirates[j].x;
				explosion.y = spacePirates[j].y;
				game.addChild(explosion);
				game.removeChild(spacePirates[j]);
				// 10% chance to spawn a LifePod
				if (Math.random() < 0.1) {
					var lifePod = new LifePod();
					lifePod.x = spacePirates[j].x;
					lifePod.y = spacePirates[j].y;
					game.addChild(lifePod);
				}
				bullets.splice(i, 1);
				spacePirates.splice(j, 1);
				LK.setScore(LK.getScore() + 5); // Add 5 points for destroying a space pirate
				scoreDisplay.setStyle({
					fill: "#00ff00"
				}); // Change text color to green
				LK.setTimeout(function () {
					scoreDisplay.setStyle({
						fill: "#ffffff"
					}); // Change text color back to white
				}, 200);
				break;
			}
		}
		// Check collision with spaceDOTs
		for (var j = spaceDOTs.length - 1; j >= 0; j--) {
			if (bullets[i] && bullets[i].intersects(spaceDOTs[j])) {
				game.removeChild(bullets[i]);
				bullets.splice(i, 1);
				spaceDOTs[j].health -= 1;
				if (spaceDOTs[j].health <= 0) {
					var explosion = new Explosion();
					explosion.x = spaceDOTs[j].x;
					explosion.y = spaceDOTs[j].y;
					game.addChild(explosion);
					LK.getSound('Explosion2').play();
					game.removeChild(spaceDOTs[j]);
					spaceDOTs.splice(j, 1);
					LK.setScore(LK.getScore() + 15); // Add 15 points for destroying a DOT
					// 100% chance to spawn a PowerBoost
					if (spaceDOTs[j]) {
						var powerBoost = new PowerBoost();
						powerBoost.x = spaceDOTs[j].x;
						powerBoost.y = spaceDOTs[j].y;
						game.addChild(powerBoost);
					}
					scoreDisplay.setStyle({
						fill: "#00ff00"
					}); // Change text color to green
					LK.setTimeout(function () {
						scoreDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				}
				break;
			}
		}
	}
	for (var i = 0; i < spacePirates.length; i++) {
		spacePirates[i].update();
		// If a space pirate is destroyed, spawn a new one
		if (spacePirates.length == 0) {
			var pirate = new SpacePirate();
			pirate.x = Math.random() * 2048;
			pirate.y = Math.random() * -2732;
			spacePirates.push(pirate);
			game.addChild(pirate);
		}
		// Check collision with truck
		if (spacePirates[i].intersects(truck)) {
			game.removeChild(spacePirates[i]);
			spacePirates.splice(i, 1);
			truck.lives -= 1; // Decrease truck lives by 1
			livesDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				livesDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
				LK.setTimeout(function () {
					livesDisplay.setStyle({
						fill: "#ff0000"
					}); // Change text color to red again
					LK.setTimeout(function () {
						livesDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				}, 200);
			}, 200);
			if (truck.lives <= 0) {
				LK.effects.flashScreen(0xff0000, 1000);
				LK.showGameOver();
			}
		}
	}
	// Update space DOTs
	for (var i = 0; i < spaceDOTs.length; i++) {
		spaceDOTs[i].update();
		// Check collision with truck
		if (spaceDOTs[i].intersects(truck)) {
			var explosion = new Explosion();
			explosion.x = spaceDOTs[i].x;
			explosion.y = spaceDOTs[i].y;
			game.addChild(explosion);
			game.removeChild(spaceDOTs[i]);
			spaceDOTs.splice(i, 1);
			LK.getSound('Hit2').play(); // Play Hit2 sound when DOT collides with truck
			LK.setScore(LK.getScore() - 15); // Subtract 15 points for collision with the truck
			scoreDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				scoreDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
			}, 200);
		}
	}
	// Update life pods
	for (var i = game.children.length - 1; i >= 0; i--) {
		var child = game.children[i];
		if (child instanceof LifePod) {
			child.update();
			if (child.intersects(truck)) {
				game.removeChild(child);
				if (child instanceof LifePod) {
					truck.lives += 1; // Increase truck lives by 1
					LK.getSound('Hit3').play(); // Play Hit3 sound when truck picks up extra life
					livesDisplay.setStyle({
						fill: "#00ff00"
					}); // Change text color to green
					LK.setTimeout(function () {
						livesDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				} else if (child instanceof PowerBoost) {
					// Fire 3 bullets: 1 straight, 2 at 30-degree angles
					var angles = [0, Math.PI / 6, -Math.PI / 6];
					for (var _i = 0, _angles = angles; _i < _angles.length; _i++) {
						var angle = _angles[_i];
						var newBullet = new Bullet();
						newBullet.x = truck.x;
						newBullet.y = truck.y;
						newBullet.speed = -5;
						newBullet.update = function () {
							this.y += this.speed * Math.cos(angle);
							this.x += this.speed * Math.sin(angle);
						};
						bullets.push(newBullet);
						game.addChild(newBullet);
					}
				}
			}
		}
	}
	for (var i = missiles.length - 1; i >= 0; i--) {
		missiles[i].update();
		// Remove missiles that go off screen
		if (missiles[i].y > 2732 || missiles[i].x < -50 || missiles[i].x > 2098) {
			game.removeChild(missiles[i]);
			missiles.splice(i, 1);
			continue;
		}
		// Check collision with truck
		if (missiles[i].intersects(truck) && !truck.invincible) {
			game.removeChild(missiles[i]);
			missiles.splice(i, 1);
			if (missiles[i] instanceof DOTMissile) {
				LK.getSound('Hit2').play(); // Play Hit2 sound when DOTmissile hits truck
				LK.setScore(LK.getScore() - 5); // Subtract 5 points for DOTmissile collision
			} else {
				truck.lives -= 1;
				LK.getSound('Hit1').play(); // Play Hit1 sound when other missile hits truck
			}
			scoreDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				scoreDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
			}, 200);
			truck.invincible = true; // Make truck invincible
			LK.setTimeout(function () {
				truck.invincible = false; // Remove invincibility after 0.5 seconds
			}, 500);
			livesDisplay.setStyle({
				fill: "#ff0000"
			}); // Change text color to red
			LK.setTimeout(function () {
				livesDisplay.setStyle({
					fill: "#ffffff"
				}); // Change text color back to white
				LK.setTimeout(function () {
					livesDisplay.setStyle({
						fill: "#ff0000"
					}); // Change text color to red again
					LK.setTimeout(function () {
						livesDisplay.setStyle({
							fill: "#ffffff"
						}); // Change text color back to white
					}, 200);
				}, 200);
			}, 200);
			if (truck.lives <= 0) {
				LK.effects.flashScreen(0xff0000, 1000);
				LK.showGameOver();
			}
			continue; // Ensure the missile is immediately removed
		}
		// Check collision with missile and truck
		if (missiles[i].intersects(truck)) {
			game.removeChild(missiles[i]);
			missiles.splice(i, 1);
		}
	}
};