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

public class RouteController : MonoBehaviour {

	public float Speed;
	public Vector3 [] Route;

	private int _currentDestination;
	private float _angle;

	void Start () {
		_currentDestination = 0;
		_angle = Mathf.Atan2 (Route [_currentDestination].z - transform.position.z, Route [_currentDestination].x - transform.position.x);
		if (name == "ship")
			transform.eulerAngles = new Vector3 (transform.eulerAngles.x, -90f - _angle * Mathf.Rad2Deg, 0f);
		else if (name != "woodenpiece")
			transform.eulerAngles = new Vector3 (transform.eulerAngles.x, 90f - _angle * Mathf.Rad2Deg, 0f);
	}
	
	// Update is called once per frame
	void Update () {
		if (Vector3.Distance (Route [_currentDestination], transform.position) < Speed * Time.deltaTime) {
			_currentDestination = (_currentDestination + 1) % Route.Length;
			_angle = Mathf.Atan2 (Route [_currentDestination].z - transform.position.z, Route [_currentDestination].x - transform.position.x);
			if (name == "ship")
				transform.eulerAngles = new Vector3 (transform.eulerAngles.x, -90f - _angle * Mathf.Rad2Deg, 0f);
			else if (name != "woodenpiece")
				transform.eulerAngles = new Vector3 (transform.eulerAngles.x, 90f - _angle * Mathf.Rad2Deg, 0f);
		} else {
			Vector3 position = transform.position;
			position.x += Speed * Mathf.Cos (_angle) * Time.deltaTime;
			position.z += Speed * Mathf.Sin (_angle) * Time.deltaTime;
			transform.position = position;
		}
		if (name == "ship") {
		
		}
	}
}
