Files
aibis-dream/Assets/Scripts/UI/DialogUI/TextBubbleGroup.cs
T
2025-03-07 15:11:58 +08:00

145 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using AibisDream.Framework;
using AibisDream.Kit;
using AibisDream.Utility;
using UnityEngine;
namespace AibisDream
{
public class TextBubbleGroup : MonoBehaviour, IDialogView
{
private readonly Dictionary<ActorRole, TextBubble> _bubbleDict = new();
#region 一些索引
private bool _skipLineDelay;
private Action _nextStep;
private Action _onTextShowed;
private bool _isAutoSkip;
private ActorRole _curRole;
[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()
{
foreach (var bubble in GetComponentsInChildren<TextBubble>(true))
{
_bubbleDict[bubble.role] = bubble;
}
}
#endregion
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, Action onTextShowed = null,
bool isAutoSkip = false)
{
gameObject.SetActive(true);
if (_curRole != character.role)
{
_bubbleDict[_curRole].Hide();
}
if (_bubbleDict.TryGetValue(character.role, out var bubble))
{
bubble.ShowLine(dialogLine, OnTextShowed);
// 设置延时
_skipLineDelay = true;
_ = CommonUtil.Delay(delayTime, () => { _skipLineDelay = false; });
}
else
{
Debug.LogWarning($"{character.GetActorName()}的Role没有对应Bubble");
return;
}
// 保留信息
_nextStep = nextStep;
_onTextShowed = onTextShowed;
_isAutoSkip = isAutoSkip;
_curRole = character.role;
}
public void ShowOptions(DialogOption[] dialogueOptions)
{
// 没有选项
}
public void HideDialog()
{
foreach (var bubble in _bubbleDict.Values)
{
bubble.Hide();
}
}
public bool TrySkipLine()
{
if (_skipLineDelay)
{
return true;
}
if (_bubbleDict.TryGetValue(_curRole, out var bubble) && bubble.IsShowingText)
{
bubble.SkipLine();
return true;
}
return false;
}
public void NextStep()
{
if (_nextStep == null) return;
EnumEventSystem.Global.Send(DialogEventEnum.LineEnd);
var next = _nextStep;
_nextStep = null;
_bubbleDict[_curRole].Hide();
next.Invoke();
}
private void OnTextShowed()
{
// 触发事件
EnumEventSystem.Global.Send(DialogEventEnum.LineShown);
// 停止语音
_onTextShowed?.Invoke();
// 执行自动跳过结局
if (_isAutoSkip)
{
_ = CommonUtil.Delay(200, NextStep);
_isAutoSkip = false;
}
}
}
}