geometry-tower-defense/Assets/GameMain/Scripts/CustomComponent/CombatNode/CombatEventBridge.cs

152 lines
5.2 KiB
C#

using System;
using GameFramework.Event;
using UnityGameFramework.Runtime;
namespace GeometryTD.CustomComponent
{
internal sealed class CombatEventBridge
{
private Action<ShowEntitySuccessEventArgs> _onShowEntitySuccess;
private Action<ShowEntityFailureEventArgs> _onShowEntityFailure;
private Action<HideEntityCompleteEventArgs> _onHideEntityComplete;
private Action<OpenUIFormSuccessEventArgs> _onOpenUIFormSuccess;
private Action<OpenUIFormFailureEventArgs> _onOpenUIFormFailure;
private Action<CloseUIFormCompleteEventArgs> _onCloseUIFormComplete;
private bool _isEntityEventSubscribed;
private bool _isUIEventSubscribed;
public void Bind(
Action<ShowEntitySuccessEventArgs> onShowEntitySuccess,
Action<ShowEntityFailureEventArgs> onShowEntityFailure,
Action<HideEntityCompleteEventArgs> onHideEntityComplete,
Action<OpenUIFormSuccessEventArgs> onOpenUIFormSuccess,
Action<OpenUIFormFailureEventArgs> onOpenUIFormFailure,
Action<CloseUIFormCompleteEventArgs> onCloseUIFormComplete)
{
_onShowEntitySuccess = onShowEntitySuccess;
_onShowEntityFailure = onShowEntityFailure;
_onHideEntityComplete = onHideEntityComplete;
_onOpenUIFormSuccess = onOpenUIFormSuccess;
_onOpenUIFormFailure = onOpenUIFormFailure;
_onCloseUIFormComplete = onCloseUIFormComplete;
EnsureEntityEventSubscribed();
EnsureUIEventSubscribed();
}
public void Unbind()
{
UnsubscribeEntityEvents();
UnsubscribeUIEvents();
_onShowEntitySuccess = null;
_onShowEntityFailure = null;
_onHideEntityComplete = null;
_onOpenUIFormSuccess = null;
_onOpenUIFormFailure = null;
_onCloseUIFormComplete = null;
}
private void EnsureEntityEventSubscribed()
{
if (_isEntityEventSubscribed)
{
return;
}
GameEntry.Event.Subscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
GameEntry.Event.Subscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
GameEntry.Event.Subscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
_isEntityEventSubscribed = true;
}
private void EnsureUIEventSubscribed()
{
if (_isUIEventSubscribed)
{
return;
}
GameEntry.Event.Subscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
GameEntry.Event.Subscribe(OpenUIFormFailureEventArgs.EventId, OnOpenUIFormFailure);
GameEntry.Event.Subscribe(CloseUIFormCompleteEventArgs.EventId, OnCloseUIFormComplete);
_isUIEventSubscribed = true;
}
private void UnsubscribeEntityEvents()
{
if (!_isEntityEventSubscribed)
{
return;
}
GameEntry.Event.Unsubscribe(ShowEntitySuccessEventArgs.EventId, OnShowEntitySuccess);
GameEntry.Event.Unsubscribe(ShowEntityFailureEventArgs.EventId, OnShowEntityFailure);
GameEntry.Event.Unsubscribe(HideEntityCompleteEventArgs.EventId, OnHideEntityComplete);
_isEntityEventSubscribed = false;
}
private void UnsubscribeUIEvents()
{
if (!_isUIEventSubscribed)
{
return;
}
GameEntry.Event.Unsubscribe(OpenUIFormSuccessEventArgs.EventId, OnOpenUIFormSuccess);
GameEntry.Event.Unsubscribe(OpenUIFormFailureEventArgs.EventId, OnOpenUIFormFailure);
GameEntry.Event.Unsubscribe(CloseUIFormCompleteEventArgs.EventId, OnCloseUIFormComplete);
_isUIEventSubscribed = false;
}
private void OnShowEntitySuccess(object sender, GameEventArgs e)
{
if (e is ShowEntitySuccessEventArgs args)
{
_onShowEntitySuccess?.Invoke(args);
}
}
private void OnShowEntityFailure(object sender, GameEventArgs e)
{
if (e is ShowEntityFailureEventArgs args)
{
_onShowEntityFailure?.Invoke(args);
}
}
private void OnHideEntityComplete(object sender, GameEventArgs e)
{
if (e is HideEntityCompleteEventArgs args)
{
_onHideEntityComplete?.Invoke(args);
}
}
private void OnOpenUIFormSuccess(object sender, GameEventArgs e)
{
if (e is OpenUIFormSuccessEventArgs args)
{
_onOpenUIFormSuccess?.Invoke(args);
}
}
private void OnOpenUIFormFailure(object sender, GameEventArgs e)
{
if (e is OpenUIFormFailureEventArgs args)
{
_onOpenUIFormFailure?.Invoke(args);
}
}
private void OnCloseUIFormComplete(object sender, GameEventArgs e)
{
if (e is CloseUIFormCompleteEventArgs args)
{
_onCloseUIFormComplete?.Invoke(args);
}
}
}
}