/**** 
* Classes
****/ 
var AsteroidPowerup = Container.expand(function () {
	var self = Container.call(this);
	//var starGraphics = self.attachAsset('starpowerup', {
	self.r = Math.random() < 0.33 ? '1' : Math.random() < 0.5 ? '2' : '3';
	var starGraphics = self.attachAsset('asteroidpowerup' + self.r, {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.random() * (2 * Math.PI)
	});
	self.type = enums.ROCK;
	self.x = Math.random() * 2048;
	self.y = Math.random() * 2732;
	self.rotSpeed = 0.025 - Math.random() * 0.05;
	self.speed = Math.random() * 5;
	self.scaleUp = Math.random() < 0.5 ? true : false;
	self.scale.x = 0.5 + Math.random() * 0.4;
	self.scale.y = self.scale.x;
	self.scaleSpeed = 0;
	self.reset = function () {
		self.rotSpeed = 0.025 - Math.random() * 0.05;
		self.speed = Math.random() * 5;
		self.scaleUp = Math.random() < 0.5 ? true : false;
		self.scale.x = 0.5 + Math.random() * 0.4;
		self.scale.y = self.scale.x;
	};
});
var Astronaut = Container.expand(function () {
	var self = Container.call(this);
	var surfaceGraphics = self.attachAsset('athlete', {
		anchorX: 0.5,
		anchorY: 0.5,
		vely: 0
	});
	self._move_migrated = function () {
		self.x += 12;
		self.rotation += 0.1;
	};
	self.vely = 0;
	self.x = 1024;
	self.y = 818;
});
var CloseButton = Container.expand(function (shopInstance) {
	var self = Container.call(this);
	self.shopInstance = shopInstance;
	var itemBg = self.attachAsset('closeButton', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: 800
	});
	self.down = function (x, y, obj) {
		//shop.hide();
		//console.log('closebutton clicked, shopInstance is: ' + self.shopInstance);
		//self.shopInstance.hide();
		shop.visible = false;
		instructionTxt.setText('Tap to throw the javelin');
		instructionTxt.alpha = 1;
		//self.shopInstance.hide();
	};
});
// DustParticle class
var DustParticle = Container.expand(function () {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('particle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20 - 10;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
// GoreParticle class
var GoreParticle = Container.expand(function () {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('particle2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
// Assets will be automatically generated based on usage in the code.
// Javelin class
var Javelin = Container.expand(function () {
	var self = Container.call(this);
	var javelinGraphics1 = self.attachAsset('javelin', {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.PI / 4
	});
	var javelinGraphics2 = self.attachAsset('javelin', {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.PI / 4,
		scaleY: -1,
		scaleX: -1,
		x: 100
	});
	self.speedX = 0;
	self.speedY = 0;
	self.scale.x = 0.8;
	self.scale.y = 0.8;
	self.rotation = -0.30;
	self.orbitSpeed = 0.045;
	self.angle = 4.6;
	self.radius = 570; //670 - 20 * (100 / 20);
	//console.log('radius ' + self.radius);
	self._move_migrated = function () {
		//self.radius = 670 - 20 * (100 / 20);
		//self.angle = (self.angle || 0) + self.orbitSpeed;
		self.angle = (self.angle || 0) + speed;
		self.angle %= Math.PI * 2;
		self.x = moonSurface.x + self.radius * Math.cos(self.angle);
		self.y = moonSurface.y + self.radius * Math.sin(self.angle);
		self.rotation = self.angle + Math.PI / 2 + 0.2;
		//self.radius -= 0.075;
		self.radius -= Math.random() * 0.02 + 0.055;
		if (self.radius < 512) {
			landed = true;
			self.rotation += 0.05;
			createDustExplosion(self.x, self.y, 20);
			//instructionTxt.setText('Nice throw! ' + LK.getScore() + ' meters');
			instructionTxt.setText('Nice throw! ' + currentScore + ' meters');
			instructionTxt.alpha = 1;
		}
	};
	self.hitUpdate = function () {
		self.x += 12;
		self.rotation += 0.1;
	};
});
// Moon surface class for visual effect
var MoonSurface = Container.expand(function () {
	var self = Container.call(this);
	var surfaceGraphics = self.attachAsset('moonSurface', {
		anchorX: 0.5,
		anchorY: 0.5 // Anchor at the bottom
	});
	self.x = 1024; // Center horizontally
	self.y = 2732 / 2; // Position at the center of the screen
});
var RockParticle = Container.expand(function (type) {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('asteroidpowerup' + type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20 - 10;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
var ShopItem = Container.expand(function (id, price) {
	var self = Container.call(this);
	// Initialize shop item properties
	self.id = id;
	self.price = price;
	self.purchased = false;
	self.maxed = false;
	var itemBg = self.attachAsset('shopItemBackground', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Attach asset based on the item id
	var itemGraphic = self.attachAsset(self.id, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var nameTxt = new Text2('', {
		size: 38,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5
	});
	if (self.id == 'shopItem1') {
		nameTxt.setText('     Strength    \n Increases your\n throw distance');
	} else if (self.id == 'shopItem2') {
		nameTxt.setText('      Mana      \n More wandering\n stars appear');
	} else if (self.id == 'shopItem3') {
		nameTxt.setText('    Signals\n  Summon the\n  unknown');
	}
	nameTxt.x = -self.width / 2;
	nameTxt.y = -self.height / 2 - nameTxt.height;
	self.addChild(nameTxt);
	var costTxt = new Text2('', {
		size: 38,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5
	});
	costTxt.setText('' + self.price);
	costTxt.x = -30;
	costTxt.y = self.height / 2 - 65;
	self.addChild(costTxt);
	var upgradeButton = self.attachAsset('upgradeButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	upgradeButton.scale.x = upgradeButton.scale.y = 0.9;
	upgradeButton.y = 275;
	var bTxt = new Text2('', {
		size: 32,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5
	});
	bTxt.setText('UPGRADE');
	bTxt.x = -64;
	bTxt.y = self.height / 2 - 62;
	if (self.id == 'shopItem3') {
		bTxt.x = -30;
		bTxt.setText('BUY');
	}
	self.addChild(bTxt);
	// Add a click or tap event listener to handle item purchase
	/* 
	self.down = function (x, y, obj) {
		if (!self.purchased && coins >= self.price) {
			coins -= self.price;
			self.purchased = true;
			// Logic to apply the purchased item effect
			console.log(self.id + " purchased!");
			// Optionally, update the item's appearance to indicate it's purchased
		} else {
			console.log("Not enough coins or item already purchased.");
		}
	};*/
	// Method to reset the item's purchase state (optional, depending on game design)
	self.reset = function () {
		self.purchased = false;
		// Optionally, update the item's appearance to indicate it's not purchased
	};
	self.updateCost = function () {
		//console.log('updateCost called');
		if (self.maxed) {
			costTxt.setText('MAX');
			upgradeButton.destroy();
			bTxt.destroy();
		} else {
			costTxt.setText('' + self.price);
			if (coins < self.price) {
				//todo: maybe grey out.	
			}
		}
	};
});
var ShopWindow = Container.expand(function () {
	var self = Container.call(this);
	// Initialize shop window properties and background graphic
	self.items = []; // Array to hold shop items
	self.x = 1024; // Center horizontally
	self.y = 1566; // Center vertically
	self.visible = false; // Initially hidden
	self.scale.x = 1.05;
	self.scale.y = 1.05;
	// Add background graphic to shop window
	var backgroundGraphic = self.attachAsset('shopBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: 0
	});
	self.addChild(backgroundGraphic);
	var item1 = new ShopItem('shopItem1', 10);
	item1.x = -280;
	item1.y = -20;
	var item2 = new ShopItem('shopItem2', 15);
	item2.x = 0;
	item2.y = -20;
	var item3 = new ShopItem('shopItem3', 100);
	item3.x = 280;
	item3.y = -20;
	self.addChild(item1);
	self.addChild(item2);
	self.addChild(item3);
	item1.down = function (x, y, obj) {
		//console.log('item1 clicked, price =', item1.price);
		if (coins >= item1.price && !item1.maxed) {
			coins -= item1.price;
			item1.price *= 2;
			if (item1.price >= 640) {
				item1.maxed = true;
			}
			item1.updateCost();
			updateCoinsText();
			midSpeed += 0.004;
			radiusUpdates++;
		}
	};
	item2.down = function (x, y, obj) {
		//console.log('item2 clicked, price =', item2.price);
		if (coins >= item2.price && !item2.maxed) {
			coins -= item2.price;
			item2.price *= 2;
			if (item2.price >= 240) {
				item2.maxed = true;
			}
			item2.updateCost();
			updateCoinsText();
			for (var i = 0; i < 5; i++) {
				var starPowerup = new StarPowerup();
				starpowerups.push(starPowerup);
				game.addChild(starPowerup);
			}
			game.addChild(shop); // Ensure shop layer is on top of the new starpowerups
		}
	};
	item3.down = function (x, y, obj) {
		if (coins >= item3.price && !item3.maxed) {
			coins -= item3.price;
			item3.maxed = true;
			item3.updateCost();
			updateCoinsText();
			/* 
			UFO_ON = true;
			var ufo = new Ufo();
			ufo.x = 1000;
			ufo.y = 1300;
			game.addChild(ufo);
			*/
			createUfo();
			game.addChild(shop); // Ensure shop layer is on top of the new starpowerups
		}
	};
	var shopTxt = new Text2('SHOP\nHere you can spend the Star Coins you earned.', {
		size: 80,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5,
		align: 'center'
	});
	shopTxt.anchor.set(0.5, 0.5); // Sets anchor to the center of the bottom edge of the text.
	shopTxt.y = -self.height / 2 + 50;
	self.addChild(shopTxt);
	var closeButton = new CloseButton(self);
	/*var closeButton = self.attachAsset('closeButton', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: 800
	});*/
	self.addChild(closeButton);
	//closeButton.down = function (x, y, obj) {
	// Method to show the shop window
	self.show = function () {
		self.visible = true;
		// Additional logic to animate the shop window appearance could be added here
	};
	// Method to hide the shop window
	self.hide = function () {
		self.visible = false;
		// Additional logic to animate the shop window disappearance could be added here
	};
});
var StarParticle = Container.expand(function (type) {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('starpowerup' + type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20 - 10;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
var StarPowerup = Container.expand(function () {
	var self = Container.call(this);
	self.r = Math.random() < 0.5 ? '' : '1';
	var starGraphics = self.attachAsset('starpowerup' + self.r, {
		//var starGraphics = self.attachAsset('starpowerup', {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.random() * (2 * Math.PI)
	});
	self.type = enums.STAR;
	self.x = Math.random() * 2048;
	self.y = Math.random() * 2732;
	self.rotSpeed = 0.1 - Math.random() * 0.2;
	self.speed = Math.random() * 5;
	self.scaleUp = Math.random() < 0.5 ? true : false;
	self.scale.x = 0.4 + Math.random();
	self.scale.y = self.scale.x;
	self.scaleSpeed = 0.01 + Math.random() * 0.02;
	self.alpha = 1; //0.75;
	self.reset = function () {
		self.rotSpeed = 0.1 - Math.random() * 0.2;
		self.speed = Math.random() * 5;
		self.scaleUp = Math.random() < 0.5 ? true : false;
		self.scale.x = 0.4 + Math.random();
		self.scale.y = self.scale.x;
		self.scaleSpeed = 0.01 + Math.random() * 0.02;
		self.y = Math.random() * 2732;
	};
});
var Ufo = Container.expand(function () {
	var self = Container.call(this);
	var ufoGraphics = self.attachAsset('ufoimage', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.moveScale = 0;
	self.move_temp = function () {
		var t = LK.ticks / 100;
		//self.scale = 1200 / (3 - Math.cos(2 * t));
		self.moveScale = 1800 / (3 - Math.cos(2 * t));
		self.x = 1024 + self.moveScale * Math.cos(t);
		self.y = 1340 + self.moveScale * Math.sin(2 * t) / 2;
		//console.log(self.x, self.y);
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 // Init game with black background
});
/**** 
* Game Code
****/ 
// Initialize enums at the top of the Classes section to ensure it's defined before use
// Function to create a dust particle explosion
var enums = {
	STAR: 1,
	ROCK: 2
};
function updateCoinsText() {
	coinsTxt.setText('Star Coins: ' + coins);
}
function createDustExplosion(x, y, count) {
	for (var i = 0; i < count; i++) {
		var particle = new DustParticle();
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createImpactExplosion(x, y, count) {
	for (var i = 0; i < count; i++) {
		var particle = new GoreParticle();
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createStarExplosion(x, y, count, type) {
	for (var i = 0; i < count; i++) {
		var particle = new StarParticle(type);
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createRockExplosion(x, y, count, type) {
	for (var i = 0; i < count; i++) {
		var particle = new RockParticle(type);
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createUfo() {
	UFO_ON = true;
	if (ufo) {
		ufo.destroy();
	}
	ufo = new Ufo();
	ufo.x = 1000;
	ufo.y = 1300;
	game.addChild(ufo);
}
// Initialize important variables and arrays
var ufo;
var javelins = [];
var starpowerups = [];
var background = game.attachAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 974,
	y: 1366,
	alpha: 0.55
});
var moonSurface = game.addChild(new MoonSurface());
// Add the astronaut to the scene
var astronaut = game.addChild(new Astronaut());
// Add the game logo to the scene
var gameLogo = game.attachAsset('gameLogo', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 - 250,
	y: 250,
	scaleX: 0.7,
	scaleY: 0.7
});
game.addChild(gameLogo);
var javelin = game.addChild(new Javelin());
javelin.x = astronaut.x - 70;
javelin.y = astronaut.y;
var radiusUpdates = 0;
var started = false;
var thrown = false;
var STARS_ON = !false;
var ASTEROIDS_ON = false;
var UFO_ON = false;
var particles = [];
var coins = 0;
var currentScore = 0; // Reset to 0 for each succesive throw
var playerBest = 0; // The Best score achieved by player.
if (STARS_ON) {
	for (var i = 0; i < 15; i++) {
		var starPowerup = new StarPowerup();
		starpowerups.push(starPowerup);
		game.addChild(starPowerup);
	}
}
if (ASTEROIDS_ON) {
	for (var j = 0; j < 10; j++) {
		var asteroidPowerup = new AsteroidPowerup();
		starpowerups.push(asteroidPowerup);
		game.addChild(asteroidPowerup);
	}
}
//game.addChild(ufo);
// Create a method to launch a javelin
function launchJavelin() {
	javelins.push(javelin);
	thrown = true;
}
// Add a text label at the bottom of the screen: "Tap to throw the javelin"
var instructionTxt = new Text2('Tap to throw the javelin', {
	size: 100,
	fill: "#ffffff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
instructionTxt.anchor.set(0.5, 3); // Sets anchor to the center of the bottom edge of the text.
LK.gui.bottom.addChild(instructionTxt);
// Create a score label at the top center of the screen
var scoreTxt = new Text2('0', {
	size: 100,
	fill: "#ffffff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
LK.gui.top.addChild(scoreTxt);
// Create a player best score label at the top center of the screen
var bestScoreTxt = new Text2('Best: 0', {
	size: 40,
	fill: "#ffffff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
bestScoreTxt.anchor.set(0.5, -2); // Sets anchor to the center of the top edge of the text.
LK.gui.top.addChild(bestScoreTxt);
var coinsTxt = new Text2('Star Coins: 0', {
	size: 50,
	fill: "#bbbbff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
coinsTxt.anchor.set(-0.2, -0.5); // Sets anchor to the center of the top edge of the text.
LK.gui.topLeft.addChild(coinsTxt);
// Create an action meter at the bottom of the screen
var actionMeter = new Container();
var actionMeterGraphics = actionMeter.attachAsset('actionMeter', {
	anchorX: 0.5,
	anchorY: 0.5
});
actionMeter.x = 1024; // Center horizontally
actionMeter.y = 2732 - 600; // Position at the bottom of the screen
actionMeter.alpha = 0;
game.addChild(actionMeter);
var actionMeterPointer = new Container();
var actionMeterPointerGraphics = actionMeterPointer.attachAsset('actionMeterPointer', {
	anchorX: 0.5,
	anchorY: 0.5,
	scaleX: 4,
	scaleY: 4
});
actionMeterPointer.x = 1024; // Center horizontally
actionMeterPointer.y = 2732 - 550;
actionMeterPointer.speed = 30;
actionMeterPointer.alpha = 0;
game.addChild(actionMeterPointer);
var midSpeed = 0.0225;
var minSpeed = 0.02;
var maxSpeed = 0.06; //0.045;
var speed = 0;
var landed = false;
var playerHit = false;
var landedCounter = 0;
var windSpeedX = 2;
var windSpeedY = 2;
/* 
if (UFO_ON) {
	var ufo = new Ufo();
	ufo.x = 1000;
	ufo.y = 1300;
	game.addChild(ufo);
}*/ 
var shop = new ShopWindow();
game.addChild(shop);
//shop.show();
game.on('down', function (x, y, obj) {
	if (shop.visible) {
		return;
	}
	if (!started) {
		started = true;
		//gameLogo.alpha = 0;
		instructionTxt.alpha = 0;
		actionMeter.alpha = 1;
		actionMeterPointer.alpha = 1;
	} else if (!thrown) {
		//speed = minSpeed + (actionMeterPointer.x - 450) / 1150 * (maxSpeed - minSpeed);
		var min = actionMeter.x - actionMeter.width / 2;
		var max = actionMeter.x + actionMeter.width / 2;
		speed = (actionMeterPointer.x - min) / (max - min);
		//console.log('speed1: ' + speed);
		speed *= midSpeed;
		//console.log('speed2: ' + speed);
		speed += minSpeed;
		//console.log('speed3: ' + speed);
		//speed = maxSpeed - speed;
		//console.log('speed ' + speed);
		launchJavelin();
		//instructionTxt.setText('Tap to jump');
		//instructionTxt.alpha = 1;
	} else {
		//if (astronaut.vely == 0) {
		if (!landed) {
			instructionTxt.alpha = 0;
		}
		if (astronaut.y >= 818) {
			astronaut.vely += 10;
		}
	}
});
var liftBoostActive = false;
var liftBoostCounter = 0;
var liftBoostResetCounter = 0;
//TODO: Mayeb half the starpowerups should travel behind the character and the moon, while the other half travel in front?
// Game tick event
LK.on('tick', function () {
	//windSpeedX = 10; //Math.sin(LK.ticks) * 20;
	//windSpeedX += Math.random() < 0.5 ? 1 : -1;
	if (UFO_ON && ufo) {
		//if (ufo.move_temp) {
		ufo.move_temp();
	}
	windSpeedY = Math.cos(LK.ticks / 30) * 2;
	var t;
	for (var i = starpowerups.length - 1; i >= 0; i--) {
		t = starpowerups[i];
		t.rotation += t.rotSpeed;
		t.x += windSpeedX + t.speed;
		t.y += windSpeedY + t.speed / 2;
		if (t.scaleUp) {
			/*t.scale.x += 0.1;
			t.scale.y += 0.1;*/
			t.scale.x += t.scaleSpeed;
			t.scale.y += t.scaleSpeed;
			if (t.scale.x > 1.4) {
				t.scaleUp = false;
			}
		} else if (!t.scaleUp) {
			/* 
			t.scale.x -= 0.1;
			t.scale.y -= 0.1;*/
			t.scale.x -= t.scaleSpeed;
			t.scale.y -= t.scaleSpeed;
			if (t.scale.x < 0.6) {
				t.scaleUp = true;
			}
		}
		if (t.x > 2100) {
			t.x = 0;
			t.reset();
		}
		if (t.y > 2800) {
			t.y = 0;
			t.reset();
		}
	}
	//ufo.move();
	if (started && !thrown) {
		actionMeterPointer.x += actionMeterPointer.speed;
		if (actionMeterPointer.x > 1600 || actionMeterPointer.x < 450) {
			actionMeterPointer.speed *= -1;
		}
	}
	if (thrown && !landed) {
		//if (astronaut.scale.x == 1 && LK.getScore() > 200) {
		if (astronaut.scale.x == 1 && currentScore > 200) {
			astronaut.scale.x = -1;
			instructionTxt.setText("Don't get hit! Tap to jump!");
			instructionTxt.alpha = 1;
			instructionTxt.y = -1200;
		}
		if (javelin.intersects(ufo)) {
			javelin.attachAsset('aliensurfer', {
				anchorY: 1
			});
			//UFO_ON = false;
			ufo.destroy();
		}
		for (var i = starpowerups.length - 1; i >= 0; i--) {
			t = starpowerups[i];
			if (javelin.intersects(t)) {
				if (t.type == enums.STAR) {
					createStarExplosion(t.x, t.y, 10, t.r);
					coins += 1;
					coinsTxt.setText('Star Coins: ' + coins);
					//speed += 0.001;
					//javelin.radius += 1;
					/* 
					if (liftBoostCounter < 20) {
						liftBoostActive = true;
						liftBoostCounter += 10;
						liftBoostResetCounter += 10;
					}*/ 
				} else if (t.type == enums.ROCK) {
					createRockExplosion(t.x, t.y, 10, t.r);
				}
				// Handle collision effect, e.g., increase score, create explosion, etc.
				//LK.setScore(LK.getScore() + 50); // Example score increase
				//t.destroy(); // Remove the starpowerup from the game
				//starpowerups.splice(i, 1); // Remove the starpowerup from the array
				t.x = 0;
				t.reset();
			}
		}
		if (liftBoostActive) {
			if (liftBoostCounter > 0) {
				liftBoostCounter--;
				//javelin.radius += 3;
				speed += 0.001;
			} else {
				lifeBoostCounter = 0;
				if (liftBoostResetCounter > 0) {
					liftBoostResetCounter--;
					//javelin.radius -= 3;
					speed -= 0.001;
				} else {
					liftBoostResetCounter = 0;
					liftBoostActive = false;
				}
			}
		}
		for (var i = javelins.length - 1; i >= 0; i--) {
			// Update the score and scorelabel text every frame when thrown is true.
			currentScore += Math.round(100 * speed);
			scoreTxt.setText(currentScore);
			if (currentScore > playerBest) {
				playerBest = currentScore;
				bestScoreTxt.setText('Best: ' + playerBest);
				LK.setScore(playerBest);
			}
			/* 
			LK.setScore(LK.getScore() + 100 * speed);
			scoreTxt.setText(LK.getScore());
			if (LK.getScore() > playerBest) {
				playerBest = LK.getScore();
				bestScoreTxt.setText('Best: ' + playerBest);
			}*/ 
			javelins[i]._move_migrated();
			//if (LK.getScore() > 300 && javelins[i].intersects(astronaut) && astronaut.y >= 818) {
			if (currentScore > 300 && javelins[i].intersects(astronaut) && astronaut.y >= 818) {
				landed = true;
				createImpactExplosion(astronaut.x, astronaut.y, 20);
				astronaut.removeChildAt(0);
				astronaut.attachAsset('athlete-hit', {
					anchorX: 0.5,
					anchorY: 0.5,
					rotation: Math.PI / 2
				});
				javelins[i]._move_migrated = javelins[i].hitUpdate;
				playerHit = true;
				//instructionTxt.setText('Uh oh :(\nTry again');
				instructionTxt.alpha = 1;
				instructionTxt.setText('Uh oh!');
			}
		}
		actionMeter.alpha -= 0.01;
		actionMeterPointer.alpha -= 0.01;
	}
	if (astronaut.vely > 0) {
		astronaut.y -= astronaut.vely;
		astronaut.vely -= 0.2;
		if (astronaut.vely < 0) {
			astronaut.vely = 0;
		}
	}
	if (astronaut.y < 818) {
		astronaut.y += 3;
	}
	if (landed && !playerHit) {
		landedCounter++;
		if (landedCounter >= 120) {
			// Show the shop and reset some vars for next throw.
			//LK.showGameOver();
			//instructionTxt.setText('SHOP\nHere you can spend the coins you collected.');
			landedCounter = 0;
			instructionTxt.alpha = 0;
			shop.show();
			//resetForNextThrow();
			started = false;
			thrown = false;
			landed = false;
			liftBoostActive = false;
			liftBoostCounter = 0;
			liftBoostResetCounter = 0;
			javelins = [];
			javelin.destroy();
			astronaut.y = 818;
			astronaut.vely = 0;
			astronaut.scale.x = 1;
			javelin = game.addChild(new Javelin());
			javelin.x = astronaut.x - 70;
			javelin.y = astronaut.y;
			javelin.radius += radiusUpdates;
			if (UFO_ON) {
				createUfo();
			}
			game.addChild(shop);
			currentScore = 0;
		}
	}
	if (playerHit) {
		javelin._move_migrated();
		astronaut._move_migrated();
		if (javelin.x > 2500) {
			LK.showGameOver();
		}
	}
	for (var i = particles.length - 1; i >= 0; i--) {
		particles[i]._update_migrated();
	}
}); /**** 
* Classes
****/ 
var AsteroidPowerup = Container.expand(function () {
	var self = Container.call(this);
	//var starGraphics = self.attachAsset('starpowerup', {
	self.r = Math.random() < 0.33 ? '1' : Math.random() < 0.5 ? '2' : '3';
	var starGraphics = self.attachAsset('asteroidpowerup' + self.r, {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.random() * (2 * Math.PI)
	});
	self.type = enums.ROCK;
	self.x = Math.random() * 2048;
	self.y = Math.random() * 2732;
	self.rotSpeed = 0.025 - Math.random() * 0.05;
	self.speed = Math.random() * 5;
	self.scaleUp = Math.random() < 0.5 ? true : false;
	self.scale.x = 0.5 + Math.random() * 0.4;
	self.scale.y = self.scale.x;
	self.scaleSpeed = 0;
	self.reset = function () {
		self.rotSpeed = 0.025 - Math.random() * 0.05;
		self.speed = Math.random() * 5;
		self.scaleUp = Math.random() < 0.5 ? true : false;
		self.scale.x = 0.5 + Math.random() * 0.4;
		self.scale.y = self.scale.x;
	};
});
var Astronaut = Container.expand(function () {
	var self = Container.call(this);
	var surfaceGraphics = self.attachAsset('athlete', {
		anchorX: 0.5,
		anchorY: 0.5,
		vely: 0
	});
	self._move_migrated = function () {
		self.x += 12;
		self.rotation += 0.1;
	};
	self.vely = 0;
	self.x = 1024;
	self.y = 818;
});
var CloseButton = Container.expand(function (shopInstance) {
	var self = Container.call(this);
	self.shopInstance = shopInstance;
	var itemBg = self.attachAsset('closeButton', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: 800
	});
	self.down = function (x, y, obj) {
		//shop.hide();
		//console.log('closebutton clicked, shopInstance is: ' + self.shopInstance);
		//self.shopInstance.hide();
		shop.visible = false;
		instructionTxt.setText('Tap to throw the javelin');
		instructionTxt.alpha = 1;
		//self.shopInstance.hide();
	};
});
// DustParticle class
var DustParticle = Container.expand(function () {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('particle', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20 - 10;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
// GoreParticle class
var GoreParticle = Container.expand(function () {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('particle2', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
// Assets will be automatically generated based on usage in the code.
// Javelin class
var Javelin = Container.expand(function () {
	var self = Container.call(this);
	var javelinGraphics1 = self.attachAsset('javelin', {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.PI / 4
	});
	var javelinGraphics2 = self.attachAsset('javelin', {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.PI / 4,
		scaleY: -1,
		scaleX: -1,
		x: 100
	});
	self.speedX = 0;
	self.speedY = 0;
	self.scale.x = 0.8;
	self.scale.y = 0.8;
	self.rotation = -0.30;
	self.orbitSpeed = 0.045;
	self.angle = 4.6;
	self.radius = 570; //670 - 20 * (100 / 20);
	//console.log('radius ' + self.radius);
	self._move_migrated = function () {
		//self.radius = 670 - 20 * (100 / 20);
		//self.angle = (self.angle || 0) + self.orbitSpeed;
		self.angle = (self.angle || 0) + speed;
		self.angle %= Math.PI * 2;
		self.x = moonSurface.x + self.radius * Math.cos(self.angle);
		self.y = moonSurface.y + self.radius * Math.sin(self.angle);
		self.rotation = self.angle + Math.PI / 2 + 0.2;
		//self.radius -= 0.075;
		self.radius -= Math.random() * 0.02 + 0.055;
		if (self.radius < 512) {
			landed = true;
			self.rotation += 0.05;
			createDustExplosion(self.x, self.y, 20);
			//instructionTxt.setText('Nice throw! ' + LK.getScore() + ' meters');
			instructionTxt.setText('Nice throw! ' + currentScore + ' meters');
			instructionTxt.alpha = 1;
		}
	};
	self.hitUpdate = function () {
		self.x += 12;
		self.rotation += 0.1;
	};
});
// Moon surface class for visual effect
var MoonSurface = Container.expand(function () {
	var self = Container.call(this);
	var surfaceGraphics = self.attachAsset('moonSurface', {
		anchorX: 0.5,
		anchorY: 0.5 // Anchor at the bottom
	});
	self.x = 1024; // Center horizontally
	self.y = 2732 / 2; // Position at the center of the screen
});
var RockParticle = Container.expand(function (type) {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('asteroidpowerup' + type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20 - 10;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
var ShopItem = Container.expand(function (id, price) {
	var self = Container.call(this);
	// Initialize shop item properties
	self.id = id;
	self.price = price;
	self.purchased = false;
	self.maxed = false;
	var itemBg = self.attachAsset('shopItemBackground', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	// Attach asset based on the item id
	var itemGraphic = self.attachAsset(self.id, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	var nameTxt = new Text2('', {
		size: 38,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5
	});
	if (self.id == 'shopItem1') {
		nameTxt.setText('     Strength    \n Increases your\n throw distance');
	} else if (self.id == 'shopItem2') {
		nameTxt.setText('      Mana      \n More wandering\n stars appear');
	} else if (self.id == 'shopItem3') {
		nameTxt.setText('    Signals\n  Summon the\n  unknown');
	}
	nameTxt.x = -self.width / 2;
	nameTxt.y = -self.height / 2 - nameTxt.height;
	self.addChild(nameTxt);
	var costTxt = new Text2('', {
		size: 38,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5
	});
	costTxt.setText('' + self.price);
	costTxt.x = -30;
	costTxt.y = self.height / 2 - 65;
	self.addChild(costTxt);
	var upgradeButton = self.attachAsset('upgradeButton', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	upgradeButton.scale.x = upgradeButton.scale.y = 0.9;
	upgradeButton.y = 275;
	var bTxt = new Text2('', {
		size: 32,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5
	});
	bTxt.setText('UPGRADE');
	bTxt.x = -64;
	bTxt.y = self.height / 2 - 62;
	if (self.id == 'shopItem3') {
		bTxt.x = -30;
		bTxt.setText('BUY');
	}
	self.addChild(bTxt);
	// Add a click or tap event listener to handle item purchase
	/* 
	self.down = function (x, y, obj) {
		if (!self.purchased && coins >= self.price) {
			coins -= self.price;
			self.purchased = true;
			// Logic to apply the purchased item effect
			console.log(self.id + " purchased!");
			// Optionally, update the item's appearance to indicate it's purchased
		} else {
			console.log("Not enough coins or item already purchased.");
		}
	};*/
	// Method to reset the item's purchase state (optional, depending on game design)
	self.reset = function () {
		self.purchased = false;
		// Optionally, update the item's appearance to indicate it's not purchased
	};
	self.updateCost = function () {
		//console.log('updateCost called');
		if (self.maxed) {
			costTxt.setText('MAX');
			upgradeButton.destroy();
			bTxt.destroy();
		} else {
			costTxt.setText('' + self.price);
			if (coins < self.price) {
				//todo: maybe grey out.	
			}
		}
	};
});
var ShopWindow = Container.expand(function () {
	var self = Container.call(this);
	// Initialize shop window properties and background graphic
	self.items = []; // Array to hold shop items
	self.x = 1024; // Center horizontally
	self.y = 1566; // Center vertically
	self.visible = false; // Initially hidden
	self.scale.x = 1.05;
	self.scale.y = 1.05;
	// Add background graphic to shop window
	var backgroundGraphic = self.attachAsset('shopBackground', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: 0
	});
	self.addChild(backgroundGraphic);
	var item1 = new ShopItem('shopItem1', 10);
	item1.x = -280;
	item1.y = -20;
	var item2 = new ShopItem('shopItem2', 15);
	item2.x = 0;
	item2.y = -20;
	var item3 = new ShopItem('shopItem3', 100);
	item3.x = 280;
	item3.y = -20;
	self.addChild(item1);
	self.addChild(item2);
	self.addChild(item3);
	item1.down = function (x, y, obj) {
		//console.log('item1 clicked, price =', item1.price);
		if (coins >= item1.price && !item1.maxed) {
			coins -= item1.price;
			item1.price *= 2;
			if (item1.price >= 640) {
				item1.maxed = true;
			}
			item1.updateCost();
			updateCoinsText();
			midSpeed += 0.004;
			radiusUpdates++;
		}
	};
	item2.down = function (x, y, obj) {
		//console.log('item2 clicked, price =', item2.price);
		if (coins >= item2.price && !item2.maxed) {
			coins -= item2.price;
			item2.price *= 2;
			if (item2.price >= 240) {
				item2.maxed = true;
			}
			item2.updateCost();
			updateCoinsText();
			for (var i = 0; i < 5; i++) {
				var starPowerup = new StarPowerup();
				starpowerups.push(starPowerup);
				game.addChild(starPowerup);
			}
			game.addChild(shop); // Ensure shop layer is on top of the new starpowerups
		}
	};
	item3.down = function (x, y, obj) {
		if (coins >= item3.price && !item3.maxed) {
			coins -= item3.price;
			item3.maxed = true;
			item3.updateCost();
			updateCoinsText();
			/* 
			UFO_ON = true;
			var ufo = new Ufo();
			ufo.x = 1000;
			ufo.y = 1300;
			game.addChild(ufo);
			*/
			createUfo();
			game.addChild(shop); // Ensure shop layer is on top of the new starpowerups
		}
	};
	var shopTxt = new Text2('SHOP\nHere you can spend the Star Coins you earned.', {
		size: 80,
		fill: "#ffffff",
		dropShadow: true,
		dropShadowColor: '#000000',
		dropShadowAngle: Math.PI / 6,
		dropShadowDistance: 5,
		align: 'center'
	});
	shopTxt.anchor.set(0.5, 0.5); // Sets anchor to the center of the bottom edge of the text.
	shopTxt.y = -self.height / 2 + 50;
	self.addChild(shopTxt);
	var closeButton = new CloseButton(self);
	/*var closeButton = self.attachAsset('closeButton', {
		anchorX: 0.5,
		anchorY: 0.5,
		x: 0,
		y: 800
	});*/
	self.addChild(closeButton);
	//closeButton.down = function (x, y, obj) {
	// Method to show the shop window
	self.show = function () {
		self.visible = true;
		// Additional logic to animate the shop window appearance could be added here
	};
	// Method to hide the shop window
	self.hide = function () {
		self.visible = false;
		// Additional logic to animate the shop window disappearance could be added here
	};
});
var StarParticle = Container.expand(function (type) {
	var self = Container.call(this);
	// Create a simple square shape for the particle
	var particleGraphics = self.attachAsset('starpowerup' + type, {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.width = 25 + Math.random() * 25;
	self.height = self.width;
	// Set initial speed and direction
	self.speedX = Math.random() * 20 - 10;
	self.speedY = Math.random() * 20 - 10;
	// Set the particle to move and fade out over time
	self._update_migrated = function () {
		self.x += self.speedX;
		self.y += self.speedY;
		self.speedX *= 0.95; // Reduce speed for spreading effect
		self.speedY *= 0.95; // Reduce speed for spreading effect
		particleGraphics.alpha *= 0.95; // Increase alpha reduction for quicker disappearance
		if (particleGraphics.alpha < 0.01) {
			self.destroy();
		}
	};
});
var StarPowerup = Container.expand(function () {
	var self = Container.call(this);
	self.r = Math.random() < 0.5 ? '' : '1';
	var starGraphics = self.attachAsset('starpowerup' + self.r, {
		//var starGraphics = self.attachAsset('starpowerup', {
		anchorX: 0.5,
		anchorY: 0.5,
		rotation: Math.random() * (2 * Math.PI)
	});
	self.type = enums.STAR;
	self.x = Math.random() * 2048;
	self.y = Math.random() * 2732;
	self.rotSpeed = 0.1 - Math.random() * 0.2;
	self.speed = Math.random() * 5;
	self.scaleUp = Math.random() < 0.5 ? true : false;
	self.scale.x = 0.4 + Math.random();
	self.scale.y = self.scale.x;
	self.scaleSpeed = 0.01 + Math.random() * 0.02;
	self.alpha = 1; //0.75;
	self.reset = function () {
		self.rotSpeed = 0.1 - Math.random() * 0.2;
		self.speed = Math.random() * 5;
		self.scaleUp = Math.random() < 0.5 ? true : false;
		self.scale.x = 0.4 + Math.random();
		self.scale.y = self.scale.x;
		self.scaleSpeed = 0.01 + Math.random() * 0.02;
		self.y = Math.random() * 2732;
	};
});
var Ufo = Container.expand(function () {
	var self = Container.call(this);
	var ufoGraphics = self.attachAsset('ufoimage', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.moveScale = 0;
	self.move_temp = function () {
		var t = LK.ticks / 100;
		//self.scale = 1200 / (3 - Math.cos(2 * t));
		self.moveScale = 1800 / (3 - Math.cos(2 * t));
		self.x = 1024 + self.moveScale * Math.cos(t);
		self.y = 1340 + self.moveScale * Math.sin(2 * t) / 2;
		//console.log(self.x, self.y);
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x000000 // Init game with black background
});
/**** 
* Game Code
****/ 
// Initialize enums at the top of the Classes section to ensure it's defined before use
// Function to create a dust particle explosion
var enums = {
	STAR: 1,
	ROCK: 2
};
function updateCoinsText() {
	coinsTxt.setText('Star Coins: ' + coins);
}
function createDustExplosion(x, y, count) {
	for (var i = 0; i < count; i++) {
		var particle = new DustParticle();
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createImpactExplosion(x, y, count) {
	for (var i = 0; i < count; i++) {
		var particle = new GoreParticle();
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createStarExplosion(x, y, count, type) {
	for (var i = 0; i < count; i++) {
		var particle = new StarParticle(type);
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createRockExplosion(x, y, count, type) {
	for (var i = 0; i < count; i++) {
		var particle = new RockParticle(type);
		particle.x = x;
		particle.y = y;
		game.addChild(particle);
		particles.push(particle);
	}
}
function createUfo() {
	UFO_ON = true;
	if (ufo) {
		ufo.destroy();
	}
	ufo = new Ufo();
	ufo.x = 1000;
	ufo.y = 1300;
	game.addChild(ufo);
}
// Initialize important variables and arrays
var ufo;
var javelins = [];
var starpowerups = [];
var background = game.attachAsset('background', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 974,
	y: 1366,
	alpha: 0.55
});
var moonSurface = game.addChild(new MoonSurface());
// Add the astronaut to the scene
var astronaut = game.addChild(new Astronaut());
// Add the game logo to the scene
var gameLogo = game.attachAsset('gameLogo', {
	anchorX: 0.5,
	anchorY: 0.5,
	x: 2048 - 250,
	y: 250,
	scaleX: 0.7,
	scaleY: 0.7
});
game.addChild(gameLogo);
var javelin = game.addChild(new Javelin());
javelin.x = astronaut.x - 70;
javelin.y = astronaut.y;
var radiusUpdates = 0;
var started = false;
var thrown = false;
var STARS_ON = !false;
var ASTEROIDS_ON = false;
var UFO_ON = false;
var particles = [];
var coins = 0;
var currentScore = 0; // Reset to 0 for each succesive throw
var playerBest = 0; // The Best score achieved by player.
if (STARS_ON) {
	for (var i = 0; i < 15; i++) {
		var starPowerup = new StarPowerup();
		starpowerups.push(starPowerup);
		game.addChild(starPowerup);
	}
}
if (ASTEROIDS_ON) {
	for (var j = 0; j < 10; j++) {
		var asteroidPowerup = new AsteroidPowerup();
		starpowerups.push(asteroidPowerup);
		game.addChild(asteroidPowerup);
	}
}
//game.addChild(ufo);
// Create a method to launch a javelin
function launchJavelin() {
	javelins.push(javelin);
	thrown = true;
}
// Add a text label at the bottom of the screen: "Tap to throw the javelin"
var instructionTxt = new Text2('Tap to throw the javelin', {
	size: 100,
	fill: "#ffffff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
instructionTxt.anchor.set(0.5, 3); // Sets anchor to the center of the bottom edge of the text.
LK.gui.bottom.addChild(instructionTxt);
// Create a score label at the top center of the screen
var scoreTxt = new Text2('0', {
	size: 100,
	fill: "#ffffff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
scoreTxt.anchor.set(0.5, 0); // Sets anchor to the center of the top edge of the text.
LK.gui.top.addChild(scoreTxt);
// Create a player best score label at the top center of the screen
var bestScoreTxt = new Text2('Best: 0', {
	size: 40,
	fill: "#ffffff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
bestScoreTxt.anchor.set(0.5, -2); // Sets anchor to the center of the top edge of the text.
LK.gui.top.addChild(bestScoreTxt);
var coinsTxt = new Text2('Star Coins: 0', {
	size: 50,
	fill: "#bbbbff",
	dropShadow: true,
	dropShadowColor: '#000000',
	dropShadowAngle: Math.PI / 6,
	dropShadowDistance: 5
});
coinsTxt.anchor.set(-0.2, -0.5); // Sets anchor to the center of the top edge of the text.
LK.gui.topLeft.addChild(coinsTxt);
// Create an action meter at the bottom of the screen
var actionMeter = new Container();
var actionMeterGraphics = actionMeter.attachAsset('actionMeter', {
	anchorX: 0.5,
	anchorY: 0.5
});
actionMeter.x = 1024; // Center horizontally
actionMeter.y = 2732 - 600; // Position at the bottom of the screen
actionMeter.alpha = 0;
game.addChild(actionMeter);
var actionMeterPointer = new Container();
var actionMeterPointerGraphics = actionMeterPointer.attachAsset('actionMeterPointer', {
	anchorX: 0.5,
	anchorY: 0.5,
	scaleX: 4,
	scaleY: 4
});
actionMeterPointer.x = 1024; // Center horizontally
actionMeterPointer.y = 2732 - 550;
actionMeterPointer.speed = 30;
actionMeterPointer.alpha = 0;
game.addChild(actionMeterPointer);
var midSpeed = 0.0225;
var minSpeed = 0.02;
var maxSpeed = 0.06; //0.045;
var speed = 0;
var landed = false;
var playerHit = false;
var landedCounter = 0;
var windSpeedX = 2;
var windSpeedY = 2;
/* 
if (UFO_ON) {
	var ufo = new Ufo();
	ufo.x = 1000;
	ufo.y = 1300;
	game.addChild(ufo);
}*/ 
var shop = new ShopWindow();
game.addChild(shop);
//shop.show();
game.on('down', function (x, y, obj) {
	if (shop.visible) {
		return;
	}
	if (!started) {
		started = true;
		//gameLogo.alpha = 0;
		instructionTxt.alpha = 0;
		actionMeter.alpha = 1;
		actionMeterPointer.alpha = 1;
	} else if (!thrown) {
		//speed = minSpeed + (actionMeterPointer.x - 450) / 1150 * (maxSpeed - minSpeed);
		var min = actionMeter.x - actionMeter.width / 2;
		var max = actionMeter.x + actionMeter.width / 2;
		speed = (actionMeterPointer.x - min) / (max - min);
		//console.log('speed1: ' + speed);
		speed *= midSpeed;
		//console.log('speed2: ' + speed);
		speed += minSpeed;
		//console.log('speed3: ' + speed);
		//speed = maxSpeed - speed;
		//console.log('speed ' + speed);
		launchJavelin();
		//instructionTxt.setText('Tap to jump');
		//instructionTxt.alpha = 1;
	} else {
		//if (astronaut.vely == 0) {
		if (!landed) {
			instructionTxt.alpha = 0;
		}
		if (astronaut.y >= 818) {
			astronaut.vely += 10;
		}
	}
});
var liftBoostActive = false;
var liftBoostCounter = 0;
var liftBoostResetCounter = 0;
//TODO: Mayeb half the starpowerups should travel behind the character and the moon, while the other half travel in front?
// Game tick event
LK.on('tick', function () {
	//windSpeedX = 10; //Math.sin(LK.ticks) * 20;
	//windSpeedX += Math.random() < 0.5 ? 1 : -1;
	if (UFO_ON && ufo) {
		//if (ufo.move_temp) {
		ufo.move_temp();
	}
	windSpeedY = Math.cos(LK.ticks / 30) * 2;
	var t;
	for (var i = starpowerups.length - 1; i >= 0; i--) {
		t = starpowerups[i];
		t.rotation += t.rotSpeed;
		t.x += windSpeedX + t.speed;
		t.y += windSpeedY + t.speed / 2;
		if (t.scaleUp) {
			/*t.scale.x += 0.1;
			t.scale.y += 0.1;*/
			t.scale.x += t.scaleSpeed;
			t.scale.y += t.scaleSpeed;
			if (t.scale.x > 1.4) {
				t.scaleUp = false;
			}
		} else if (!t.scaleUp) {
			/* 
			t.scale.x -= 0.1;
			t.scale.y -= 0.1;*/
			t.scale.x -= t.scaleSpeed;
			t.scale.y -= t.scaleSpeed;
			if (t.scale.x < 0.6) {
				t.scaleUp = true;
			}
		}
		if (t.x > 2100) {
			t.x = 0;
			t.reset();
		}
		if (t.y > 2800) {
			t.y = 0;
			t.reset();
		}
	}
	//ufo.move();
	if (started && !thrown) {
		actionMeterPointer.x += actionMeterPointer.speed;
		if (actionMeterPointer.x > 1600 || actionMeterPointer.x < 450) {
			actionMeterPointer.speed *= -1;
		}
	}
	if (thrown && !landed) {
		//if (astronaut.scale.x == 1 && LK.getScore() > 200) {
		if (astronaut.scale.x == 1 && currentScore > 200) {
			astronaut.scale.x = -1;
			instructionTxt.setText("Don't get hit! Tap to jump!");
			instructionTxt.alpha = 1;
			instructionTxt.y = -1200;
		}
		if (javelin.intersects(ufo)) {
			javelin.attachAsset('aliensurfer', {
				anchorY: 1
			});
			//UFO_ON = false;
			ufo.destroy();
		}
		for (var i = starpowerups.length - 1; i >= 0; i--) {
			t = starpowerups[i];
			if (javelin.intersects(t)) {
				if (t.type == enums.STAR) {
					createStarExplosion(t.x, t.y, 10, t.r);
					coins += 1;
					coinsTxt.setText('Star Coins: ' + coins);
					//speed += 0.001;
					//javelin.radius += 1;
					/* 
					if (liftBoostCounter < 20) {
						liftBoostActive = true;
						liftBoostCounter += 10;
						liftBoostResetCounter += 10;
					}*/ 
				} else if (t.type == enums.ROCK) {
					createRockExplosion(t.x, t.y, 10, t.r);
				}
				// Handle collision effect, e.g., increase score, create explosion, etc.
				//LK.setScore(LK.getScore() + 50); // Example score increase
				//t.destroy(); // Remove the starpowerup from the game
				//starpowerups.splice(i, 1); // Remove the starpowerup from the array
				t.x = 0;
				t.reset();
			}
		}
		if (liftBoostActive) {
			if (liftBoostCounter > 0) {
				liftBoostCounter--;
				//javelin.radius += 3;
				speed += 0.001;
			} else {
				lifeBoostCounter = 0;
				if (liftBoostResetCounter > 0) {
					liftBoostResetCounter--;
					//javelin.radius -= 3;
					speed -= 0.001;
				} else {
					liftBoostResetCounter = 0;
					liftBoostActive = false;
				}
			}
		}
		for (var i = javelins.length - 1; i >= 0; i--) {
			// Update the score and scorelabel text every frame when thrown is true.
			currentScore += Math.round(100 * speed);
			scoreTxt.setText(currentScore);
			if (currentScore > playerBest) {
				playerBest = currentScore;
				bestScoreTxt.setText('Best: ' + playerBest);
				LK.setScore(playerBest);
			}
			/* 
			LK.setScore(LK.getScore() + 100 * speed);
			scoreTxt.setText(LK.getScore());
			if (LK.getScore() > playerBest) {
				playerBest = LK.getScore();
				bestScoreTxt.setText('Best: ' + playerBest);
			}*/ 
			javelins[i]._move_migrated();
			//if (LK.getScore() > 300 && javelins[i].intersects(astronaut) && astronaut.y >= 818) {
			if (currentScore > 300 && javelins[i].intersects(astronaut) && astronaut.y >= 818) {
				landed = true;
				createImpactExplosion(astronaut.x, astronaut.y, 20);
				astronaut.removeChildAt(0);
				astronaut.attachAsset('athlete-hit', {
					anchorX: 0.5,
					anchorY: 0.5,
					rotation: Math.PI / 2
				});
				javelins[i]._move_migrated = javelins[i].hitUpdate;
				playerHit = true;
				//instructionTxt.setText('Uh oh :(\nTry again');
				instructionTxt.alpha = 1;
				instructionTxt.setText('Uh oh!');
			}
		}
		actionMeter.alpha -= 0.01;
		actionMeterPointer.alpha -= 0.01;
	}
	if (astronaut.vely > 0) {
		astronaut.y -= astronaut.vely;
		astronaut.vely -= 0.2;
		if (astronaut.vely < 0) {
			astronaut.vely = 0;
		}
	}
	if (astronaut.y < 818) {
		astronaut.y += 3;
	}
	if (landed && !playerHit) {
		landedCounter++;
		if (landedCounter >= 120) {
			// Show the shop and reset some vars for next throw.
			//LK.showGameOver();
			//instructionTxt.setText('SHOP\nHere you can spend the coins you collected.');
			landedCounter = 0;
			instructionTxt.alpha = 0;
			shop.show();
			//resetForNextThrow();
			started = false;
			thrown = false;
			landed = false;
			liftBoostActive = false;
			liftBoostCounter = 0;
			liftBoostResetCounter = 0;
			javelins = [];
			javelin.destroy();
			astronaut.y = 818;
			astronaut.vely = 0;
			astronaut.scale.x = 1;
			javelin = game.addChild(new Javelin());
			javelin.x = astronaut.x - 70;
			javelin.y = astronaut.y;
			javelin.radius += radiusUpdates;
			if (UFO_ON) {
				createUfo();
			}
			game.addChild(shop);
			currentScore = 0;
		}
	}
	if (playerHit) {
		javelin._move_migrated();
		astronaut._move_migrated();
		if (javelin.x > 2500) {
			LK.showGameOver();
		}
	}
	for (var i = particles.length - 1; i >= 0; i--) {
		particles[i]._update_migrated();
	}
});
:quality(85)/https://cdn.frvr.ai/661557c7581bcd5d64e47e6c.png%3F3) 
 pixelart. a beautiful moon.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/66155e6a581bcd5d64e47e8d.png%3F3) 
 pixelart a beautiful starry sky seen in empty outer space.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/6615610e581bcd5d64e47f0e.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/661561a9581bcd5d64e47f39.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/661561e0581bcd5d64e47f48.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6615650a581bcd5d64e47ff7.png%3F3) 
 pixelart. a javelin used for olympic games and athletics. Just the spear itself, horizontally laid out in the image. It should be a slim metal spear, pointy in both ends, and with a grip somewhere off center of the shaft.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/6617e01658043b89d4ba9a83.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6617e44058043b89d4ba9ab4.png%3F3) 
 pixelart. a metallic triangular pointer.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/661bbffe581bcd5d64e48bd8.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/661cf77f3c9c5fe61a4c435f.png%3F3) 
 pixelart lettering of the word 'LUNAR' with some blank space around it.
:quality(85)/https://cdn.frvr.ai/661ff191b4cc8b71379d4108.png%3F3) 
 replace inpainted area with transparency.
:quality(85)/https://cdn.frvr.ai/6620cadf29676213360ab702.png%3F3) 
 pixelart. asteroid with craters. subtle shading.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/6620f04c29676213360ab7d2.png%3F3) 
 pixelart. asteroid with craters. subtle shading.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/66223d3beff7d73bc2d1d290.png%3F3) 
 A small light yellow star.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/66223ef2eff7d73bc2d1d2b1.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/66227d52eff7d73bc2d1d454.png%3F3) 
 delete the inpainted areas.
:quality(85)/https://cdn.frvr.ai/6627be482c6f48349704d643.png%3F3) 
 Pixelart. A background window for an in-game shop, with a space theme. The center part should be a large blank area with space for the items and labels for sale in the game shop. The blank space could have the form of a black computer screen inside a spaceship.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/6627c19f2c6f48349704d66b.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6627c89c2c6f48349704d6b1.png%3F3) 
 Pixelart. An icon of a an arm holding javelin with fire around it.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.
:quality(85)/https://cdn.frvr.ai/6627cd4a2c6f48349704d6c7.png%3F3) 
 :quality(85)/https://cdn.frvr.ai/6627e2462c6f48349704d73d.png%3F3) 
 Turn in-painted area solid blue like the area around it.
:quality(85)/https://cdn.frvr.ai/662a22eb777dbb646cb325e9.png%3F3) 
 Pixelart. A rectangular silvery button with the text 'CLOSE'.. Single Game Texture. In-Game asset. 2d. Blank background. High contrast. No shadows.