剧情+对话完善
This commit is contained in:
@@ -5,9 +5,9 @@ using UnityEngine;
|
||||
namespace Ichni.Story
|
||||
{
|
||||
/// <summary>
|
||||
/// 全局剧情变量的统一数据枢纽。
|
||||
/// 当前章节剧情变量的统一数据枢纽。
|
||||
/// 所有外部系统(包括 Yarn、UI、小游戏)读写剧情变量的唯一官方入口。
|
||||
/// 自动桥接至 GameSaveManager 的 StorySaveModule 中持久化。
|
||||
/// 自动桥接至 GameSaveManager 的 StorySaveModule 中当前章节的持久化容器。
|
||||
/// </summary>
|
||||
public static class StoryVariables
|
||||
{
|
||||
@@ -19,7 +19,7 @@ namespace Ichni.Story
|
||||
get
|
||||
{
|
||||
if (GameSaveManager.instance != null && GameSaveManager.instance.StorySaveModule != null)
|
||||
return GameSaveManager.instance.StorySaveModule.variables;
|
||||
return GameSaveManager.instance.StorySaveModule.GetActiveChapterVariables();
|
||||
return _fallback;
|
||||
}
|
||||
}
|
||||
@@ -32,6 +32,17 @@ namespace Ichni.Story
|
||||
if (vars.floatVariables.TryGetValue(key, out float f)) return f;
|
||||
if (vars.boolVariables.TryGetValue(key, out bool b)) return b ? 1f : 0f;
|
||||
if (vars.stringVariables.TryGetValue(key, out string s) && float.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out float p)) return p;
|
||||
if (TryGetInitialVariable(key, out StoryVariableDefinition definition))
|
||||
{
|
||||
return definition.type switch
|
||||
{
|
||||
StoryVariableType.Bool => definition.boolValue ? 1f : 0f,
|
||||
StoryVariableType.Int => definition.intValue,
|
||||
StoryVariableType.Float => definition.floatValue,
|
||||
StoryVariableType.String when float.TryParse(definition.stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out float parsed) => parsed,
|
||||
_ => defaultValue
|
||||
};
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@@ -41,6 +52,17 @@ namespace Ichni.Story
|
||||
if (vars.floatVariables.TryGetValue(key, out float f)) return Mathf.RoundToInt(f);
|
||||
if (vars.boolVariables.TryGetValue(key, out bool b)) return b ? 1 : 0;
|
||||
if (vars.stringVariables.TryGetValue(key, out string s) && float.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out float p)) return Mathf.RoundToInt(p);
|
||||
if (TryGetInitialVariable(key, out StoryVariableDefinition definition))
|
||||
{
|
||||
return definition.type switch
|
||||
{
|
||||
StoryVariableType.Bool => definition.boolValue ? 1 : 0,
|
||||
StoryVariableType.Int => definition.intValue,
|
||||
StoryVariableType.Float => Mathf.RoundToInt(definition.floatValue),
|
||||
StoryVariableType.String when float.TryParse(definition.stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out float parsed) => Mathf.RoundToInt(parsed),
|
||||
_ => defaultValue
|
||||
};
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@@ -50,6 +72,17 @@ namespace Ichni.Story
|
||||
if (vars.boolVariables.TryGetValue(key, out bool b)) return b;
|
||||
if (vars.floatVariables.TryGetValue(key, out float f)) return f != 0f;
|
||||
if (vars.stringVariables.TryGetValue(key, out string s) && bool.TryParse(s, out bool p)) return p;
|
||||
if (TryGetInitialVariable(key, out StoryVariableDefinition definition))
|
||||
{
|
||||
return definition.type switch
|
||||
{
|
||||
StoryVariableType.Bool => definition.boolValue,
|
||||
StoryVariableType.Int => definition.intValue != 0,
|
||||
StoryVariableType.Float => !Mathf.Approximately(definition.floatValue, 0f),
|
||||
StoryVariableType.String when bool.TryParse(definition.stringValue, out bool parsed) => parsed,
|
||||
_ => defaultValue
|
||||
};
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@@ -59,13 +92,25 @@ namespace Ichni.Story
|
||||
if (vars.stringVariables.TryGetValue(key, out string s)) return s;
|
||||
if (vars.floatVariables.TryGetValue(key, out float f)) return f.ToString(CultureInfo.InvariantCulture);
|
||||
if (vars.boolVariables.TryGetValue(key, out bool b)) return b.ToString();
|
||||
if (TryGetInitialVariable(key, out StoryVariableDefinition definition))
|
||||
{
|
||||
return definition.type switch
|
||||
{
|
||||
StoryVariableType.Bool => definition.boolValue.ToString(),
|
||||
StoryVariableType.Int => definition.intValue.ToString(CultureInfo.InvariantCulture),
|
||||
StoryVariableType.Float => definition.floatValue.ToString(CultureInfo.InvariantCulture),
|
||||
StoryVariableType.String => definition.stringValue ?? string.Empty,
|
||||
_ => defaultValue
|
||||
};
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public static bool HasVariable(string key)
|
||||
{
|
||||
StoryVariablesSave vars = Variables;
|
||||
return vars.floatVariables.ContainsKey(key) || vars.boolVariables.ContainsKey(key) || vars.stringVariables.ContainsKey(key);
|
||||
return vars.floatVariables.ContainsKey(key) || vars.boolVariables.ContainsKey(key) || vars.stringVariables.ContainsKey(key) ||
|
||||
TryGetInitialVariable(key, out _);
|
||||
}
|
||||
|
||||
// ── 写入 ────────────────────────────────────────────────────────────────
|
||||
@@ -124,6 +169,20 @@ namespace Ichni.Story
|
||||
if (!keepBool) vars.boolVariables.Remove(name);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从当前 StoryTree 的 StoryData 查询变量默认值。没有章节上下文时不虚构默认值,
|
||||
/// 由调用方传入的 defaultValue 作为独立测试场景的回退。
|
||||
/// </summary>
|
||||
private static bool TryGetInitialVariable(string key, out StoryVariableDefinition definition)
|
||||
{
|
||||
StoryData storyData = StoryManager.instance?.treeController?.ActiveStoryData;
|
||||
if (storyData != null)
|
||||
return storyData.TryGetInitialVariable(key, out definition);
|
||||
|
||||
definition = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// ── 字典批量操作(内部供 Yarn Adapter 使用) ─────────────────────────────
|
||||
|
||||
internal static void SetAllVariables(Dictionary<string, float> floats, Dictionary<string, string> strings, Dictionary<string, bool> bools, bool clear = true)
|
||||
|
||||
Reference in New Issue
Block a user