154 lines
4.1 KiB
C#
154 lines
4.1 KiB
C#
using System;
|
|
using System.Collections;
|
|
using AibisDream.Framework;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogController : Singleton<DialogController>
|
|
{
|
|
private DialogueRunner _dialogueRunner;
|
|
private VariableStorageBehaviour _variableStorage;
|
|
|
|
# region controller状态标识
|
|
|
|
private bool _isTalking;
|
|
|
|
public bool IsTalking
|
|
{
|
|
set
|
|
{
|
|
if (_isTalking == value) return;
|
|
|
|
_isTalking = value;
|
|
Debug.Log($"IsTalking 状态更改: {_isTalking}");
|
|
if (value)
|
|
{
|
|
OnDialogueStart?.Invoke();
|
|
}
|
|
else
|
|
{
|
|
OnDialogueComplete?.Invoke();
|
|
}
|
|
}
|
|
get => _isTalking;
|
|
}
|
|
|
|
public bool quickRunMode;
|
|
|
|
# endregion
|
|
|
|
public static UnityAction OnDialogueStart;
|
|
public static UnityAction OnDialogueComplete;
|
|
|
|
private void Start()
|
|
{
|
|
_dialogueRunner = GetComponentInChildren<DialogueRunner>();
|
|
_variableStorage = GetComponentInChildren<InMemoryVariableStorage>();
|
|
|
|
_dialogueRunner.onDialogueStart.AddListener(() =>
|
|
{
|
|
EnumEventSystem.Global.Send(InteractionEventEnum.End);
|
|
OnDialogueStart?.Invoke();
|
|
IsTalking = true;
|
|
});
|
|
_dialogueRunner.onDialogueComplete.AddListener(() =>
|
|
{
|
|
EnumEventSystem.Global.Send(InteractionEventEnum.Start);
|
|
OnDialogueComplete?.Invoke();
|
|
IsTalking = false;
|
|
});
|
|
}
|
|
|
|
#region 变量操作
|
|
|
|
public bool TryGetVariable<T>(string variableName, out T result)
|
|
{
|
|
return _variableStorage.TryGetValue(variableName, out result);
|
|
}
|
|
|
|
public void SetVariable(string variableName, string value)
|
|
{
|
|
_variableStorage.SetValue(variableName, value);
|
|
}
|
|
|
|
public void SetVariable(string variableName, bool value)
|
|
{
|
|
_variableStorage.SetValue(variableName, value);
|
|
}
|
|
|
|
public void SetVariable(string variableName, float value)
|
|
{
|
|
_variableStorage.SetValue(variableName, value);
|
|
}
|
|
|
|
public bool Contains(string variableName)
|
|
{
|
|
return _variableStorage.Contains(variableName);
|
|
}
|
|
|
|
public void ClearVariables()
|
|
{
|
|
_variableStorage.Clear();
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 开启对话
|
|
/// </summary>
|
|
/// <param name="yarnProject">Yarn组</param>
|
|
public void StartDialog(YarnProject yarnProject)
|
|
{
|
|
_dialogueRunner.SetProject(yarnProject);
|
|
_dialogueRunner.StartDialogue("Start");
|
|
}
|
|
public void StopDialog()
|
|
{
|
|
_dialogueRunner.Stop();
|
|
}
|
|
|
|
public void LoadDialog(YarnProject yarnProject)
|
|
{
|
|
_dialogueRunner.SetProject(yarnProject);
|
|
_dialogueRunner.StartDialogue("Center");
|
|
}
|
|
|
|
public void StartDialogNode(string nodeName)
|
|
{
|
|
if (CheckIfNodeExistInCurrentYarnProject(nodeName))
|
|
{
|
|
_dialogueRunner.StartDialogue(nodeName);
|
|
}
|
|
}
|
|
|
|
public 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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 跳到下一句
|
|
/// </summary>
|
|
public void JumpLine()
|
|
{
|
|
if (!DialogCanvasManager.Instance.TrySkipLine())
|
|
{
|
|
DialogCanvasManager.Instance.NextStep();
|
|
}
|
|
}
|
|
}
|
|
} |