using UnityEngine;
using System.Collections;

public class StabScript : MonoBehaviour {

    public float timeToStart;
    public GameObject target;
    public float speed;
    public float waitToRun;

    private float _timeStarting;
    private bool _isStarting = true;
    private Animator _animator;
    private MovingScript.Dir _direction = MovingScript.Dir.NONE;
    private bool _isMoving = false;

    private bool _startedCorrutine = false;

	// Use this for initialization
	void Start () {
        _animator = GetComponent<Animator>();
	}
	
	// Update is called once per frame
	void Update () {
	
        if (_isStarting)
        {
            _timeStarting += Time.deltaTime;

            if (_timeStarting >= timeToStart)
            {
                _isStarting = false;
                GetComponent<QueueScript>().enabled = false;
                _isMoving = true;
            }

            return;
        }

        if (_isMoving)
        {
            float l_hDir = target.transform.position.x - gameObject.transform.position.x;
            float l_vDir = target.transform.position.y - gameObject.transform.position.y;
            Vector3 l_vector = new Vector3(l_hDir, l_vDir, l_vDir);
            l_vector = l_vector.normalized;

            transform.Translate(l_vector * speed * Time.deltaTime);
        }
        else
        {
            _animator.SetInteger("Special", 1);
            if (!_startedCorrutine)
            {
                _startedCorrutine = true;
                StartCoroutine(Run());
            }
            
        }
	}

    void OnTriggerEnter2D (Collider2D coll)
    {
        if (_isMoving && coll.gameObject.name == target.name)
        {
            
            _isMoving = false;
        }
    }

    IEnumerator Run()
    {
        yield return new WaitForSeconds(waitToRun);
        this.GetComponent<RunAfterKill>().enabled = true;
    }
}
