using UnityEngine;
using System.Collections;

public class Slime : Enemy {

    public Vector3[] waypoints;
    public float speed = 1.0f;

    private Vector3 originalPosition;
    private int currentWaypoint;
    private int targetWaypoint;
    private float waypointProgress = 0;

	void Start () 
    {
        base.Init();
        originalPosition = transform.position;
        currentWaypoint = -1;
        targetWaypoint = 0;
	}
	
	void Update () 
    {
        if (isRealForm)
            return;

        if (waypoints.Length <= 0)
            return;

        waypointProgress += Time.deltaTime * speed;
        Vector3 currentPoint = currentWaypoint == -1 ? originalPosition : waypoints[currentWaypoint];
        Vector3 targetPoint = targetWaypoint == -1 ? originalPosition : waypoints[targetWaypoint];
        transform.position = Vector3.Lerp(currentPoint, targetPoint, waypointProgress);
        if (waypointProgress >= 1)
        {
            waypointProgress = 0;
            currentWaypoint = targetWaypoint;
            targetWaypoint = (targetWaypoint + 1) >= waypoints.Length ? -1 : targetWaypoint + 1;
        }
        transform.forward = targetPoint - currentPoint;

        CheckPlayerPosition();
        CheckLightSources();
	}

    

    void OnDrawGizmosSelected()
    {
        Vector3 lastWaypoint = transform.position;
        foreach (Vector3 point in waypoints)
        {
            Gizmos.DrawLine(lastWaypoint, point);
            lastWaypoint = point;
        }
        Gizmos.DrawLine(lastWaypoint, transform.position);
    }
}
