NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Framework/Condition/ConditionManager.cs

91 lines
2.6 KiB
C#
Raw Normal View History

2023-11-01 13:33:20 +08:00
using System;
using System.Reflection;
2023-11-01 15:03:24 +08:00
using cfg;
2023-11-01 13:33:20 +08:00
using cfg.ActorCfg;
using PhxhSDK;
using static Framework.EventManager;
namespace Framework.Condition
{
public interface ICondition
{
public EventName SignalName { get; }
2023-11-01 15:03:24 +08:00
bool Check(cfg.DataCondition cfg);
2023-11-01 13:33:20 +08:00
}
public sealed class ConditionManager : Singlenton<ConditionManager>, IInitable
{
private const string CONDITION_PREFIX = "Condition";
private ICondition[] m_conditions;
public void Init()
{
2023-11-01 15:21:28 +08:00
m_conditions = new ICondition[(int)ConditionType.Max];
for (int i = 1; i < (int)ConditionType.Max; i++)
2023-11-01 13:33:20 +08:00
{
2023-11-01 15:21:28 +08:00
var conditionType = ((ConditionType)i).ToString();
2023-11-01 13:33:20 +08:00
var typeName = CONDITION_PREFIX + conditionType;
var type = CommonUtilsFramework.GamePlayAssembly.GetType(typeName);
if (type == null)
{
DebugUtil.LogError("Cant find Type,name:{0}", typeName);
continue;
}
if (Activator.CreateInstance(type) is ICondition condition)
{
m_conditions[i] = condition;
}
}
}
public void Release()
{
m_conditions = null;
}
2023-11-01 15:03:24 +08:00
private ICondition GetCondition(int conditionID, out DataCondition conditionCfg)
2023-11-01 13:33:20 +08:00
{
2023-11-01 15:03:24 +08:00
conditionCfg = TableManager.Instance.Tables.ConditionConfig.Get((int)conditionID);
2023-11-01 13:33:20 +08:00
if (conditionCfg == null)
{
throw new Exception($"cant find condition cfg!condition ID:{conditionID}");
}
if (m_conditions[(int)conditionCfg.Type] == null)
{
throw new Exception(
$"condition type is outside of range!condition ID:{conditionID} type:{(int)conditionCfg.Type}");
}
return m_conditions[(int)conditionCfg.Type];
}
public bool CheckCondition(int conditionID)
{
return GetCondition(conditionID, out var cfg).Check(cfg);
}
2023-11-01 16:18:08 +08:00
public bool CheckCondition(int[] conditionGroup)
{
foreach (var conditionID in conditionGroup)
{
var isSuccess = CheckCondition(conditionID);
if (!isSuccess)
{
return false;
}
}
return true;
}
2023-11-01 13:33:20 +08:00
public EventName GetConditionSignal(int conditionID)
{
return GetCondition(conditionID, out var cfg).SignalName;
}
}
}