using System;
using System.Collections.Generic;
using System.Threading;
namespace GameLogic
{
///
/// event execute
///
internal struct GameEventInfo {
public GameEventHandler eventHandler;
public object[] args;
}
///
/// Event handler.
/// external event function format
///
public delegate void GameEventHandler(params object[] args);
public class GameEvent {
///
/// event execute list
///
private List mAsyncEventList;
///
/// The event list.
///
private Dictionary> mEventDic;
///
/// just use once of event
///
private Dictionary mUseOnceEventDic;
public GameEvent() {
mAsyncEventList = new List();
mEventDic = new Dictionary>();
mUseOnceEventDic = new Dictionary();
}
///
/// Adds the event.
///
public void AddEvent(string type, GameEventHandler handler, bool isUseOnce = false) {
List handlerList = mEventDic.ContainsKey(type) ? mEventDic[type] : null;
if (handlerList == null) {
mEventDic[type] = new List();
}
if (mEventDic[type].Contains(handler))
return;
mEventDic[type].Add(handler);
if (isUseOnce && !mUseOnceEventDic.ContainsKey(type))
mUseOnceEventDic.Add(type, handler);
}
///
/// Removes the event.
///
public void RemoveEvent(string type, GameEventHandler handler) {
List handlerList = mEventDic.ContainsKey(type) ? mEventDic[type] : null;
if (handlerList != null && handlerList.Contains(handler)) {
handlerList.Remove(handler);
}
if (mUseOnceEventDic.ContainsKey(type) && mUseOnceEventDic[type] == handler)
mUseOnceEventDic.Remove(type);
}
///
/// Removes All this type of event.
///
public void RemoveEvent(string type) {
if (mEventDic.ContainsKey(type))
mEventDic.Remove(type);
if (mUseOnceEventDic.ContainsKey(type))
mUseOnceEventDic.Remove(type);
}
///
/// remove all event
///
public void RemoveEvent() {
mEventDic.Clear();
mUseOnceEventDic.Clear();
}
///
/// Dispatch the specified type, target and args. sync type.
///
public void DispatchEvent(string type, params object[] args) {
List handlerList = mEventDic.ContainsKey(type) ? mEventDic[type] : null;
if (handlerList != null && HasEvent(type)) {
for (short i = 0; i < handlerList.Count; i++) {
handlerList[i](args);
if (mUseOnceEventDic.ContainsKey(type) && mUseOnceEventDic[type] == handlerList[i])
RemoveEvent(type, handlerList[i]);
}
}
}
///
/// Dispatch the specified type, target and args. async type, in idle frame execute function
///
public void DispatchAsyncEvent(string type, params object[] args) {
List handlerList = mEventDic.ContainsKey(type) ? mEventDic[type] : null;
if (handlerList != null && HasEvent(type)) {
for (short i = 0; i < handlerList.Count; i++) {
mAsyncEventList.Add(new GameEventInfo() { args = args, eventHandler = handlerList[i] });
if (mUseOnceEventDic.ContainsKey(type) && mUseOnceEventDic[type] == handlerList[i])
RemoveEvent(type, handlerList[i]);
}
}
}
///
/// Dispatch the specified type, target and args. in new child thread execute function
///
[System.Obsolete("Do not use temporary")]
public void DispatchThreadEvent(string type, object args) {
///////////////
List handlerList = mEventDic.ContainsKey(type) ? mEventDic[type] : null;
if (handlerList != null && HasEvent(type)) {
for (short i = 0; i < handlerList.Count; i++) {
Thread thread = new Thread((object arg) => handlerList[i]());
thread.Start(args);
if (mUseOnceEventDic.ContainsKey(type) && mUseOnceEventDic[type] == handlerList[i])
RemoveEvent(type, handlerList[i]);
}
}
}
///
///
///
///
///
public bool HasEvent(string type) {
return mEventDic.ContainsKey(type);
}
///
/// 消息循环
///
public void UpdateEvent()
{
for (short i = 0; i < mAsyncEventList.Count; i++) {
GameEventInfo taskEvent = mAsyncEventList[i];
mAsyncEventList.Remove(taskEvent);
taskEvent.eventHandler(taskEvent.args);
}
}
///
///
///
public void Dispose()
{
mUseOnceEventDic.Clear();
mEventDic.Clear();
mAsyncEventList.Clear();
mAsyncEventList = null;
mEventDic = null;
mUseOnceEventDic = null;
}
}
}