using UnityEngine;
using System.Collections;
using System;

public class EnemyBehaviour2 : MonoBehaviour {
	
	private const float SPEED = 2.0f;
	
	float moveRemaining = 1;
	int versor = -1;
	bool isAttacking = false;
	int round = 1; 
	
	// Use this for initialization
	void Start () {
		this.gameObject.renderer.enabled = false;
		this.gameObject.collider2D.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {

		if (this.gameObject.renderer.enabled == true) {
			float t = Time.time;
			float delta = Time.deltaTime * SPEED;
			Vector3 vec = new Vector3 (delta*versor, 0, 0);
			vec = Vector3.ClampMagnitude(vec, delta);
			this.transform.Translate(vec);
			if (t > moveRemaining){
				versor = versor * -1;
				moveRemaining = t+1f;
				if (versor == -1) {
					round++;
					isAttacking = (round % 3 == 0);
					Animator anim = GetComponent<Animator>();
					if (isAttacking){
						anim.CrossFade("UfoAttack", 0f);
						this.gameObject.collider2D.enabled = true;
					}
					else {
						anim.CrossFade("UfoIdle", 0f);
						this.gameObject.collider2D.enabled = false;
					}
				}
			}
		}
	}

}
