238 lines
7.0 KiB
C#
238 lines
7.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
using Yarn.Unity;
|
|
|
|
namespace AibisDream
|
|
{
|
|
public class DialogController : Singleton<DialogController>
|
|
{
|
|
private DialogueRunner dialogueRunner;
|
|
|
|
private VariableStorageBehaviour _variableStorage;
|
|
|
|
private Dictionary<string, Action<YarnOption[]>> commandMap = new();
|
|
|
|
private bool _isTalking;
|
|
|
|
public bool IsTalking
|
|
{
|
|
set
|
|
{
|
|
if (_isTalking == value) return;
|
|
|
|
_isTalking = value;
|
|
if (value)
|
|
{
|
|
OnDialogueStart?.Invoke();
|
|
}
|
|
else
|
|
{
|
|
OnDialogueComplete?.Invoke();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static UnityAction OnDialogueStart;
|
|
public static UnityAction OnDialogueComplete;
|
|
|
|
public bool quickRunMode;
|
|
|
|
private void Start()
|
|
{
|
|
dialogueRunner = GetComponentInChildren<DialogueRunner>();
|
|
_variableStorage = GetComponentInChildren<InMemoryVariableStorage>();
|
|
|
|
dialogueRunner.onDialogueStart.AddListener(() => { OnDialogueStart?.Invoke(); });
|
|
dialogueRunner.onDialogueComplete.AddListener(() => { OnDialogueComplete?.Invoke(); });
|
|
dialogueRunner.AddCommandListener(_ => { 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);
|
|
}
|
|
|
|
#endregion
|
|
|
|
/// <summary>
|
|
/// 开启对话
|
|
/// </summary>
|
|
/// <param name="yarnProject">Yarn组</param>
|
|
public void StartDialog(YarnProject yarnProject)
|
|
{
|
|
dialogueRunner.SetProject(yarnProject);
|
|
dialogueRunner.StartDialogue("Start");
|
|
}
|
|
|
|
/// <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} 不存在");
|
|
}
|
|
}
|
|
|
|
#region 命令名
|
|
|
|
[YarnCommand("switch_tv")]
|
|
public static void SwitchTV(string picName)
|
|
{
|
|
MainUIController.Instance.SwitchTV(picName);
|
|
}
|
|
|
|
[YarnCommand("indoor")]
|
|
public static void Indoor(float duration = 1)
|
|
{
|
|
MainUIController.Instance.TVFadeOut(duration);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 角色初始化
|
|
/// </summary>
|
|
/// <param name="picName">角色图片名</param>
|
|
[YarnCommand("character_init")]
|
|
public static void CharacterInit(string picName)
|
|
{
|
|
MainSceneController.Instance.Guest.SwitchImage(picName);
|
|
MainSceneController.Instance.Guest.Show();
|
|
}
|
|
|
|
[YarnCommand("character_switch_image")]
|
|
public static void CharacterSwitchImage(string picPath)
|
|
{
|
|
// TODO 统一路径后就不用路径做参数了
|
|
MainSceneController.Instance.Guest.SwitchImage(picPath);
|
|
}
|
|
|
|
[YarnCommand("character_fade_in")]
|
|
public static IEnumerator CharacterFadeIn(float duration = 1)
|
|
{
|
|
return MainSceneController.Instance.Guest.FadeInSync(duration);
|
|
}
|
|
|
|
[YarnCommand("character_fade_Out")]
|
|
public static IEnumerator CharacterFadeOut(float duration = 1)
|
|
{
|
|
return MainSceneController.Instance.Guest.FadeOutSync(duration);
|
|
}
|
|
|
|
[YarnCommand("character_move_to_fix")]
|
|
public static IEnumerator CharacterMoveToFix()
|
|
{
|
|
return MainSceneController.Instance.MoveToFix();
|
|
}
|
|
|
|
[YarnCommand("load_scene")]
|
|
public static IEnumerator LoadScene(string sceneName, float duration = 1)
|
|
{
|
|
HideDialog();
|
|
yield return MainUIController.Instance.FadeInSync(duration);
|
|
yield return SceneLoader.Instance.LoadSceneAsync(sceneName);
|
|
yield return MainUIController.Instance.FadeOutSync(duration);
|
|
}
|
|
|
|
[YarnCommand("unload_scene")]
|
|
public static IEnumerator UnloadScene(string sceneName)
|
|
{
|
|
yield return SceneLoader.Instance.UnloadSceneAsync(sceneName);
|
|
}
|
|
|
|
[YarnCommand("hide_dialog")]
|
|
public static void HideDialog()
|
|
{
|
|
DialogUiViewer.Instance.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("fade_in")]
|
|
public static IEnumerator FadeIn()
|
|
{
|
|
return MainUIController.Instance.FadeInSync(1);
|
|
}
|
|
|
|
[YarnCommand("fade_out")]
|
|
public static IEnumerator FadeOut()
|
|
{
|
|
return MainUIController.Instance.FadeOutSync(1);
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |