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

public class ElevatorScript : MonoBehaviour
{
    public Transform target;
    public float speed = 10.0f;
    public bool isActive = true;

    public List<Transform> pointList = new List<Transform>();
    private int pointIndex = 0;
    private Rigidbody selfRig;

    private void Start()
    {
        selfRig = GetComponent<Rigidbody>();
        //target = point2;
        target = pointList[0];
    }

    private void FixedUpdate()
    {
        if (isActive == true)
        {
            float step = speed * Time.fixedDeltaTime;
            //transform.position = Vector3.MoveTowards(transform.position, target.position, step);
            selfRig.MovePosition(Vector3.MoveTowards(transform.position, target.position, step));

            if ((target.position - transform.position).magnitude < 0.5f)
            {
                //print("close to target");

                if (pointIndex < pointList.Count-1)
                {
                    pointIndex++;
                }
                else
                {
                    pointIndex = 0;
                }

                target = pointList[pointIndex];
            }
        }
    }

    private void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.tag == "Player")
        {
            isActive = true;
        }
    }
}
