/**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
// Floating asteroids for background effect
var BackgroundAsteroid = Container.expand(function () {
	var self = Container.call(this);
	// Create asteroid graphics using planet asset
	var asteroidGraphics = self.attachAsset('Planet2', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.1,
		scaleY: 0.1,
		alpha: 0.3
	});
	// Initialize random movement
	self.initMovement = function () {
		// Random starting position
		self.x = Math.random() * 2048;
		self.y = Math.random() * 2732;
		// Start floating animation
		self.startFloating();
	};
	// Create floating animation
	self.startFloating = function () {
		var targetX = Math.random() * 2048;
		var targetY = Math.random() * 2732;
		var duration = 8000 + Math.random() * 12000; // 8-20 seconds
		tween(self, {
			x: targetX,
			y: targetY
		}, {
			duration: duration,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				self.startFloating(); // Continue floating
			}
		});
		// Add gentle rotation
		tween(self, {
			rotation: self.rotation + Math.PI * 2
		}, {
			duration: duration,
			easing: tween.linear
		});
		// Add gentle scale pulsing
		var scaleVariation = 0.05 + Math.random() * 0.05;
		tween(asteroidGraphics, {
			scaleX: 0.1 + scaleVariation,
			scaleY: 0.1 + scaleVariation
		}, {
			duration: 3000 + Math.random() * 4000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(asteroidGraphics, {
					scaleX: 0.1,
					scaleY: 0.1
				}, {
					duration: 3000 + Math.random() * 4000,
					easing: tween.easeInOut
				});
			}
		});
	};
	return self;
});
// Twinkling stars for background effect
var BackgroundStar = Container.expand(function () {
	var self = Container.call(this);
	// Create star graphics using small trail dot
	var starGraphics = self.attachAsset('TrailDot', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.05,
		scaleY: 0.05,
		alpha: 0.6
	});
	// Initialize twinkling effect
	self.initTwinkle = function () {
		// Random starting position
		self.x = Math.random() * 2048;
		self.y = Math.random() * 2732;
		// Start twinkling
		self.startTwinkling();
	};
	// Create twinkling animation
	self.startTwinkling = function () {
		var fadeOutDuration = 1000 + Math.random() * 2000;
		var fadeInDuration = 1000 + Math.random() * 2000;
		tween(starGraphics, {
			alpha: 0.1
		}, {
			duration: fadeOutDuration,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(starGraphics, {
					alpha: 0.6 + Math.random() * 0.4
				}, {
					duration: fadeInDuration,
					easing: tween.easeInOut,
					onFinish: function onFinish() {
						self.startTwinkling(); // Continue twinkling
					}
				});
			}
		});
	};
	return self;
});
// Orbit trail visualization
var OrbitTrail = Container.expand(function () {
	var self = Container.call(this);
	// Draw the satellite's trail (optimized - only update every few frames)
	self.drawTrail = function (points) {
		// Only redraw trail every 5 frames to reduce lag
		if (LK.ticks % 5 !== 0) return;
		self.removeChildren();
		if (points.length < 2) return;
		// Reduce number of trail points rendered for performance
		var step = Math.max(1, Math.floor(points.length / 15));
		for (var i = step; i < points.length; i += step) {
			var point = points[i];
			// Create a small dot for each trail point
			var dot = LK.getAsset('TrailDot', {
				anchorX: 0.5,
				anchorY: 0.5,
				x: point.x,
				y: point.y,
				alpha: i / points.length * 0.5
			});
			dot.scaleX = 0.08;
			dot.scaleY = 0.08;
			self.addChild(dot);
		}
	};
	return self;
});
// Initialize game variables
// Planet class for the gravitational body
var Planet = Container.expand(function () {
	var self = Container.call(this);
	// Create planet graphics
	var planetGraphics = self.attachAsset('Planet', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1.5,
		scaleY: 1.5
	});
	// Orbit zones
	self.safeOrbitMin = 200; // Minimum safe orbit distance
	self.safeOrbitMax = 500; // Maximum safe orbit distance
	self.gravity = 0.15; // Gravity strength
	// Visual indicators for orbit zones
	var orbitZone = self.addChild(new Container());
	// Initialize orbit visualization
	self.initOrbitZones = function () {
		// Create outer safe orbit zone
		var outerZone = LK.getAsset('OrbitZone', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: 0,
			alpha: 0.1
		});
		outerZone.scaleX = self.safeOrbitMax * 2 / 100;
		outerZone.scaleY = self.safeOrbitMax * 2 / 100;
		orbitZone.addChild(outerZone);
		// Create inner safe orbit zone
		var innerZone = LK.getAsset('OrbitZone', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: 0,
			alpha: 0.1
		});
		innerZone.scaleX = self.safeOrbitMin * 2 / 100;
		innerZone.scaleY = self.safeOrbitMin * 2 / 100;
		orbitZone.addChild(innerZone);
	};
	// Apply gravity to a satellite
	self.applyGravityTo = function (satellite) {
		// Calculate direction from satellite to planet
		var dx = self.x - satellite.x;
		var dy = self.y - satellite.y;
		// Calculate distance (squared for efficiency)
		var distSquared = dx * dx + dy * dy;
		var distance = Math.sqrt(distSquared);
		// Only apply gravity if not too close
		if (distance > 10) {
			// Calculate gravity force (inverse square law)
			var force = self.gravity / distance;
			// Apply force to satellite velocity
			satellite.velocity.x += dx / distance * force;
			satellite.velocity.y += dy / distance * force;
		}
		return distance;
	};
	return self;
});
// Satellite class for the player-controlled object
var Satellite = Container.expand(function () {
	var self = Container.call(this);
	// Create satellite graphics
	var satelliteGraphics = self.attachAsset('Planet2', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.3,
		scaleY: 0.3
	});
	// Physics properties
	self.velocity = {
		x: 0,
		y: 0
	};
	self.lastX = 0;
	self.lastY = 0;
	self.isDragging = false;
	self.thrustDirection = {
		x: 0,
		y: 0
	};
	self.thrustPower = 0;
	self.maxThrust = 0.5;
	// Trail effect
	self.trailPoints = [];
	self.maxTrailPoints = 50;
	// Show thrust line when dragging
	self.thrustLine = self.addChild(new Container());
	// Initialize with a small orbit velocity
	self.initOrbit = function (planetX, planetY) {
		// Set initial position
		self.x = planetX;
		self.y = planetY - 300;
		self.lastX = self.x;
		self.lastY = self.y;
		// Set initial velocity for orbit (perpendicular to planet)
		self.velocity.x = 2;
		self.velocity.y = 0;
	};
	// Update satellite position based on physics
	self.update = function () {
		// Store last position
		self.lastX = self.x;
		self.lastY = self.y;
		// Add thrust if dragging
		if (self.isDragging && self.thrustPower > 0) {
			self.velocity.x += self.thrustDirection.x * self.thrustPower;
			self.velocity.y += self.thrustDirection.y * self.thrustPower;
		}
		// Update position based on velocity
		self.x += self.velocity.x;
		self.y += self.velocity.y;
		// Update trail
		self.updateTrail();
		// Update thrust visualization
		self.updateThrustLine();
	};
	// Add a point to the trail (less frequently for performance)
	self.updateTrail = function () {
		if (LK.ticks % 8 === 0) {
			self.trailPoints.push({
				x: self.x,
				y: self.y
			});
			if (self.trailPoints.length > 25) {
				self.trailPoints.shift();
			}
		}
	};
	// Update the thrust line visualization
	self.updateThrustLine = function () {
		self.thrustLine.removeChildren();
		if (self.isDragging && self.thrustPower > 0) {
			// Create a line showing thrust direction
			var line = LK.getAsset('ThrustLine', {
				anchorX: 0,
				anchorY: 0.5,
				x: 0,
				y: 0
			});
			line.rotation = Math.atan2(-self.thrustDirection.y, -self.thrustDirection.x);
			line.scaleX = self.thrustPower * 50;
			self.thrustLine.addChild(line);
		}
	};
	// Start applying thrust
	self.startThrust = function (x, y) {
		self.isDragging = true;
		self.updateThrustVector(x, y);
	};
	// Update thrust direction and power while dragging
	self.updateThrustVector = function (x, y) {
		if (self.isDragging) {
			// Calculate thrust direction from satellite to touch point
			var dx = self.x - x;
			var dy = self.y - y;
			// Calculate distance
			var distance = Math.sqrt(dx * dx + dy * dy);
			if (distance > 0) {
				// Normalize direction vector
				self.thrustDirection.x = dx / distance;
				self.thrustDirection.y = dy / distance;
				// Scale thrust power by distance, capped at maxThrust
				self.thrustPower = Math.min(distance / 100, self.maxThrust);
			}
		}
	};
	// Stop applying thrust
	self.endThrust = function () {
		self.isDragging = false;
		self.thrustPower = 0;
		self.thrustDirection.x = 0;
		self.thrustDirection.y = 0;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
// Initialize game variables
var planet;
var satellite;
var orbitTrail;
var gameStarted = false;
var gameOver = false;
var timeInOrbit = 0;
var scoreText;
var statusText;
var backgroundAsteroids = [];
var backgroundStars = [];
var dragStartPos = {
	x: 0,
	y: 0
};
// Set up the game environment
function setupGame() {
	// Set space background
	game.setBackgroundColor(0x000022);
	// Create and position the planet in center
	planet = new Planet();
	planet.x = 2048 / 2;
	planet.y = 2732 / 2;
	planet.initOrbitZones();
	game.addChild(planet);
	// Add pulsing glow effect to planet
	function startPlanetGlow() {
		tween(planet, {
			alpha: 0.8
		}, {
			duration: 3000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(planet, {
					alpha: 1.0
				}, {
					duration: 3000,
					easing: tween.easeInOut,
					onFinish: startPlanetGlow
				});
			}
		});
	}
	startPlanetGlow();
	// Create satellite
	satellite = new Satellite();
	satellite.initOrbit(planet.x, planet.y);
	game.addChild(satellite);
	// Create orbit trail
	orbitTrail = new OrbitTrail();
	game.addChild(orbitTrail);
	// Create background effects
	// Add floating asteroids
	for (var i = 0; i < 8; i++) {
		var asteroid = new BackgroundAsteroid();
		asteroid.initMovement();
		backgroundAsteroids.push(asteroid);
		game.addChild(asteroid);
	}
	// Add twinkling stars
	for (var i = 0; i < 25; i++) {
		var star = new BackgroundStar();
		star.initTwinkle();
		backgroundStars.push(star);
		game.addChild(star);
	}
	// Create score display
	scoreText = new Text2('Time in orbit: 0', {
		size: 60,
		fill: 0xFFFFFF
	});
	scoreText.anchor.set(0.5, 0);
	scoreText.y = 50;
	LK.gui.top.addChild(scoreText);
	// Create status text
	statusText = new Text2('Tap and drag to apply thrust', {
		size: 50,
		fill: 0xFFFFFF
	});
	statusText.anchor.set(0.5, 1);
	statusText.y = -50;
	LK.gui.bottom.addChild(statusText);
	// Start the game
	gameStarted = true;
}
// Update game state each frame
game.update = function () {
	if (!gameStarted) {
		setupGame();
		return;
	}
	if (gameOver) return;
	// Update satellite physics
	satellite.update();
	// Apply planet's gravity to the satellite
	var distance = planet.applyGravityTo(satellite);
	// Check if satellite is in safe orbit
	var inSafeOrbit = distance >= planet.safeOrbitMin && distance <= planet.safeOrbitMax;
	// Update time in orbit if in safe zone
	if (inSafeOrbit) {
		if (LK.ticks % 60 === 0) {
			// Increase score every second
			timeInOrbit++;
			scoreText.setText('Time in orbit: ' + timeInOrbit);
			LK.setScore(timeInOrbit);
		}
		// Only update status text occasionally to reduce lag
		if (LK.ticks % 15 === 0) {
			statusText.setText('Stable orbit: ' + Math.round(distance));
			if (!statusText.isGreen) {
				statusText.style = {
					fill: 0x33FF33
				};
				statusText.isGreen = true;
				statusText.isRed = false;
				statusText.isOrange = false;
			}
		}
	} else if (distance < planet.safeOrbitMin) {
		if (LK.ticks % 15 === 0) {
			statusText.setText('Warning: Orbit too close!');
			if (!statusText.isRed) {
				statusText.style = {
					fill: 0xFF3333
				};
				statusText.isRed = true;
				statusText.isGreen = false;
				statusText.isOrange = false;
			}
		}
	} else if (distance > planet.safeOrbitMax) {
		if (LK.ticks % 15 === 0) {
			statusText.setText('Warning: Orbit too far!');
			if (!statusText.isOrange) {
				statusText.style = {
					fill: 0xFF9933
				};
				statusText.isOrange = true;
				statusText.isGreen = false;
				statusText.isRed = false;
			}
		}
	}
	// Check for crash into planet
	if (distance < 70) {
		// Planet radius is approximately 75px
		endGame("Crash landing!");
	}
	// Check if satellite has gone too far from the planet
	if (distance > 1500) {
		endGame("Satellite lost in space");
	}
	// Draw orbit trail
	orbitTrail.drawTrail(satellite.trailPoints);
};
// Handle touch/mouse down on game
game.down = function (x, y, obj) {
	if (gameOver) return;
	dragStartPos.x = x;
	dragStartPos.y = y;
	// Start applying thrust
	satellite.startThrust(x, y);
};
// Handle touch/mouse move on game
game.move = function (x, y, obj) {
	if (gameOver) return;
	// Update thrust vector
	satellite.updateThrustVector(x, y);
};
// Handle touch/mouse up on game
game.up = function (x, y, obj) {
	if (gameOver) return;
	// Stop applying thrust
	satellite.endThrust();
};
// End the game with a specific message
function endGame(message) {
	gameOver = true;
	statusText.setText(message);
	statusText.style = {
		fill: 0xFF0000
	};
	statusText.isRed = true;
	statusText.isGreen = false;
	statusText.isOrange = false;
	// Flash screen and show game over
	LK.effects.flashScreen(0xff0000, 1000);
	LK.setTimeout(function () {
		LK.showGameOver();
	}, 1500);
}
// Start background music
LK.playMusic('Bgmusic');
// Setup game
setupGame(); /**** 
* Plugins
****/ 
var tween = LK.import("@upit/tween.v1");
/**** 
* Classes
****/ 
// Floating asteroids for background effect
var BackgroundAsteroid = Container.expand(function () {
	var self = Container.call(this);
	// Create asteroid graphics using planet asset
	var asteroidGraphics = self.attachAsset('Planet2', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.1,
		scaleY: 0.1,
		alpha: 0.3
	});
	// Initialize random movement
	self.initMovement = function () {
		// Random starting position
		self.x = Math.random() * 2048;
		self.y = Math.random() * 2732;
		// Start floating animation
		self.startFloating();
	};
	// Create floating animation
	self.startFloating = function () {
		var targetX = Math.random() * 2048;
		var targetY = Math.random() * 2732;
		var duration = 8000 + Math.random() * 12000; // 8-20 seconds
		tween(self, {
			x: targetX,
			y: targetY
		}, {
			duration: duration,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				self.startFloating(); // Continue floating
			}
		});
		// Add gentle rotation
		tween(self, {
			rotation: self.rotation + Math.PI * 2
		}, {
			duration: duration,
			easing: tween.linear
		});
		// Add gentle scale pulsing
		var scaleVariation = 0.05 + Math.random() * 0.05;
		tween(asteroidGraphics, {
			scaleX: 0.1 + scaleVariation,
			scaleY: 0.1 + scaleVariation
		}, {
			duration: 3000 + Math.random() * 4000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(asteroidGraphics, {
					scaleX: 0.1,
					scaleY: 0.1
				}, {
					duration: 3000 + Math.random() * 4000,
					easing: tween.easeInOut
				});
			}
		});
	};
	return self;
});
// Twinkling stars for background effect
var BackgroundStar = Container.expand(function () {
	var self = Container.call(this);
	// Create star graphics using small trail dot
	var starGraphics = self.attachAsset('TrailDot', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.05,
		scaleY: 0.05,
		alpha: 0.6
	});
	// Initialize twinkling effect
	self.initTwinkle = function () {
		// Random starting position
		self.x = Math.random() * 2048;
		self.y = Math.random() * 2732;
		// Start twinkling
		self.startTwinkling();
	};
	// Create twinkling animation
	self.startTwinkling = function () {
		var fadeOutDuration = 1000 + Math.random() * 2000;
		var fadeInDuration = 1000 + Math.random() * 2000;
		tween(starGraphics, {
			alpha: 0.1
		}, {
			duration: fadeOutDuration,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(starGraphics, {
					alpha: 0.6 + Math.random() * 0.4
				}, {
					duration: fadeInDuration,
					easing: tween.easeInOut,
					onFinish: function onFinish() {
						self.startTwinkling(); // Continue twinkling
					}
				});
			}
		});
	};
	return self;
});
// Orbit trail visualization
var OrbitTrail = Container.expand(function () {
	var self = Container.call(this);
	// Draw the satellite's trail (optimized - only update every few frames)
	self.drawTrail = function (points) {
		// Only redraw trail every 5 frames to reduce lag
		if (LK.ticks % 5 !== 0) return;
		self.removeChildren();
		if (points.length < 2) return;
		// Reduce number of trail points rendered for performance
		var step = Math.max(1, Math.floor(points.length / 15));
		for (var i = step; i < points.length; i += step) {
			var point = points[i];
			// Create a small dot for each trail point
			var dot = LK.getAsset('TrailDot', {
				anchorX: 0.5,
				anchorY: 0.5,
				x: point.x,
				y: point.y,
				alpha: i / points.length * 0.5
			});
			dot.scaleX = 0.08;
			dot.scaleY = 0.08;
			self.addChild(dot);
		}
	};
	return self;
});
// Initialize game variables
// Planet class for the gravitational body
var Planet = Container.expand(function () {
	var self = Container.call(this);
	// Create planet graphics
	var planetGraphics = self.attachAsset('Planet', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 1.5,
		scaleY: 1.5
	});
	// Orbit zones
	self.safeOrbitMin = 200; // Minimum safe orbit distance
	self.safeOrbitMax = 500; // Maximum safe orbit distance
	self.gravity = 0.15; // Gravity strength
	// Visual indicators for orbit zones
	var orbitZone = self.addChild(new Container());
	// Initialize orbit visualization
	self.initOrbitZones = function () {
		// Create outer safe orbit zone
		var outerZone = LK.getAsset('OrbitZone', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: 0,
			alpha: 0.1
		});
		outerZone.scaleX = self.safeOrbitMax * 2 / 100;
		outerZone.scaleY = self.safeOrbitMax * 2 / 100;
		orbitZone.addChild(outerZone);
		// Create inner safe orbit zone
		var innerZone = LK.getAsset('OrbitZone', {
			anchorX: 0.5,
			anchorY: 0.5,
			x: 0,
			y: 0,
			alpha: 0.1
		});
		innerZone.scaleX = self.safeOrbitMin * 2 / 100;
		innerZone.scaleY = self.safeOrbitMin * 2 / 100;
		orbitZone.addChild(innerZone);
	};
	// Apply gravity to a satellite
	self.applyGravityTo = function (satellite) {
		// Calculate direction from satellite to planet
		var dx = self.x - satellite.x;
		var dy = self.y - satellite.y;
		// Calculate distance (squared for efficiency)
		var distSquared = dx * dx + dy * dy;
		var distance = Math.sqrt(distSquared);
		// Only apply gravity if not too close
		if (distance > 10) {
			// Calculate gravity force (inverse square law)
			var force = self.gravity / distance;
			// Apply force to satellite velocity
			satellite.velocity.x += dx / distance * force;
			satellite.velocity.y += dy / distance * force;
		}
		return distance;
	};
	return self;
});
// Satellite class for the player-controlled object
var Satellite = Container.expand(function () {
	var self = Container.call(this);
	// Create satellite graphics
	var satelliteGraphics = self.attachAsset('Planet2', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 0.3,
		scaleY: 0.3
	});
	// Physics properties
	self.velocity = {
		x: 0,
		y: 0
	};
	self.lastX = 0;
	self.lastY = 0;
	self.isDragging = false;
	self.thrustDirection = {
		x: 0,
		y: 0
	};
	self.thrustPower = 0;
	self.maxThrust = 0.5;
	// Trail effect
	self.trailPoints = [];
	self.maxTrailPoints = 50;
	// Show thrust line when dragging
	self.thrustLine = self.addChild(new Container());
	// Initialize with a small orbit velocity
	self.initOrbit = function (planetX, planetY) {
		// Set initial position
		self.x = planetX;
		self.y = planetY - 300;
		self.lastX = self.x;
		self.lastY = self.y;
		// Set initial velocity for orbit (perpendicular to planet)
		self.velocity.x = 2;
		self.velocity.y = 0;
	};
	// Update satellite position based on physics
	self.update = function () {
		// Store last position
		self.lastX = self.x;
		self.lastY = self.y;
		// Add thrust if dragging
		if (self.isDragging && self.thrustPower > 0) {
			self.velocity.x += self.thrustDirection.x * self.thrustPower;
			self.velocity.y += self.thrustDirection.y * self.thrustPower;
		}
		// Update position based on velocity
		self.x += self.velocity.x;
		self.y += self.velocity.y;
		// Update trail
		self.updateTrail();
		// Update thrust visualization
		self.updateThrustLine();
	};
	// Add a point to the trail (less frequently for performance)
	self.updateTrail = function () {
		if (LK.ticks % 8 === 0) {
			self.trailPoints.push({
				x: self.x,
				y: self.y
			});
			if (self.trailPoints.length > 25) {
				self.trailPoints.shift();
			}
		}
	};
	// Update the thrust line visualization
	self.updateThrustLine = function () {
		self.thrustLine.removeChildren();
		if (self.isDragging && self.thrustPower > 0) {
			// Create a line showing thrust direction
			var line = LK.getAsset('ThrustLine', {
				anchorX: 0,
				anchorY: 0.5,
				x: 0,
				y: 0
			});
			line.rotation = Math.atan2(-self.thrustDirection.y, -self.thrustDirection.x);
			line.scaleX = self.thrustPower * 50;
			self.thrustLine.addChild(line);
		}
	};
	// Start applying thrust
	self.startThrust = function (x, y) {
		self.isDragging = true;
		self.updateThrustVector(x, y);
	};
	// Update thrust direction and power while dragging
	self.updateThrustVector = function (x, y) {
		if (self.isDragging) {
			// Calculate thrust direction from satellite to touch point
			var dx = self.x - x;
			var dy = self.y - y;
			// Calculate distance
			var distance = Math.sqrt(dx * dx + dy * dy);
			if (distance > 0) {
				// Normalize direction vector
				self.thrustDirection.x = dx / distance;
				self.thrustDirection.y = dy / distance;
				// Scale thrust power by distance, capped at maxThrust
				self.thrustPower = Math.min(distance / 100, self.maxThrust);
			}
		}
	};
	// Stop applying thrust
	self.endThrust = function () {
		self.isDragging = false;
		self.thrustPower = 0;
		self.thrustDirection.x = 0;
		self.thrustDirection.y = 0;
	};
	return self;
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000
});
/**** 
* Game Code
****/ 
// Initialize game variables
var planet;
var satellite;
var orbitTrail;
var gameStarted = false;
var gameOver = false;
var timeInOrbit = 0;
var scoreText;
var statusText;
var backgroundAsteroids = [];
var backgroundStars = [];
var dragStartPos = {
	x: 0,
	y: 0
};
// Set up the game environment
function setupGame() {
	// Set space background
	game.setBackgroundColor(0x000022);
	// Create and position the planet in center
	planet = new Planet();
	planet.x = 2048 / 2;
	planet.y = 2732 / 2;
	planet.initOrbitZones();
	game.addChild(planet);
	// Add pulsing glow effect to planet
	function startPlanetGlow() {
		tween(planet, {
			alpha: 0.8
		}, {
			duration: 3000,
			easing: tween.easeInOut,
			onFinish: function onFinish() {
				tween(planet, {
					alpha: 1.0
				}, {
					duration: 3000,
					easing: tween.easeInOut,
					onFinish: startPlanetGlow
				});
			}
		});
	}
	startPlanetGlow();
	// Create satellite
	satellite = new Satellite();
	satellite.initOrbit(planet.x, planet.y);
	game.addChild(satellite);
	// Create orbit trail
	orbitTrail = new OrbitTrail();
	game.addChild(orbitTrail);
	// Create background effects
	// Add floating asteroids
	for (var i = 0; i < 8; i++) {
		var asteroid = new BackgroundAsteroid();
		asteroid.initMovement();
		backgroundAsteroids.push(asteroid);
		game.addChild(asteroid);
	}
	// Add twinkling stars
	for (var i = 0; i < 25; i++) {
		var star = new BackgroundStar();
		star.initTwinkle();
		backgroundStars.push(star);
		game.addChild(star);
	}
	// Create score display
	scoreText = new Text2('Time in orbit: 0', {
		size: 60,
		fill: 0xFFFFFF
	});
	scoreText.anchor.set(0.5, 0);
	scoreText.y = 50;
	LK.gui.top.addChild(scoreText);
	// Create status text
	statusText = new Text2('Tap and drag to apply thrust', {
		size: 50,
		fill: 0xFFFFFF
	});
	statusText.anchor.set(0.5, 1);
	statusText.y = -50;
	LK.gui.bottom.addChild(statusText);
	// Start the game
	gameStarted = true;
}
// Update game state each frame
game.update = function () {
	if (!gameStarted) {
		setupGame();
		return;
	}
	if (gameOver) return;
	// Update satellite physics
	satellite.update();
	// Apply planet's gravity to the satellite
	var distance = planet.applyGravityTo(satellite);
	// Check if satellite is in safe orbit
	var inSafeOrbit = distance >= planet.safeOrbitMin && distance <= planet.safeOrbitMax;
	// Update time in orbit if in safe zone
	if (inSafeOrbit) {
		if (LK.ticks % 60 === 0) {
			// Increase score every second
			timeInOrbit++;
			scoreText.setText('Time in orbit: ' + timeInOrbit);
			LK.setScore(timeInOrbit);
		}
		// Only update status text occasionally to reduce lag
		if (LK.ticks % 15 === 0) {
			statusText.setText('Stable orbit: ' + Math.round(distance));
			if (!statusText.isGreen) {
				statusText.style = {
					fill: 0x33FF33
				};
				statusText.isGreen = true;
				statusText.isRed = false;
				statusText.isOrange = false;
			}
		}
	} else if (distance < planet.safeOrbitMin) {
		if (LK.ticks % 15 === 0) {
			statusText.setText('Warning: Orbit too close!');
			if (!statusText.isRed) {
				statusText.style = {
					fill: 0xFF3333
				};
				statusText.isRed = true;
				statusText.isGreen = false;
				statusText.isOrange = false;
			}
		}
	} else if (distance > planet.safeOrbitMax) {
		if (LK.ticks % 15 === 0) {
			statusText.setText('Warning: Orbit too far!');
			if (!statusText.isOrange) {
				statusText.style = {
					fill: 0xFF9933
				};
				statusText.isOrange = true;
				statusText.isGreen = false;
				statusText.isRed = false;
			}
		}
	}
	// Check for crash into planet
	if (distance < 70) {
		// Planet radius is approximately 75px
		endGame("Crash landing!");
	}
	// Check if satellite has gone too far from the planet
	if (distance > 1500) {
		endGame("Satellite lost in space");
	}
	// Draw orbit trail
	orbitTrail.drawTrail(satellite.trailPoints);
};
// Handle touch/mouse down on game
game.down = function (x, y, obj) {
	if (gameOver) return;
	dragStartPos.x = x;
	dragStartPos.y = y;
	// Start applying thrust
	satellite.startThrust(x, y);
};
// Handle touch/mouse move on game
game.move = function (x, y, obj) {
	if (gameOver) return;
	// Update thrust vector
	satellite.updateThrustVector(x, y);
};
// Handle touch/mouse up on game
game.up = function (x, y, obj) {
	if (gameOver) return;
	// Stop applying thrust
	satellite.endThrust();
};
// End the game with a specific message
function endGame(message) {
	gameOver = true;
	statusText.setText(message);
	statusText.style = {
		fill: 0xFF0000
	};
	statusText.isRed = true;
	statusText.isGreen = false;
	statusText.isOrange = false;
	// Flash screen and show game over
	LK.effects.flashScreen(0xff0000, 1000);
	LK.setTimeout(function () {
		LK.showGameOver();
	}, 1500);
}
// Start background music
LK.playMusic('Bgmusic');
// Setup game
setupGame();