chenxin
2020-10-28 8716403033b7c70b00b61bc6c74af04e4d248ecc
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
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; } = 50f;
 
        public Canvas canvas;
 
        // Start is called before the first frame update
        private void Start()
        {
            AllDropList = new List<EndlessDrop>();
            DropObjDic = new Dictionary<int, GameObject>();
        }
 
        // 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)
                {
                    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("MainUI");
 
            Transform mainUITransform = mainUI.GetComponent<Transform>();
            obj.GetComponent<Transform>().SetParent(mainUITransform, true);
 
            // Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
            // Vector2 targetPos;
            // RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, screenPos, canvas.worldCamera, out targetPos);
            // RectTransform rectTransform =  obj.GetComponent<RectTransform>();
            // rectTransform.anchoredPosition = pos;
            // obj.transform.localRotation = Quaternion.identity;
 
            // Vector3 screenPos = Camera.main.WorldToScreenPoint(worldPos);
            // screenPos.z = 0;
            
            obj.transform.position = worldPos;
            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.y += pos1.y;
            //     obj.transform.position = objPos;
            // }
 
            dropReward.PlayParticle();
            DropObjDic.Add(drop.Id, obj);
        }
 
        /// <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);
            }
        }
    }
}