Story流程+本地化
This commit is contained in:
@@ -124,18 +124,77 @@ namespace Ichni.Story
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 选曲页确认启动游戏时调用。只有章节和稳定歌曲 ID 都命中剧情入口目标时,
|
||||
/// 才将对应 SongBlock 标记为完成,并要求 GameScene 返回剧情页。
|
||||
/// 从 SongSelection 页进入当前章节的 StoryPage。
|
||||
/// <para>若 <paramref name="song"/> 在该章节 StoryData 中存在唯一的 SongBlock,视口会定位到它;
|
||||
/// 没有对应 SongBlock 或当前未选中歌曲时,视口回到剧情树开头。</para>
|
||||
/// <para>这不是 Story SongBlock 的“返回”操作,因此会清除一次性选曲上下文,且不会写入歌曲、剧情或选项存档。</para>
|
||||
/// </summary>
|
||||
public bool TryOpenStoryForSong(ChapterSelectionUnit chapter, SongItemData song)
|
||||
{
|
||||
if (chapter == null || treeController == null || storyUIPage == null ||
|
||||
MenuManager.instance == null || MenuManager.instance.songSelectionUIPage == null ||
|
||||
ChapterSelectionManager.instance == null)
|
||||
{
|
||||
Debug.LogWarning("[StoryManager] 从选曲页打开剧情失败:章节、页面或 StoryTree 引用未就绪。");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (GetStoryData(chapter.chapterIndex) == null)
|
||||
{
|
||||
Debug.LogWarning($"[StoryManager] 章节 '{chapter.chapterIndex}' 未登记 StoryData,无法从选曲页打开剧情。");
|
||||
return false;
|
||||
}
|
||||
|
||||
UnlockSaveModule unlockSaveModule = GameSaveManager.instance?.UnlockSaveModule;
|
||||
if (unlockSaveModule == null || !unlockSaveModule.CanAccessChapter(chapter))
|
||||
{
|
||||
Debug.LogWarning($"[StoryManager] 章节 '{chapter.chapterIndex}' 尚不可进入剧情,已取消从选曲页跳转。");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 此入口只负责浏览剧情。若此前由 SongBlock 打开选曲页,主动进入剧情时应放弃旧的
|
||||
// “目标歌曲 -> GameScene 后返回原 Block”上下文,避免下一次普通游玩误命中旧目标。
|
||||
InformationTransistor.instance?.storySongEntryContext?.Clear();
|
||||
|
||||
ChapterSelectionManager.instance.currentChapter = chapter;
|
||||
AudioManager.SetSwitch(chapter.chapterSwitch);
|
||||
OpenChapter(chapter.chapterIndex);
|
||||
|
||||
StoryBlockView targetBlock = FindSongBlockView(song);
|
||||
// 与 SongSelection 的 Back 保持一致:离开选曲页进入剧情时,必须立即停止当前歌曲试听。
|
||||
// StoryPage 不承载试听音频,若遗漏该步骤会让歌曲预览跨页面持续播放。
|
||||
SongSelectionManager.instance?.StopPreviewSong();
|
||||
MenuManager.instance.songSelectionUIPage.FadeOut();
|
||||
// StoryPage 激活后、淡入前先完成定位。UIPageBase 此时保持 alpha = 0,
|
||||
// 因此玩家不会先看到剧情树开头、再跳到目标 SongBlock。
|
||||
storyUIPage.FadeIn(beforeFadeAction: () =>
|
||||
{
|
||||
if (targetBlock != null)
|
||||
treeController.FocusBlock(targetBlock);
|
||||
else
|
||||
treeController.FocusStart();
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 选曲页确认启动游戏时调用。
|
||||
/// <para>
|
||||
/// SongBlock 的完成状态统一由 <c>SongSaves.isTried</c> 判断,并已在
|
||||
/// <c>PrepareGameLaunch</c> 中写入;这里仅清理由 StoryPage 发起的临时选曲上下文。
|
||||
/// GameScene 无论从何种歌曲入口启动,都会返回 SongSelection;TutorialBlock 仍通过其独立
|
||||
/// 的 <c>MenuReturnDestination.Story</c> 流程返回剧情。
|
||||
/// </para>
|
||||
/// </summary>
|
||||
public void HandleSongSelectionConfirmed(ChapterSelectionUnit chapter, SongItemData song)
|
||||
{
|
||||
StorySongEntryContext context = InformationTransistor.instance?.storySongEntryContext;
|
||||
if (context == null || !context.IsTargetSong(chapter, song))
|
||||
if (context == null || !context.isActive)
|
||||
return;
|
||||
|
||||
// isTried 已在 PrepareGameLaunch 中落盘;此处不再直接写 StorySave,避免 SongBlock
|
||||
// 同时拥有一次性选曲上下文与歌曲存档两套完成来源。
|
||||
context.returnToStoryAfterGame = true;
|
||||
// 玩家已经确认进入 GameScene,无论选择的是入口歌曲还是改选了其它歌曲,旧的剧情入口
|
||||
// 上下文都不能遗留到下一次选曲。否则它可能在后续流程中错误影响页面返回或焦点恢复。
|
||||
context.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -191,6 +250,29 @@ namespace Ichni.Story
|
||||
}).AddTo(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在当前已构建的 StoryTree 中查找歌曲对应的 SongBlock 视图。
|
||||
/// StoryData 自检会提示同章节重复 Song ID;运行时仍采用第一个匹配项作为防御性回退,
|
||||
/// 以避免错误配置直接阻断玩家进入剧情页。
|
||||
/// </summary>
|
||||
private StoryBlockView FindSongBlockView(SongItemData song)
|
||||
{
|
||||
if (song == null || string.IsNullOrWhiteSpace(song.songName) || treeController == null)
|
||||
return null;
|
||||
|
||||
foreach (StoryBlockView view in treeController.blocks)
|
||||
{
|
||||
StoryBlockDefinition definition = treeController.GetBlockDefinition(view.blockId);
|
||||
if (definition != null && definition.blockType == StoryBlockType.Song &&
|
||||
definition.songName == song.songName)
|
||||
{
|
||||
return view;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 清除全部剧情存档(所有章节故事树、章节变量与由剧情授予的内容解锁 Key)。
|
||||
/// </summary>
|
||||
|
||||
@@ -47,12 +47,12 @@ namespace Ichni.Story
|
||||
public GameObject tutorialBlockPrefab;
|
||||
public GameObject connectorPrefab;
|
||||
|
||||
[Header("Layout Settings")]
|
||||
/// <summary>
|
||||
/// 相邻叙事列之间的固定距离。它是 StoryData.gridColumn 的坐标步距,
|
||||
/// 不等于也不会覆盖任何 Block Prefab 的宽度。
|
||||
/// 默认值为 Important TextBlock 宽度 600 加最小横向留白 120。
|
||||
/// </summary>
|
||||
[Header("Layout Settings")]
|
||||
[Tooltip("StoryData.gridColumn 的水平步距,不会改变任何 Prefab 的实际宽度。")]
|
||||
public float columnStep = 720f;
|
||||
|
||||
@@ -72,8 +72,15 @@ namespace Ichni.Story
|
||||
/// 最左 Block 的真实左边缘会被放置在 <see cref="marginLeft"/> 加本值的位置;
|
||||
/// 默认 1600,等价于在原有布局基础上将整棵剧情树向右平移 1600 单位。
|
||||
/// </summary>
|
||||
[Tooltip("为左侧 Helper 等常驻 UI 预留的额外空间。默认 1600,会让所有 Block 整体向右移动 1600。")]
|
||||
public float helperReservedLeftSpace = 1600f;
|
||||
[Tooltip("为左侧 Helper 等常驻 UI 预留的额外空间。默认 1200,会让所有 Block 整体向右移动 1200。")]
|
||||
public float helperReservedLeftSpace = 1200f;
|
||||
/// <summary>
|
||||
/// 为覆盖在剧情页底部的常驻 UI(当前为 Helper)预留的额外可滚动空间。
|
||||
/// Content 使用垂直中心 Pivot;布局时会同步将树向上平移本值的一半,
|
||||
/// 从而使本值完整地增加在内容下方,而非被平均分配到上下两侧。
|
||||
/// </summary>
|
||||
[Tooltip("为底部 Helper 等常驻 UI 预留的额外可滚动空间。值越大,树拖到顶部时越能完整显示底部 Block。")]
|
||||
public float helperReservedLowerSpace = 150f;
|
||||
|
||||
public float marginRight = 50f;
|
||||
public float marginTop = 50f;
|
||||
@@ -93,6 +100,15 @@ namespace Ichni.Story
|
||||
// 未来 Timeline 在独立 UI 容器中生成 Marker 时,也必须叠加此偏移量才能与 Block 中心对齐。
|
||||
private float _layoutHorizontalOffset;
|
||||
|
||||
// Content 使用垂直中心 Pivot。此偏移把 helperReservedLowerSpace 完整分配给内容下方;
|
||||
// SetUpBackground 可能重复执行,因此每次计算前都必须先撤销上一次偏移,避免位置累积漂移。
|
||||
private float _layoutVerticalOffset;
|
||||
|
||||
// StoryTree 的“开头”由场景中 Content 的初始位置决定,而不是硬编码为 Vector2.zero。
|
||||
// 因此 UI 设计调整默认视口时,不需要同步修改导航代码。
|
||||
private Vector2 _initialContentPosition;
|
||||
private bool _hasInitialContentPosition;
|
||||
|
||||
/// <summary>当前已构建章节的 StoryData(供对话控制器切换 YarnProject 等使用)。</summary>
|
||||
public StoryData ActiveStoryData => _storyData;
|
||||
|
||||
@@ -105,6 +121,15 @@ namespace Ichni.Story
|
||||
/// </summary>
|
||||
public float LayoutHorizontalOffset => _layoutHorizontalOffset;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (content == null)
|
||||
return;
|
||||
|
||||
_initialContentPosition = content.anchoredPosition;
|
||||
_hasInitialContentPosition = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定叙事列在 BlockContainer 中的最终中心 X 坐标。
|
||||
/// Timeline Marker 必须使用此方法,而不是自行只计算 gridColumn * columnStep,
|
||||
@@ -620,6 +645,15 @@ namespace Ichni.Story
|
||||
return;
|
||||
}
|
||||
|
||||
// 以未附加底部预留区的原始网格位置重新计算边界,保证重复布局保持稳定。
|
||||
if (!Mathf.Approximately(_layoutVerticalOffset, 0f))
|
||||
{
|
||||
foreach (StoryBlockView block in blocks)
|
||||
block.blockRect.anchoredPosition -= Vector2.up * _layoutVerticalOffset;
|
||||
|
||||
_layoutVerticalOffset = 0f;
|
||||
}
|
||||
|
||||
float minLeft = float.PositiveInfinity;
|
||||
float maxRight = float.NegativeInfinity;
|
||||
float maxVertical = 0f; // 距垂直中线的最大延伸(上下取较大者,用于对称扩展 content 高度)
|
||||
@@ -648,18 +682,29 @@ namespace Ichni.Story
|
||||
float horizontalCorrection = targetLeftEdge - minLeft;
|
||||
if (!Mathf.Approximately(horizontalCorrection, 0f))
|
||||
{
|
||||
foreach (StoryBlockView block in blocks)
|
||||
block.blockRect.anchoredPosition += Vector2.right * horizontalCorrection;
|
||||
|
||||
foreach (StoryBlockView block in blocks) block.blockRect.anchoredPosition += Vector2.right * horizontalCorrection;
|
||||
maxRight += horizontalCorrection;
|
||||
}
|
||||
|
||||
|
||||
// SetUpBackground 可能因尺寸变化被重复调用;偏移量必须累积,不能在第二次调用时
|
||||
// 被归零,否则未来 Timeline 会失去与已平移 Block 的横向对齐。
|
||||
_layoutHorizontalOffset += horizontalCorrection;
|
||||
|
||||
float width = Mathf.Max(maxRight + marginRight, minContentWidth);
|
||||
float height = Mathf.Max(maxVertical * 2f + marginTop + marginBottom, minContentHeight);
|
||||
float baseHeight = Mathf.Max(maxVertical * 2f + marginTop + marginBottom, minContentHeight);
|
||||
float lowerReservedSpace = Mathf.Max(0f, helperReservedLowerSpace);
|
||||
float height = baseHeight + lowerReservedSpace;
|
||||
|
||||
// 将 Content 扩展出的空间全部留在下方:Content 顶边会上移 lower/2,
|
||||
// Block 也上移 lower/2,因此顶部视觉边界不变;Content 底边下移 lower/2,
|
||||
// 与 Block 的上移共同形成完整 lower 的新增底部拖拽空间。
|
||||
_layoutVerticalOffset = lowerReservedSpace * 0.5f;
|
||||
if (!Mathf.Approximately(_layoutVerticalOffset, 0f))
|
||||
{
|
||||
foreach (StoryBlockView block in blocks)
|
||||
block.blockRect.anchoredPosition += Vector2.up * _layoutVerticalOffset;
|
||||
}
|
||||
|
||||
content.sizeDelta = new Vector2(width, height);
|
||||
}
|
||||
@@ -686,6 +731,61 @@ namespace Ichni.Story
|
||||
public StoryBlockView GetBlockView(string blockId) =>
|
||||
blocks.FirstOrDefault(b => b.blockId == blockId);
|
||||
|
||||
/// <summary>
|
||||
/// 将 StoryTree 视口居中到指定 Block。使用实际 RectTransform 的屏幕位置计算偏移,
|
||||
/// 因而适用于不同尺寸的 Block、不同分辨率以及包含 Helper 预留区的布局。
|
||||
/// </summary>
|
||||
public void FocusBlock(StoryBlockView block)
|
||||
{
|
||||
if (block == null || block.blockRect == null || content == null || storyScrollRect == null)
|
||||
{
|
||||
FocusStart();
|
||||
return;
|
||||
}
|
||||
|
||||
Canvas.ForceUpdateCanvases();
|
||||
|
||||
RectTransform viewport = storyScrollRect.viewport != null
|
||||
? storyScrollRect.viewport
|
||||
: storyScrollRect.GetComponent<RectTransform>();
|
||||
Canvas canvas = storyScrollRect.GetComponentInParent<Canvas>();
|
||||
Camera uiCamera = canvas != null && canvas.renderMode != RenderMode.ScreenSpaceOverlay
|
||||
? canvas.worldCamera
|
||||
: null;
|
||||
|
||||
Vector3 blockCenterWorld = block.blockRect.TransformPoint(block.blockRect.rect.center);
|
||||
Vector2 blockCenterScreen = RectTransformUtility.WorldToScreenPoint(uiCamera, blockCenterWorld);
|
||||
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(
|
||||
viewport, blockCenterScreen, uiCamera, out Vector2 blockCenterInViewport))
|
||||
{
|
||||
FocusStart();
|
||||
return;
|
||||
}
|
||||
|
||||
// 局部坐标原点位于 Viewport 中心时,目标在右侧意味着 Content 应向左移动;
|
||||
// 直接减去该偏移即可,且不依赖 Content 或 Block 的 Pivot 配置。
|
||||
content.anchoredPosition -= blockCenterInViewport;
|
||||
storyScrollRect.StopMovement();
|
||||
currentBlock = block;
|
||||
storyTimeline?.RefreshMarkerPositions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 恢复剧情树在场景中设计的默认浏览位置,用于没有对应 SongBlock 时的安全回退。
|
||||
/// </summary>
|
||||
public void FocusStart()
|
||||
{
|
||||
if (content == null)
|
||||
return;
|
||||
|
||||
if (_hasInitialContentPosition)
|
||||
content.anchoredPosition = _initialContentPosition;
|
||||
|
||||
storyScrollRect?.StopMovement();
|
||||
currentBlock = null;
|
||||
storyTimeline?.RefreshMarkerPositions();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 为 Timeline、Yarn 和调试工具提供当前章节静态 Block 定义的查询入口。
|
||||
/// </summary>
|
||||
@@ -704,6 +804,7 @@ namespace Ichni.Story
|
||||
connectors.Clear();
|
||||
|
||||
_layoutHorizontalOffset = 0f;
|
||||
_layoutVerticalOffset = 0f;
|
||||
currentBlock = null;
|
||||
|
||||
if (content != null)
|
||||
|
||||
@@ -103,8 +103,6 @@ namespace Ichni.Story.UI
|
||||
|
||||
protected override void OnClicked()
|
||||
{
|
||||
Debug.Log($"[YarnDiag] TextBlockView.OnClicked: blockId={blockId}, yarnNodeName={YarnNodeName}");
|
||||
|
||||
if (StoryDialogueController.instance == null)
|
||||
{
|
||||
Debug.LogWarning($"[TextBlockView] 场景中缺少 StoryDialogueController,无法播放 block '{blockId}' 的对话。");
|
||||
|
||||
Reference in New Issue
Block a user