using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class KnapsackTimeCDMgr : SingletonMonobehaviour { private List itemCDList = new List(); private List normalCDList = new List(); public event Action RefresCoolTimeAct; public event Action RefreshItemOverdueTimeAct; public event Action RefreshItemOverdueAct; public event Action RefreshNormalCDAct; int secondBuf = -1; int minuteBuf = -1; private void LateUpdate() { CheckItemCoolTime(TimeRefreshType.Frame); var minute = DateTime.Now.Minute; var second = DateTime.Now.Second; if (second != secondBuf) { CheckNormalCoolTime(TimeRefreshType.Second); secondBuf = second; } if (minute != minuteBuf) { CheckItemCoolTime(TimeRefreshType.Minute); minuteBuf = minute; } } private void CheckItemCoolTime(TimeRefreshType type) { for (int i = itemCDList.Count - 1; i > -1; i--) { double remianCD = itemCDList[i].GetRemainTime(); switch (type) { case TimeRefreshType.Frame: if (RefresCoolTimeAct != null) { RefresCoolTimeAct(itemCDList[i].guid); } break; case TimeRefreshType.Minute: if (RefreshItemOverdueTimeAct != null) { RefreshItemOverdueTimeAct(itemCDList[i].guid); } break; } if (remianCD <= 0) { string guid = itemCDList[i].guid; itemCDList.RemoveAt(i); if (RefreshItemOverdueAct != null) { RefreshItemOverdueAct(guid); } } } } private void CheckNormalCoolTime(TimeRefreshType type) { for (int i = 0; i < normalCDList.Count; i++) { double remianCD = normalCDList[i].GetRemainTime(); switch (type) { case TimeRefreshType.Frame: break; case TimeRefreshType.Second: if (RefreshNormalCDAct != null) { RefreshNormalCDAct(normalCDList[i].code, (int)remianCD); } break; case TimeRefreshType.Minute: break; } if (remianCD <= 0) { UnRegister(normalCDList[i].code); } } } public void Register(string guid, int itemId, double cdTime) { int index; if (ContainsItemCD(guid, out index)) { itemCDList[index].SetTime(cdTime); } else { ItemCDCool cool = new ItemCDCool(guid, itemId, cdTime); itemCDList.Add(cool); } } public void Register(int code, double cdTime) { int index; if (ContainsNormalCD(code, out index)) { normalCDList[index].SetTime(cdTime); } else { NormalCDCool cool = new NormalCDCool(code, cdTime); normalCDList.Add(cool); } } public void UnRegister(string guid) { int index; if (ContainsItemCD(guid, out index)) { itemCDList.RemoveAt(index); } } public void UnRegister(int code) { int index; if (ContainsNormalCD(code, out index)) { normalCDList.RemoveAt(index); } } public ItemCDCool GetItemCoolById(string guid) { int index; if (ContainsItemCD(guid, out index)) { return itemCDList[index]; } return null; } public int GetNormalCDTime(int code) { int index; if (ContainsNormalCD(code, out index)) { return (int)normalCDList[index].GetRemainTime(); } return 0; } List typeCoollist = new List(); public List GetItemCoolByCDType(int type, out bool isCool) { typeCoollist.Clear(); isCool = false; for (int i = 0; i < itemCDList.Count; i++) { ItemConfig itemConfig = ItemConfig.Get(itemCDList[i].itemId); if (itemConfig != null && itemConfig.CDType == type && itemCDList[i].GetRemainTime() > 0) { typeCoollist.Add(itemCDList[i]); isCool = true; } } return typeCoollist; } private bool ContainsItemCD(string guid, out int index) { index = 0; for (int i = 0; i < itemCDList.Count; i++) { if (itemCDList[i].guid == guid) { index = i; return true; } } return false; } private bool ContainsNormalCD(int code, out int index) { index = 0; for (int i = 0; i < normalCDList.Count; i++) { if (normalCDList[i].code == code) { index = i; return true; } } return false; } public enum TimeRefreshType { Frame, //每帧 Second, //每秒 Minute //每分 } } public class ItemCDCool { public string guid; public int itemId; public double totalTime; public DateTime startTime { get; private set; } public ItemCDCool(string guid, int itemId, double totalTime) { this.guid = guid; this.itemId = itemId; this.totalTime = totalTime; this.startTime = DateTime.Now; } public void SetTime(double totalTime) { this.totalTime = totalTime; this.startTime = DateTime.Now; } public double GetRemainTime() { double remainTime = totalTime - (DateTime.Now - startTime).TotalSeconds; if (remainTime <= 0) { remainTime = 0; } return remainTime; } } public class NormalCDCool { public int code; public double totalTime; public DateTime startTime { get; private set; } public NormalCDCool(int code, double totalTime) { this.code = code; this.totalTime = totalTime; this.startTime = DateTime.Now; } public void SetTime(double totalTime) { this.totalTime = totalTime; this.startTime = DateTime.Now; } public double GetRemainTime() { double remainTime = totalTime - (DateTime.Now - startTime).TotalSeconds; if (remainTime <= 0) { remainTime = 0; } return remainTime; } }