NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/PopUp/PopUpManager.cs

98 lines
2.7 KiB
C#

using PhxhSDK;
using System.Collections.Generic;
namespace Gameplay.PopUp
{
/// <summary>
/// 弹窗管理器
/// </summary>
public class PopUpManager : Singlenton<PopUpManager>, IInitable
{
/// <summary>
/// 弹窗
/// </summary>
private HashSet<IPopUp> setPopUp;
/// <summary>
/// 弹窗队列字典
/// </summary>
private Dictionary<E_PopUpTrigger, LinkedList<IPopUp>> dicPopUpQueue;
public void Init()
{
setPopUp = new HashSet<IPopUp>();
dicPopUpQueue = new Dictionary<E_PopUpTrigger, LinkedList<IPopUp>>();
}
public void Release()
{
if (null != dicPopUpQueue)
{
foreach (var i in dicPopUpQueue.Values)
{
i.Clear();
}
dicPopUpQueue.Clear();
}
setPopUp?.Clear();
}
/// <summary>
/// 添加弹窗
/// </summary>
public void AddPopUp(IPopUp newPopUp)
{
if (setPopUp.Contains(newPopUp))
return;
setPopUp.Add(newPopUp);
if (!dicPopUpQueue.TryGetValue(newPopUp.TriggerType, out LinkedList<IPopUp> linkPopUp))
{
linkPopUp = new LinkedList<IPopUp>();
dicPopUpQueue.Add(newPopUp.TriggerType, linkPopUp);
}
if (linkPopUp.Count <= 0)
{
linkPopUp.AddLast(newPopUp);
return;
}
LinkedListNode<IPopUp> nodePopUp = linkPopUp.First;
while (null != nodePopUp && null != nodePopUp.Value)
{
if (nodePopUp.Value.Priority > newPopUp.Priority) // 根据优先级排序
{
linkPopUp.AddBefore(nodePopUp, newPopUp);
return;
}
nodePopUp = nodePopUp.Next;
}
linkPopUp.AddLast(newPopUp);
}
/// <summary>
/// 触发弹窗
/// </summary>
/// <param name="triggerType">触发类型</param>
public void TriggerPopUp(E_PopUpTrigger triggerType)
{
if (!dicPopUpQueue.TryGetValue(triggerType, out LinkedList<IPopUp> linkPopUp))
return;
if (linkPopUp.Count <= 0)
return;
LinkedListNode<IPopUp> commandFirstNode = linkPopUp.First;
linkPopUp.RemoveFirst();
if (!setPopUp.Contains(commandFirstNode.Value))
return;
setPopUp.Remove(commandFirstNode.Value);
commandFirstNode.Value.Execute();
}
}
}