feat: 扩展事件系统,改进变量处理和调试功能

本次提交对事件系统进行了多项增强和修复,主要包括:

-   为`Effect_SetVariable`添加了类型支持(Int, Float, String, Bool),允许更精确地设置变量类型 [在`EventDef_Wula.xml`, `EventDef_WULA_FE_Spiritualist.xml`, `Effect.cs`中]。
-   改进了`Condition`类,增加了类型检查和错误处理,避免了类型不匹配导致的错误 [在`Condition.cs`中]。
-   修复了`Effect_ModifyVariable`中的错误,允许使用变量名作为修改值,并支持int和float类型的操作 [在`Effect.cs`中]。
-   添加了`Effect_StoreFactionGoodwill`,用于存储派系好感度到变量中 [在`Effect.cs`中]。
-   增加了`Dialog_ManageEventVariables`,用于调试和管理事件变量 [在`DebugActions.cs`, `Dialog_ManageEventVariables.cs`中]。
-   改进了`EventVariableManager`,增加了类型转换的错误处理和日志记录,并添加了获取所有变量的函数 [在`EventVariableManager.cs`中]。

这些改动提高了事件系统的稳定性和可扩展性,并为调试提供了更多工具。
This commit is contained in:
2025-08-16 17:47:31 +08:00
parent c2be626ed5
commit 1a49972ea0
9 changed files with 326 additions and 74 deletions

View File

@@ -19,26 +19,53 @@ namespace WulaFallenEmpire
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
if (!eventVarManager.HasVariable(name))
{
eventVarManager.SetVariable(name, "0");
reason = $"Variable '{name}' not found.";
return false;
}
object variable = eventVarManager.GetVariable<object>(name);
string compareValue = value;
object variable = eventVarManager.GetVariable<object>(name);
string compareValueStr = value;
if (!string.IsNullOrEmpty(valueVariableName))
{
compareValue = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
if (compareValue == null)
compareValueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
if (compareValueStr == null)
{
reason = $"Comparison variable '{valueVariableName}' not set.";
return false;
}
}
bool met = variable.ToString() == compareValue;
bool met = false;
try
{
if (variable is int)
{
met = (int)variable == int.Parse(compareValueStr);
}
else if (variable is float)
{
met = (float)variable == float.Parse(compareValueStr);
}
else if (variable is bool)
{
met = (bool)variable == bool.Parse(compareValueStr);
}
else
{
met = variable?.ToString() == compareValueStr;
}
}
catch (System.Exception e)
{
Log.Warning($"[EventSystem] Condition_VariableEquals: Could not compare '{variable}' and '{compareValueStr}'. Error: {e.Message}");
reason = "Type mismatch or parsing error during comparison.";
return false;
}
if (!met)
{
reason = $"Requires {name} = {compareValue} (Current: {variable})";
reason = $"Requires {name} = {compareValueStr} (Current: {variable})";
}
else
{
@@ -62,6 +89,7 @@ namespace WulaFallenEmpire
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
if (!eventVarManager.HasVariable(name))
{
Log.Message($"[EventSystem] {GetType().Name}: Variable '{name}' not found, defaulting to 0f.");
eventVarManager.SetVariable(name, 0f);
}
@@ -74,11 +102,13 @@ namespace WulaFallenEmpire
if (float.IsNaN(compareValue))
{
reason = $"Comparison variable '{valueVariableName}' not set or not a number.";
Log.Warning($"[EventSystem] {GetType().Name} check for '{name}' failed: {reason}");
return false;
}
}
bool met = Compare(variable, compareValue);
Log.Message($"[EventSystem] {GetType().Name} check: Name='{name}', CurrentValue='{variable}', CompareValue='{compareValue}', Met={met}");
if (!met)
{
reason = $"Requires {name} {GetOperatorString()} {compareValue} (Current: {variable})";
@@ -126,26 +156,54 @@ namespace WulaFallenEmpire
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
if (!eventVarManager.HasVariable(name))
{
eventVarManager.SetVariable(name, "0");
reason = $"Variable '{name}' not found.";
return false;
}
object variable = eventVarManager.GetVariable<object>(name);
string compareValueStr = value;
string compareValue = value;
if (!string.IsNullOrEmpty(valueVariableName))
{
compareValue = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
if (compareValue == null)
compareValueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
if (compareValueStr == null)
{
reason = $"Comparison variable '{valueVariableName}' not set.";
return false;
}
}
bool met = variable.ToString() != compareValue;
bool met = false;
try
{
if (variable is int)
{
met = (int)variable != int.Parse(compareValueStr);
}
else if (variable is float)
{
met = (float)variable != float.Parse(compareValueStr);
}
else if (variable is bool)
{
met = (bool)variable != bool.Parse(compareValueStr);
}
else
{
met = variable?.ToString() != compareValueStr;
}
}
catch (System.Exception e)
{
Log.Warning($"[EventSystem] Condition_VariableNotEqual: Could not compare '{variable}' and '{compareValueStr}'. Error: {e.Message}");
reason = "Type mismatch or parsing error during comparison.";
return false;
}
Log.Message($"[EventSystem] Condition_VariableNotEqual check: Name='{name}', Type='{variable?.GetType().Name ?? "null"}', CurrentValue='{variable}', CompareValue='{compareValueStr}', Met={met}");
if (!met)
{
reason = $"Requires {name} != {compareValue} (Current: {variable})";
reason = $"Requires {name} != {compareValueStr} (Current: {variable})";
}
else
{