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

public class SolMinion : MonoBehaviour {

    GameObject pathGO;

    Transform targetPathNode;
    int pathNodeIndex = 0;

    float speed = 5f;

    public float health = 1f;

	// Use this for initialization
	void Start () {
        pathGO = GameObject.Find("Path");
    }

    void GetNextPathNode()
    {
        targetPathNode = pathGO.transform.GetChild(pathNodeIndex);
        pathNodeIndex++;
    }
	
	// Update is called once per frame
	void Update () {
		if(targetPathNode == null)
        {
            GetNextPathNode();
            if(targetPathNode == null)
            {
                ReachedGoal();
            }
        }

        Vector3 dir = targetPathNode.position - this.transform.localPosition;

        float distThisFrame = speed * Time.deltaTime;

        if(dir.magnitude <= distThisFrame)
        {
            targetPathNode = null;
        }
        else
        {
            transform.Translate(dir.normalized * distThisFrame, Space.World);
            Quaternion targetRotation = Quaternion.LookRotation(dir);
            //this.transform.rotation = Quaternion.LookRotation(dir);
            this.transform.rotation = Quaternion.Lerp(this.transform.rotation, targetRotation, Time.deltaTime);
        }
	}

    void ReachedGoal()
    {
        Destroy(gameObject);
    }

    public void TakeDamage(float damage)
    {
        health -= damage;
        if(health <= 0)
        {
            Die();
        }
    }

    public void Die()
    {
        Destroy(gameObject);
    }
}