#pragma strict
var laserspeed: float;
var direction: Vector3;
var smallAsteroid: Rigidbody;
var ran : float;
var score : SpaceshipController;
var enemyspaceship1lives: int;
var lives : SpaceshipController;

function Start ()
{
	enemyspaceship1lives = 5;
	score = GameObject.FindGameObjectWithTag("Player").GetComponent(SpaceshipController);
	lives = GameObject.FindGameObjectWithTag("Player").GetComponent(SpaceshipController);
		
	if (this.tag == "FriendlyLaser")
	{
		direction = Vector3.down;
	}
	else
	{
		direction = Vector3.left;
	}
}

function Update () {
	transform.Translate(direction * 10 * Time.deltaTime);
	
	if (this.transform.position.x > MovementController.Rightmost() || this.transform.position.x < MovementController.Leftmost())
	{
		GameObject.Destroy(this.gameObject);
	}
}

function OnTriggerEnter(otherObject:Collider)
{
	if (this.tag == "FriendlyLaser")
	{
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "Asteroid")
		{
			ran = Random.RandomRange(0f, 2f);
			
			for(var i : int=0; i < 2; i++)
			{
				var asteroid: Rigidbody;
				asteroid = Instantiate(smallAsteroid, gameObject.transform.position, gameObject.transform.rotation);
				var ac: AsteroidController = asteroid.gameObject.GetComponent(AsteroidController);
			
				ac.speedY = ran;
				ran = -ran;
			}
	
			score.score = score.score + 1; 
			Destroy(this.gameObject);
			Destroy(otherObject.gameObject);
		}
		
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "SmallAsteroid")
		{
			score.score = score.score + 2;
			Destroy(this.gameObject);
			Destroy(otherObject.gameObject);
		}
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "EnemyLaser")
		{
			Destroy(this.gameObject);
			Destroy(otherObject.gameObject);
		}
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "EnemySpaceShip1")
		{
			otherObject.transform.SendMessage("DecreaseHealth");
			Destroy(this.gameObject);

		}
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "Boss1Right")
		{
			otherObject.transform.SendMessage("DecreaseWingsHealth");
			Destroy(this.gameObject);
		}
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "Boss1Left")
		{
			otherObject.transform.SendMessage("DecreaseWingsHealth");
			Destroy(this.gameObject);
		}
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "Boss1Mid")
		{
			otherObject.transform.SendMessage("DecreaseMiddleHealth");
			Destroy(this.gameObject);
		}
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "EnemySpaceShip2")
		{
			score.score = score.score + 3; 
			Destroy(otherObject.gameObject);
			Destroy(this.gameObject);
		}
		if (otherObject.gameObject.tag != "Player" && otherObject.gameObject.tag == "Boss2")
		{
			otherObject.transform.SendMessage("DecreaseBossHealth");
			Destroy(this.gameObject);
		}
	}
	if (this.tag == "EnemyLaser")
	{
		if (otherObject.gameObject.tag != "EnemySpaceShip1" && otherObject.gameObject.tag == "Player")
		{
			lives.lives = lives.lives - 1;
			GameObject.Destroy(this.gameObject);
			if (lives.lives <= 0)
			{	
				Application.LoadLevel("GameOverScreen");
			}	
		}
	}
}