/**** 
* Classes
****/ 
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('enemyBullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.move = function () {
		self.y += self.speed;
	};
});
// Assets will be automatically generated based on usage in the code.
// Hero class
var Hero = Container.expand(function () {
	var self = Container.call(this);
	var heroGraphics = self.attachAsset('hero', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 2,
		scaleY: 2
	});
	heroGraphics.tint = 0xFF69B4;
	self.move = function (x, y) {
		self.x = x;
		self.y = y;
	};
	self.on('down', function (obj) {
		self.shoot();
	});
	self.shoot = function () {
		var newRay = new HeroRay();
		newRay.x = self.x;
		newRay.y = self.y;
		heroRays.push(newRay);
		game.addChild(newRay);
	};
});
// HeroRay class
var HeroRay = Container.expand(function () {
	var self = Container.call(this);
	var rayGraphics = self.attachAsset('heroRay', {
		anchorX: 0.5,
		anchorY: 0.5,
		shape: 'box',
		scaleX: 0.5,
		scaleY: 0.5
	});
	self.speed = -5;
	self.move = function () {
		self.y += self.speed;
		self.rotation += 0.1;
	};
});
// NeonGreenBall class
var NeonGreenBall = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('neonGreenBall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.move = function () {
		self.y += self.speed;
	};
});
// NeonGreenText class
var NeonGreenText = Container.expand(function () {
	var self = Container.call(this);
	var textGraphics = self.attachAsset('neonGreenText', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.move = function () {
		self.y += self.speed;
	};
});
// SurpriseOrangeBall class
var SurpriseOrangeBall = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('surpriseOrangeBall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	self.move = function () {
		self.y += self.speed;
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x006400 // Init game with dark green background
});
/**** 
* Game Code
****/ 
// Global variables
var hero = new Hero();
hero.x = 1024; // Center horizontally
hero.y = 2500; // Near the bottom
// Add hero to the game
game.addChild(hero);
// Create a new Text2 object for the score display with white color and blue border
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff",
	stroke: "#0000ff",
	strokeThickness: 5
});
scoreTxt.anchor.set(1, 0); // Sets anchor to the top right of the text.
LK.gui.topRight.addChild(scoreTxt); // Add the score text to the top right of the GUI overlay.
// Touch event to move hero and destroy neon green balls
var dragNode = null;
game.on('down', function (obj) {
	dragNode = hero;
});
game.on('down', function (obj) {
	if (dragNode) {
		var pos = obj.event.getLocalPosition(game);
		dragNode.move(pos.x, dragNode.y);
		var newRay = new HeroRay();
		newRay.x = pos.x;
		newRay.y = pos.y;
		heroRays.push(newRay);
		game.addChild(newRay);
	}
});
game.on('up', function (obj) {
	dragNode = null;
});
// Global variables
var neonGreenBalls = [];
var heroRays = [];
// Game tick event
LK.on('tick', function () {
	// Move neon green balls and hero rays
	for (var i = neonGreenBalls.length - 1; i >= 0; i--) {
		neonGreenBalls[i].move();
		if (neonGreenBalls[i].y > 2732 || hero.intersects(neonGreenBalls[i])) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
		}
	}
	for (var i = heroRays.length - 1; i >= 0; i--) {
		heroRays[i].move();
		if (heroRays[i].y < 0) {
			heroRays[i].destroy();
			heroRays.splice(i, 1);
		}
		for (var j = neonGreenBalls.length - 1; j >= 0; j--) {
			if (heroRays[i] && heroRays[i].intersects(neonGreenBalls[j])) {
				neonGreenBalls[j].destroy();
				neonGreenBalls.splice(j, 1);
				heroRays[i].destroy();
				heroRays.splice(i, 1);
				LK.setScore(LK.getScore() + 1);
				scoreTxt.setText(LK.getScore());
			}
		}
	}
	// Spawn neon green balls
	if (LK.ticks % 60 == 0) {
		var newBall = new NeonGreenBall();
		newBall.x = Math.random() * 2048;
		newBall.y = 0;
		// Increase the speed of the balls every ten points
		if (LK.getScore() % 10 == 0) {
			newBall.speed = 5 + Math.floor(LK.getScore() / 10) * 5;
			// Spawn a surprise orange ball every ten points only after ten points have been added
			if (LK.getScore() >= 10) {
				var surpriseBall = new SurpriseOrangeBall();
				surpriseBall.x = Math.random() * 2048;
				surpriseBall.y = 0;
				// Increase the speed of the orange balls every twenty points
				if (LK.getScore() % 20 == 0) {
					surpriseBall.speed += Math.floor(LK.getScore() / 20);
				}
				neonGreenBalls.push(surpriseBall);
				game.addChild(surpriseBall);
			}
		} else {
			newBall.speed = 5;
		}
		neonGreenBalls.push(newBall);
		game.addChild(newBall);
	}
}); /**** 
* Classes
****/ 
// EnemyBullet class
var EnemyBullet = Container.expand(function () {
	var self = Container.call(this);
	var bulletGraphics = self.attachAsset('enemyBullet', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.move = function () {
		self.y += self.speed;
	};
});
// Assets will be automatically generated based on usage in the code.
// Hero class
var Hero = Container.expand(function () {
	var self = Container.call(this);
	var heroGraphics = self.attachAsset('hero', {
		anchorX: 0.5,
		anchorY: 0.5,
		scaleX: 2,
		scaleY: 2
	});
	heroGraphics.tint = 0xFF69B4;
	self.move = function (x, y) {
		self.x = x;
		self.y = y;
	};
	self.on('down', function (obj) {
		self.shoot();
	});
	self.shoot = function () {
		var newRay = new HeroRay();
		newRay.x = self.x;
		newRay.y = self.y;
		heroRays.push(newRay);
		game.addChild(newRay);
	};
});
// HeroRay class
var HeroRay = Container.expand(function () {
	var self = Container.call(this);
	var rayGraphics = self.attachAsset('heroRay', {
		anchorX: 0.5,
		anchorY: 0.5,
		shape: 'box',
		scaleX: 0.5,
		scaleY: 0.5
	});
	self.speed = -5;
	self.move = function () {
		self.y += self.speed;
		self.rotation += 0.1;
	};
});
// NeonGreenBall class
var NeonGreenBall = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('neonGreenBall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.move = function () {
		self.y += self.speed;
	};
});
// NeonGreenText class
var NeonGreenText = Container.expand(function () {
	var self = Container.call(this);
	var textGraphics = self.attachAsset('neonGreenText', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 5;
	self.move = function () {
		self.y += self.speed;
	};
});
// SurpriseOrangeBall class
var SurpriseOrangeBall = Container.expand(function () {
	var self = Container.call(this);
	var ballGraphics = self.attachAsset('surpriseOrangeBall', {
		anchorX: 0.5,
		anchorY: 0.5
	});
	self.speed = 10;
	self.move = function () {
		self.y += self.speed;
	};
});
/**** 
* Initialize Game
****/ 
var game = new LK.Game({
	backgroundColor: 0x006400 // Init game with dark green background
});
/**** 
* Game Code
****/ 
// Global variables
var hero = new Hero();
hero.x = 1024; // Center horizontally
hero.y = 2500; // Near the bottom
// Add hero to the game
game.addChild(hero);
// Create a new Text2 object for the score display with white color and blue border
var scoreTxt = new Text2('0', {
	size: 150,
	fill: "#ffffff",
	stroke: "#0000ff",
	strokeThickness: 5
});
scoreTxt.anchor.set(1, 0); // Sets anchor to the top right of the text.
LK.gui.topRight.addChild(scoreTxt); // Add the score text to the top right of the GUI overlay.
// Touch event to move hero and destroy neon green balls
var dragNode = null;
game.on('down', function (obj) {
	dragNode = hero;
});
game.on('down', function (obj) {
	if (dragNode) {
		var pos = obj.event.getLocalPosition(game);
		dragNode.move(pos.x, dragNode.y);
		var newRay = new HeroRay();
		newRay.x = pos.x;
		newRay.y = pos.y;
		heroRays.push(newRay);
		game.addChild(newRay);
	}
});
game.on('up', function (obj) {
	dragNode = null;
});
// Global variables
var neonGreenBalls = [];
var heroRays = [];
// Game tick event
LK.on('tick', function () {
	// Move neon green balls and hero rays
	for (var i = neonGreenBalls.length - 1; i >= 0; i--) {
		neonGreenBalls[i].move();
		if (neonGreenBalls[i].y > 2732 || hero.intersects(neonGreenBalls[i])) {
			LK.effects.flashScreen(0xff0000, 1000);
			LK.showGameOver();
		}
	}
	for (var i = heroRays.length - 1; i >= 0; i--) {
		heroRays[i].move();
		if (heroRays[i].y < 0) {
			heroRays[i].destroy();
			heroRays.splice(i, 1);
		}
		for (var j = neonGreenBalls.length - 1; j >= 0; j--) {
			if (heroRays[i] && heroRays[i].intersects(neonGreenBalls[j])) {
				neonGreenBalls[j].destroy();
				neonGreenBalls.splice(j, 1);
				heroRays[i].destroy();
				heroRays.splice(i, 1);
				LK.setScore(LK.getScore() + 1);
				scoreTxt.setText(LK.getScore());
			}
		}
	}
	// Spawn neon green balls
	if (LK.ticks % 60 == 0) {
		var newBall = new NeonGreenBall();
		newBall.x = Math.random() * 2048;
		newBall.y = 0;
		// Increase the speed of the balls every ten points
		if (LK.getScore() % 10 == 0) {
			newBall.speed = 5 + Math.floor(LK.getScore() / 10) * 5;
			// Spawn a surprise orange ball every ten points only after ten points have been added
			if (LK.getScore() >= 10) {
				var surpriseBall = new SurpriseOrangeBall();
				surpriseBall.x = Math.random() * 2048;
				surpriseBall.y = 0;
				// Increase the speed of the orange balls every twenty points
				if (LK.getScore() % 20 == 0) {
					surpriseBall.speed += Math.floor(LK.getScore() / 20);
				}
				neonGreenBalls.push(surpriseBall);
				game.addChild(surpriseBall);
			}
		} else {
			newBall.speed = 5;
		}
		neonGreenBalls.push(newBall);
		game.addChild(newBall);
	}
});