using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UI_ZhuZhan_Effect : MonoBehaviour {
? ? public GameObject[] m_EffectList;
? ? [Header("貝塞爾曲線點的數(shù)量")]
? ? public int m_VecPointNum = 3;
? ? [Header("貝塞爾曲線坡峰")]
? ? public int m_SlopePeak = 50;
? ? //[Header("到終點的偏移距離叉存,與斜率相乘得到最終位置點")]
? ? //public float m_OffectValue = 130;
? ? //[Header("正弦函數(shù)坡長")]
? ? //public float m_SlopeLength = 3;
? ? //[Header("正弦函數(shù)坡峰數(shù)組")]
? ? //public float[] m_SlopePeak;
? ? public void Init(Vector3 startPoint, Vector3 endPoint, Action action)
? ? {
? ? ? ? float distance = Vector3.Distance(startPoint, endPoint) / 180;
? ? ? ? //m_SlopeLength = 50;
? ? ? ? //float SlopeValue = Mathf.Atan2((endPoint.x - startPoint.x), (endPoint.z - startPoint.z));
? ? ? ? //float angle = SlopeValue * (180 / Mathf.PI);
? ? ? ? //angle = 1 - Mathf.Abs(angle) / 180;
? ? ? ? //m_OffectValue = angle * 200;
? ? ? ? //Debug.Log("斜率:" + angle);
? ? ? ? Vector3 centerPos = new Vector3((startPoint.x + endPoint.x) / 2, endPoint.y, (startPoint.z + endPoint.z) / 2);
? ? ? ? float SlopeValue1 = (endPoint.x - startPoint.x) / (endPoint.z - startPoint.z);
? ? ? ? float SlopeValue2 = (-1)/SlopeValue1;
? ? ? ? float b = centerPos.x - centerPos.z * SlopeValue2;
? ? ? ? Sequence sequence = DOTween.Sequence();
? ? ? ? int index = 0;
? ? ? ? for (int i = 0; i < m_EffectList.Length; i++)
? ? ? ? {
? ? ? ? ? ? m_EffectList[i].transform.position = startPoint;
? ? ? ? ? ? if (index == 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? centerPos = new Vector3(SlopeValue2*((startPoint.z + endPoint.z) / 2 + (m_SlopePeak + distance) / SlopeValue2) + b, endPoint.y, (startPoint.z + endPoint.z) / 2 + (m_SlopePeak + distance) / SlopeValue2);
? ? ? ? ? ? }
? ? ? ? ? ? else if(index == 1)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? centerPos = new Vector3(SlopeValue2 * ((startPoint.z + endPoint.z) / 2 - (m_SlopePeak + distance) / SlopeValue2) + b, endPoint.y, (startPoint.z + endPoint.z) / 2 - (m_SlopePeak + distance) / SlopeValue2);
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? centerPos = new Vector3((startPoint.x + endPoint.x) / 2, endPoint.y, (startPoint.z + endPoint.z) / 2);
? ? ? ? ? ? ? ? index = 0;
? ? ? ? ? ? }
? ? ? ? ? ? index++;
? ? ? ? ? ? Vector3[] path = GetBeizerList(startPoint, centerPos, endPoint, m_VecPointNum);//m_SlopePeak[i], 0, m_OffectValue * (SlopeValue <= 0 ? 1 : -1)
? ? ? ? ? ? if (i==0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sequence.Append(m_EffectList[i].transform.DOLocalPath(path, 1.5f, PathType.CatmullRom));
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? sequence.Insert(0,m_EffectList[i].transform.DOLocalPath(path, 1.5f, PathType.CatmullRom));
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? sequence.AppendCallback(delegate ()
? ? ? ? {
? ? ? ? ? ? action.InvokeIfNotNull();
? ? ? ? });
? ? }
? ? /// <summary>
? ? /// 獲取存儲貝塞爾曲線點的數(shù)組
? ? /// </summary>
? ? /// <param name="startPoint"></param>起始點
? ? /// <param name="controlPoint"></param>控制點
? ? /// <param name="endPoint"></param>目標(biāo)點
? ? /// <param name="segmentNum"></param>采樣點的數(shù)量
? ? /// <returns></returns>存儲貝塞爾曲線點的數(shù)組
? ? public Vector3[] GetBeizerList(Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint, int segmentNum)//, float slopePeak, float offectValue
? ? {
? ? ? ? Vector3[] path = new Vector3[segmentNum+1];
? ? ? ? path[0] = startPoint;
? ? ? ? for (int i = 1; i <= segmentNum; i++)
? ? ? ? {
? ? ? ? ? ? float t = i / (float)segmentNum;
? ? ? ? ? ? Vector3 pixel = CalculateCubicBezierPoint(t, startPoint,
? ? ? ? ? ? ? ? controlPoint, endPoint);
? ? ? ? ? ? //if (slopePeak!=0)
? ? ? ? ? ? //{
? ? ? ? ? ? //? ? float innner = (2 * (float)Mathf.PI) / m_SlopeLength;
? ? ? ? ? ? //? ? pixel.x = (float)Mathf.Sin(innner * pixel.z) * slopePeak - offectValue;
? ? ? ? ? ? //}
? ? ? ? ? ? path[i] = pixel;
? ? ? ? }
? ? ? ? path[segmentNum] = endPoint;
? ? ? ? return path;
? ? }
? ? /// <summary>
? ? /// 根據(jù)T值,計算貝塞爾曲線上面相對應(yīng)的點
? ? /// </summary>
? ? /// <param name="t"></param>T值
? ? /// <param name="p0"></param>起始點
? ? /// <param name="p1"></param>控制點
? ? /// <param name="p2"></param>目標(biāo)點
? ? /// <returns></returns>根據(jù)T值計算出來的貝賽爾曲線點
? ? private Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2)
? ? {
? ? ? ? float u = 1 - t;
? ? ? ? float tt = t * t;
? ? ? ? float uu = u * u;
? ? ? ? Vector3 p = uu * p0;
? ? ? ? p += 2 * u * t * p1;
? ? ? ? p += tt * p2;
? ? ? ? return p;
? ? }
}