#pragma strict

var enemyObj : GameObject;
var oloObj : GameObject;

var playerTarget : GameObject;

private var enemies;

var minSpawnTime = 0.2f;
var maxSpawnTime = 0.5f;

var enemyDelta_X = 300;
var enemyDelta_Y = 300;

var minX = -49;
var maxX = 54;
var minY = -9;
var maxY = 51;

var color = 0;

var enemySpeed = 5.0f;
var enemyProjectileSpeed = 10.0f;

var spawnFilter = 0.80f;

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

function Start () {
	InvokeRepeating("SpawnEnemy", 1, 0.2f);
	InvokeRepeating("SpawnEnemyNoFilter", 1, 2.0f);
	//InvokeRepeating("SpawnOlo", 1, 2.0f);
	
	//InvokeRepeating("ChangeColour", 20, 20.0f);
}

function SpawnEnemy() {

	if(playerTarget.GetComponent(Shoot).playing) {
		var val = Random.Range(0.0f, 1.0f);
		
		if(val > spawnFilter) {
			var enemyXpos = Random.Range(minX, maxX);
			var enemyYpos = Random.Range(minY, maxY);
			
			var enemyObjSpawned = Instantiate(enemyObj, new Vector3(enemyXpos, enemyYpos, transform.position.z), transform.rotation);
			enemyObjSpawned.GetComponent(Enemy).enemySpeed = enemySpeed;
			enemyObjSpawned.GetComponent(Enemy).enemyProjectileSpeed = enemyProjectileSpeed;
			enemyObjSpawned.renderer.material = GetMaterial(color);
		}
	}
}

function SpawnEnemyNoFilter() {

	if(playerTarget.GetComponent(Shoot).playing) {
		var enemyXpos = Random.Range(minX, maxX);
		var enemyYpos = Random.Range(minY, maxY);
		
		var enemyObjSpawned = Instantiate(enemyObj, new Vector3(enemyXpos, enemyYpos, transform.position.z), transform.rotation);
		enemyObjSpawned.GetComponent(Enemy).enemySpeed = enemySpeed;
		enemyObjSpawned.GetComponent(Enemy).enemyProjectileSpeed = enemyProjectileSpeed;
		enemyObjSpawned.renderer.material = GetMaterial(color);
	}
}

function SpawnOlo() {
	var index = 0;
	
	var enemyXpos = Random.Range(minX, maxX);
	var enemyYpos = Random.Range(minY, maxY);
	
	var oloObjSpawned = Instantiate(oloObj, new Vector3(enemyXpos, enemyYpos, transform.position.z), transform.rotation);
	//oloObjSpawned.renderer.material = GetMaterial(color);
}

function Update () {
	if(Input.GetKeyDown("l")) {
	
		SpawnOlo();
	
	}
}

function ChangeColour(dir) {

	if(dir == 'left') {
		color --;
	} else {
		color ++;
	}
	
	if(color == 4) {
		color = 0;
	} else if(color == -1) {
		color = 3;
	}
	
	enemies = GameObject.FindGameObjectsWithTag("Nemici");
	
	for(var enemy : GameObject in enemies) {
		if(enemy != null) {
			enemy.renderer.material = GetMaterial(color);
		}
	}

}

function GetMaterial(c) : Material {

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

	}

}