forked from QianMo/Unity-Design-Pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventQueueManager.cs
More file actions
156 lines (135 loc) · 4.31 KB
/
Copy pathEventQueueManager.cs
File metadata and controls
156 lines (135 loc) · 4.31 KB
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
//-------------------------------------------------------------------------------------
// EventQueueManager.cs
// Reference :https://github.com/GandhiGames/message_queue
// http://gandhigames.co.uk/message-queue/
//-------------------------------------------------------------------------------------
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
namespace EventQueuePatternExample
{
/// <summary>
/// 事件Manager
/// </summary>
public class EventQueueManager
{
private static EventQueueManager _instance;
public static EventQueueManager Instance
{
get
{
if (_instance == null)
{
_instance = new EventQueueManager();
}
return _instance;
}
}
//泛型代理
public delegate void EventDelegateX<T>(T e) where T : GameEvent;
//普通代理
private delegate void EventDelegateX(GameEvent e);
private Dictionary<System.Type, EventDelegateX> DelegatesMap = new Dictionary<System.Type, EventDelegateX>();
private Dictionary<System.Delegate, EventDelegateX> DelegateLookupMap = new Dictionary<System.Delegate, EventDelegateX>();
/// <summary>
/// 添加Listener
/// </summary>
public void AddListener<T>(EventDelegateX<T> del) where T : GameEvent
{
EventDelegateX internalDelegate = (e) => { del((T)e); };
//已存在,返回
if (DelegateLookupMap.ContainsKey(del) && DelegateLookupMap[del] == internalDelegate)
{
return;
}
//加入delegateLookup中
DelegateLookupMap[del] = internalDelegate;
//加入delegates中
EventDelegateX tempDel;
if (DelegatesMap.TryGetValue(typeof(T), out tempDel))
{
DelegatesMap[typeof(T)] = tempDel += internalDelegate;
}
else {
DelegatesMap[typeof(T)] = internalDelegate;
}
}
/// <summary>
/// 删除Listener
/// </summary>
public void RemoveListener<T>(EventDelegateX<T> del) where T : GameEvent
{
EventDelegateX internalDelegate;
if (DelegateLookupMap.TryGetValue(del, out internalDelegate))
{
EventDelegateX tempDel;
if (DelegatesMap.TryGetValue(typeof(T), out tempDel))
{
tempDel -= internalDelegate;
if (tempDel == null)
{
DelegatesMap.Remove(typeof(T));
}
else {
DelegatesMap[typeof(T)] = tempDel;
}
}
DelegateLookupMap.Remove(del);
}
}
/// <summary>
/// 在队列中加入事件
/// </summary>
public void AddEventToQueue(GameEvent e)
{
EventDelegateX del;
if (DelegatesMap.TryGetValue(e.GetType(), out del))
{
del.Invoke(e);
}
}
}
/// <summary>
/// 事件优先级枚举
/// </summary>
public enum MessagePriority
{
Low,
Medium,
High
}
/// <summary>
/// 事件接口
/// </summary>
public interface IMessageEvent
{
DateTime timeRaised { get; }
float displayTime { get; }
MessagePriority priority { get; }
object message { get; }
}
/// <summary>
/// 事件实体类
/// </summary>
public class MessageEvent : GameEvent, IMessageEvent
{
public DateTime timeRaised { private set; get; }
public MessagePriority priority { private set; get; }
public float displayTime { private set; get; }
public object message { private set; get; }
public MessageEvent(object message, float displayTime, MessagePriority priority)
{
this.message = message;
this.displayTime = displayTime;
this.priority = priority;
timeRaised = DateTime.Now;
}
}
/// <summary>
/// 事件抽象类
/// </summary>
public abstract class GameEvent
{
}
}