Files
aibis-dream/Assets/Scripts/UI/DialogUI/DialogBubbleViewer.cs
T
2024-09-20 01:48:50 +08:00

111 lines
3.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using AibisDream.Kit;
using UnityEngine;
using Yarn.Unity;
namespace AibisDream
{
public class DialogBubbleViewer : MonoBehaviour, IDialogView
{
private const string PlayerBubble = "Player Bubble";
private const string ActorBubble = "Actor Bubble";
private const string NormalBox = "Normal Box";
private IDialogView _normalBox;
private IDialogView _playerBubble;
private IDialogView _actorBubble;
// 应该会有自定义位置的泡泡,但目前还没想好怎么做
private Dictionary<string, IDialogView> _customBubblePool = new();
private List<IDialogView> _dialogViews;
private IDialogView _currentView;
private void Awake()
{
Init();
}
private void Init()
{
_normalBox = transform.Find(NormalBox).GetComponent<IDialogView>();
_playerBubble = transform.Find(PlayerBubble).GetComponent<IDialogView>();
_actorBubble = transform.Find(ActorBubble).GetComponent<IDialogView>();
_dialogViews = new List<IDialogView> { _normalBox, _playerBubble, _actorBubble };
_dialogViews.ForEach(item => item.HideDialog());
}
public void ShowLine(string dialogLine, CharacterVo character, Action nextStep, Action onTextShowed,
bool isAutoSkip = false)
{
gameObject.SetActive(true);
// 根据角色信息气泡
_currentView = SelectBubbleByCharacter(character);
// 处理其他泡泡
ProcessOtherBox();
// 显示对话
_currentView.ShowLine(dialogLine, character, nextStep, onTextShowed, isAutoSkip);
}
public void ShowOptions(DialogueOption[] dialogueOptions, Action<int> onOptionSelected)
{
gameObject.SetActive(true);
_currentView = _normalBox;
// 目前只有正常Box可以展示选项
// 处理其他泡泡
ProcessOtherBox();
// 展示选项
_normalBox.ShowOptions(dialogueOptions, onOptionSelected);
}
public void HideDialog()
{
gameObject.SetActive(false);
// 隐藏整个对话组件
_dialogViews?.ForEach(item => item.HideDialog());
}
public bool TrySkipLine()
{
return _currentView == null || _currentView.TrySkipLine();
}
private IDialogView SelectBubbleByCharacter(CharacterVo characterVo)
{
var targetBubble = characterVo.role switch
{
ActorRole.Player => _playerBubble,
ActorRole.MainActor => _actorBubble,
ActorRole.Aside => _normalBox,
_ => _normalBox
};
foreach (var bubble in _dialogViews.Where(bubble => bubble != targetBubble))
{
bubble.HideDialog();
}
return targetBubble;
}
private void ProcessOtherBox()
{
if (_currentView == _actorBubble || _currentView == _playerBubble)
{
_normalBox.HideDialog();
}
if (_currentView == _normalBox)
{
_actorBubble.HideDialog();
_playerBubble.HideDialog();
}
}
}
}