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

@@ -163,25 +163,34 @@ namespace WulaFallenEmpire
{
public string name;
public string value;
public string type; // Int, Float, String, Bool
public bool forceSet = false;
public override void Execute(Dialog_CustomDisplay dialog = null)
{
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
if (!eventVarManager.HasVariable(name))
if (!forceSet && eventVarManager.HasVariable(name))
{
if (int.TryParse(value, out int intValue))
return;
}
object realValue = value;
if (!string.IsNullOrEmpty(type))
{
if (type.Equals("int", System.StringComparison.OrdinalIgnoreCase) && int.TryParse(value, out int intVal))
{
eventVarManager.SetVariable(name, intValue);
realValue = intVal;
}
else if (float.TryParse(value, out float floatValue))
else if (type.Equals("float", System.StringComparison.OrdinalIgnoreCase) && float.TryParse(value, out float floatVal))
{
eventVarManager.SetVariable(name, floatValue);
realValue = floatVal;
}
else
else if (type.Equals("bool", System.StringComparison.OrdinalIgnoreCase) && bool.TryParse(value, out bool boolVal))
{
eventVarManager.SetVariable(name, value);
realValue = boolVal;
}
}
eventVarManager.SetVariable(name, realValue);
}
}
@@ -337,14 +346,14 @@ namespace WulaFallenEmpire
Add,
Subtract,
Multiply,
Divide,
Set
Divide
}
public class Effect_ModifyVariable : Effect
{
public string name;
public float value;
public string value;
public string valueVariableName;
public VariableOperation operation;
public override void Execute(Dialog_CustomDisplay dialog = null)
@@ -354,43 +363,70 @@ namespace WulaFallenEmpire
Log.Error("[WulaFallenEmpire] Effect_ModifyVariable has a null or empty name.");
return;
}
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
if (!eventVarManager.HasVariable(name))
// Determine the value to modify by
string valueStr = value;
if (!string.IsNullOrEmpty(valueVariableName))
{
eventVarManager.SetVariable(name, 0f);
valueStr = eventVarManager.GetVariable<object>(valueVariableName)?.ToString();
if (valueStr == null)
{
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable: valueVariableName '{valueVariableName}' not found.");
return;
}
}
float currentValue = eventVarManager.GetVariable<float>(name);
switch (operation)
// Get the target variable, or initialize it
object variable = eventVarManager.GetVariable<object>(name);
if (variable == null)
{
case VariableOperation.Add:
currentValue += value;
break;
case VariableOperation.Subtract:
currentValue -= value;
break;
case VariableOperation.Multiply:
currentValue *= value;
break;
case VariableOperation.Divide:
if (value != 0)
{
currentValue /= value;
}
else
{
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable tried to divide by zero for variable '{name}'.");
}
break;
case VariableOperation.Set:
currentValue = value;
break;
}
Log.Message($"[EventSystem] Effect_ModifyVariable: Variable '{name}' not found, initializing to 0.");
variable = 0;
}
eventVarManager.SetVariable(name, currentValue);
object originalValue = variable;
object newValue = null;
// Perform operation based on type
try
{
if (variable is int || (variable is float && !valueStr.Contains("."))) // Allow int ops
{
int currentVal = System.Convert.ToInt32(variable);
int modVal = int.Parse(valueStr);
newValue = (int)Modify((float)currentVal, (float)modVal, operation);
}
else // Default to float operation
{
float currentVal = System.Convert.ToSingle(variable);
float modVal = float.Parse(valueStr);
newValue = Modify(currentVal, modVal, operation);
}
Log.Message($"[EventSystem] Modifying variable '{name}'. Operation: {operation}. Value: {valueStr}. From: {originalValue} To: {newValue}");
eventVarManager.SetVariable(name, newValue);
}
catch (System.Exception e)
{
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable: Could not parse or operate on value '{valueStr}' for variable '{name}'. Error: {e.Message}");
}
}
private float Modify(float current, float modifier, VariableOperation op)
{
switch (op)
{
case VariableOperation.Add: return current + modifier;
case VariableOperation.Subtract: return current - modifier;
case VariableOperation.Multiply: return current * modifier;
case VariableOperation.Divide:
if (modifier != 0) return current / modifier;
Log.Error($"[WulaFallenEmpire] Effect_ModifyVariable tried to divide by zero.");
return current;
default: return current;
}
}
}
@@ -556,4 +592,33 @@ namespace WulaFallenEmpire
}
}
}
public class Effect_StoreFactionGoodwill : Effect
{
public FactionDef factionDef;
public string variableName;
public override void Execute(Dialog_CustomDisplay dialog = null)
{
if (factionDef == null || string.IsNullOrEmpty(variableName))
{
Log.Error("[WulaFallenEmpire] Effect_StoreFactionGoodwill is not configured correctly.");
return;
}
var eventVarManager = Find.World.GetComponent<EventVariableManager>();
Faction faction = Find.FactionManager.FirstFactionOfDef(factionDef);
if (faction != null)
{
int goodwill = faction.GoodwillWith(Faction.OfPlayer);
Log.Message($"[EventSystem] Storing goodwill for faction '{faction.Name}' ({goodwill}) into variable '{variableName}'.");
eventVarManager.SetVariable(variableName, goodwill);
}
else
{
Log.Warning($"[EventSystem] Effect_StoreFactionGoodwill: Faction '{factionDef.defName}' not found. Storing 0 in variable '{variableName}'.");
eventVarManager.SetVariable(variableName, 0);
}
}
}
}