-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathDialogueManager.cs
More file actions
182 lines (149 loc) · 5.08 KB
/
DialogueManager.cs
File metadata and controls
182 lines (149 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Serialization;
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.
/// Keeps track of choices in the dialogue (if any) and then gives back control to gameplay when appropriate.
/// </summary>
public class DialogueManager : MonoBehaviour
{
// [SerializeField] private ChoiceBox _choiceBox; // TODO: Demonstration purpose only. Remove or adjust later.
[SerializeField] private InputReader _inputReader = default;
private int _counter;
private bool _reachedEndOfDialogue { get => _counter >= _currentDialogue.DialogueLines.Count; }
[Header("Listening on channels")]
[SerializeField] private DialogueDataChannelSO _startDialogue = default;
[SerializeField] private DialogueChoiceChannelSO _makeDialogueChoiceEvent = default;
[Header("BoradCasting on channels")]
[SerializeField] private DialogueLineChannelSO _openUIDialogueEvent = default;
[SerializeField] private DialogueChoicesChannelSO _showChoicesUIEvent = default;
[SerializeField] private DialogueDataChannelSO _endDialogue = default;
[SerializeField] private VoidEventChannelSO _continueWithStep = default;
[SerializeField] private VoidEventChannelSO _playWinningQuest = default;
[SerializeField] private VoidEventChannelSO _playLosingQuest = default;
[SerializeField] private VoidEventChannelSO _closeDialogueUIEvent = default;
[Header("Gameplay Components")]
[SerializeField]
private GameStateSO _gameState = default;
private DialogueDataSO _currentDialogue = default;
private void Start()
{
_startDialogue.OnEventRaised += DisplayDialogueData;
}
/// <summary>
/// Displays DialogueData in the UI, one by one.
/// </summary>
/// <param name="dialogueDataSO"></param>
public void DisplayDialogueData(DialogueDataSO dialogueDataSO)
{
if (_gameState.CurrentGameState != GameState.Cutscene)
_gameState.UpdateGameState(GameState.Dialogue);
BeginDialogueData(dialogueDataSO);
if (_currentDialogue.DialogueLines != null)
DisplayDialogueLine(_currentDialogue.DialogueLines[_counter], dialogueDataSO.Actor);
else
{
Debug.LogError("Check Dialogue");
}
}
/// <summary>
/// Prepare DialogueManager when first time displaying DialogueData.
/// <param name="dialogueDataSO"></param>
private void BeginDialogueData(DialogueDataSO dialogueDataSO)
{
_counter = 0;
_inputReader.EnableDialogueInput();
_inputReader.advanceDialogueEvent += OnAdvance;
_currentDialogue = dialogueDataSO;
}
/// <summary>
/// Displays a line of dialogue in the UI, by requesting it to the <c>DialogueManager</c>.
/// This function is also called by <c>DialogueBehaviour</c> from clips on Timeline during cutscenes.
/// </summary>
/// <param name="dialogueLine"></param>
public void DisplayDialogueLine(LocalizedString dialogueLine, ActorSO actor)
{
_openUIDialogueEvent.RaiseEvent(dialogueLine, actor);
}
private void OnAdvance()
{
_counter++;
if (!_reachedEndOfDialogue)
{
DisplayDialogueLine(_currentDialogue.DialogueLines[_counter], _currentDialogue.Actor);
}
else
{
if (_currentDialogue.Choices.Count > 0)
{
DisplayChoices(_currentDialogue.Choices);
}
else
{
DialogueEndedAndCloseDialogueUI();
}
}
}
private void DisplayChoices(List<Choice> choices)
{
_inputReader.advanceDialogueEvent -= OnAdvance;
_makeDialogueChoiceEvent.OnEventRaised += MakeDialogueChoice;
_showChoicesUIEvent.RaiseEvent(choices);
}
private void MakeDialogueChoice(Choice choice)
{
_makeDialogueChoiceEvent.OnEventRaised -= MakeDialogueChoice;
switch (choice.ActionType)
{
case ChoiceActionType.continueWithStep:
if (_continueWithStep != null)
_continueWithStep.RaiseEvent();
if (choice.NextDialogue != null)
DisplayDialogueData(choice.NextDialogue);
break;
case ChoiceActionType.winningChoice:
if (_playWinningQuest != null)
_playWinningQuest.RaiseEvent();
if (choice.NextDialogue != null)
DisplayDialogueData(choice.NextDialogue);
else
DialogueEndedAndCloseDialogueUI();
break;
case ChoiceActionType.losingChoice:
if (_playLosingQuest != null)
_playLosingQuest.RaiseEvent();
if (choice.NextDialogue != null)
DisplayDialogueData(choice.NextDialogue);
else
DialogueEndedAndCloseDialogueUI();
break;
case ChoiceActionType.doNothing
:
if (choice.NextDialogue != null)
DisplayDialogueData(choice.NextDialogue);
else
DialogueEndedAndCloseDialogueUI();
break;
}
}
void DialogueEnded()
{
if (_endDialogue != null)
_endDialogue.RaiseEvent(_currentDialogue);
_gameState.ResetToPreviousGameState();
}
public void DialogueEndedAndCloseDialogueUI()
{
if (_endDialogue != null)
_endDialogue.RaiseEvent(_currentDialogue);
if (_closeDialogueUIEvent != null)
_closeDialogueUIEvent.RaiseEvent();
_gameState.ResetToPreviousGameState();
_inputReader.advanceDialogueEvent -= OnAdvance;
_inputReader.EnableGameplayInput();
}
}