using UnityEngine;
using System.Collections;

public class Spawner_Sc : MonoBehaviour {


	public GameObject[] EnemyType = new GameObject[4];
    public GameObject Mark;
	public int SpawnXRandomRange = 20;
	public int SpawnYRandomRange = 15;
    int SpawnYRandomPos;
    int SpawnXRandomPos;



    private float spawnTime = 1;
	private float spawnerRepeatTime = 100;
	private float spawnNumber;
    private float HalfLifeStart = 10;
    private float HalfLifeRepeat = 10;

	GameObject[] gos;




	// Use this for initialization
	void Start () {
        InvokeRepeating("HalfLife", HalfLifeStart, HalfLifeRepeat);
	}
	
	// Update is called once per frame
	void Update () {

		gos = GameObject.FindGameObjectsWithTag ("Enemy");



		if (gos.Length.Equals (0)) {
			Spawn ();
		}

	
	}

	void Spawn()
	{
        
		SpawnXRandomPos = Random.Range (SpawnXRandomRange * -1, SpawnXRandomRange);
		SpawnYRandomPos = Random.Range (SpawnYRandomRange * -1, SpawnYRandomRange);
		int EnemyTypeNumber = Random.Range (0, 4);

        //Debug.Log("SpawnXRandomPos=" + SpawnXRandomPos);
        //Debug.Log("SpawnYRandomPos=" + SpawnYRandomPos);
        var enemyClone = Instantiate (EnemyType[EnemyTypeNumber], new Vector3 (SpawnXRandomPos,SpawnYRandomPos,0), new Quaternion());
        //var MarkClone = Instantiate(Mark, new Vector3(SpawnXRandomPos, SpawnYRandomPos, 0), new Quaternion());
        
		enemyClone.name = "enemy" + spawnNumber;
		spawnNumber++;

	}

    void HalfLife()
    {
        float remove = spawnerRepeatTime / 5f;
        spawnerRepeatTime = spawnerRepeatTime - remove;
        InvokeRepeating("Spawn", spawnTime, spawnerRepeatTime);


    }

 


}
