339 lines
9.7 KiB
C#
339 lines
9.7 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using AibisDream.Kit;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogController : Singleton<DialogController>
|
|
{
|
|
private DialogueRunner _dialogueRunner;
|
|
private VariableStorageBehaviour _variableStorage;
|
|
|
|
private readonly Dictionary<string, Action<YarnOption[]>> _commandMap = new();
|
|
private readonly Dictionary<string, Action<YarnContinueStep>> _pauseCommandMap = new();
|
|
|
|
# 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 IsBlock { get; private set; }
|
|
|
|
public bool quickRunMode;
|
|
|
|
# endregion
|
|
|
|
public static UnityAction OnDialogueStart;
|
|
public static UnityAction OnDialogueComplete;
|
|
|
|
private void Start()
|
|
{
|
|
_dialogueRunner = GetComponentInChildren<DialogueRunner>();
|
|
_variableStorage = GetComponentInChildren<InMemoryVariableStorage>();
|
|
|
|
_dialogueRunner.onDialogueStart.AddListener(() =>
|
|
{
|
|
OnDialogueStart?.Invoke();
|
|
IsTalking = true;
|
|
});
|
|
_dialogueRunner.onDialogueComplete.AddListener(() =>
|
|
{
|
|
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 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;
|
|
}
|
|
}
|
|
|
|
#region 选项指令
|
|
|
|
/// <summary>
|
|
/// 调用命令
|
|
/// </summary>
|
|
/// <param name="commandName">命令名</param>
|
|
/// <param name="options">选项</param>
|
|
public void InvokeOptionCommand(string commandName, YarnOption[] options)
|
|
{
|
|
if (commandName == null)
|
|
{
|
|
Debug.Log("命令名为空");
|
|
return;
|
|
}
|
|
|
|
if (_commandMap.TryGetValue(commandName, out var action))
|
|
{
|
|
action(options);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"命令 {commandName} 不存在");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册命令
|
|
/// </summary>
|
|
/// <param name="commandName">命令名称</param>
|
|
/// <param name="commandAction">注册名称</param>
|
|
public void RegisterOptionCommand(string commandName, Action<YarnOption[]> commandAction)
|
|
{
|
|
if (_commandMap.ContainsKey(commandName))
|
|
{
|
|
Debug.Log($"命令 {commandName} 已经存在,将被替换");
|
|
}
|
|
|
|
_commandMap[commandName] = commandAction;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除命令
|
|
/// </summary>
|
|
/// <param name="commandName">命令名称</param>
|
|
public void RemoveOptionCommand(string commandName)
|
|
{
|
|
if (_commandMap.ContainsKey(commandName))
|
|
{
|
|
_commandMap.Remove(commandName);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"命令 {commandName} 不存在");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 暂停选项指令
|
|
|
|
/// <summary>
|
|
/// 执行暂停命令
|
|
/// </summary>
|
|
/// <param name="commandName">命令名</param>
|
|
/// <param name="pauseCommand">参数</param>
|
|
public void InvokePauseCommand(string commandName, YarnContinueStep pauseCommand)
|
|
{
|
|
if (string.IsNullOrEmpty(commandName))
|
|
{
|
|
Debug.Log("命令名为空");
|
|
return;
|
|
}
|
|
|
|
if (_pauseCommandMap.TryGetValue(commandName, out var action))
|
|
{
|
|
action(pauseCommand);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"命令 {commandName} 不存在");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 注册暂停命令
|
|
/// </summary>
|
|
/// <param name="commandName">命令名</param>
|
|
/// <param name="commandAction">注册命令函数</param>
|
|
public void RegisterPauseCommand(string commandName, Action<YarnContinueStep> commandAction)
|
|
{
|
|
if (_pauseCommandMap.ContainsKey(commandName))
|
|
{
|
|
Debug.Log($"命令 {commandName} 已经存在,将被替换");
|
|
}
|
|
|
|
_pauseCommandMap[commandName] = commandAction;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 移除命令
|
|
/// </summary>
|
|
/// <param name="commandName">命令名称</param>
|
|
public void RemovePauseCommand(string commandName)
|
|
{
|
|
if (_pauseCommandMap.ContainsKey(commandName))
|
|
{
|
|
_pauseCommandMap.Remove(commandName);
|
|
}
|
|
else
|
|
{
|
|
Debug.Log($"命令 {commandName} 不存在");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
#region 命令名
|
|
|
|
[YarnCommand("NextYarn")]
|
|
public static void NextYarn()
|
|
{
|
|
Instance.ClearVariables();
|
|
Instance.StartCoroutine(GameLoopManager.Instance.NextSceneSo());
|
|
}
|
|
|
|
[YarnCommand("switch_tv")]
|
|
public static void SwitchTV(string picName)
|
|
{
|
|
MainUIController.Instance.SwitchTV(picName);
|
|
}
|
|
|
|
[YarnCommand("show_tv_scene")]
|
|
public static void ShowTVScene(float duration)
|
|
{
|
|
MainUIController.Instance.TVFadeIn(duration);
|
|
}
|
|
|
|
[YarnCommand("load_scene")]
|
|
public static IEnumerator LoadScene(string sceneName, float duration = 1)
|
|
{
|
|
HideDialog();
|
|
Camera.main.orthographicSize = 5;
|
|
Camera.main.transform.position = new Vector3(0, 0, -10);
|
|
yield return SceneLoader.Instance.LoadSceneAsync(sceneName);
|
|
DialogCanvasManager.Instance.SwitchDialogView(sceneName == "ClinicScene"
|
|
? DialogViewType.Bubble
|
|
: DialogViewType.OldBox);
|
|
|
|
AudioManager.PlayAmb2Audio("amb_general");
|
|
}
|
|
|
|
[YarnCommand("unload_scene")]
|
|
public static IEnumerator UnloadScene(string sceneName)
|
|
{
|
|
yield return SceneLoader.Instance.UnloadSceneAsync(sceneName);
|
|
}
|
|
|
|
[YarnCommand("hide_dialog")]
|
|
public static void HideDialog()
|
|
{
|
|
DialogCanvasManager.GetCurView().HideDialog();
|
|
}
|
|
|
|
[YarnCommand("fade_in")]
|
|
public static IEnumerator FadeIn(float duration)
|
|
{
|
|
return MainUIController.Instance.FadeInSync(duration);
|
|
}
|
|
|
|
[YarnCommand("fade_out")]
|
|
public static IEnumerator FadeOut(float duration)
|
|
{
|
|
return MainUIController.Instance.FadeOutSync(duration);
|
|
}
|
|
|
|
[YarnCommand("show_obj")]
|
|
public static IEnumerator ShowObj(string picName)
|
|
{
|
|
return MainUIController.Instance.ShowObj(picName);
|
|
}
|
|
|
|
[YarnCommand("hide_obj")]
|
|
public static IEnumerator HideObj()
|
|
{
|
|
return MainUIController.Instance.HideObj();
|
|
}
|
|
|
|
[YarnCommand("switch_dialog_view")]
|
|
public static void SwitchDialogView(string viewName)
|
|
{
|
|
DialogCanvasManager.Instance.SwitchDialogView(Enum.Parse<DialogViewType>(viewName));
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |