202 lines
5.5 KiB
C#
202 lines
5.5 KiB
C#
using System;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogController : Singleton<DialogController>
|
|
{
|
|
private DialogueRunner _dialogueRunner;
|
|
private LocalisedLineProvider _lineProvider;
|
|
|
|
[SerializeField] private LineAdvanceInput lineAdvanceInput;
|
|
|
|
private void Start()
|
|
{
|
|
_dialogueRunner = GetComponentInChildren<DialogueRunner>();
|
|
_lineProvider = GetComponentInChildren<LocalisedLineProvider>();
|
|
|
|
_dialogueRunner.onDialogueStart.AddListener(() =>
|
|
{
|
|
EnumEventSystem.Global.Send(InteractionEventEnum.DialogStart);
|
|
});
|
|
_dialogueRunner.onDialogueComplete.AddListener(() =>
|
|
{
|
|
EnumEventSystem.Global.Send(InteractionEventEnum.DialogEnd);
|
|
});
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
EnumEventSystem.Global.Register(GameLoopEnum.GameStart, ResetAdvanceMode);
|
|
|
|
EnumEventSystem.Global.Register(DialogEventEnum.OptionShow, OnOptionShow);
|
|
EnumEventSystem.Global.Register<DialogEventEnum, LineInfo>(DialogEventEnum.OptionSelected, OnOptionSelected);
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
EnumEventSystem.Global.UnRegister(GameLoopEnum.GameStart, ResetAdvanceMode);
|
|
|
|
EnumEventSystem.Global.UnRegister(DialogEventEnum.OptionShow, OnOptionShow);
|
|
EnumEventSystem.Global.UnRegister<DialogEventEnum, LineInfo>(DialogEventEnum.OptionSelected, OnOptionSelected);
|
|
}
|
|
|
|
private void ResetAdvanceMode()
|
|
{
|
|
advanceMode = new AdvanceMode
|
|
{
|
|
isAuto = false,
|
|
isQuick = false
|
|
};
|
|
|
|
isInOption = false;
|
|
lineSyncToken = null;
|
|
|
|
Time.timeScale = 1f;
|
|
lineAdvanceInput.ResetAdvanceMode();
|
|
}
|
|
|
|
#region Yarn控制
|
|
|
|
/// <summary>
|
|
/// 开启对话
|
|
/// </summary>
|
|
/// <param name="yarnProject">Yarn组</param>
|
|
public void StartDialog(YarnProject yarnProject)
|
|
{
|
|
_dialogueRunner.SetProject(yarnProject);
|
|
_lineProvider.InitStringTable(yarnProject.name);
|
|
_dialogueRunner.StartDialogue("Start");
|
|
}
|
|
|
|
public void StopDialog()
|
|
{
|
|
_dialogueRunner.Stop();
|
|
StorageSystem.Instance.Clear();
|
|
DialogUIManager.Instance.HideDialog();
|
|
}
|
|
|
|
public void LoadDialog(YarnProject yarnProject)
|
|
{
|
|
_dialogueRunner.SetProject(yarnProject);
|
|
_lineProvider.InitStringTable(yarnProject.name);
|
|
_dialogueRunner.StartDialogue("Center");
|
|
}
|
|
|
|
public void StartDialogNode(string nodeName)
|
|
{
|
|
// 检查对话是否正在运行
|
|
if (_dialogueRunner.IsDialogueRunning)
|
|
{
|
|
Debug.LogWarning($"对话正在运行中,无法启动新节点: {nodeName}");
|
|
return;
|
|
}
|
|
|
|
if (CheckIfNodeExistInCurrentYarnProject(nodeName))
|
|
{
|
|
_dialogueRunner.StartDialogue(nodeName);
|
|
}
|
|
}
|
|
|
|
private bool CheckIfNodeExistInCurrentYarnProject(string nodeName)
|
|
{
|
|
YarnProject currentProject = _dialogueRunner.yarnProject;
|
|
string[] nodeNames = currentProject.NodeNames;
|
|
if (Array.Exists(nodeNames, item => item == nodeName))
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"节点 {nodeName} 不存在!");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 推进方式修改
|
|
|
|
public AdvanceMode advanceMode;
|
|
|
|
public void SwitchAutoMode()
|
|
{
|
|
advanceMode.isAuto = !advanceMode.isAuto;
|
|
lineAdvanceInput.SwitchAdvanceHandler(advanceMode.CurrentMode);
|
|
}
|
|
|
|
public void SwitchQuickMode()
|
|
{
|
|
advanceMode.isQuick = !advanceMode.isQuick;
|
|
|
|
lineAdvanceInput.SwitchAdvanceHandler(advanceMode.CurrentMode);
|
|
Time.timeScale = advanceMode.CurrentMode == AdvanceModeEnum.Quick ? 8f : 1f;
|
|
}
|
|
|
|
#endregion
|
|
|
|
public LineSyncToken lineSyncToken;
|
|
public bool isInOption;
|
|
|
|
public LineSyncState GetDialogState()
|
|
{
|
|
if (lineSyncToken != null && lineSyncToken.state != LineSyncState.Advanced)
|
|
{
|
|
return lineSyncToken.state;
|
|
}
|
|
else if (isInOption)
|
|
{
|
|
return LineSyncState.InOption;
|
|
}
|
|
else
|
|
{
|
|
return LineSyncState.Default;
|
|
}
|
|
}
|
|
|
|
private void OnOptionShow()
|
|
{
|
|
isInOption = true;
|
|
}
|
|
|
|
private void OnOptionSelected(LineInfo lineInfo)
|
|
{
|
|
isInOption = false;
|
|
}
|
|
}
|
|
|
|
public struct AdvanceMode
|
|
{
|
|
public bool isAuto;
|
|
public bool isQuick;
|
|
|
|
public AdvanceModeEnum CurrentMode
|
|
{
|
|
get
|
|
{
|
|
if (isQuick)
|
|
{
|
|
return AdvanceModeEnum.Quick;
|
|
}
|
|
else if (isAuto)
|
|
{
|
|
return AdvanceModeEnum.Auto;
|
|
}
|
|
else
|
|
{
|
|
return AdvanceModeEnum.Default;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum AdvanceModeEnum
|
|
{
|
|
Default,
|
|
Auto,
|
|
Quick
|
|
}
|
|
} |