using System.Collections ;
using System ;
using TMPro ;
using UnityEngine.UI ;
/// <summary>
/// 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.
{
{
[SerializeField] private ChoiceBox _choiceBox ; // TODO: Remove. // Demonstration purpose only. Delete later.
/// <summary>
/// Called to begin a dialogue, i.e. a sequence of lines that require the player's input to move forward.
/// </summary>
/// <param name="firstLine"></param>
public void BeginDialogue ( DialogueLineSO firstLine )
private DialogueDataSO _currentDialogueDataSO ;
private int _counter ;
private bool _reachedEndOfDialogue { get = > _counter > = _currentDialogueDataSO . dialogueLines . Count ; }
private void OnAdvance ( )
{
_counter + + ;
if ( ! _reachedEndOfDialogue )
{
DisplayDialogueLine ( _currentDialogueDataSO . dialogueLines [ _counter ] ) ;
}
else
{
if ( _currentDialogueDataSO . Choices . Count > 0 )
{
DisplayChoices ( _currentDialogueDataSO . Choices ) ;
}
else
{
DialogueEnded ( ) ;
}
}
}
public void DialogueEnded ( )
{
// TODO: Disable dialogue box.
Debug . Log ( "Dialogue Ended" ) ;
_inputReader . advanceDialogueEvent - = OnAdvance ;
_inputReader . EnableGameplayInput ( ) ;
}
private void DisplayChoices ( List < Choice > choices )
{
_inputReader . advanceDialogueEvent - = OnAdvance ;
// Demonstration purpose only. Delete later.
_choiceBox . Show ( _currentDialogueDataSO . Choices , this ) ;
}
public void DisplayDialogueData ( DialogueDataSO dialogueDataSO )
{
BeginDialogueData ( dialogueDataSO ) ;
DisplayDialogueLine ( _currentDialogueDataSO . dialogueLines [ _counter ] ) ;
}
private void BeginDialogueData ( DialogueDataSO dialogueDataSO )
_counter = 0 ;
DisplayDialogueLine ( firstLine ) ;
_inputReader . advanceDialogueEvent + = OnAdvance ;
_currentDialogueDataSO = dialogueDataSO ;
}
/// <summary>
Debug . Log ( "A line of dialogue has been spoken: \"" + dialogueLine . Sentence + "\" by " + dialogueLine . Actor . ActorName ) ;
}
}
// Demonstration purpose only. Delete later.
// TODO: Remove.
[Serializable]
public class ChoiceBox
{
[SerializeField] private GameObject _gameObject ;
[SerializeField] private Transform _contentTrans ;
[SerializeField] private GameObject _buttonPrefab ;
public void Show ( List < Choice > 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 < Button > ( _buttonPrefab . GetComponent < Button > ( ) , _contentTrans ) ;
choiceButton . GetComponentInChildren < TMP_Text > ( ) . text = choices [ i ] . OptionName ;
int index = i ;
choiceButton . onClick . AddListener ( ( ) = > OnChoiceButtonClick ( choices [ index ] , dialogueManager ) ) ;
}
}
private void OnChoiceButtonClick ( Choice choice , DialogueManager dialogueManager )
{
if ( choice . Response ! = null )
dialogueManager . DisplayDialogueData ( choice . Response ) ;
else
dialogueManager . DialogueEnded ( ) ;
_gameObject . SetActive ( false ) ;
}
}