135 lines
3.7 KiB
C#
135 lines
3.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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(List<float> args);
|
|
}
|
|
|
|
|
|
public sealed class ConditionManager : Singlenton<ConditionManager>, IInitable
|
|
{
|
|
private const string CONDITION_PREFIX = "Condition";
|
|
|
|
//避免GC
|
|
private List<float> _tempConditionArgs = new();
|
|
|
|
private ICondition[] m_conditions;
|
|
|
|
#region MainLife
|
|
|
|
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;
|
|
_tempConditionArgs = null;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region API
|
|
|
|
public bool CheckCondition(int conditionID)
|
|
{
|
|
var conditionCfg = GetConditionCfg(conditionID);
|
|
GetConditionCfgArgs(conditionCfg, ref _tempConditionArgs);
|
|
return CheckCondition(conditionCfg.Type, _tempConditionArgs);
|
|
}
|
|
|
|
public bool CheckCondition(int[] conditionGroup)
|
|
{
|
|
foreach (var conditionID in conditionGroup)
|
|
{
|
|
var isSuccess = CheckCondition(conditionID);
|
|
if (!isSuccess)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool CheckCondition(ConditionType conditionType, List<float> conditionArg)
|
|
{
|
|
if (m_conditions[(int)conditionType] == null)
|
|
{
|
|
throw new Exception(
|
|
$"condition type is outside of range!condition type:{(int)conditionType}");
|
|
}
|
|
|
|
return m_conditions[(int)conditionType].Check(conditionArg);
|
|
}
|
|
|
|
public EventName GetConditionSignal(int conditionID)
|
|
{
|
|
var condition = GetConditionByID(conditionID);
|
|
if (condition != null)
|
|
{
|
|
return condition.SignalName;
|
|
}
|
|
|
|
DebugUtil.LogError($"cant find condition!conditionID:{conditionID}");
|
|
return EventName.None;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region InnerFunction
|
|
|
|
private DataCondition GetConditionCfg(int conditionID)
|
|
{
|
|
return TableManager.Instance.Tables.ConditionConfig.Get((int)conditionID);
|
|
}
|
|
|
|
private ICondition GetConditionByID(int conditionID)
|
|
{
|
|
var conditionCfg = GetConditionCfg(conditionID);
|
|
if (conditionCfg != null && m_conditions[(int)conditionCfg.Type] != null)
|
|
{
|
|
return m_conditions[(int)conditionCfg.Type];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private void GetConditionCfgArgs(DataCondition conditionCfg, ref List<float> _args)
|
|
{
|
|
_args.Clear();
|
|
_args.Add(conditionCfg.Param1);
|
|
_args.Add(conditionCfg.Param2);
|
|
_args.Add(conditionCfg.Param3);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |