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

public class Route : MonoBehaviour
{
    private Vector2 Interval = new Vector2(0, 0.02f);
    public List<Material> ListLineMat;
    /// <summary>
    /// 旋转方向
    /// </summary>
    public Transform Point;
    public Vector3 EndPos;
    public Vector3 StartPos;

    private Material material;
    private Vector2 offset;

    void Start()
    {
        material = this.GetComponent<MeshRenderer>().material;
        offset = Vector2.zero;
    }

    void Update()
    {
        offset -= Interval;
        material.SetTextureOffset("_MainTex", offset);
    }

    /// <summary>
    ///  设置Route 位置 长度 朝向 旋转
    /// </summary>
    /// <param name="endPos"></param>
    /// <param name="exitPos"></param>
    public void SetRoute(Vector3 endPos, Vector3 startPos, int Index)
    {
        this.GetComponent<MeshRenderer>().material = ListLineMat[Index];
        if (material == null)
            material = this.GetComponent<MeshRenderer>().material;
        this.EndPos = endPos;
        this.StartPos = startPos;
        //   Debug.Log("Route    " + endPos + "    " + exitPos);
        transform.localScale += new Vector3(0, 0, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) / 12 - (transform.localScale.x * 2));
        material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(EndPos, StartPos)) * 1.5f));
        transform.localPosition = (StartPos + EndPos) / 2.0f;
        Point.localPosition = StartPos + new Vector3(0, transform.localPosition.y, 0);
        transform.LookAt(Point);
    }

    /// <summary>
    /// 3DMapRoute
    /// </summary>
    /// <param name="endPos"></param>
    /// <param name="exitPos"></param>
    public void SetMapRoute()
    {
        if (material == null)
            material = this.GetComponent<MeshRenderer>().material;
        material.SetTextureScale("_MainTex", new Vector2(1, 2f));
    }
    public void SetMapRoute(Vector3 endPos, Vector3 exitPos)
    {
        if (material == null)
            material = this.GetComponent<MeshRenderer>().material;
        material.SetTextureScale("_MainTex", new Vector2(1, Mathf.Abs(Vector3.Distance(endPos, exitPos)) * 0.7f));
    }
}