Files
aibis-dream/Assets/Scripts/UI/DialogUI/TextBubbleGroup.cs
T
2025-05-08 18:26:08 +08:00

160 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.Utility;
using UnityEngine;
namespace AibisDream
{
public class TextBubbleGroup : MonoBehaviour, IDialogView
{
private Dictionary<ActorRole, TextBubble[]> _bubbleDict;
#region 一些索引
private bool _skipLineDelay;
private Action _nextStep;
private bool _isAutoSkip;
private TextBubble _curBubble;
[Header("跳过阻塞延迟时间/ms")] [SerializeField]
private int delayTime = 300;
#endregion
#region 初始化
private void Awake()
{
InitBubbles();
// 注册泡泡
DialogCanvasManager.Instance.RegisterDialogView(DialogViewType.Bubble, this);
DialogCanvasManager.Instance.SwitchDialogView(DialogViewType.Bubble);
}
private void Start()
{
HideDialog();
}
private void OnDestroy()
{
DialogCanvasManager.Instance.SwitchDialogView(DialogViewType.OldBox);
DialogCanvasManager.Instance.UnregisterDialogView(DialogViewType.Bubble);
}
private void InitBubbles()
{
_bubbleDict = GetComponentsInChildren<TextBubble>().GroupBy(item => item.role)
.ToDictionary(group => group.Key, group => group.ToArray());
}
#endregion
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, string lineId, bool isAutoSkip = false)
{
gameObject.SetActive(true);
if (TryGetBubble(character.role, character.bubbleIdx ,out var bubble))
{
bubble.ShowLine(dialogLine, OnTextShowed);
// 设置延时
_skipLineDelay = true;
_ = CommonUtil.Delay(delayTime, () => { _skipLineDelay = false; });
// 将非选定的Bubble隐藏
HideDialog(bubble);
}
else
{
Debug.LogWarning($"{character.GetActorName()}的Role没有对应Bubble");
return;
}
// 保留信息
_nextStep = nextStep;
_isAutoSkip = isAutoSkip;
_curBubble = bubble;
}
public void ShowOptions(DialogOption[] dialogueOptions)
{
// 没有选项
}
public void HideDialog()
{
foreach (var textBubble in _bubbleDict.Values.SelectMany(inner => inner))
{
textBubble.Hide();
}
}
public void HideDialog(TextBubble activeBubble)
{
foreach (var textBubble in _bubbleDict.Values.SelectMany(inner => inner))
{
if (activeBubble != textBubble)
{
textBubble.Hide();
}
}
}
public bool TrySkipLine(bool isForceSkip)
{
if (_skipLineDelay && !isForceSkip)
{
return true;
}
if (_curBubble.IsShowingText)
{
_curBubble.SkipLine();
return true;
}
return false;
}
public void NextStep()
{
if (_nextStep == null) return;
EnumEventSystem.Global.Send(DialogEventEnum.LineEnd);
var next = _nextStep;
_nextStep = null;
_curBubble?.Hide();
next?.Invoke();
}
private void OnTextShowed()
{
// 触发事件
EnumEventSystem.Global.Send(DialogEventEnum.LineShown);
// 执行自动跳过结局
if (_isAutoSkip)
{
_ = CommonUtil.Delay(200, NextStep);
_isAutoSkip = false;
}
}
private bool TryGetBubble(ActorRole role, int idx, out TextBubble textBubble)
{
if (_bubbleDict.TryGetValue(role, out var bubbles))
{
textBubble = bubbles.FirstOrDefault(item => item.idx == idx);
return textBubble != null;
}
textBubble = null;
return false;
}
}
}