#pragma strict

var enemySpeed = 5.0f;

var projectile : GameObject;

var explosionParticleSystem : GameObject;

var players;

var navicellaWhite : Material;

var enemyProjectileSpeed;

function Start () {
	players = GameObject.FindGameObjectsWithTag("Player");
	
	InvokeRepeating("ShootProjectile", 1, 1.0f);
}

function Update () {

	for(var player : GameObject in players) {
		if(player != null) {
			if(this.renderer.bounds.Intersects(player.renderer.bounds)) {
				//Danneggia il giocatore
				player.GetComponent(Shoot).score -= 100;
				player.GetComponent(Shoot).Shake();
				Destroy(gameObject);
				Instantiate(player.GetComponent(Shoot).explosionParticleSystem, player.transform.position, player.transform.rotation);
				Debug.Log("Colpito");
				players = GameObject.FindGameObjectsWithTag("Player");
				break;
			}
		}
	}
	
	

	transform.position.z -= enemySpeed;
	
	if(transform.position.z < -200) {
		Destroy(gameObject);
	}
	
	if(transform.position.z < 0) {
		this.renderer.material = navicellaWhite;
	}
}

function ShootProjectile() {

	var projectileShot = Instantiate(projectile, transform.position, transform.rotation);
	projectileShot.GetComponent(EnemyProjectileRun).speed = enemyProjectileSpeed;

}

