using System.Security.AccessControl;
|
using System.Collections.Generic;
|
using DG.Tweening;
|
using KTGMGemClient;
|
using TowerDefense.Level;
|
using UnityEngine;
|
using UnityEngine.UI;
|
|
/// <summary>
|
/// 图片飞向宝箱脚本
|
/// </summary>
|
public class FlyImage : MonoBehaviour
|
{
|
private Vector3 endP;//终点
|
private Vector3 p1;//控制点,来调节曲线效果
|
|
private Vector3 startP;//
|
private int index;
|
private List<Vector3> point = new List<Vector3>();
|
|
private RectTransform ts;
|
/// <summary>
|
/// Awake is called when the script instance is being loaded.
|
/// </summary>
|
void Awake()
|
{
|
tmpImage = transform.GetComponent<Image>();
|
ts = transform.GetComponent<RectTransform>();
|
}
|
private Image tmpImage;
|
private bool isBroadCast;
|
private int tmpCurrency;
|
public void Init(bool isBC, int currency)
|
{
|
isBroadCast = isBC;
|
tmpCurrency = currency;
|
}
|
/// <summary>
|
/// 设置终点
|
/// </summary>
|
/// <param name="endP">终点</param>
|
/// <param name="useBezier">是否使用贝塞尔</param>
|
public void SetDestination(Sprite sp, Vector3 startP, Vector3 endP, bool useBezier)
|
{
|
if (sp != null)
|
tmpImage.sprite = sp;
|
transform.position = startP;
|
ts.localPosition += RandomPos();
|
if (useBezier)
|
{
|
index = 0;
|
//startP = transform.position;
|
p1 = transform.position + GetOffect();
|
|
point = new List<Vector3>();
|
for (int i = 1; i < 101; i++)
|
{
|
Vector3 Bezierposition = BezierMath2(transform.position, p1, endP, (float)(i * 0.01));
|
point.Add(Bezierposition);
|
}
|
|
Bezier_Move();
|
}
|
else
|
{
|
transform.DOMove(endP, 2f).SetEase(Ease.OutQuad).OnComplete(Close);
|
}
|
}
|
|
bool isLeft;
|
private Vector3 RandomPos()
|
{
|
Vector3 tmpV = Vector3.zero;
|
tmpV.y += UnityEngine.Random.Range(30, 50);
|
int a = UnityEngine.Random.Range(-20, 20);
|
isLeft = a > 0;
|
tmpV.x += a;
|
return tmpV;
|
}
|
|
private Vector3 GetOffect()
|
{
|
Vector3 tmpV = Vector3.zero;
|
if (isLeft)
|
{
|
tmpV.x -= UnityEngine.Random.Range(80, 150);
|
tmpV.y += UnityEngine.Random.Range(500, 600);
|
}
|
else
|
{
|
tmpV.x += UnityEngine.Random.Range(500, 600);
|
tmpV.y += UnityEngine.Random.Range(80, 150);
|
}
|
|
return tmpV;
|
}
|
|
/// <summary>
|
/// 控制物体沿曲线移动
|
/// </summary>
|
private void Bezier_Move()
|
{
|
if (index + 3 >= point.Count)
|
{
|
//到终点了
|
Close();
|
return;
|
}
|
else
|
{
|
transform.DOMove(point[index], 0.01f).OnComplete(Bezier_Move);
|
|
index += 3;
|
}
|
}
|
|
private void Close()
|
{
|
//Debug.Log("到终点了,可以消失了" + EndlessLevelManager.instance.DropCoin);
|
|
gameObject.SetActive(false);
|
|
if (isBroadCast)
|
{
|
EndlessLevelManager.instance.Currency.AddCurrency(tmpCurrency);
|
EventCenter.Ins.BroadCast((int)KTGMGemClient.EventType.PlayGetGoldPS);
|
}
|
|
//Core.Utilities.Poolable.TryPool(gameObject);
|
}
|
|
/// <summary>
|
/// 计算贝塞尔曲线
|
/// </summary>
|
/// <param name="startP">开始点</param>
|
/// <param name="p1">控制点,来调节曲线效果</param>
|
/// <param name="endP">终点</param>
|
/// <param name="t">i * 0.01 100个点里面的比例</param>
|
/// <returns></returns>
|
private Vector3 BezierMath2(Vector3 startP, Vector3 p1, Vector3 endP, float t)
|
{
|
return (1 - t) * (1 - t) * startP + 2 * (1 - t) * t * p1 + t * t * endP;
|
}
|
|
// <summary>
|
// 三次贝塞尔
|
// </summary>
|
public static Vector3 Bezier_3(Vector3 startP, Vector3 p1, Vector3 p2, Vector3 endP, float t)
|
{
|
return (1 - t) * ((1 - t) * ((1 - t) * startP + t * p1) + t * ((1 - t) * p1 + t * p2)) + t * ((1 - t) * ((1 - t) * p1 + t * p2) + t * ((1 - t) * p2 + t * endP));
|
}
|
}
|