-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNovelGame.cs
258 lines (222 loc) · 6.49 KB
/
NovelGame.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
using System;
using Godot;
using GodotInk;
public partial class NovelGame : Node2D
{
[Export] private InkStory? _story;
[Export] private NarrativeReader? _narrativeReader;
[Export] private DialogueReader? _dialogueReader;
[Export] private Walkaround? _walkaround;
private InkStory Story => _story ?? throw new ArgumentNullException(nameof(_story));
private bool TEMP_AlreadyShownEndOfStory;
public enum NarrativeMode
{
Narration,
Dialogue,
Walkaround
}
public NarrativeMode currentMode { get; private set; } = NarrativeMode.Narration;
private NovelReader? ActiveReader
{
get
{
return currentMode switch
{
NarrativeMode.Narration => _narrativeReader ?? throw new ArgumentNullException(nameof(_narrativeReader)),
NarrativeMode.Dialogue => _dialogueReader ?? throw new ArgumentNullException(nameof(_dialogueReader)),
_ => null
};
}
}
public override void _Ready()
{
// TODO: Validate configuration and quit if things are missing
if (_narrativeReader != null) BindReaderEvents(_narrativeReader);
if (_dialogueReader != null) BindReaderEvents(_dialogueReader);
if (_walkaround != null) BindWalkaroundEvents(_walkaround);
Story.BindExternalFunction("clear_screen", ClearScreen);
Story.BindExternalFunction("narration_mode", () => SetMode(NarrativeMode.Narration));
Story.BindExternalFunction("dialogue_mode", () => SetMode(NarrativeMode.Dialogue));
Story.BindExternalFunction("walkaround_mode", (string location, string spawn) =>
{
SetMode(NarrativeMode.Walkaround);
ChangeLocation(location, spawn);
});
Story.BindExternalFunction("speaker_name", (string name) => SetSpeakerName(name));
Story.BindExternalFunction("left_character", (string name, string expression) => SetLeftCharacter(name, expression));
Story.BindExternalFunction("right_character", (string name, string expression) => SetRightCharacter(name, expression));
PrepareReader();
Advance();
}
public bool HasTaggedChoice(string tag)
{
foreach (InkChoice choice in Story.CurrentChoices)
{
if (choice.Tags != null && choice.Tags.Contains(tag))
{
return true;
}
}
return false;
}
private void BindReaderEvents(NovelReader reader)
{
reader.TypingCompleted += OnTypingCompleted;
reader.TextAdvanced += OnTextAdvanced;
reader.ChoiceSelected += OnChoiceSelectedByIndex;
}
private void BindWalkaroundEvents(Walkaround walkaround)
{
walkaround.ChoiceInteracted += OnChoiceSelectedByTag;
}
private void OnTypingCompleted()
{
if (ActiveReader != null && Story.CurrentChoices.Count > 0)
{
ActiveReader.AddChoices(Story.CurrentChoices);
}
}
private void OnTextAdvanced()
{
Advance();
}
private void OnChoiceSelectedByIndex(int choiceIndex)
{
if (choiceIndex < 0 || choiceIndex >= Story.CurrentChoices.Count)
{
throw new ArgumentOutOfRangeException(nameof(choiceIndex));
}
Story.ChooseChoiceIndex(choiceIndex);
ClearScreen();
Advance();
}
private void OnChoiceSelectedByTag(string choiceTag)
{
for (int choiceIndex = 0; choiceIndex < Story.CurrentChoices.Count; choiceIndex++)
{
InkChoice choice = Story.CurrentChoices[choiceIndex];
if (choice.Tags != null && choice.Tags.Contains(choiceTag))
{
Story.ChooseChoiceIndex(choiceIndex);
Advance();
return;
}
}
GD.PushError("Attempted to select choice with tag \"" + choiceTag + "\" but no matching choice was found.");
}
private void Advance()
{
if (!Story.CanContinue && AtEndOfStory())
{
OnEndOfStory();
return;
}
// Continue the story, then if we have an active reader showing, show the next line. Note that the call to
// Story.Continue() here may change modes, changing (or nulling) ActiveReader in the process.
string nextLine = Story.Continue();
ActiveReader?.AddLine(nextLine);
}
private void SetMode(NarrativeMode newMode)
{
if (newMode != currentMode)
{
currentMode = newMode;
switch (newMode)
{
case NarrativeMode.Narration:
case NarrativeMode.Dialogue:
PrepareReader();
break;
case NarrativeMode.Walkaround:
PrepareWalkaround();
break;
default:
throw new ArgumentOutOfRangeException(nameof(newMode), newMode, null);
}
}
}
private void SetSpeakerName(string name)
{
if (ActiveReader == null) GD.PushError("Attempted to set speaker name while no reader was active.");
ActiveReader?.SetSpeakerName(name);
}
private void SetLeftCharacter(string name, string expression)
{
if (ActiveReader == null) GD.PushError("Attempted to set visible character while no reader was active.");
ActiveReader?.SetLeftCharacter(name, expression);
}
private void SetRightCharacter(string name, string expression)
{
if (ActiveReader == null) GD.PushError("Attempted to set visible character while no reader was active.");
ActiveReader?.SetRightCharacter(name, expression);
}
// Shows the correct reader for the current narrative mode and resets it to a blank state.
private void PrepareReader()
{
// Switch to the correct reader.
switch (currentMode)
{
case NarrativeMode.Narration:
_dialogueReader?.Hide();
_narrativeReader?.Show();
break;
case NarrativeMode.Dialogue:
_narrativeReader?.Hide();
_dialogueReader?.Show();
break;
default:
throw new ArgumentOutOfRangeException(nameof(currentMode), currentMode,
"Reader is not available in non-text modes.");
}
_walkaround?.Hide();
ActiveReader?.Reset();
ActiveReader?.GrabFocus();
}
private void PrepareWalkaround()
{
_walkaround?.Show();
_dialogueReader?.Hide();
_narrativeReader?.Hide();
}
private void ChangeLocation(string location, string spawn)
{
if (_walkaround?.currentRoomName != location)
{
_walkaround?.LoadRoom(location);
_walkaround?.WarpToPoint(spawn);
}
else
{
// commenting out because it's spammy
//GD.Print("Requested to move to location \"" + location + "\" at point \"" + spawn + "\", but we were already in that room. Skipping teleport.");
}
}
private void ClearScreen()
{
ActiveReader?.ClearScreen();
}
private bool AtEndOfStory()
{
return !Story.CanContinue && Story.CurrentChoices.Count == 0;
}
private void OnEndOfStory()
{
if (TEMP_AlreadyShownEndOfStory)
{
GD.PrintErr("Already called EOS.");
return;
}
TEMP_AlreadyShownEndOfStory = true;
switch (currentMode)
{
case NarrativeMode.Narration:
case NarrativeMode.Dialogue:
ActiveReader?.AddRawLabel("End of story reached.");
break;
default:
SetMode(NarrativeMode.Narration);
ActiveReader?.AddRawLabel("End of story reached.");
break;
}
}
}