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

public class VertEnemyGenerator : MonoBehaviour {

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

	private bool isDown = true;
	private bool isUp = false;
	public float speed = 5f;

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

	void Update () {

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

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

				GeneratorPosition = this.gameObject.transform.position;
				Instantiate (enemy);
				enemy.transform.position = GeneratorPosition;


				count++;
			}
		} 
		if (count >= 3) {
			startTime = Time.time;
			generateTime = Time.time + 3f;
			count = 0;
		}
		if (isUp == true) {
			myRb.velocity = new Vector3 (0f, -speed, 0f);
		}
		if (isDown == true) {
			myRb.velocity = new Vector3 (0f, speed, 0f);
		}
		if (transform.position.y >= 4.5f) {
			isDown = false;
			isUp = true;
		}
		if (transform.position.y <= -4.5f) {
			isUp = false;
			isDown = true;
		}
	}
}
