91 lines
2.6 KiB
C#
91 lines
2.6 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using cfg;
|
|
using cfg.ActorCfg;
|
|
using PhxhSDK;
|
|
using static Framework.EventManager;
|
|
|
|
namespace Framework.Condition
|
|
{
|
|
public interface ICondition
|
|
{
|
|
public EventName SignalName { get; }
|
|
|
|
bool Check(cfg.DataCondition cfg);
|
|
}
|
|
|
|
|
|
public sealed class ConditionManager : Singlenton<ConditionManager>, IInitable
|
|
{
|
|
private const string CONDITION_PREFIX = "Condition";
|
|
|
|
private ICondition[] m_conditions;
|
|
|
|
public void Init()
|
|
{
|
|
m_conditions = new ICondition[(int)ConditionType.Max];
|
|
for (int i = 1; i < (int)ConditionType.Max; i++)
|
|
{
|
|
var conditionType = ((ConditionType)i).ToString();
|
|
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;
|
|
}
|
|
|
|
private ICondition GetCondition(int conditionID, out DataCondition conditionCfg)
|
|
{
|
|
conditionCfg = TableManager.Instance.Tables.ConditionConfig.Get((int)conditionID);
|
|
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);
|
|
}
|
|
|
|
public bool CheckCondition(int[] conditionGroup)
|
|
{
|
|
foreach (var conditionID in conditionGroup)
|
|
{
|
|
var isSuccess = CheckCondition(conditionID);
|
|
if (!isSuccess)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public EventName GetConditionSignal(int conditionID)
|
|
{
|
|
return GetCondition(conditionID, out var cfg).SignalName;
|
|
}
|
|
}
|
|
} |