using System; using GameFramework.Event; using UnityGameFramework.Runtime; namespace GeometryTD.CustomComponent { internal sealed class CombatEventBridge { private Action _onShowEntitySuccess; private Action _onShowEntityFailure; private Action _onHideEntityComplete; private Action _onOpenUIFormSuccess; private Action _onOpenUIFormFailure; private Action _onCloseUIFormComplete; private bool _isEntityEventSubscribed; private bool _isUIEventSubscribed; public void Bind( Action onShowEntitySuccess, Action onShowEntityFailure, Action onHideEntityComplete, Action onOpenUIFormSuccess, Action onOpenUIFormFailure, Action 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); } } } }