Files
aibis-dream/Assets/Scripts/UI/DialogUI/Bubbles/BubbleGroup.cs
T

203 lines
5.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AibisDream.Framework;
using AibisDream.Kit;
using UnityEngine;
namespace AibisDream
{
public class BubbleGroup : Singleton<BubbleGroup>, IDialogView
{
[SerializeField] private CenterBubble centerBubble;
private Dictionary<ActorRole, Bubble[]> _bubbleDict;
private BubbleFactory _bubbleFactory;
#region 一些索引
private Action _nextStep;
private IBubble _curBubble;
private IUnRegister _unRegister;
#endregion
#region 生命周期
public override void OnSingletonInit()
{
// 工厂
_bubbleFactory = new BubbleFactory(transform);
// 加载选项
InitOptions();
}
private void Start()
{
HideDialog();
}
public override void OnSingletonDestroy()
{
_unRegister.UnRegister();
}
public void LoadBubbles(BubbleSlotGroupData groupData)
{
// 回收旧泡泡
foreach (var oldBubble in _bubbleDict.Values.SelectMany(item => item))
{
oldBubble.Dispose();
}
// 加载新泡泡
_bubbleDict = groupData.bubbleSlots
.Select(item => _bubbleFactory.Create(item))
.GroupBy(item => item.Role)
.ToDictionary(group => group.Key, group => group.ToArray());
}
private void InitOptions()
{
_bubbleDict = new Dictionary<ActorRole, Bubble[]>();
}
#endregion
#region 旧对话框功能实现
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, string lineId,
bool isAutoSkip = false)
{
Debug.Log($"[BubbleGroup] ShowLine - 开始显示对话: {lineId}");
Debug.Log($"[BubbleGroup] ShowLine - nextStep是否为null: {nextStep == null}");
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;
Debug.Log($"[BubbleGroup] ShowLine - 设置完成,_nextStep是否为null: {_nextStep == null}");
}
public void HideDialog()
{
foreach (var textBubble in _bubbleDict.Values.SelectMany(inner => inner))
{
textBubble.Hide();
}
centerBubble.Hide();
}
public bool TrySkipLine(bool isForceSkip = false)
{
if (_curBubble == null)
{
return true;
}
return _curBubble.SkipLine();
}
public void NextStep()
{
Debug.Log($"[BubbleGroup] NextStep - _nextStep是否为null: {_nextStep == null}");
if (_nextStep == null)
{
Debug.Log("[BubbleGroup] NextStep - _nextStep为null,提前返回");
return;
}
var next = _nextStep;
_nextStep = null;
_curBubble?.Hide();
_curBubble = null;
next?.Invoke();
}
public void OnLocalizationChanged(string value)
{
}
private void HideDialog(IBubble activeBubble)
{
foreach (var textBubble in _bubbleDict.Values.SelectMany(inner => inner))
{
if (activeBubble != (IBubble)textBubble)
{
textBubble.Hide();
}
}
if (activeBubble != (IBubble)centerBubble)
{
centerBubble.Hide();
}
}
private bool TryGetBubble(ActorRole role, int idx, out IBubble bubble)
{
if (role == ActorRole.Center)
{
bubble = centerBubble;
return true;
}
if (_bubbleDict.TryGetValue(role, out var bubbles))
{
bubble = bubbles.FirstOrDefault(item => item.Idx == idx);
return bubble != null;
}
bubble = null;
return false;
}
#endregion
#region 新对话框功能实现
public void RunLine(LineSyncToken syncToken)
{
var character = syncToken.lineInfo.character;
if (TryGetBubble(character.role, character.bubbleIdx, out var bubble))
{
// 注册事件
bubble.OnLineShown += syncToken.TextShown;
syncToken.SkipTextAnima += () => bubble.SkipLine();
// 播放文字
bubble.ShowActorName(character);
bubble.ShowLine(syncToken.lineInfo.lineText);
// 将非选定的Bubble隐藏
HideDialog(bubble);
}
else
{
Debug.LogWarning($"{character.GetActorName()}的Role {character.role}没有对应Bubble");
HideDialog();
}
}
#endregion
}
}