feat(event): 在事件描述中添加变量支持
事件对话框现在会格式化描述文本,将 `{变量名}` 这样的占位符替换为来自 `EventContext` 的当前值。
这允许事件文本能够动态变化,以反映游戏状态或事件中的选择。添加了 `Wula_Test_VariableEvent` 测试事件来演示和验证此功能。
This commit is contained in:
Binary file not shown.
57
1.6/Defs/EventDefs/EventDef_VariableTest.xml
Normal file
57
1.6/Defs/EventDefs/EventDef_VariableTest.xml
Normal file
@@ -0,0 +1,57 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<Defs>
|
||||
|
||||
<WulaFallenEmpire.EventDef>
|
||||
<defName>Wula_Test_VariableEvent</defName>
|
||||
<label>变量测试事件</label>
|
||||
<characterName>测试员</characterName>
|
||||
<portraitPath>Textures/Wula/Events/Portraits/Wula_Leader</portraitPath>
|
||||
|
||||
<immediateEffects>
|
||||
<li>
|
||||
<effects>
|
||||
<li Class="WulaFallenEmpire.Effect_SetVariable">
|
||||
<name>testNumber</name>
|
||||
<value>100</value>
|
||||
</li>
|
||||
</effects>
|
||||
</li>
|
||||
</immediateEffects>
|
||||
|
||||
<descriptions>
|
||||
<li>变量 testNumber 的当前值为 {testNumber}。点击下面的按钮来改变它。</li>
|
||||
</descriptions>
|
||||
|
||||
<options>
|
||||
<li>
|
||||
<label>将 testNumber 增加50并重新打开</label>
|
||||
<optionEffects>
|
||||
<li>
|
||||
<effects>
|
||||
<li Class="WulaFallenEmpire.Effect_ModifyVariable">
|
||||
<name>testNumber</name>
|
||||
<operation>Add</operation>
|
||||
<value>50</value>
|
||||
</li>
|
||||
<li Class="WulaFallenEmpire.Effect_CloseDialog" />
|
||||
<li Class="WulaFallenEmpire.Effect_OpenCustomUI">
|
||||
<defName>Wula_Test_VariableEvent</defName>
|
||||
</li>
|
||||
</effects>
|
||||
</li>
|
||||
</optionEffects>
|
||||
</li>
|
||||
<li>
|
||||
<label>关闭</label>
|
||||
<optionEffects>
|
||||
<li>
|
||||
<effects>
|
||||
<li Class="WulaFallenEmpire.Effect_CloseDialog" />
|
||||
</effects>
|
||||
</li>
|
||||
</optionEffects>
|
||||
</li>
|
||||
</options>
|
||||
</WulaFallenEmpire.EventDef>
|
||||
|
||||
</Defs>
|
||||
@@ -84,6 +84,9 @@ namespace WulaFallenEmpire
|
||||
}
|
||||
|
||||
HandleAction(def.immediateEffects);
|
||||
|
||||
// Format the description AFTER immediate effects have run
|
||||
selectedDescription = FormatDescription(selectedDescription);
|
||||
}
|
||||
|
||||
public override void DoWindowContents(Rect inRect)
|
||||
@@ -256,5 +259,14 @@ namespace WulaFallenEmpire
|
||||
base.PostClose();
|
||||
HandleAction(def.dismissEffects);
|
||||
}
|
||||
private string FormatDescription(string description)
|
||||
{
|
||||
var variables = EventContext.GetAllVariables();
|
||||
foreach (var variable in variables)
|
||||
{
|
||||
description = description.Replace("{" + variable.Key + "}", variable.Value.ToString());
|
||||
}
|
||||
return description;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,6 +48,11 @@ namespace WulaFallenEmpire
|
||||
return variables.ContainsKey(name);
|
||||
}
|
||||
|
||||
public static Dictionary<string, object> GetAllVariables()
|
||||
{
|
||||
return new Dictionary<string, object>(variables);
|
||||
}
|
||||
|
||||
public static void Clear()
|
||||
{
|
||||
variables.Clear();
|
||||
|
||||
Reference in New Issue
Block a user