using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {
	public int Health;
	public int Direction;
	public GameObject bullet;
	public Transform posbullet;
	public int randomtime;
	// Use this for initialization
	void Start () {
		randomtime= Random.Range (1, 5);
		InvokeRepeating ("random", 7, 7);

		InvokeRepeating ("Fire", randomtime, randomtime);


		Direction = Random.Range (1, 5);
	}
	
	// Update is called once per frame
	void Update () {
		if (Time.timeScale != 0) {

			transform.Translate (0, -0.02f, 0);
		}
		if (Direction == 1) {

			transform.rotation= Quaternion.Euler(0,0,90);
		}
		if (Direction == 2) {
			
			transform.rotation= Quaternion.Euler(0,0,270);
		}
		if (Direction == 3) {
			
			transform.rotation= Quaternion.Euler(0,0,180);
		}
		if (Direction == 4) {
			
			transform.rotation= Quaternion.Euler(0,0,0);
		}
	}

	void OnCollisionEnter2D(Collision2D col){
		if (Direction == 1) {

			do {
				Direction = Random.Range (1, 5);

			} while(Direction == 1);

		} else if (Direction == 2) {

			do {
				Direction = Random.Range (1, 5);
				
			} while(Direction == 2);
		}else if (Direction == 3) {
			
			do {
				Direction = Random.Range (1, 5);
				
			} while(Direction == 3);
		}else if (Direction == 4) {
			
			do {
				Direction = Random.Range (1, 5);
				
			} while(Direction == 4);
		}
	}

	void random(){

	 Direction = Random.Range (1, 5);

	}
	void Fire(){
		Instantiate (bullet, posbullet.position, posbullet.rotation);
		
	}
}
