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 { /// /// 掉落道具预制体列表 /// public List PropList; /// /// 获得掉落事件 /// public event Action ObtainDropEvent; /// /// 所有掉落列表,其中包括已获得的还有未拾取的掉落 /// public List AllDropList; /// /// 掉落的唯一id /// private int dropId; /// /// 所有掉落在场景内的物体字典,使用唯一id索引方便查找 /// private Dictionary DropObjDic; /// /// 掉落半径 /// public float DropRadius { get; set; } = 50f; // Start is called before the first frame update private void Start() { AllDropList = new List(); DropObjDic = new Dictionary(); } // 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); } } } /// /// 获取一个唯一的掉落id /// private int GetDropId() { return dropId++; } /// /// 手动拾取了掉落 /// /// public void OnClickDrop(EndlessDrop data) { data.IsPickupCompleted = true; SafelyCallObtainDrop(data); RemoveDrop(data.Id); } /// /// 根据reward获得预制体对象 /// /// private GameObject GetPrefabByReward(reward data) { switch (data.type) { case CURRENCY.Money: return PropList[0]; case CURRENCY.Gold: return PropList[1]; case CURRENCY.Box: return PropList[1 + data.id]; } return null; } /// /// 获得一些掉落 /// /// /// 小怪的世界坐标 public void AddDrop(List list, Vector3 pos) { for (int i = 0; i < list.Count; ++i) { EndlessDrop drop = new EndlessDrop(); drop.Reward = list[i]; drop.Id = GetDropId(); GameObject prefabObj = GetPrefabByReward(list[i]); if (prefabObj == null) { Debug.LogError($"--------------------- type: {list[i].type} id:{list[i].id} 没有对应的预制体 ---------------------"); continue; } EndlessDropReward dropReward = prefabObj.GetComponent(); drop.AutoPickupTime = dropReward.AutoPickupTime; CreateDrop(drop, pos, list.Count > 0); AllDropList.Add(drop); } } /// /// 根据唯一id移除掉落 /// /// private void RemoveDrop(int id) { if (DropObjDic.ContainsKey(id)) { Destroy(DropObjDic[id]); DropObjDic.Remove(id); } } /// /// 在场景中创建掉落的icon /// /// /// /// 是否需要随机位置 private void CreateDrop(EndlessDrop drop, Vector3 pos, bool isRandom = false) { GameObject dropLayer = GameObject.Find("DropLayer"); GameObject prefabObj = GetPrefabByReward(drop.Reward); GameObject obj = Instantiate(prefabObj); GameObject mainUI = GameObject.Find("MainUI"); Transform mainUITransform = mainUI.GetComponent(); obj.GetComponent().SetParent(mainUITransform, true); Vector3 screenPos = Camera.main.WorldToScreenPoint(pos); screenPos.z = 0; obj.transform.position = screenPos; 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; } obj.GetComponent().DropData = drop; DropObjDic.Add(drop.Id, obj); } /// /// 调用掉落获得事件 /// /// private void SafelyCallObtainDrop(EndlessDrop data) { if (ObtainDropEvent != null) ObtainDropEvent(data); } /// /// 获得所有已经拾取的掉落 /// public List GetAllObtainedDrop() { List ret = new List(); for (int i = 0; i < AllDropList.Count; ++i) { if (AllDropList[i].IsPickupCompleted) ret.Add(AllDropList[i]); } return ret; } /// /// 检查是否所有的掉落都被拾取 /// public bool CheckAllDropPicked() { for (int i = 0; i < AllDropList.Count; ++i) { if (!AllDropList[i].IsPickupCompleted) return false; } return true; } } }