-
Notifications
You must be signed in to change notification settings - Fork 0
/
DialogueManager.cs
1257 lines (1038 loc) · 50.4 KB
/
DialogueManager.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//TODO: Shops should be opened via choices in dialogue (Buy vs sell choice buttons)
//TODO: everything is messed up lol so much redddddd
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.Serialization;
using UnityEngine.UI;
using Dialogue_Scripts;
public delegate void DialogueStartedEvent();
public delegate void DialogueEndedEvent();
public delegate void DialoguePausedEvent();
public delegate void DialogueUnpausedEvent();
public class DialogueManager : MonoBehaviour
{
[NonSerialized]
public UnityEvent dialogueStartEvent;
[NonSerialized]
public UnityEvent dialogueEndEvent;
[NonSerialized]
public UnityEvent dialoguePauseEvent;
[NonSerialized]
public UnityEvent dialogueUnpauseEvent;
// public event DialogueStartedEvent _dialogueStartedEvent;
// public event DialogueEndedEvent _dialogueEndedEvent;
private static DialogueManager _instance;
public static DialogueManager Instance => _instance;
//should i be sorting the variables by type name or actual var name?
/***********************CHOICES VARIABLES*********************/
/// <summary>
/// Integer that defines the index of the actively selected choice button. Possible values start at 0 and increment by +1 for each button.
/// </summary>
private int _currentSelectedChoice;
private bool _displayingChoices;
private GameObject _choiceButton;
/***********************END CHOICES VARIABLES*********************/
/***********************DIALOGUE READING VARIABLES*********************/
private bool _displayAll;
//if the dialogue is activated? on? i can't think of a word
private bool _dialogueOn;
//if the dialogue is still going through an animation display?
private bool _displayingDialogue;
//if the line still has dialogue remaining to display (but was too large to display in one step)
private bool _dialogueRemaining;
public bool DisplayingDialogue => _displayingDialogue;
//private char[] _chars;
private Dictionary<int, DialogueNode> _dialogueTree;
private IEnumerator _displayLine;
private int _currentNode;
private int _lineIndex;
/*
private string _previousSpeaker;
*/
private int _previousSpeakerIndex;
private Dictionary<string, Sprite> _speakerImages;
//i have no idea what this variable is for
//private bool waitingToDisplay;
/// <summary>
/// Boolean that shows whether the History is displayed.
/// True if History is displayed. False otherwise.
/// </summary>
private bool _historyDisplayed;
//todo: convert entire access to either static or object oriented. Right now it's conflicting but shouldn't create issues since there should only be one instance of this script.
/// <summary>
/// Static access to determine if the dialogue is currently paused.
/// </summary>
public static bool paused;
//todo: a PlayerPrefs that makes it completely skip the text animation (rn it just displays it extremely fast and moves to the next line without other input)
/// <summary>
/// Boolean which determines if the player wants to skip text.
/// True if skipping, false otherwise.
/// Will not skip if displaying choices.
/// </summary>
private bool skipText;
/***********************END DIALOGUE READING VARIABLES*********************/
/***********************UI VARIABLES*********************/
public GameObject choiceBox;
//a list containing the _choices buttons
private List<GameObject> _choices;
//a list containing the child object named Highlight (idk what else to call it | I'm too lazy to rename it)
private List<Image> _choicesSelectionIndicator;
private List<GameObject> _textChoices;
public Text nameTextBox;
public Image speakerImage;
public Text textBox;
private RectTransform _textBoxRectTransform;
//the parent object that holds all the history gameobjects
public GameObject historyParent;
//i don't know why we can't just stuff and all this feels excessive
public GameObject historyContent;
public ScrollRect historyScrollRect;
public GameObject scrollBar;
public GameObject spriteContainer, dialogueContainer;
private Sprite speakerImageDefault;
//the image that appears to show the text can be continued or whatever I can't think rn
public GameObject NextIndicator;
private TextGenerator _textGenerator;
private TextGenerationSettings _generationSettings;
/***********************END UI VARIABLES*********************/
/***********************PLAYER PREFS VARIABLES*********************/
//are these necessary?
private const string PrefsKeySpeed = "Display Speed";
private int _displaySpeed;
//run with the assumption that 1 is true for the pref (since player prefs can't have a bool lmao)
private const string PrefsKeyAuto = "Auto Scroll on Click";
private bool _autoScrollOnClick;
//If true, the player does not need to hold the ctrl key to skip text
private bool skipTextModeToggle;
private const string PrefsKeySkipTextToggle = "Skip Text Toggle";
/***********************END PLAYER PREFS VARIABLES*********************/
/***********************HISTORY VARIABLES*********************/
//no this does not include the ui vars. I'm not sure how to sort these tbh
private GameObject _nameCardHistoryPrefab, _textHistoryPrefab, _nameWrapperPrefab, _textWrapperPrefab, _choiceWrapperPrefab;
//literally contains everything that i'm throwing in the history
private List<Text> _historyTextList;
private int currentHistoryIndex;
private Transform _currentTextWrapper;
private bool _createdChoice;
//and yeah, it might be better to do some other indenting magic than creating multiple text objects but I'm stoopid
/***********************END HISTORY VARIABLES*********************/
public static bool isDialogueFinished;
private bool started =false;
private void Start()
{
}
void OnSceneChange()
{
}
private void Update()
{
//dialogue script can be paused with a line that contains --PAUSE-- in it
//this does not display anything; it will simply move to the next line in numerical order
if (!paused)
{
//if the dialogue is on (since we don't need this displaying or whatever when we hide the box)
if (_dialogueOn)
{
//if the player has the setting so they have to hold to skip text
//and if they are holding a ctrl key
if (!skipTextModeToggle)
{
if ((Input.GetKey(KeyCode.LeftControl)) || Input.GetKey(KeyCode.RightControl)||Input.GetKey(KeyCode.Equals))
{
skipText = true;
}
else
{
skipText = false;
}
}
//if the user has it togglable, and they press one of the keys, it switches the skip mode
else if (Input.GetKeyDown(KeyCode.LeftControl) || Input.GetKeyDown(KeyCode.RightControl)||Input.GetKey(KeyCode.Equals))
{
skipText = !skipText;
}
var dialogueNode = _dialogueTree[_currentNode];
if (!_historyDisplayed)
{
if (Input.GetKeyDown(KeyCode.Backspace) || Input.GetKeyDown(KeyCode.H) ||
Input.GetAxisRaw("Mouse ScrollWheel") > 0f)
{
DisplayHistory();
return;
}
if (_displayingChoices)
{
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) ||
Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.UpArrow))
{
UpdateSelectedChoice(_currentSelectedChoice - 1);
}
else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) ||
Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.DownArrow))
{
UpdateSelectedChoice(_currentSelectedChoice + 1);
}
if ((Input.GetKeyDown(KeyCode.Return)||Input.GetKeyDown(KeyCode.KeypadEnter)||Input.GetKeyDown(KeyCode.Space))&& _currentSelectedChoice>=0)
{
StoredMakeChoice();
}
}
else
{
if (skipText && !_dialogueRemaining &&!_displayingDialogue && !_dialogueTree[_currentNode].ConnectionsAreChoices && !_displayingChoices)
{
if (!NextLine(dialogueNode.Connection[0]))
{
EndDialogue();
return;
}
}
if (MoveDialogueForward())
{
//is the animation still playing? if so, then skip the animation completely
//(all this is is changing the variable that acts in the coroutine)
if (_displayingDialogue)
{
_displayAll = true;
}
else
{
if (!_dialogueRemaining &&!_dialogueTree[_currentNode].ConnectionsAreChoices && !_displayingChoices)
{
//because the dialogue has no choices, we can simply check its first connection
//if that connection returns false, that means the dialogue is over
if (!NextLine(dialogueNode.Connection[0]))
{
EndDialogue();
return;
}
}
}
}
}
/*
else if(!_displayingDialogue)
*/
}
else
{
if (Input.GetKeyDown(KeyCode.Backspace) || Input.GetKeyDown(KeyCode.H) || ((historyScrollRect.verticalNormalizedPosition<=.05f||!scrollBar.activeSelf)&&Input.GetAxisRaw("Mouse ScrollWheel")<0f)
//|| todo: scroll closes history when history is at bottom
/*Input.GetAxisRaw("Mouse ScrollWheel") < 0f*/
)
{
CloseHistory();
}
}
}
if (isDialogueFinished)
{
EndDialogue();
}
}
}
public void StartDialogue(TextAsset textFile)
{
paused = false;
dialogueStartEvent.Invoke();
_historyDisplayed = false;
_dialogueOn = true;
//if, for whatever reason, the player manages to start another dialogue prior to finishing another
if (!isDialogueFinished)
{
foreach (var VARIABLE in _choices)
{
Destroy(VARIABLE);
}
_choices.Clear();
_choicesSelectionIndicator.Clear();
StopAllCoroutines();
}
textBox.gameObject.SetActive(true);
_displayingChoices = false;
EventSystem.current.SetSelectedGameObject(null);
isDialogueFinished = false;
spriteContainer.SetActive(true);
dialogueContainer.SetActive(true);
speakerImage.gameObject.SetActive(true);
choiceBox.SetActive(false);
if (_choices.Count > 0)
_choices.Clear();
if(_textChoices.Count>0)
_textChoices.Clear();
if (PlayerPrefs.HasKey(PrefsKeySpeed))
{
_displaySpeed = PlayerPrefs.GetInt(PrefsKeySpeed);
}
else
{
_displaySpeed = 1;
PlayerPrefs.SetInt(PrefsKeySpeed,1);
}
if (PlayerPrefs.HasKey(PrefsKeyAuto))
{
_autoScrollOnClick = PlayerPrefs.GetInt(PrefsKeyAuto) == 1;
}
else
{
_autoScrollOnClick = false;
PlayerPrefs.SetInt(PrefsKeyAuto,0);
}
textBox.text = "";
//empties the historyText list
/*foreach (var historyText in _historyTextList)
{
Destroy(historyText);
}
*/
foreach (Transform child in historyContent.transform)
{
Destroy(child.gameObject);
}
_historyTextList.Clear();
_previousSpeakerIndex = 0;
_dialogueTree = DialogueNode.CreateDialogueNodes(textFile);
_currentNode = 1;
//creates the first name wrapper, adds the text to the wrapper, and updates the speaker variables
GameObject nameWrapper = Instantiate(_nameWrapperPrefab, historyContent.transform, false);
nameWrapper.GetComponent<VerticalLayoutGroup>().padding = new RectOffset();
_historyTextList.Add(Instantiate(_nameCardHistoryPrefab,nameWrapper.transform,false).GetComponent<Text>());
_historyTextList[0].text = nameTextBox.text = _dialogueTree[_currentNode].Name;
_currentTextWrapper = Instantiate(_textWrapperPrefab, historyContent.transform, false).transform;
_dialogueOn = true;
_displayLine = DisplayLine();
speakerImage.sprite = _speakerImages[_historyTextList[0].text];
currentHistoryIndex = 0;
StartCoroutine(DisplayLine());
}
public void EndDialogue()
{
isDialogueFinished = true;
_dialogueOn = false;
_displayingDialogue = false;
dialogueEndEvent?.Invoke();
/*if (delegatesStart.Count > 0)
{
foreach (var delegateStart in delegatesStart)
{
_dialogueStart -= delegateStart;
}
}
if (delegatesEnd.Count > 0)
{
foreach (var delegateEnd in delegatesEnd)
{
_dialogueEnd -= delegateEnd;
}
}*/
spriteContainer.SetActive(false);
speakerImage.gameObject.SetActive(false);
dialogueContainer.SetActive(false);
}
//this is literally only if we have a pause inside a dialogue file
//this should be called externally (there is no way for the dialogue to unpause itself, nor should there be)
public void UnPauseDialogue()
{
paused = false;
dialogueUnpauseEvent?.Invoke();
textBox.gameObject.SetActive(true);
spriteContainer.SetActive(true);
dialogueContainer.SetActive(true);
textBox.text = "";
var nextMessage = _dialogueTree[_currentNode].Message;
if (nextMessage.Equals("END"))
{
EndDialogue();
return;
}
if (nextMessage.Equals("PAUSE"))
{
PauseDialogue();
return;
}
NextLine(_currentNode);
}
public void PauseDialogue()
{
dialoguePauseEvent?.Invoke();
paused = true;
textBox.gameObject.SetActive(false);
spriteContainer.SetActive(false);
/*
_dialoguePaused?.Invoke();
*/
dialogueContainer.SetActive(false);
}
//returns true if there is another line
//returns false if there is no more lines to read
private bool NextLine(int currentNode)
{
NextIndicator.SetActive(false);
if (!speakerImage.enabled)
{
speakerImage.enabled = true;
}
StopCoroutine(_displayLine);
/*if (currentNode == -1)
{
StopAllCoroutines();
return false;
}*/
if (_dialogueTree[currentNode].Message.ToUpper().StartsWith("END"))
{
return false;
}
if (_dialogueTree[currentNode].Message.StartsWith("PAUSE"))
{
_currentNode = currentNode+1;
PauseDialogue();
return true;
}
var currentName = _dialogueTree[currentNode].Name;
if (currentName.ToLower().StartsWith("null"))
{
speakerImage.sprite = speakerImageDefault;
nameTextBox.text = "";
GameObject wrapper = Instantiate(_nameWrapperPrefab,historyContent.transform,false);
_historyTextList.Add(Instantiate(_nameCardHistoryPrefab, wrapper.transform, false).GetComponent<Text>());
_previousSpeakerIndex = _historyTextList.Count - 1;
_historyTextList[++currentHistoryIndex].text = " ";
_currentTextWrapper = Instantiate(_textWrapperPrefab, historyContent.transform, false).transform;
}
else if (!nameTextBox.text.StartsWith(currentName))
{
nameTextBox.text = currentName;
if (_speakerImages.ContainsKey(currentName))
{
speakerImage.sprite = _speakerImages[currentName];
}
else
{
//todo: probably set this to an unknown image sprite (like a sprite with a question mark)?
speakerImage.sprite = speakerImageDefault;
//may want to use a null sprite instead?
}
GameObject wrapper = Instantiate(_nameWrapperPrefab,historyContent.transform,false);
_historyTextList.Add(Instantiate(_nameCardHistoryPrefab, wrapper.transform, false).GetComponent<Text>());
_previousSpeakerIndex = _historyTextList.Count - 1;
_historyTextList[++currentHistoryIndex].text = currentName;
_currentTextWrapper = Instantiate(_textWrapperPrefab, historyContent.transform, false).transform;
}
_currentNode = currentNode;
_displayLine = DisplayLine();
textBox.text = "";
StartCoroutine(_displayLine);
return true;
}
//returns true if the player presses space, enter, or lmb
private bool MoveDialogueForward()
{
return Input.GetKeyDown(KeyCode.Space) || Input.GetKeyDown(KeyCode.KeypadEnter) ||
Input.GetKeyDown(KeyCode.Return) || Input.GetMouseButtonDown(0);
}
private IEnumerator DisplayLine()
{
if (_createdChoice)
{
_createdChoice = false;
//it'll look better like this, trust me
GameObject nameWrapper = Instantiate(_nameWrapperPrefab, historyContent.transform, false);
_historyTextList.Add(Instantiate(_nameCardHistoryPrefab,nameWrapper.transform,false).GetComponent<Text>());
_historyTextList[++currentHistoryIndex].text = _historyTextList[_previousSpeakerIndex].text;
//creates a new text wrapper
_currentTextWrapper = Instantiate(_textWrapperPrefab, historyContent.transform, false).transform;
}
var message = _dialogueTree[_currentNode].Message;
_historyTextList.Add(Instantiate(_textHistoryPrefab,_currentTextWrapper,false).GetComponent<Text>());
currentHistoryIndex++;
//if the previous object in the list is a name card
//then set an extra 5 units between the new object and old
if (_previousSpeakerIndex + 1 == currentHistoryIndex)
{
_currentTextWrapper.GetComponent<VerticalLayoutGroup>().padding.top += 5;
}
_historyTextList[currentHistoryIndex].text = "";
/*
_chars = message.ToCharArray();
*/
_displayingDialogue = true;
var sets = MaxVerticalWordDisplay(textBox,message);
var startTime = 0f;
char[] charsOfThisLine;
_dialogueRemaining = true;
for (var index = 0; index < sets.Count; index++)
{
_displayingDialogue = true;
_displayAll = false;
var set = sets[index];
charsOfThisLine = set.ToCharArray();
for (_lineIndex = 0; _lineIndex < charsOfThisLine.Length; _lineIndex++)
{
while (_historyDisplayed)
{
yield return null;
}
if (_displayAll)
{
for (; _lineIndex < charsOfThisLine.Length; _lineIndex++)
{
//stopcoroutine wasn't working, i'm guessing because it can't pause the actual loops?
while (_historyDisplayed)
{
yield return null;
}
textBox.text += charsOfThisLine[_lineIndex];
}
_displayAll = false;
//yield return null;
break;
}
textBox.text += charsOfThisLine[_lineIndex].ToString();
//the delay between each character displayed
if (!skipText)
yield return new WaitForSeconds(.05f / _displaySpeed);
else
{
yield return new WaitForSeconds(0.0005f);
}
}
_displayingDialogue = false;
//necessary so the textbox won't be blank at the end of line
if (index + 1 == sets.Count)
{
break;
}
startTime = Time.time;
//in order to not break the update method's check to skip all animations
//waits 1.5secs before continuing the message
//we do this instead of instantly displaying the rest of the message because, depending on the text speed
//not everyone would have finished reading the initial half of the message
NextIndicator.SetActive(true);
_historyTextList[currentHistoryIndex].text += textBox.text;
while (!skipText && (!MoveDialogueForward()|| (_autoScrollOnClick &&Time.time - startTime>=1.5f)))
{
//copied and pasted from above
//in case the player decides to open the history, we do not want auto scroll to activate
while (_historyDisplayed&&!skipText)
{
//this is lazy, right? It can't be this easy...
startTime += Time.deltaTime;
yield return null;
}
yield return null;
}
NextIndicator.SetActive(false);
//todo?maybe i'll make a crappy text scrolling animation idk
textBox.text = "";
}
_dialogueRemaining = false;
_historyTextList[currentHistoryIndex].text += textBox.text;
var currentNode = _dialogueTree[_currentNode];
//checks if the connections are choices and
//if the choices are displayed
//i'm doing this because for some reason holding control while selecting a choice would sometimes call this instead
//it shouldn't, really shouldn't but I can't figure it out
if (_dialogueTree[_currentNode].ConnectionsAreChoices)
{
if(!_displayingChoices)
DisplayChoices(currentNode.Connection.Count);
}
//since we don't want the indicator to appear when there are choices to be made
//indicator implies that the player can click to move on
else
NextIndicator.SetActive(true);
}
public void DisplayHistory()
{
_historyDisplayed = true;
historyParent.SetActive(true);
historyScrollRect.normalizedPosition =new Vector2(0,0);
}
public void CloseHistory()
{
_historyDisplayed = false;
historyParent.SetActive(false);
}
//CHOICES METHODS
private void DisplayChoices(int choiceCount)
{
choiceBox.SetActive(true);
if (choiceCount < 1)
return;
_choices.Clear();
_textChoices.Clear();
DialogueNode node = _dialogueTree[_currentNode];
/*else*/
//90 between all items
//63 height
if (_choices.Capacity < choiceCount)
_choices.Capacity = choiceCount;
if (_choicesSelectionIndicator.Capacity < choiceCount)
_choicesSelectionIndicator.Capacity = choiceCount;
//creates a new section containing the choices in the history
GameObject choiceHistoryWrapper = Instantiate(_choiceWrapperPrefab, historyContent.transform, false);
//its either the first or second object, and it better follow the stupid programming index conventions
GameObject choiceHistoryContent = choiceHistoryWrapper.transform.GetChild(1).gameObject;
//this code gives us a list of choices to be used for adding the buttons
List<DialogueNode> choices = new List<DialogueNode>(choiceCount);
List<int> nodeInts = new List<int>(choiceCount);
//this code only adds the choices (from the connections list) that have not been viewed yet
for (int i = 0;i<node.Connection.Count;i++)
{
//connection is an integer of the node value in the DialogueTree
var connection = node.Connection[i];
if (!_dialogueTree[connection].ChoiceHasBeenViewed)
{
choices.Add(_dialogueTree[connection]);
nodeInts.Add(connection);
}
}
for (int i = 0; i < choiceCount && i < choices.Count; i++)
{
DialogueNode choice = choices[i];
GameObject tempGameObject = Instantiate(_choiceButton, choiceBox.transform);
_choices.Add(tempGameObject);
//the name of the buttons are their nodes
_choices[i].name = nodeInts[i].ToString();
Text txt =
_choices[i].GetComponentInChildren<Text>();
_textChoices.Add(txt.gameObject);
txt.text = choice.Message;
txt.gameObject.name = i.ToString();
EventTrigger trigger = _choices[i].AddComponent<EventTrigger>();
var entry = new EventTrigger.Entry {eventID = EventTriggerType.PointerEnter};
entry.callback.AddListener((eventData) => {UpdateSelectedChoice(txt.gameObject.name);});
trigger.triggers.Add(entry);
//it's not an actual highlight, i'm just too lazy to rename the arrow
_choicesSelectionIndicator.Add(_choices[i].transform.Find("Highlight").GetComponent<Image>());
if (i == 0)
{
_choicesSelectionIndicator[i].gameObject.SetActive(true);
}
_choices[i].GetComponent<Button>().onClick.AddListener(MakeChoice);
_historyTextList.Add(Instantiate(_textHistoryPrefab, choiceHistoryContent.transform, false).GetComponent<Text>());
_historyTextList[++currentHistoryIndex].text = txt.text;
_historyTextList[currentHistoryIndex].color = Color.gray;
}
_displayingChoices = true;
_createdChoice = true;
_currentSelectedChoice = 0;
}
//i think nameOfChoice is the name of the choice??????/??
public void UpdateSelectedChoice(string nameOfChoice)
{
int i;
if (!int.TryParse(nameOfChoice, out i))
{
return;
}
if (_currentSelectedChoice < -5)
{
_currentSelectedChoice = i;
_choicesSelectionIndicator[i].gameObject.SetActive(true);
var position = _textChoices[i].transform.localPosition;
position = new Vector2(position.x+50, position.y);
_textChoices[i].transform.localPosition = position;
position = _choicesSelectionIndicator[i].transform.localPosition;
position = new Vector2(position.x+50, position.y);
_choicesSelectionIndicator[i].transform.localPosition = position;
EventSystem.current.SetSelectedGameObject(_choices[_currentSelectedChoice]);
return;
}
//removing effects from old choice
var oldChoice = _textChoices[_currentSelectedChoice];
var selectionPosition = _choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition;
selectionPosition = new Vector2(selectionPosition.x - 50, selectionPosition.y);
_choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition = selectionPosition;
_choicesSelectionIndicator[_currentSelectedChoice].gameObject.SetActive(false);
var localPosition = oldChoice.transform.localPosition;
localPosition = new Vector2(localPosition.x-50,localPosition.y);
oldChoice.transform.localPosition = localPosition;
_currentSelectedChoice = i;
var currentChoice = _textChoices[_currentSelectedChoice];
selectionPosition = _choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition;
selectionPosition = new Vector2(selectionPosition.x + 50, selectionPosition.y);
_choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition = selectionPosition;
_choicesSelectionIndicator[_currentSelectedChoice].gameObject.SetActive(true);
localPosition = currentChoice.transform.localPosition;
localPosition = new Vector2(localPosition.x+50, localPosition.y);
currentChoice.transform.localPosition = localPosition;
EventSystem.current.SetSelectedGameObject(_choices[_currentSelectedChoice]);
/*
* var oldChoice = _choices[currentSelectedChoice];
//ChangeColor(_choicesHighlights[currentSelectedChoice],-1,-1,-1,0);
_choicesHighlights[currentSelectedChoice].gameObject.SetActive(false);
var localPosition = oldChoice.transform.localPosition;
localPosition = new Vector2(localPosition.x-50,localPosition.y);
oldChoice.transform.localPosition = localPosition;
if (--currentSelectedChoice < 0)
{
currentSelectedChoice = _choices.Count - 1;
}
var currentChoice = _choices[currentSelectedChoice];
//ChangeColor(_choicesHighlights[currentSelectedChoice],-1,-1,-1,.4f);
_choicesHighlights[currentSelectedChoice].gameObject.SetActive(true);
localPosition = currentChoice.transform.localPosition;
localPosition = new Vector2(localPosition.x+50, localPosition.y);
currentChoice.transform.localPosition = localPosition;
EventSystem.current.SetSelectedGameObject(_choices[currentSelectedChoice]);
*/
}
private void UpdateSelectedChoice(int newChoiceIndex)
{
if (newChoiceIndex < -5)
{
_currentSelectedChoice = 0;
_choicesSelectionIndicator[0].gameObject.SetActive(true);
var positionO = _textChoices[0].transform.localPosition;
positionO = new Vector2(positionO.x+50, positionO.y);
_textChoices[0].transform.localPosition = positionO;
positionO = _choicesSelectionIndicator[0].transform.localPosition;
positionO = new Vector2(positionO.x+50, positionO.y);
_choicesSelectionIndicator[0].transform.localPosition = positionO;
EventSystem.current.SetSelectedGameObject(_choices[_currentSelectedChoice]);
return;
}
var selectionPosition = _choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition;
selectionPosition = new Vector2(selectionPosition.x - 50, selectionPosition.y);
_choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition = selectionPosition;
_choicesSelectionIndicator[_currentSelectedChoice].gameObject.SetActive(false);
var oldChoice = _textChoices[_currentSelectedChoice];
var localPosition = oldChoice.transform.localPosition;
localPosition = new Vector2(localPosition.x-50,localPosition.y);
oldChoice.transform.localPosition = localPosition;
//if the newChoiceIndex is less than 0, set currentSelectedChoice to the max index
//if it's not, check if it is past the max index
//if it is, set currentSelectedChoice to 0
//if it isn't, set currentSelectedChoice to newChoiceIndex
_currentSelectedChoice = newChoiceIndex < 0 ? _choices.Count - 1 : newChoiceIndex >= _choices.Count ? 0: newChoiceIndex;
//wtf is this? it's been months since I made this code and I have no idea what this is...
/*if (newChoiceIndex < _choices.Count&& newChoiceIndex >=0)
{
}*/
var currentChoice = _textChoices[_currentSelectedChoice];
selectionPosition = _choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition;
selectionPosition = new Vector2(selectionPosition.x + 50, selectionPosition.y);
_choicesSelectionIndicator[_currentSelectedChoice].transform.localPosition = selectionPosition;
_choicesSelectionIndicator[_currentSelectedChoice].gameObject.SetActive(true);
localPosition = currentChoice.transform.localPosition;
localPosition = new Vector2(localPosition.x+50, localPosition.y);
currentChoice.transform.localPosition = localPosition;
EventSystem.current.SetSelectedGameObject(_choices[_currentSelectedChoice]);
}
/// <summary>
/// Uses the currently selected GameObject via the EventSystem to try to make a choice.
/// Will fail if the name of the GameObject is not a valid int.
/// <seealso cref="StoredMakeChoice"/>
/// </summary>
public void MakeChoice()
{
//tries to make the name of the object into an int, and checks if the value is a valid line number
if (int.TryParse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name,
out var activatorName) && activatorName >0)
{
_dialogueTree[activatorName].ChoiceHasBeenViewed = true;
_historyTextList[currentHistoryIndex - (_choices.Count-_currentSelectedChoice-1)].color = Color.red;
choiceBox.SetActive(false);
EventSystem.current.SetSelectedGameObject(null);
foreach (var choice in _choices)
{
Destroy(choice);
}
_choices.Clear();
_choicesSelectionIndicator.Clear();
_displayingChoices = false;
if (_dialogueTree[_dialogueTree[activatorName].Connection[0]].isEnd())
{
EndDialogue();
StopAllCoroutines();
}
else if(_dialogueTree[activatorName].ConnectionsAreChoices)
{
_currentNode = activatorName;
DisplayChoices(_dialogueTree[activatorName].Connection.Count);
}
else
{
NextLine(_dialogueTree[activatorName].Connection[0]);
}
return;
}
}
/// <summary>
/// Uses the stored integer in <see cref="_currentSelectedChoice"/> to make the choice as opposed to using the selected GameObject.
/// Will not make a choice if the integer stored is an invalid index.
/// <seealso cref="MakeChoice"/>
/// </summary>
private void StoredMakeChoice()
{
//if it's a negative value (shouldn't be able to be at this point with my code) or if it's greater than the amount of indexes in the array
if (_currentSelectedChoice < 0 || _currentSelectedChoice >= _choices.Count || !int.TryParse(_choices[_currentSelectedChoice].name, out var choiceNodeNum))
return;
_dialogueTree[choiceNodeNum].ChoiceHasBeenViewed = true;
//this is copied and pasted from above, expect it to break because of my inability to check my work
_historyTextList[currentHistoryIndex - (_choices.Count-_currentSelectedChoice-1)].color = Color.red;
choiceBox.SetActive(false);
EventSystem.current.SetSelectedGameObject(null);
foreach (var choice in _choices)
{
Destroy(choice);
}
_choices.Clear();
_choicesSelectionIndicator.Clear();
_displayingChoices = false;
if (_dialogueTree[_dialogueTree[choiceNodeNum].Connection[0]].isEnd())
{
EndDialogue();
StopAllCoroutines();
}
else if(_dialogueTree[choiceNodeNum].ConnectionsAreChoices)
{
_currentNode = choiceNodeNum;
DisplayChoices(_dialogueTree[choiceNodeNum].Connection.Count);
}
else
{
NextLine(_dialogueTree[choiceNodeNum].Connection[0]);
}
}
//
/*
private IEnumerator AddChoicesToHistory()
{
yield return new WaitForEndOfFrame();
currentHistoryIndex++;
_historyTextList.Add(Instantiate(_textHistoryPrefab).GetComponent<Text>());
_historyTextList[currentHistoryIndex].transform.SetParent(historyContent.transform);
_historyTextList[currentHistoryIndex].transform.localPosition =
new Vector2(40,
_historyTextList[currentHistoryIndex-1].transform.localPosition.y-_historyTextList[currentHistoryIndex-1].preferredHeight+10);
_historyTextList[currentHistoryIndex].text = "Choice";
_historyTextList[currentHistoryIndex].color = Color.gray;
foreach (var t in _choices)
{
var txt = t.GetComponentInChildren<Text>();