using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyGenerator : MonoBehaviour {

//	public GameObject enemy;
	Vector3 GeneratorPosition;
	public float startTime;
	public float generateTime;
	public int count = 0;
	private Rigidbody2D myRb;

	private bool isRight = true;
	private bool isLeft = false;
	public float speed = 5f;

	public GameObject[] myEnemies = new GameObject[5];
	public GameObject red;
	public GameObject blue;
	public GameObject green;
	public GameObject yellow;
	public GameObject colour;

	void Start () {
		startTime = Time.time;
		generateTime = startTime + 2f;
		myRb = GetComponent<Rigidbody2D>();

		myEnemies [0] = red;
		myEnemies [1] = blue;
		myEnemies [2] = green;
		myEnemies [3] = yellow;
		myEnemies [4] = colour;
	}

	void Update () {
		
		int enemy = Random.Range (0,4);

		if (Time.time >= startTime + 1.5f && count < 3) {

			//count only goes up every 0.5 seconds
		if (Time.time >= generateTime + 1f) {
			generateTime = Time.time;

			GeneratorPosition = this.gameObject.transform.position;
			Instantiate (myEnemies[enemy]);
			myEnemies[enemy].transform.position = GeneratorPosition;
			count++;
			}
		} 
		if (count >= 3) {
			startTime = Time.time;
			generateTime = Time.time + 3f;
			count = 0;
		}
		if (isLeft == true) {
			myRb.velocity = new Vector3 (-speed, 0f, 0f);
		}
		if (isRight == true) {
			myRb.velocity = new Vector3 (speed, 0f, 0f);
		}
		if (transform.position.x >= 4.5f) {
			isRight = false;
			isLeft = true;
		}
		if (transform.position.x <= -4.5f) {
			isLeft = false;
			isRight = true;
		}
	}
}