You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

243 lines
7.8 KiB

using System;
using System.Collections.Generic;
using CoreGame.Render;
using xFrame;
// public delegate void EventCallBack(object self, int eventId, object value);
// public class CallbackWrapper
// {
// public static int s_NextHandle = 0;
// private CallbackWrapper(EventCallBack callback, object self)
// {
// this.callback = callback;
// this.self = self;
// }
//
// public void GenerateNewHandle()
// {
// m_Handle = ++s_NextHandle;
// }
// public static bool operator ==(in CallbackWrapper a, in CallbackWrapper b)
// {
// return b != null && a != null && a.m_Handle == b.m_Handle;
// }
//
// public static bool operator !=(in CallbackWrapper a, in CallbackWrapper b)
// {
// return b != null && a != null && a.m_Handle != b.m_Handle;
// }
// private int m_Handle;
// public int Handle => m_Handle;
// public EventCallBack callback = null;
// public object self = null;
// public bool isDelete = false;
// /// <summary>
// /// 创建之后必须存起来,取消注册用
// /// </summary>
// /// <param name="callback"></param>
// /// <param name="self"></param>
// /// <returns></returns>
//
// // public void SetInValid()
// // {
// // isDelete = true;
// // }
// public static CallbackWrapper New(EventCallBack callback, object self = null)
// {
// // pool
// var callbackWrapper = new CallbackWrapper(callback, self);
// callbackWrapper.GenerateNewHandle();
// return callbackWrapper;
// }
//
// public static void Free(ref CallbackWrapper wrapper)
// {
//
// }
// }
//
// public class CallbackData
// {
// #if !GAME_RELEASE
// public string name;
// #endif
// public readonly List<CallbackWrapper> callbackList = new();
// }
//
public partial class CombatEntity
{
private readonly Dictionary<int, CallbackData> m_EventMap = new();
public void ClearAllEvent()
{
try
{
m_EventMap.Clear();
}
catch (Exception e)
{
XLog.LogError(e.ToString());
}
}
public void AddEventCallBack(ClientEvent evtId, in CallbackWrapper callback)
{
m_EventMap.TryGetValue((int)evtId, out var callbackData);
if (callbackData == null)
{
callbackData = new CallbackData();
#if !GAME_RELEASE
callbackData.name = "EventMgr." + evtId;
#endif
m_EventMap.Add((int)evtId, callbackData);
}
BattleLogger.LogInfoByEntity(this,$"AddEventCallBack {evtId}");
callbackData.callbackList.Add(callback);
}
public void RemoveCallback(ClientEvent evtId, ref CallbackWrapper callback)
{
callback.isDelete = true;
// todo tick
m_EventMap.TryGetValue((int)evtId, out var callbackData);
if (callbackData == null)
{
BattleLogger.LogInfoByEntity(this,$"消息{evtId},添加和删除不匹配, 严格检测");
return;
}
BattleLogger.LogInfoByEntity(this,$"RemoveCallback {evtId}");
callbackData.callbackList.Remove(callback);
}
public void DispatchEvent(ClientEvent evtId, object value)
{
if (m_EventMap.TryGetValue((int)evtId, out var callbackData))
{
for(int i = 0; i < callbackData.callbackList.Count; ++i)
{
var wrapper = callbackData.callbackList[i];
BattleLogger.LogInfoByEntity(this, $"DispatchEvent isDelete: {wrapper.isDelete} {evtId} => {value}");
if (!wrapper.isDelete)
{
wrapper.callback(wrapper.self, (int)evtId, value);
}
}
}
}
}
namespace CoreGame.Render
{
/// <summary>
/// 事件服务,使用的时候暂时要区分逻辑表现事件
/// </summary>
// EventSrv.AddLogicEventCallBack(ClientEvent.EndChangeHpEvt, null);
// EventSrv.DispatchLogicEvent(ClientEvent.EndChangeHpEvt, null);
// EventSrv.RemoveLogicCallback(ClientEvent.EndChangeHpEvt, null);
public static class EventSrv
{
private static Dictionary<int, CallbackData> s_EventCallBackMap = new Dictionary<int, CallbackData>();
private static List<int> s_RemoveCallbackList = new List<int>();
private static int s_StackCount = 0;
public static void Init()
{
// s_StackCount = 0;
}
public static void UnInit()
{
// s_EventCallBackMap.Clear();
// s_RemoveCallbackList.Clear();
}
public static void AddEventCallBack(ClientEvent evtId, in CallbackWrapper callback)
{
s_EventCallBackMap.TryGetValue((int)evtId, out var callbackData);
if (callbackData == null)
{
callbackData = new CallbackData();
#if !GAME_RELEASE
callbackData.name = "EventMgr." + evtId;
#endif
s_EventCallBackMap.Add((int)evtId, callbackData);
}
BattleLogger.LogInfo($"【Global】 AddEventCallBack {evtId}");
callbackData.callbackList.Add(callback);
}
public static void DispatchLogicEvent(ClientEvent evtId, object value)
{
if (s_EventCallBackMap.TryGetValue((int)evtId, out var callbackData))
{
for(int i = 0; i < callbackData.callbackList.Count; ++i)
{
var wrapper = callbackData.callbackList[i];
BattleLogger.LogInfo($"【Global】 DispatchEvent isDelete: {wrapper.isDelete} {evtId} => {value}");
if (!wrapper.isDelete)
{
wrapper.callback(wrapper.self, (int)evtId, value);
}
}
}
}
public static void RemoveCallback(ClientEvent id, ref CallbackWrapper callback)
{
s_EventCallBackMap.TryGetValue((int)id, out var callbackData);
int count = 0;
if (callbackData != null)
{
for (int i = 0; i < callbackData.callbackList.Count; ++i)
{
if (callbackData.callbackList[i].Handle == callback.Handle)
{
callbackData.callbackList[i].isDelete = true;
count++;
}
}
}
if (count > 1)
{
// 如果有特殊需求的话 可以在add的时候返回callbackid 根据callbackid删除
BattleLogger.LogInfo("RemoveCallback中存在多个符合的项, 理论上remove的时候列表中只应该有一个符合项");
}
s_RemoveCallbackList.Add((int)id);
}
public static void Tick()
{
if (s_RemoveCallbackList.Count > 0)
{
for (var index = 0; index < s_RemoveCallbackList.Count; index++)
{
RemoveAllDeleteCallback(s_RemoveCallbackList[index]);
}
}
s_RemoveCallbackList.Clear();
}
static void RemoveAllDeleteCallback(int id)
{
s_EventCallBackMap.TryGetValue(id, out var callbackData);
if (callbackData != null)
{
for (int i = 0; i < callbackData.callbackList.Count;)
{
if (callbackData.callbackList[i].isDelete)
{
callbackData.callbackList.RemoveAt(i);
BattleLogger.LogInfo($"【Global】 RemoveAllDeleteCallback {(ClientEvent) id}");
}
else
{
i++;
}
}
}
}
}
}