Files
aibis-dream/Assets/Scripts/UI/DialogUI/Bubbles/BubbleGroup.cs
T
2025-06-04 21:45:50 +08:00

155 lines
3.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AibisDream.Framework;
using AibisDream.Kit;
using UnityEngine;
namespace AibisDream
{
public class BubbleGroup : MonoBehaviour, IDialogView
{
private BubbleOption[] _options;
private Dictionary<ActorRole, Bubble[]> _bubbleDict;
#region 一些索引
private Action _nextStep;
private Bubble _curBubble;
#endregion
#region 生命周期
private void Awake()
{
// 注册泡泡
EnumEventSystem.Global.Register(DialogEventEnum.OptionSelected, HideOptions);
}
private void Start()
{
HideDialog();
}
private void OnDestroy()
{
EnumEventSystem.Global.UnRegister(DialogEventEnum.OptionSelected, HideOptions);
}
public void Init()
{
}
#endregion
#region 对话框功能实现
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.ShowActorName(character);
bubble.ShowLine(dialogLine);
// 将非选定的Bubble隐藏
HideDialog(bubble);
}
else
{
Debug.LogWarning($"{character.GetActorName()}的Role没有对应Bubble");
return;
}
// 保留信息
_nextStep = nextStep;
_curBubble = bubble;
}
public void ShowOptions(DialogOption[] dialogueOptions)
{
HideDialog();
// 要求最多三个选项
for (var i = 0; i < dialogueOptions.Length; i++)
{
_options[i].ShowOption(dialogueOptions[i]);
}
}
public void HideDialog()
{
foreach (var textBubble in _bubbleDict.Values.SelectMany(inner => inner))
{
textBubble.Hide();
}
HideOptions();
}
public bool TrySkipLine(bool isForceSkip = false)
{
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();
}
public void OnLocalizationChanged(string value)
{
}
#endregion
private void HideDialog(Bubble activeBubble)
{
foreach (var textBubble in _bubbleDict.Values.SelectMany(inner => inner))
{
if (activeBubble != textBubble)
{
textBubble.Hide();
}
}
HideOptions();
}
private bool TryGetBubble(ActorRole role, int idx, out Bubble bubble)
{
if (_bubbleDict.TryGetValue(role, out var bubbles))
{
bubble = bubbles.FirstOrDefault(item => item.idx == idx);
return bubble != null;
}
bubble = null;
return false;
}
private void HideOptions()
{
foreach (var option in _options)
{
option.Hide();
}
}
}
}