using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
///
/// Takes care of all things dialogue, whether they are coming from within a Timeline or just from the interaction with a character, or by any other mean.
/// Keeps track of choices in the dialogue (if any) and then gives back control to gameplay when appropriate.
///
public class DialogueManager : MonoBehaviour
{
[SerializeField] private ChoiceBox _choiceBox; // TODO: Demonstration purpose only. Remove or adjust later.
[SerializeField] private InputReader _inputReader = default;
private DialogueDataSO _currentDialogueDataSO;
private int _counter;
private bool _reachedEndOfDialogue { get => _counter >= _currentDialogueDataSO.dialogueLines.Count; }
///
/// Displays DialogueData in the UI, one by one.
///
///
public void DisplayDialogueData(DialogueDataSO dialogueDataSO)
{
BeginDialogueData(dialogueDataSO);
DisplayDialogueLine(_currentDialogueDataSO.dialogueLines[_counter]);
}
///
/// Prepare DialogueManager when first time displaying DialogueData.
///
private void BeginDialogueData(DialogueDataSO dialogueDataSO)
{
_counter = 0;
_inputReader.EnableDialogueInput();
_inputReader.advanceDialogueEvent += OnAdvance;
_currentDialogueDataSO = dialogueDataSO;
}
///
/// Displays a line of dialogue in the UI, by requesting it to the DialogueManager.
/// This function is also called by DialogueBehaviour from clips on Timeline during cutscenes.
///
///
public void DisplayDialogueLine(DialogueLineSO dialogueLine)
{
//TODO: Interface with a UIManager to allow displayal of the line of dialogue in the UI
Debug.Log("A line of dialogue has been spoken: \"" + dialogueLine.Sentence + "\" by " + dialogueLine.Actor.ActorName);
}
private void OnAdvance()
{
_counter++;
if (!_reachedEndOfDialogue)
{
DisplayDialogueLine(_currentDialogueDataSO.dialogueLines[_counter]);
}
else
{
if(_currentDialogueDataSO.Choices.Count > 0)
{
DisplayChoices(_currentDialogueDataSO.Choices);
}
else
{
DialogueEnded();
}
}
}
private void DisplayChoices(List choices)
{
_inputReader.advanceDialogueEvent -= OnAdvance;
// TODO: Demonstration purpose only. Remove or adjust later.
_choiceBox.Show(_currentDialogueDataSO.Choices, this);
}
public void DialogueEnded()
{
// TODO: Disable dialogue box.
Debug.Log("Dialogue Ended");
_inputReader.advanceDialogueEvent -= OnAdvance;
_inputReader.EnableGameplayInput();
}
}
// TODO: Demonstration purpose only. Remove or adjust later.
[Serializable]
public class ChoiceBox
{
[SerializeField] private GameObject _gameObject;
[SerializeField] private Transform _contentTrans;
[SerializeField] private GameObject _buttonPrefab;
public void Show(List choices, DialogueManager dialogueManager)
{
_gameObject.SetActive(true);
// Refresh choice box
foreach(Transform child in _contentTrans)
{
GameObject.Destroy(child.gameObject);
}
for(int i = 0; i < choices.Count; i++)
{
Button choiceButton = GameObject.Instantiate