chenxin
2020-11-26 9af3e3dbede79e70262ff9d206299dcca363c1bb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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;
    /// <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 isUp;
    private Vector3 RandomPos()
    {
        Vector3 tmpV = Vector3.zero;
        tmpV.x -= UnityEngine.Random.Range(10, 20);
        int a = UnityEngine.Random.Range(-20, 20);
        isUp = a > 0;
        tmpV.z += a;
        return tmpV;
    }
 
    private Vector3 GetOffect()
    {
        Vector3 tmpV = Vector3.zero;
        tmpV.x -= UnityEngine.Random.Range(10, 15);
        if (isUp)
        {
            tmpV.z += UnityEngine.Random.Range(10, 20);
        }
        else
        {
            tmpV.z -= UnityEngine.Random.Range(10, 20);
        }
 
        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);
 
        EndlessLevelManager.instance.Currency.AddCurrency(1);//每个金币增加1
        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));
    }
}