-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathDialogueDataSO.cs
203 lines (164 loc) · 4.46 KB
/
DialogueDataSO.cs
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Localization;
using UnityEditor.Localization;
using UnityEditor;
public enum DialogueType
{
startDialogue,
winDialogue,
loseDialogue,
defaultDialogue,
}
public enum ChoiceActionType
{
doNothing,
continueWithStep,
winningChoice,
losingChoice,
}
/// <summary>
/// A Dialogue is a list of consecutive DialogueLines. They play in sequence using the input of the player to skip forward.
/// In future versions it might contain support for branching conversations.
/// </summary>
[CreateAssetMenu(fileName = "newDialogue", menuName = "Dialogues/Dialogue Data")]
public class DialogueDataSO : ScriptableObject
{
[SerializeField] private ActorSO _actor = default;
[SerializeField] private List<LocalizedString> _dialogueLines = default;
[SerializeField] private List<Choice> _choices = default;
[SerializeField] private DialogueType _dialogueType = default;
public ActorSO Actor => _actor;
public List<LocalizedString> DialogueLines => _dialogueLines;
public List<Choice> Choices => _choices;
public DialogueType DialogueType
{
get { return _dialogueType; }
set { _dialogueType = value; }
}
public void SetActor(ActorSO newActor)
{
_actor = newActor;
}
public DialogueDataSO()
{
}
public DialogueDataSO(DialogueDataSO dialogue)
{
_actor = dialogue.Actor;
_dialogueLines = new List<LocalizedString>(dialogue.DialogueLines);
_choices = new List<Choice>();
if (dialogue.Choices != null)
for (int i = 0; i < dialogue.Choices.Count; i++)
{
_choices.Add(new Choice(dialogue.Choices[i]));
}
_dialogueType = dialogue.DialogueType;
}
#if UNITY_EDITOR
//TODO: Add support for branching conversations
// Maybe add 2 (or more) special line slots which represent a choice in a conversation
// Each line would also have an event associated, or another Dialogue
private void OnEnable()
{
SetDialogueLines();
}
void SetDialogueLines()
{
if (_dialogueLines == null)
_dialogueLines = new List<LocalizedString>();
_dialogueLines.Clear();
StringTableCollection collection = LocalizationEditorSettings.GetStringTableCollection("Questline Dialogue");
if (collection != null)
{
int index = 0;
LocalizedString _dialogueLine = null;
do
{
index++;
string key = "L" + index + "-" + this.name;
if (collection.SharedData.Contains(key))
{
_dialogueLine = new LocalizedString() { TableReference = "Questline Dialogue", TableEntryReference = key };
_dialogueLines.Add(_dialogueLine);
}
else
{
_dialogueLine = null;
}
} while (_dialogueLine != null);
}
}
public void CreateLine()
{
if (_dialogueLines == null)
_dialogueLines = new List<LocalizedString>();
_dialogueLines.Clear();
StringTableCollection collection = LocalizationEditorSettings.GetStringTableCollection("Questline Dialogue");
if (collection != null)
{
string DefaultKey = "L" + 1 + "-" + this.name;
if (!collection.SharedData.Contains(DefaultKey))
{
collection.SharedData.AddKey(DefaultKey);
}
}
SetDialogueLines();
}
public void RemoveLineFromSharedTable()
{
StringTableCollection collection = LocalizationEditorSettings.GetStringTableCollection("Questline Dialogue");
if (collection != null)
{
int index = 0;
LocalizedString _dialogueLine = null;
do
{
index++;
string key = "L" + index + "-" + this.name;
if (collection.SharedData.Contains(key))
{
collection.SharedData.RemoveKey(key);
}
else
{
_dialogueLine = null;
}
} while (_dialogueLine != null);
}
}
/// <summary>
/// This function is only useful for the Questline Tool in Editor to remove a Questline
/// </summary>
/// <returns>The local path</returns>
public string GetPath()
{
return AssetDatabase.GetAssetPath(this);
}
#endif
}
[Serializable]
public class Choice
{
[SerializeField] private LocalizedString _response = default;
[SerializeField] private DialogueDataSO _nextDialogue = default;
[SerializeField] private ChoiceActionType _actionType = default;
public LocalizedString Response => _response;
public DialogueDataSO NextDialogue => _nextDialogue;
public ChoiceActionType ActionType => _actionType;
public void SetNextDialogue(DialogueDataSO dialogue)
{
_nextDialogue = dialogue;
}
public Choice()
{
}
public Choice(Choice choice)
{
_response = choice.Response;
_nextDialogue = choice.NextDialogue;
_actionType = ActionType;
}
}