using UnityEngine;
using System.Collections;

public class SpawnerBehaviour : MonoBehaviour {
	
	bool hasSpawned = false;
	bool pairOne = false;
	bool pairTwo = false;
	bool pairThree = false;
	
	GameObject player1, player2;
	
	// Use this for initialization
	void Start () {
		player1 = GameObject.Find ("Player1");
		player2 = GameObject.Find ("Player2");
	}
	
	// Update is called once per frame
	void Update () {
		if (!hasSpawned) {
			pairOne = (Random.Range(0,2)==1);
			pairTwo = (Random.Range(0,2)==1);
			pairThree = (Random.Range(0,2)==1);
			GameObject target;
			if (pairOne)
				target = GameObject.Find("Alien1");
			else
				target = GameObject.Find("Alien2");
			target.renderer.enabled = true;


			if (pairTwo)
				target = GameObject.Find("Ship1");
			else
				target = GameObject.Find("Ship2");
			target.renderer.enabled = true;
			target.collider2D.enabled = true;
			if (target.name == "Ship1")
				GameObject.Find("Alien3").renderer.enabled = true;
			if (pairThree)
				target = GameObject.Find("Alien4");
			else
				target = GameObject.Find("Alien5");
			target.renderer.enabled = true;
			hasSpawned = true;
		}
		checkVictoryCondition();
	}
	
	void checkVictoryCondition() {
		CharacterBehaviour c1 = (CharacterBehaviour)player1.GetComponent(typeof(CharacterBehaviour));
		CharacterBehaviour c2 = (CharacterBehaviour)player2.GetComponent(typeof(CharacterBehaviour));
		if ((c1.State == States.KO || c2.State == States.KO) && (WinPlatform.p1win || WinPlatform.p2win))
			RefereeBehaviour.IsLevelOver = true;
		else if (WinPlatform.p1win && WinPlatform.p2win)
			RefereeBehaviour.IsLevelOver = true;
	}
}
