#pragma strict

var projectile : GameObject;

var shootRate = 0.5f;

var enemySpawnerTarget : GameObject;
private var enemySpawner : EnemySpawner;

var myCamera : Camera;
var explosionParticleSystem : GameObject;

var joystickPlayer : JoystickManager;

var scoreText : GameObject;

var playerId =0;
var speed = 10.0f;

var minX = -49.0f;
var maxX = 54.5f;
var minY = -9.0f;
var maxY = 51.0f;

var startQty = 50;
var ammo : float[] = new float[4];

var ammoDecrease = 0.5f;

var score = 0;
var playing = false;

//-----------------------------------------
var powerUp = 0;
var powerUpColor = 0;
var powerUpValue = 0;
//-----------------------------------------

var navicellaRed : Material;
var navicellaYellow : Material;
var navicellaGreen : Material;
var navicellaBlue : Material;
var navicellaWhite : Material;

function Start () {
	//gameObject.GetComponent(ParticleSystem).Stop();
//	playerId = this.playerId;
	enemySpawner = enemySpawnerTarget.GetComponent(EnemySpawner);
	joystickPlayer = new JoystickManager();
	InvokeRepeating("ShootProjectile", 0, shootRate);
	
	//InvokeRepeating("Shake", 0, 2.0f);
	
	for(var i = 0; i < 4; i++) {
		ammo[i] = startQty;
	}
	
	//Material navicellaYellow = (Material)Resources.Load("NavicellaYellow", typeof(Material));
}

function Update () {

	if(playing) {
		//transform.position.x += Input.GetAxis("Horizontal") * speed;
		//transform.position.y += Input.GetAxis("Vertical") * speed;
		transform.position.x += joystickPlayer.GetX(playerId) * speed;
		transform.position.y += joystickPlayer.GetY(playerId) * speed;
		
		var colorePremuto = joystickPlayer.checkButton(playerId);
		
		if(colorePremuto != -1) {
			UsePowerUp(colorePremuto);
		}
		
		if(transform.position.x > maxX) {
			transform.position.x = maxX;
		}
		if(transform.position.x < minX) {
			transform.position.x = minX;
		}
		if(transform.position.y > maxY) {
			transform.position.y = maxY;
		}
		if(transform.position.y < minY) {
			transform.position.y = minY;
		}

		if(ammo[enemySpawner.color] > ammoDecrease) {
			this.renderer.material = GetMaterial(enemySpawner.color);
		}
		
		if(score < 0) {
			score = 0;
		}
		
		scoreText.GetComponent(TextMesh).text = "" + score;
	}
}

function ShootProjectile() {

	if(playing) {
		

		if(ammo[enemySpawner.color] > ammoDecrease) {
			var projectileShot = Instantiate(projectile, transform.position, transform.rotation);
			projectileShot.GetComponent(ProjectileRun).spawnedFrom = gameObject;
			
			for(var i = 0; i < 4; i++) {
				ammo[i] -= ammoDecrease;
				if(ammo[i] < 0) {
					ammo[i] = 0;
				}
			}
		
			projectileShot.renderer.material = GetMaterial(enemySpawner.color);
		} else {
			//projectileShot.GetComponent(ProjectileRun).uneffective = true;
			//projectileShot.renderer.material = GetMaterial(4);
			
			gameObject.renderer.material = GetMaterial(4);
		}
	}
	
}

function UsePowerUp(col) {
	switch(powerUp) {
		case 0:
			break;
		case 1:
			var enemySpawners = GameObject.FindGameObjectsWithTag("EnemySpawners");
			for(var enemySpawner : GameObject in enemySpawners) {
				if(enemySpawner.GetComponent(EnemySpawner).color == col) {
					Debug.Log(enemySpawner.name);
					var playerTarget = enemySpawner.GetComponent(EnemySpawner).playerTarget;
					//playerTarget.GetComponent(Shoot).ammo[0] += 100;
					
					playerTarget.GetComponent(Shoot).ammo[powerUpColor] += powerUpValue;
					if(playerTarget.GetComponent(Shoot).ammo[powerUpColor] > 100)
						playerTarget.GetComponent(Shoot).ammo[powerUpColor] = 100;
						
					powerUp = 0;
					
				}
			}
			break;
	}

}

function GetMaterial(c) : Material {

	switch(c) {
		case 0:
			return navicellaRed;
		case 1:
			return navicellaYellow;
		case 2:
			return navicellaGreen;
		case 3:
			return navicellaBlue;
		case 4:
			return navicellaWhite;

	}

}

function Shake() {

	myCamera.GetComponent(CameraShake).Shake();

}