Files
ft/Client/Assets/Scripts/Activity/Sentry/AActivitySentry.cs
2026-06-29 21:18:33 +08:00

178 lines
5.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Reflection;
using System.Collections.Generic;
using Activity.HomeEntranceBtn;
using OrderManager;
using asap.core;
using cfg;
using UnityEngine;
namespace Activity
{
/// <summary>
/// 活动哨兵基类 - 只负责执行活动开关,不负责监听和判断
/// </summary>
public abstract class AActivitySentry<TM,TD> : IActivitySentry where TM : AEventDataManager<TD> where TD : IEventData ,new()
{
protected AActivitySentry()
{
var attribute = GetType().GetCustomAttribute<ActivitySentryAttribute>();
if (attribute == null)
{
Debug.LogError($"{GetType().Name} must have ActivitySentryAttribute");
return;
}
EventType = attribute.EventType;
EventSubType = attribute.EventSubType;
}
public int EventType { get; }
public int EventSubType { get; }
public IHomeEntranceBtn HomeEntranceBtn { get; set; }
public FishingEvent CurrentEventConfig { get; set; }
public List<FishingEvent> RelatedEvents { get; set; }
protected bool IsActive { get; private set; } = false;
// DI 注入的依赖(用于替代静态方法调用)
protected ActivityScopeManager _scopeManager;
/// <summary>
/// 由 ActivityManager 在创建 Sentry 时调用,注入依赖
/// </summary>
public void InjectDependencies(ActivityScopeManager scopeManager )
{
_scopeManager = scopeManager;
}
/// <summary>
/// 便捷方法:解析活动管理器
/// </summary>
protected TM ResolveManager()
{
return _scopeManager.GetOrRegisterInstance<TM>((EventType, EventSubType));
}
/// <summary>
/// 活动开启(由 Manager 调用)
/// </summary>
public void Open(FishingEvent fishingEvent)
{
if (fishingEvent == null) return;
var previousEventConfig = CurrentEventConfig;
CurrentEventConfig = fishingEvent;
IsActive = true;
// 如果是切换到新活动,先调用切换钩子
if (previousEventConfig != fishingEvent)
OnEventChanged(previousEventConfig, fishingEvent);
// 调用激活钩子
OnEventActive(fishingEvent);
}
/// <summary>
/// 活动关闭(由 Manager 调用)
/// </summary>
public void Close(FishingEvent fishingEvent)
{
if (fishingEvent == null) return;
OnEventClosed(fishingEvent);
IsActive = false;
CurrentEventConfig =null;
}
public void RegisterHomeEntranceBtn(IHomeEntranceBtn homeEntranceBtn)
{
HomeEntranceBtn = homeEntranceBtn;
if(IsActive)
homeEntranceBtn?.OnOpen(CurrentEventConfig,ResolveManager());
}
public void UnregisterHomeEntranceBtn()
{
HomeEntranceBtn = null;
}
/// <inheritdoc />
public virtual void OnActivityTokenBalanceChanged(int eventId)
{
if (!IsActive || CurrentEventConfig == null || CurrentEventConfig.ID != eventId)
return;
var manager = ResolveManager();
if (manager == null)
return;
if (manager is IActivityEventRedPoint<TD> red)
{
try
{
red.SetRedPoint(manager.LoadData());
}
catch (Exception e)
{
Debug.LogError($"[{GetType().Name}] SetRedPoint after token balance change failed: {e.Message}");
}
}
}
#region
/// <summary>
/// 活动切换时调用
/// </summary>
protected virtual void OnEventChanged(FishingEvent previousEventConfig, FishingEvent newEventConfig)
{
if(newEventConfig==null) return;
ResolveManager()?.RefreshData(newEventConfig);
}
/// <summary>
/// 活动激活时调用
/// </summary>
protected virtual void OnEventActive(FishingEvent fishingEvent)
{
// 活动开启时注册到 OrderManager
var manager = ResolveManager();
if (manager is IOrderReplenishment repl)
OrderManager.OrderManager.RegisterReplenishment(repl);
if (manager is IOrderSuccess succ)
OrderManager.OrderManager.RegisterOrderSuccess(succ);
//todo 这里通知homepanle 实例化 homeEntranceBtn prefab
HomeEntranceBtn?.OnOpen(fishingEvent, manager);
}
/// <summary>
/// 活动关闭时调用
/// </summary>
protected virtual void OnEventClosed(FishingEvent fishingEvent)
{
// 活动关闭时取消注册
var manager = ResolveManager();
if (manager is IOrderReplenishment repl)
OrderManager.OrderManager.UnregisterReplenishment(repl);
if (manager is IOrderSuccess succ)
OrderManager.OrderManager.UnregisterOrderSuccess(succ);
// 通知按钮活动关闭
HomeEntranceBtn?.OnClose(fishingEvent);
}
#endregion
#region IDisposable
public virtual void Dispose()
{
if (!IsActive) return;
Close(CurrentEventConfig);
}
#endregion
}
}