NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/System/SystemManager.cs

115 lines
2.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using cfg;
using Framework;
using Framework.Condition;
using PhxhSDK;
using UnityEngine;
public class SystemRuntimeInfo
{
private DataSystemCfg _cfg;
private bool _isOpen;
public ESystemType Type => _cfg.Id;
public int[] unlockCondition => _cfg.Unlock;
public bool IsOpen
{
get => _isOpen;
set
{
if (_isOpen != value)
{
_isOpen = value;
DebugUtil.Log($"系统{Type} {(_isOpen?"":"")}");
}
}
}
public SystemRuntimeInfo(DataSystemCfg cfg)
{
_cfg = cfg;
if (unlockCondition == null || unlockCondition.Length == 0)
{
IsOpen = true;
return;
}
ConditionReactor.Instance.AddReaction(unlockCondition, OnConditionInvoke);
}
private void OnConditionInvoke(bool conditionResult, object callBackParam, int reactionID)
{
if (conditionResult)
{
IsOpen = true;
ConditionReactor.Instance.RemoveReaction(reactionID);
}
}
}
/// <summary>
/// 管理游戏中所有的系统
/// </summary>
public class SystemManager : Singlenton<SystemManager>, IInitable
{
private Dictionary<ESystemType, SystemRuntimeInfo> _allSystemInfo = new();
#region MainLife
public void Init()
{
RegisterSignal();
}
private void RegisterSignal()
{
EventManager.Instance.Register(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
}
private void OnAllConfigLoad()
{
var allSystemCfg = TableManager.Instance.Tables.SystemCfg.DataMap;
_allSystemInfo.Clear();
foreach (var kv in allSystemCfg)
{
var systemType = kv.Key;
if (systemType == ESystemType.None)
continue;
SystemRuntimeInfo systemRuntimeInfo = new(kv.Value);
_allSystemInfo.Add(systemType, systemRuntimeInfo);
}
}
private void UnRegisterSignal()
{
EventManager.Instance.Unregister(EventManager.EventName.AllConfigLoad, OnAllConfigLoad);
}
public void Release()
{
UnRegisterSignal();
}
#endregion
#region API
public SystemRuntimeInfo GetSystemRuntimeInfo(ESystemType systemType)
{
return _allSystemInfo.GetValueOrDefault(systemType);
}
public bool IsSystemOpen(ESystemType systemType)
{
var systemInfo = GetSystemRuntimeInfo(systemType);
if (systemInfo == null)
return true;
return systemInfo.IsOpen;
}
#endregion
}