162 lines
5.5 KiB
C#
162 lines
5.5 KiB
C#
|
|
using System;
|
||
|
|
using System.Collections;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using JetBrains.Annotations;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
public class CardEventOS : MonoBehaviour
|
||
|
|
{
|
||
|
|
public static CardEventOS Instance { get; private set; }
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
if (Instance != null && Instance != this)
|
||
|
|
{
|
||
|
|
Destroy(gameObject);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
Instance = this;
|
||
|
|
DontDestroyOnLoad(gameObject);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
private readonly HashSet<CardEventData.LOCK_EXCLUSIVE_CARD_ENUM> _exclusive_lock_queue = new();
|
||
|
|
|
||
|
|
public bool LOCK_APPLY(CardEventData.LOCK_EXCLUSIVE_CARD_ENUM lock_name)
|
||
|
|
{
|
||
|
|
if (!_exclusive_lock_queue.Add(lock_name))
|
||
|
|
{
|
||
|
|
print($"拒绝加锁: {lock_name}");
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
print($"申请加锁: {lock_name}");
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void LOCK_RELEASE(CardEventData.LOCK_EXCLUSIVE_CARD_ENUM lock_name)
|
||
|
|
{
|
||
|
|
if (!_exclusive_lock_queue.Contains(lock_name)) return;
|
||
|
|
_exclusive_lock_queue.Remove(lock_name);
|
||
|
|
print($"释放锁: {lock_name}");
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool LOCK_GET(CardEventData.LOCK_EXCLUSIVE_CARD_ENUM lock_name)
|
||
|
|
{
|
||
|
|
return _exclusive_lock_queue.Contains(lock_name);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
// *************** 事件系统 ****************
|
||
|
|
private readonly Dictionary<CardEventData.EVENT_REGISTER_CARD_ENUM, List<Action<object>>> eventActions = new();
|
||
|
|
|
||
|
|
private readonly Dictionary<CardEventData.EVENT_REGISTER_CARD_ENUM, Type> eventBindMap = new()
|
||
|
|
{
|
||
|
|
{ CardEventData.EVENT_REGISTER_CARD_ENUM.EXCUSE_ME_PLEASE_LET_ME_DRAW_CARD, typeof(CardEventData.STRUCT_EVENT_DRAW_CARD) },
|
||
|
|
};
|
||
|
|
|
||
|
|
public void EVENT_REGISTER<T>(CardEventData.EVENT_REGISTER_CARD_ENUM event_name, Action<T> callback) where T : struct
|
||
|
|
{
|
||
|
|
if (!eventBindMap.ContainsKey(event_name))
|
||
|
|
{
|
||
|
|
Debug.LogError($"注册事件错误: 未绑定的数据类型{event_name}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (eventBindMap[event_name] != typeof(T))
|
||
|
|
{
|
||
|
|
Debug.LogError($"注册事件错误: 数据类型不匹配{event_name}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!eventActions.ContainsKey(event_name))
|
||
|
|
eventActions[event_name] = new List<Action<object>>();
|
||
|
|
void wrapper(object obj) => callback((T)obj);
|
||
|
|
eventActions[event_name].Add(wrapper);
|
||
|
|
Debug.Log($"注册事件: {event_name}");
|
||
|
|
}
|
||
|
|
|
||
|
|
public void EVENT_TRIGGER(CardEventData.EVENT_REGISTER_CARD_ENUM event_name, object data)
|
||
|
|
{
|
||
|
|
if (!eventBindMap.ContainsKey(event_name))
|
||
|
|
{
|
||
|
|
Debug.LogError($"触发事件错误: 未绑定的数据类型{event_name}");
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (data == null || data.GetType() != eventBindMap[event_name])
|
||
|
|
{
|
||
|
|
Debug.LogError($"触发事件错误: 数据类型不匹配{event_name}");
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!eventActions.ContainsKey(event_name))
|
||
|
|
{
|
||
|
|
Debug.LogError($"触发事件错误: 未找到事件");
|
||
|
|
}
|
||
|
|
|
||
|
|
eventActions[event_name].ForEach(action => action.Invoke(data));
|
||
|
|
Debug.Log($"触发事件: {event_name}");
|
||
|
|
}
|
||
|
|
|
||
|
|
// public void EVENT_UNREGISTER(CardEventData.EVENT_REGISTER_CARD_ENUM event_name, Action<object> callback)
|
||
|
|
// {
|
||
|
|
// if (eventActions.TryGetValue(event_name, out var callback_list))
|
||
|
|
// {
|
||
|
|
// callback_list.Remove(callback);
|
||
|
|
// if (callback_list.Count == 0)
|
||
|
|
// eventActions.Remove(event_name);
|
||
|
|
// Debug.Log($"取消注册事件: {event_name}");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
|
||
|
|
// ============ 触发事件 ============
|
||
|
|
|
||
|
|
|
||
|
|
// public void EVENT_REGISTER<T>(CardEventData.EVENT_REGISTER_CARD_ENUM event_name, Action<T> callback) where T : struct
|
||
|
|
// {
|
||
|
|
// if (!eventMap.TryGetValue(typeof(T), out var eventQueue))
|
||
|
|
// {
|
||
|
|
// Debug.LogError($"未找到与类型 {typeof(T).Name} 匹配的事件字典!");
|
||
|
|
// return;
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// register_event();
|
||
|
|
//
|
||
|
|
// void register_event()
|
||
|
|
// {
|
||
|
|
// if (!eventQueue.ContainsKey(event_name))
|
||
|
|
// eventQueue[event_name] = callback;
|
||
|
|
// else
|
||
|
|
// eventQueue[event_name] += callback;
|
||
|
|
// print($"注册事件: {event_name}");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// // ============= 取消注册 =============
|
||
|
|
// public void EVENT_UNREGISTER(EVENT_REGISTER_CARD_ENUM event_name, Action<object> callback)
|
||
|
|
// {
|
||
|
|
// void unregister_event(Dictionary<EVENT_REGISTER_CARD_ENUM, Action<object>> eventQueue)
|
||
|
|
// {
|
||
|
|
// if (!eventQueue.ContainsKey(event_name)) return;
|
||
|
|
// eventQueue[event_name] -= callback;
|
||
|
|
// print($"取消注册事件: {event_name}");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
//
|
||
|
|
// // ============= 触发事件 =============
|
||
|
|
// public void EVENT_RAISE(EVENT_REGISTER_CARD_ENUM event_name, object args)
|
||
|
|
// {
|
||
|
|
// void raise_event(Dictionary<EVENT_REGISTER_CARD_ENUM, Action<object>> eventQueue)
|
||
|
|
// {
|
||
|
|
// if (eventQueue.ContainsKey(event_name))
|
||
|
|
// {
|
||
|
|
// print($"触发事件: {event_name}");
|
||
|
|
// eventQueue[event_name]?.Invoke(args);
|
||
|
|
// }
|
||
|
|
// else
|
||
|
|
// print($"未找到事件: {event_name}");
|
||
|
|
// }
|
||
|
|
// }
|
||
|
|
// // *************************************
|
||
|
|
}
|