174 lines
6.7 KiB
C#
174 lines
6.7 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace SLSUtilities.Narrative
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 条件匹配评估工具类。
|
|||
|
|
/// 读取并比对 StorySystem.Variables 中的运行时状态,支持多种类型的变量与运算符。
|
|||
|
|
/// </summary>
|
|||
|
|
public static class NarrativeConditionEvaluator
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// 评估一系列条件是否全部满足 (AND 关系)。
|
|||
|
|
/// </summary>
|
|||
|
|
public static bool Evaluate(List<NarrativeCondition> conditions)
|
|||
|
|
{
|
|||
|
|
if (conditions == null || conditions.Count == 0)
|
|||
|
|
return true; // 无条件默认满足
|
|||
|
|
|
|||
|
|
if (StorySystem.Variables == null)
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning("[StorySystem] 评估条件失败:StorySystem.Variables 尚未初始化。");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
foreach (var cond in conditions)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(cond.key))
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning("[StorySystem] 条件配置错误:变量 key 为空。");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!EvaluateCondition(cond))
|
|||
|
|
return false; // 任何一个条件不满足,直接返回 false
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static bool EvaluateCondition(NarrativeCondition cond)
|
|||
|
|
{
|
|||
|
|
switch (cond.type)
|
|||
|
|
{
|
|||
|
|
case NarrativeCondition.ConditionType.Bool:
|
|||
|
|
return EvaluateBool(cond);
|
|||
|
|
case NarrativeCondition.ConditionType.Int:
|
|||
|
|
return EvaluateInt(cond);
|
|||
|
|
case NarrativeCondition.ConditionType.Float:
|
|||
|
|
return EvaluateFloat(cond);
|
|||
|
|
case NarrativeCondition.ConditionType.String:
|
|||
|
|
return EvaluateString(cond);
|
|||
|
|
default:
|
|||
|
|
Debug.LogWarning($"[StorySystem] 未知的条件类型: {cond.type}");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static bool EvaluateBool(NarrativeCondition cond)
|
|||
|
|
{
|
|||
|
|
bool currentVal = false;
|
|||
|
|
if (!StorySystem.Variables.boolVariables.TryGetValue(cond.key, out currentVal))
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning($"[StorySystem] 变量未找到: Bool '{cond.key}',使用默认值 false 匹配。");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!bool.TryParse(cond.value, out bool targetVal))
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"[StorySystem] 无法将目标值 '{cond.value}' 解析为 Bool。条件键: {cond.key}");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
switch (cond.op)
|
|||
|
|
{
|
|||
|
|
case NarrativeCondition.CompareOp.Equal:
|
|||
|
|
return currentVal == targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.NotEqual:
|
|||
|
|
return currentVal != targetVal;
|
|||
|
|
default:
|
|||
|
|
Debug.LogWarning($"[StorySystem] 布尔类型不支持比较运算符: {cond.op},必须为 Equal 或 NotEqual。");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static bool EvaluateInt(NarrativeCondition cond)
|
|||
|
|
{
|
|||
|
|
int currentVal = 0;
|
|||
|
|
if (!StorySystem.Variables.intVariables.TryGetValue(cond.key, out currentVal))
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning($"[StorySystem] 变量未找到: Int '{cond.key}',使用默认值 0 匹配。");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!int.TryParse(cond.value, out int targetVal))
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"[StorySystem] 无法将目标值 '{cond.value}' 解析为 Int。条件键: {cond.key}");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
switch (cond.op)
|
|||
|
|
{
|
|||
|
|
case NarrativeCondition.CompareOp.Equal:
|
|||
|
|
return currentVal == targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.NotEqual:
|
|||
|
|
return currentVal != targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.Greater:
|
|||
|
|
return currentVal > targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.GreaterOrEqual:
|
|||
|
|
return currentVal >= targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.Less:
|
|||
|
|
return currentVal < targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.LessOrEqual:
|
|||
|
|
return currentVal <= targetVal;
|
|||
|
|
default:
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static bool EvaluateFloat(NarrativeCondition cond)
|
|||
|
|
{
|
|||
|
|
float currentVal = 0f;
|
|||
|
|
if (!StorySystem.Variables.floatVariables.TryGetValue(cond.key, out currentVal))
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning($"[StorySystem] 变量未找到: Float '{cond.key}',使用默认值 0.0 匹配。");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!float.TryParse(cond.value, out float targetVal))
|
|||
|
|
{
|
|||
|
|
Debug.LogError($"[StorySystem] 无法将目标值 '{cond.value}' 解析为 Float。条件键: {cond.key}");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
switch (cond.op)
|
|||
|
|
{
|
|||
|
|
case NarrativeCondition.CompareOp.Equal:
|
|||
|
|
return Mathf.Approximately(currentVal, targetVal);
|
|||
|
|
case NarrativeCondition.CompareOp.NotEqual:
|
|||
|
|
return !Mathf.Approximately(currentVal, targetVal);
|
|||
|
|
case NarrativeCondition.CompareOp.Greater:
|
|||
|
|
return currentVal > targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.GreaterOrEqual:
|
|||
|
|
return currentVal >= targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.Less:
|
|||
|
|
return currentVal < targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.LessOrEqual:
|
|||
|
|
return currentVal <= targetVal;
|
|||
|
|
default:
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static bool EvaluateString(NarrativeCondition cond)
|
|||
|
|
{
|
|||
|
|
string currentVal = string.Empty;
|
|||
|
|
if (!StorySystem.Variables.stringVariables.TryGetValue(cond.key, out currentVal))
|
|||
|
|
{
|
|||
|
|
Debug.LogWarning($"[StorySystem] 变量未找到: String '{cond.key}',使用默认空字符串匹配。");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
string targetVal = cond.value ?? string.Empty;
|
|||
|
|
|
|||
|
|
switch (cond.op)
|
|||
|
|
{
|
|||
|
|
case NarrativeCondition.CompareOp.Equal:
|
|||
|
|
return currentVal == targetVal;
|
|||
|
|
case NarrativeCondition.CompareOp.NotEqual:
|
|||
|
|
return currentVal != targetVal;
|
|||
|
|
default:
|
|||
|
|
Debug.LogWarning($"[StorySystem] 字符串类型不支持比较运算符: {cond.op},必须为 Equal 或 NotEqual。");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|