wangguan
2020-11-12 a7956acb794f93fc01e16963efe1f38743b110f9
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using KTGMGemClient;
using Protobuf;
using System;
using Core.Utilities;
 
/**
 * 无尽模式掉落管理器
 * @Author: chenxin
 * @Date: 2020-10-20 15:37:08
 */
namespace TowerDefense.Level
{
    public class EndlessDropManager : Singleton<EndlessDropManager>
    {
        /// <summary>
        /// 获得掉落事件
        /// </summary>
        public event Action<EndlessDrop> ObtainDropEvent;
 
        /// <summary>
        /// 所有掉落列表,其中包括已获得的还有未拾取的掉落
        /// </summary>
        public List<EndlessDrop> AllDropList;
 
        /// <summary>
        /// 掉落的唯一id
        /// </summary>
        private int dropId;
 
        /// <summary>
        /// 所有掉落在场景内的物体字典,使用唯一id索引方便查找
        /// </summary>
        private Dictionary<int, GameObject> DropObjDic;
 
        private string dropPath = "UI/Props/EndlessDropEffect";
 
        /// <summary>
        /// 自动拾取时间,先统一处理
        /// </summary>
        public float AutoPickupTime = 5f;
 
        /// <summary>
        /// 掉落半径
        /// </summary>
        public float DropRadius { get; set; } = 5f;
 
        public Canvas canvas;
 
        // Start is called before the first frame update
        private void Start()
        {
            AllDropList = new List<EndlessDrop>();
            DropObjDic = new Dictionary<int, GameObject>();
            allIconLis = new List<FlyImage>();
        }
 
        // Update is called once per frame
        private void Update()
        {
            for (int i = 0; i < AllDropList.Count; ++i)
            {
                if (AllDropList[i].IsPickupCompleted) continue;
 
                AllDropList[i].ElapsedTime += Time.deltaTime;
 
                if (AllDropList[i].ElapsedTime >= AllDropList[i].AutoPickupTime)
                {
                    EndlessDropReward dropReward = DropObjDic[AllDropList[i].Id].GetComponent<EndlessDropReward>();
                    // dropReward?.OnClick();
 
                    //AllDropList[i].IsPickupCompleted = true;
                    //SafelyCallObtainDrop(AllDropList[i]);
                    //RemoveDrop(AllDropList[i].Id);
                }
            }
        }
 
        /// <summary>
        /// 获取一个唯一的掉落id
        /// </summary>
        private int GetDropId()
        {
            return dropId++;
        }
 
        /// <summary>
        /// 手动拾取了掉落
        /// </summary>
        /// <param name="data"></param>
        public void OnClickDrop(EndlessDrop data)
        {
            data.IsPickupCompleted = true;
            SafelyCallObtainDrop(data);
            RemoveDrop(data.Id);
        }
 
        /// <summary>
        /// 获得一些掉落
        /// </summary>
        /// <param name="list"></param>
        /// <param name="pos">小怪的世界坐标</param>
        public void AddDrop(List<reward> list, Vector3 pos)
        {
            // for (int i = 0; i < list.Count; ++i)
            // {
            //     EndlessDrop drop = new EndlessDrop();
            //     drop.Reward = list[i];
            //     drop.Id = GetDropId();
            //     drop.AutoPickupTime = AutoPickupTime;
            //     CreateDrop(drop, pos, list.Count > 0);
            //     AllDropList.Add(drop);
            // }
        }
 
        /// <summary>
        /// 根据唯一id移除掉落
        /// </summary>
        /// <param name="id"></param>
        private void RemoveDrop(int id)
        {
            if (DropObjDic.ContainsKey(id))
            {
                Destroy(DropObjDic[id]);
                DropObjDic.Remove(id);
            }
        }
 
        /// <summary>
        /// 在场景中创建掉落的icon
        /// </summary>
        /// <param name="drop"></param>
        /// <param name="worldPos">世界坐标</param>
        /// <param name="isRandom">是否需要随机位置</param>
        private void CreateDrop(EndlessDrop drop, Vector3 worldPos, bool isRandom = false)
        {
            GameObject prefabObj = Resources.Load<GameObject>(dropPath);
            GameObject obj = Instantiate(prefabObj);
            EndlessDropReward dropReward = obj.GetComponent<EndlessDropReward>();
 
            dropReward.DropData = drop;
            dropReward.SetIcon();
 
            GameObject mainUI = GameObject.Find("BottomUI");
 
            Transform mainUITransform = mainUI.transform;
            obj.transform.SetParent(mainUITransform, false);
 
            // Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
            // Vector2 pos;
            // RectTransformUtility.ScreenPointToLocalPointInRectangle(mainUITransform as RectTransform, screenPos, Camera.main, out pos);
            // (obj.transform as RectTransform).anchoredPosition = pos;
 
            obj.GetComponent<Transform>().SetParent(mainUITransform, true);
 
            obj.transform.position = worldPos;
 
            Vector3 offect = obj.GetComponent<RectTransform>().anchoredPosition3D;
            offect.z = 0;
            obj.GetComponent<RectTransform>().anchoredPosition3D = offect;
            // obj.transform.localRotation = Quaternion.identity;
            // obj.transform.localScale = Vector3.one;
 
            // if (isRandom)
            // {
            //     Vector2 p = UnityEngine.Random.insideUnitCircle * DropRadius;
            //     Vector3 pos1 = p.normalized * p.magnitude;
            //     Vector3 objPos = obj.transform.position;
            //     objPos.x += pos1.x;
            //     objPos.z += pos1.y;
            //     obj.transform.position = objPos;
            // }
 
            dropReward.PlayParticle();
            DropObjDic.Add(drop.Id, obj);
        }
 
 
        List<FlyImage> allIconLis;
        public GameObject drapIcon;
 
        public FlyImage CreateIcon(Transform ts)
        {
            for (int i = 0; i < allIconLis.Count; i++)
            {
                if (!allIconLis[i].gameObject.activeSelf)
                {
                    allIconLis[i].gameObject.SetActive(true);
                    return allIconLis[i];
                }
            }
 
            GameObject obj = Instantiate(drapIcon, ts);
            FlyImage fly = obj.GetComponent<FlyImage>();
            allIconLis.Add(fly);
            return fly;
 
        }
 
        public void FlyIcon(Transform ts, Sprite sp, Vector3 startP, Vector3 endP, bool useBezier)
        {
            StartCoroutine(CreateIcon(ts, sp, startP, endP, useBezier));
        }
 
        IEnumerator CreateIcon(Transform ts, Sprite sp, Vector3 startP, Vector3 endP, bool useBezier)
        {
            FlyImage fly;
 
            // for (int i = 0; i < 5; i++)
            // {
            //     fly = CreateIcon(ts);
            //     fly.SetDestination(sp, startP, endP, useBezier);
            // }
            // yield return new WaitForSeconds(0.2f);
 
            for (int i = 0; i < 15; i++)
            {
                fly = CreateIcon(ts);
                fly.SetDestination(sp, startP, endP, useBezier);
                yield return new WaitForSeconds(0.01f);
 
            }
 
            yield break;
        }
 
        /// <summary>
        /// 调用掉落获得事件
        /// </summary>
        /// <param name="data"></param>
        private void SafelyCallObtainDrop(EndlessDrop data)
        {
            if (ObtainDropEvent != null)
                ObtainDropEvent(data);
        }
 
        /// <summary>
        /// 获得所有已经拾取的掉落
        /// </summary>
        public List<EndlessDrop> GetAllObtainedDrop()
        {
            List<EndlessDrop> ret = new List<EndlessDrop>();
 
            for (int i = 0; i < AllDropList.Count; ++i)
            {
                if (AllDropList[i].IsPickupCompleted)
                    ret.Add(AllDropList[i]);
            }
 
            return ret;
        }
 
        /// <summary>
        /// 检查是否所有的掉落都被拾取
        /// </summary>
        public bool CheckAllDropPicked()
        {
            for (int i = 0; i < AllDropList.Count; ++i)
            {
                if (!AllDropList[i].IsPickupCompleted) return false;
            }
 
            return true;
        }
 
        /// <summary>
        /// 拾取所有还未拾取的掉落
        /// </summary>
        public void PickUpAllDrop()
        {
            for (int i = 0; i < AllDropList.Count; ++i)
            {
                if (AllDropList[i].IsPickupCompleted) continue;
 
                AllDropList[i].IsPickupCompleted = true;
                SafelyCallObtainDrop(AllDropList[i]);
                RemoveDrop(AllDropList[i].Id);
            }
        }
    }
}