-
Notifications
You must be signed in to change notification settings - Fork 0
/
CutsceneManager.cs
796 lines (707 loc) · 20 KB
/
CutsceneManager.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using System;
public class CutsceneManager : MonoBehaviourPun
{
private PlayerController playerController;
private PlayerManager playerManager;
private AffectionTracker affectionTracker;
[SerializeField]
private int playerNumber;
private GameManager gameManager;
private DynamicReference dynamicReference;
public CameraController cameraController;
private ObjectDetector objectDetector;
[SerializeField]
private DialogWindow dialogWindow;
//[SerializeField]
//private CameraScreen cameraScreen;
[SerializeField]
private bool cutsceneInProgress;
public CutsceneData cutsceneData; // Must be serialized to assign the data properly
private Cutscene currentCutscene;
private CutsceneAction currentAction;
private List<CutsceneAction> responseActions;
private bool waitingOnInput;
private int selectionIndex;
private bool readyToContinue;
private float timeOfLastChange;
private float shiftDelay;
private bool monitorScrollState;
private float timeOfPause;
private float pauseLength;
void Start ()
{
InitializeProperties();
}
void Update ()
{
if(cutsceneInProgress)
{
CheckScrollState();
if(waitingOnInput)
{
UpdateResponseSelection();
} else if (timeOfPause > 0)
{
CheckPause();
}
}
}
public bool CutsceneIsInProgress()
{
return cutsceneInProgress;
}
void InitializeProperties()
{
playerController = GetComponent<PlayerController>();
playerManager = GetComponent<PlayerManager>();
affectionTracker = GetComponent<AffectionTracker>();
playerNumber = playerManager.GetPlayerNumber();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
dynamicReference = gameManager.GetComponent<DynamicReference>();
objectDetector = transform.Find("Character").Find("Object Detector").GetComponent<ObjectDetector>();
AssignDialogWindow();
cutsceneInProgress = false;
waitingOnInput = false;
timeOfPause = 0;
pauseLength = 0;
selectionIndex = 0;
shiftDelay = 0.25f;
readyToContinue = true;
monitorScrollState = false;
responseActions = new List<CutsceneAction>();
}
public void AssignDialogWindow()
{
// Called during initialization
if(gameManager.GetNumberOfLocalPlayers() > 1)
{
dialogWindow = GameObject.Find("Dialog " + playerNumber).GetComponent<DialogWindow>();
//cameraScreen = GameObject.Find("Screen " + playerNumber).GetComponent<CameraScreen>();
} else
{
dialogWindow = GameObject.Find("Dialog Full").GetComponent<DialogWindow>();
//cameraScreen = GameObject.Find("Screen Full").GetComponent<CameraScreen>();
}
if(cameraController != null)
{
cameraController.SetViewport(playerNumber, gameManager.GetNumberOfLocalPlayers());
}
}
public void InteractPressed()
{
// Called from PlayerController when the interact button is pressed
if(cutsceneInProgress)
{
if(waitingOnInput)
{
ExecuteResponse();
} else if(readyToContinue)
{
GoToNextAction();
} else
{
SkipAction();
}
} else
{
StartDetectedCutscene();
}
}
void StartDetectedCutscene()
{
GameObject detectedObject = objectDetector.objectOfInterest;
if(detectedObject)
{
CutsceneHolder detectedCutsceneHolder = detectedObject.GetComponent<CutsceneHolder>();
if(detectedCutsceneHolder)
{
StartCutscene(detectedCutsceneHolder.cutsceneID);
}
}
}
public void StartCutscene(int cutsceneID)
{
if(cutsceneInProgress)
{
Debug.LogWarning("Cannot start new cutscene while another cutscene is in progress");
} else
{
int cutsceneIndex = GetCutsceneIndex(cutsceneID);
if(cutsceneIndex >= 0)
{
playerManager.CloseAllMenus();
playerManager.SetHotbarVisibility(false);
playerController.SetCutsceneState(true);
cutsceneInProgress = true;
waitingOnInput = false;
responseActions = null;
selectionIndex = 1;
dialogWindow.SetHighlightRow(selectionIndex);
dialogWindow.ResetText();
currentCutscene = cutsceneData.listItems[cutsceneIndex].cutscene;
currentAction = currentCutscene.cutsceneActions[0];
InterpretCurrentAction();
} else
{
Debug.LogError("Cutscene ID " + cutsceneID + " not found in list of indexes");
}
}
}
void EndCutscene()
{
dialogWindow.SetVisibility(false);
dialogWindow.ClearPortrait(true);
dialogWindow.ClearPortrait(false);
playerManager.SetHotbarVisibility(true);
currentCutscene = null;
currentAction = null;
cutsceneInProgress = false;
waitingOnInput = false;
selectionIndex = 0;
playerController.SetCutsceneState(false);
cameraController.ResetView();
//cameraScreen.ResetValues();
}
void GoToNextAction()
{
waitingOnInput = false;
if(currentAction.indexesOfNextActions.Count > 0)
{
if(currentCutscene.cutsceneActions[currentAction.indexesOfNextActions[0]].myActionType == CutsceneAction.actionType.Response)
{
SetResponses(currentAction);
} else
{
currentAction = currentCutscene.cutsceneActions[currentAction.indexesOfNextActions[0]];
InterpretCurrentAction();
}
} else
{
EndCutscene();
}
}
void SkipAction()
{
if(currentAction.myActionType == CutsceneAction.actionType.Dialogue)
{
dialogWindow.SkipScrollingText();
readyToContinue = true;
}
}
int GetCutsceneIndex(int cutsceneID)
{
if(cutsceneID < cutsceneData.nextCutsceneID)
{
return cutsceneData.cutsceneIndexes[cutsceneID];
} else
{
return -1;
}
}
string CheckDynamicNames(string lookupName)
{
string newName = lookupName;
if(lookupName == "Player")
{
// TODO: Get player's character name
} else if(lookupName == "Test")
{
newName = "Bob the Test";
}
return newName;
}
void CheckScrollState()
{
if(monitorScrollState)
{
if(!dialogWindow.GetScrollState())
{
monitorScrollState = false;
readyToContinue = true;
}
}
}
void InterpretCurrentAction()
{
if(currentAction == null)
{
Debug.LogError("Invalid cutscene action called");
} else
{
string actionType = currentAction.myActionType.ToString();
if(actionType == "Start")
{
GoToNextAction();
} else if(actionType == "Pivot")
{
GoToNextAction();
} else if(actionType == "Dialogue")
{
InterpretDialogue(currentAction);
} else if(actionType == "Portrait")
{
InterpretPortrait(currentAction);
} else if(actionType == "Response")
{
// Responses are initiated from GoToNextAction() and updated from Update(), no action needed here
} else if(actionType == "Animation")
{
InterpretAnimation(currentAction);
} else if(actionType == "Variable")
{
InterpretVariable(currentAction);
} else if(actionType == "Condition")
{
InterpretCondition(currentAction);
} else if(actionType == "MultiCondition")
{
InterpretMultiCondition(currentAction);
} else if(actionType == "Camera")
{
InterpretCameraAction(currentAction);
} else if(actionType == "Pause")
{
InterpretPauseAction(currentAction);
} else
{
Debug.LogWarning("Unhandled action of type: " + actionType);
EndCutscene();
}
}
}
void InterpretDialogue(CutsceneAction cutsceneAction)
{
string newDialogueLines = playerManager.RewriteString(cutsceneAction.textContent);
string newSpeakerName = playerManager.RewriteString(cutsceneAction.titleText);
readyToContinue = false;
monitorScrollState = true;
timeOfLastChange = Time.time;
dialogWindow.SetMessage(newSpeakerName, newDialogueLines, cutsceneAction.rightActor);
}
bool CheckAllConditions(CutsceneAction cutsceneAction)
{
int numberOfConditionsMet = 0;
bool conditionsMet = false;
if(cutsceneAction.conditionValues == null)
{
conditionsMet = true;
} else
{
if(cutsceneAction.conditionValues.Count == 0)
{
// It shouldn't be possible for this to happen - condition values found, but count is 0
conditionsMet = true;
} else
{
for(int j=0; j<cutsceneAction.conditionValues.Count; j++)
{
if(playerManager.CheckDynamicCondition(cutsceneAction.conditionVariables[j], cutsceneAction.conditionValues[j]))
{
numberOfConditionsMet++;
}
}
if(cutsceneAction.titleText == "AND" && numberOfConditionsMet == cutsceneAction.conditionValues.Count)
{
conditionsMet = true;
} else if(cutsceneAction.titleText == "OR" && numberOfConditionsMet > 0)
{
conditionsMet = true;
} else if(cutsceneAction.titleText == "NAND" && numberOfConditionsMet == 0)
{
conditionsMet = true;
} else if(cutsceneAction.titleText == "XOR" && numberOfConditionsMet == 1)
{
conditionsMet = true;
}
}
}
return conditionsMet;
}
void InterpretCondition(CutsceneAction cutsceneAction)
{
string variableName = cutsceneAction.titleText;
int conditionIndex = 0;
for(int i=0; i<cutsceneAction.conditionValues.Count; i++)
{
if(playerManager.CheckDynamicCondition(variableName, cutsceneAction.conditionValues[i]))
{
conditionIndex = i+1;
break;
}
}
if(conditionIndex < currentCutscene.cutsceneActions.Count && currentAction.indexesOfNextActions[conditionIndex] >= 0)
{
currentAction = currentCutscene.cutsceneActions[currentAction.indexesOfNextActions[conditionIndex]];
InterpretCurrentAction();
} else
{
EndCutscene();
}
}
void InterpretMultiCondition(CutsceneAction cutsceneAction)
{
if(CheckAllConditions(cutsceneAction))
{
currentAction = currentCutscene.cutsceneActions[cutsceneAction.indexesOfNextActions[1]];
InterpretCurrentAction();
} else
{
if(cutsceneAction.indexesOfNextActions[0] > 0)
{
currentAction = currentCutscene.cutsceneActions[cutsceneAction.indexesOfNextActions[0]];
InterpretCurrentAction();
} else
{
// No else path available
EndCutscene();
}
}
}
void InterpretVariable(CutsceneAction cutsceneAction)
{
string variableName = cutsceneAction.titleText;
string variableValue = cutsceneAction.textContent;
object parentObject = this;
bool affectionVariable = false;
// Special rules using . to indicate that the variable being changed is on something other than the cutscene manager
if(variableName.Contains("."))
{
int indexOfDot = variableName.IndexOf(".");
string variablePrefix = variableName.Substring(0, indexOfDot).ToLower();
variableName = variableName.Substring(indexOfDot+1, variableName.Length-indexOfDot-1);
if (variablePrefix == "aff" || variablePrefix == "affection")
{
affectionVariable = true;
}
}
if(affectionVariable)
{
// Affection variables have their own special processing
AffectionTracker affectionTracker = this.gameObject.GetComponent<AffectionTracker>();
if(affectionTracker)
{
int affectionChange;
bool validIntValue = int.TryParse(variableValue, out affectionChange);
if(validIntValue)
{
affectionTracker.AddAffection(variableName, affectionChange);
} else
{
Debug.LogWarning("Invalid affection change value provided");
}
}
} else
{
int variableCacheIndex = playerManager.GetVariableIndex(variableName);
if(variableCacheIndex >= 0)
{
playerManager.SetVariable(variableCacheIndex, variableValue);
} else
{
Debug.LogWarning("Variable " + variableName + " not found while attempting to change");
}
}
GoToNextAction();
}
void SetResponses(CutsceneAction cutsceneAction)
{
CutsceneAction tempAction;
responseActions = new List<CutsceneAction>();
string newResponseText = "";
for(int i=0; i<cutsceneAction.indexesOfNextActions.Count; i++)
{
tempAction = currentCutscene.cutsceneActions[cutsceneAction.indexesOfNextActions[i]];
if(CheckAllConditions(tempAction))
{
// Only add each action to the list, if its conditions have been met
responseActions.Add(tempAction);
newResponseText += tempAction.textContent + "\n";
}
}
if(responseActions.Count>0)
{
dialogWindow.SetResponses(newResponseText);
selectionIndex = 1;
waitingOnInput = true;
} else
{
// All responses have conditions, which have not been met
EndCutscene();
}
}
void UpdateResponseSelection()
{
Vector2 mousePosition = Input.mousePosition;
int hoverIndex = dialogWindow.GetResponseHoverIndex(mousePosition);
if(hoverIndex > responseActions.Count)
{
// The dialog window can bring back hover indexes above the number of responses, since it doesn't track how many there are
hoverIndex = 0;
}
if(hoverIndex > 0)
{
// Current mouse position is over a response
selectionIndex = hoverIndex;
timeOfLastChange = 0;
dialogWindow.SetHighlightRow(selectionIndex);
} else
{
// Current mouse position is NOT over a response
float verticalAxis = Input.GetAxis("Vertical" + playerNumber);
float horizontalAxis = Input.GetAxis("Horizontal" + playerNumber);
if(Mathf.Abs(verticalAxis) > 0.1f)
{
if(verticalAxis > 0)
{
// Up
ShiftResponseSelection(false);
} else
{
// Down
ShiftResponseSelection(true);
}
} else if(Mathf.Abs(horizontalAxis) > 0.1f)
{
if(horizontalAxis > 0)
{
// Right
ShiftResponseSelection(true);
} else
{
// Left
ShiftResponseSelection(false);
}
} else
{
// The mouse isn't over any responses and no axis is being moved, player might have released their axis
timeOfLastChange = 0;
}
}
}
void ShiftResponseSelection(bool shiftDown)
{
if(Time.time - timeOfLastChange >= shiftDelay)
{
if(shiftDown)
{
if(selectionIndex >= responseActions.Count)
{
selectionIndex = 1;
} else
{
selectionIndex++;
}
} else
{
if(selectionIndex <= 1)
{
selectionIndex = responseActions.Count;
} else
{
selectionIndex--;
}
}
dialogWindow.SetHighlightRow(selectionIndex);
timeOfLastChange = Time.time;
}
}
void ExecuteResponse()
{
currentAction = responseActions[selectionIndex-1];
responseActions = null;
selectionIndex = 1;
GoToNextAction();
}
void InterpretAnimation(CutsceneAction cutsceneAction)
{
GameObject actorObject;
Animator actorAnimator;
actorObject = GameObject.Find(cutsceneAction.titleText);
if(actorObject)
{
actorAnimator = actorObject.GetComponent<Animator>();
if(actorAnimator)
{
actorAnimator.SetTrigger(cutsceneAction.textContent);
}
}
GoToNextAction();
}
void InterpretCameraAction(CutsceneAction cutsceneAction)
{
string eventName = cutsceneAction.conditionValues[0].ToLower();
List<string> eventParameters = new List<string>();
string newFollowTarget = cutsceneAction.conditionValues[1];
bool waitForCompletion = cutsceneAction.rightActor;
string newPositionX_string = cutsceneAction.conditionValues[2];
string newPositionY_string = cutsceneAction.conditionValues[3];
string newSpeed_string = cutsceneAction.conditionValues[4];
float newPositionX;
float newPositionY;
float newSpeed;
if(eventName.Length > 0)
{
// Some events have parameters, separated by dots, extract them if found before processing the event
if(eventName.Contains("."))
{
int numberOfParameters = eventName.Length - eventName.Replace(".","").Length + 1;
string tempString = eventName;
string nextValue = "";
for(int i=0; i<numberOfParameters; i++)
{
nextValue = "";
if(tempString.Contains("."))
{
nextValue = tempString.Substring(0, tempString.IndexOf("."));
} else
{
nextValue = tempString;
}
if(nextValue.Length > 0)
{
eventParameters.Add(nextValue);
} else
{
Debug.LogWarning("Invalid event parameter given, canceling all camera actions");
return;
}
if(i<numberOfParameters-1)
{
tempString = tempString.Substring(nextValue.Length+1, tempString.Length - nextValue.Length - 1);
}
}
eventName = eventParameters[0];
eventParameters.RemoveAt(0);
if(eventName.Length <= 0)
{
Debug.LogWarning("Invalid event given");
}
}
// -----BEGIN CAMERA EVENTS-----
if(eventName == "reset")
{
cameraController.ResetView();
} else if(eventName == "tint")
{
if(eventParameters.Count == 4)
{
float newColorR = NormalizeColor( StringToFloat(eventParameters[0]) );
float newColorG = NormalizeColor( StringToFloat(eventParameters[1]) );
float newColorB = NormalizeColor( StringToFloat(eventParameters[2]) );
float newColorA = NormalizeColor( StringToFloat(eventParameters[3]) );
float newTintSpeed = StringToFloat(newSpeed_string);
if(newSpeed_string.Length == 0)
{
//cameraScreen.SetTint(newColorR, newColorG, newColorB, newColorA);
} else
{
//cameraScreen.SetTint(newColorR, newColorG, newColorB, newColorA, newTintSpeed);
}
} else
{
Debug.LogWarning("Invalid number of camera tint parameters (" + eventParameters.Count + ") provided, no tint applied");
}
}
// -----END CAMERA EVENTS-----
}
// New follow target
if(newFollowTarget.Length > 0)
{
GameObject newFollowTargetObject = GameObject.Find(newFollowTarget);
if(newFollowTargetObject)
{
cameraController.FollowNewTarget(newFollowTargetObject.transform);
} else
{
Debug.LogWarning("Unable to find target named: " + newFollowTarget);
}
}
// New position/offset
if(newPositionX_string.Length > 0 || newPositionY_string.Length > 0)
{
newPositionX = StringToFloat(newPositionX_string);
newPositionY = StringToFloat(newPositionY_string);
cameraController.SetOffset(newPositionX, newPositionY);
}
// New speed - this is also used for some events (tint)
if(newSpeed_string.Length > 0)
{
newSpeed = StringToFloat(newSpeed_string);
cameraController.SetMoveSpeed(newSpeed);
}
}
float StringToFloat(string stringToConvert)
{
// Converts a String to a Float value, with the default of 0 if an invalid or blank string is provided
// Any special action on null values must be checked before calling this, since 0 and null will both come back as 0
float floatValue = 0;
if(stringToConvert.Length > 0)
{
if (float.TryParse(stringToConvert, out floatValue))
{
floatValue = float.Parse(stringToConvert);
}
}
return floatValue;
}
float NormalizeColor(float colorNumber)
{
if(colorNumber < 0)
{
colorNumber = 0;
} else if (colorNumber > 255)
{
colorNumber = 255;
}
colorNumber = colorNumber / 255;
return colorNumber;
}
void InterpretPortrait(CutsceneAction cutsceneAction)
{
string characterName = cutsceneAction.titleText;
string expressionName = cutsceneAction.textContent;
if(characterName.Length > 0)
{
if(characterName.ToLower() == "clear")
{
dialogWindow.ClearPortrait(cutsceneAction.rightActor);
} else
{
dialogWindow.SetPortrait(characterName, cutsceneAction.rightActor);
}
}
if(expressionName.Length > 0)
{
dialogWindow.SetExpression(expressionName, cutsceneAction.rightActor);
}
GoToNextAction();
}
void InterpretPauseAction(CutsceneAction cutsceneAction)
{
string pauseLength_string = cutsceneAction.titleText;
float newPauseLength;
if(float.TryParse(pauseLength_string, out newPauseLength))
{
timeOfPause = Time.time;
pauseLength = newPauseLength;
dialogWindow.SetVisibility(false);
} else
{
Debug.LogWarning("Invalid pause time provided");
}
}
void CheckPause()
{
if(Time.time - timeOfPause >= pauseLength)
{
timeOfPause = 0;
pauseLength = 0;
dialogWindow.SetVisibility(true);
GoToNextAction();
}
}
}