/
ChapterController.cs
1383 lines (1263 loc) · 53.6 KB
/
ChapterController.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
using Assets.Script.Model;
using Assets.Script.Model.Components;
using Assets.Script.Model.Datas;
using Assets.Script.Utility;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Video;
namespace Assets.Script.Chapter
{
public class ChapterController : MonoBehaviour
{
#region Fields
private static string rootPath;
private static string savedDataPath;
private static string savedDataFile;
private GameObject titleContainer;
private GameObject lineContainer;
private GameObject popupWindow;
private GameObject menuField;
private GameObject selectorField;
private Text actorName;
private Text line; // Script line showing text
private Font font;
#region Game objects
private GameObject mainCanvas;
#region Prefabs
private Text historyTextPrefab;
private Button saveDataModelPrefab;
private Button selectorOptionPrefab;
#endregion
#region History gameobjects
private GameObject historyField;
private GameObject historyTexts;
private Text currentActiveHistoryText;
#endregion
#region Saved data field
private GameObject savedDataField;
private GameObject savedDataPanel;
private static List<List<Button>> savedDataButtons;
private static List<SavedDataModel> savedDatas;
private static int savdDataPageCount;
// Last loaded savedDataPage
private static int lastLoadedSavedDataPage;
#endregion
#region Setting field
private GameObject settingField;
#endregion
#region Operation buttons
private GameObject operationButtons;
private GameObject autoPlayButton;
private GameObject skipButton;
private GameObject saveButton;
private GameObject loadButton;
private GameObject settingButton;
#endregion
#region Selector field
private GameObject selector;
#endregion
#endregion
// Game objects end
// TODO: GalgameScript object may be a large data, so something must be done to optimize RAM.
// Current displaying script
private GalgameScript currentScript;
private SpriteRenderer bgSpriteRenderer;
private GameObject videoPlayer;
private GameObject audioSource;
private GameObject voiceAudioSource;
private List<GalgameAction> galgameActions;
private Dictionary<string, GalgameAction> galgameActionsMap;
private GalgameAction currentGalgameAction;
private PSelectorOption ActiveSelectorOption;
private int currentSelectorOptionActionIndex;
private int currentSelectorOptionLineIndex;
// Current showing line's index in `currentScript`
private int currentLineIndex;
// Current showing line text's index of char
private int currentLineCharIndex;
// Is a line text is showing
private bool isShowingLine;
// Is now a option line text display time
private bool isShowingSelectorOptionActionTime;
// Is auto-reading mode is actived
// private bool isAutoReadingModeOn;
// Is skip mode is actived
// private bool isSkipModeOn;
// The previous skip time
private DateTime preSkipTime;
// Is menu is actived
public bool isMenuActive;
// Is saving saved data now
private bool isSavingGameData;
// Is loading saved data now
private bool isLoadingSavedData;
// Next line to display
private string nextLine;
private GameController gameController;
private Coroutine currentTextShowCoroutine;
private Coroutine currentLineSwitchCoroutine;
private Coroutine hidehistoryFieldCoroutine;
private static WaitForSeconds textShowWaitForSeconds;
private static WaitForSeconds lineSwitchWaitForSeconds;
private static WaitForSeconds skipModeLineSwitchWaitForSeconds;
private VideoPlayer _video;
private AudioSource _bgmAudio;
private AudioSource _voiceAudio;
private AudioClip bgmMusic;
#endregion
// Use this for initialization
void Start()
{
mainCanvas = GameObject.Find("DisplayCanvas");
gameController = Camera.main.GetComponent<GameController>();
// Container
titleContainer = mainCanvas.transform.Find("TitleContainer").gameObject;
lineContainer = mainCanvas.transform.Find("LineContainer").gameObject;
historyField = mainCanvas.transform.Find("HistoryField").gameObject;
savedDataField = mainCanvas.transform.Find("SavedDataField").gameObject;
settingField = mainCanvas.transform.Find("SettingField").gameObject;
popupWindow = mainCanvas.transform.Find("PopupWindow").gameObject;
menuField = mainCanvas.transform.Find("MenuField").gameObject;
selectorField = mainCanvas.transform.Find("SelectorField").gameObject;
historyTexts = mainCanvas.transform.Find("HistoryField/Viewport/Content/HistoryTexts").gameObject;
operationButtons = mainCanvas.transform.Find("LineContainer/OperationBtn").gameObject;
// Prefabs
historyTextPrefab = Resources.Load<GameObject>("Prefabs/HistoryText").GetComponent<Text>();
saveDataModelPrefab = Resources.Load<GameObject>("Prefabs/SaveDataModel").GetComponent<Button>();
selectorOptionPrefab = Resources.Load<GameObject>("Prefabs/SelectorOption").GetComponent<Button>();
bgSpriteRenderer = GameObject.Find("Background/Bg").GetComponent<SpriteRenderer>();
videoPlayer = GameObject.Find("VideoPlayer/VideoPlayer");
audioSource = GameObject.Find("AudioSource/BgmAudioSource");
voiceAudioSource = GameObject.Find("AudioSource/VoiceAudioSource");
currentScript = Resources.Load<GalgameScript>("Chapter/Chapter-01");
// Init line
// Init currentScript, galgameActions, currentLineIndex
// currentLineIndex = 0; // ?
line = lineContainer.transform.Find("Line").GetComponent<Text>();
actorName = lineContainer.transform.Find("ActorName").GetComponent<Text>();
galgameActions = currentScript.GalgameActions;
if (null != currentScript.Bg)
{
bgSpriteRenderer.sprite = currentScript.Bg;
}
// Reload galgameAction as a hashtable
galgameActionsMap = new Dictionary<string, GalgameAction>();
foreach (GalgameAction a in currentScript.GalgameActions)
{
galgameActionsMap.Add(a.Id, a);
}
textShowWaitForSeconds = new WaitForSeconds(SettingModel.textShowDuration);
lineSwitchWaitForSeconds = new WaitForSeconds(SettingModel.lineSwitchDuration);
skipModeLineSwitchWaitForSeconds = new WaitForSeconds(SettingModel.skipModeLineSwitchDuration);
// opearation buttons
autoPlayButton = operationButtons.transform.Find("AutoPlayBtn").gameObject;
skipButton = operationButtons.transform.Find("SkipBtn").gameObject;
saveButton = operationButtons.transform.Find("SaveBtn").gameObject;
loadButton = operationButtons.transform.Find("LoadBtn").gameObject;
settingButton = operationButtons.transform.Find("SettingBtn").gameObject;
// media player
_video = videoPlayer.GetComponent<VideoPlayer>();
_bgmAudio = audioSource.GetComponent<AudioSource>();
_voiceAudio = voiceAudioSource.GetComponent<AudioSource>();
// selector field
selector = selectorField.transform.Find("Selector").gameObject;
// savedDatas = gameController.LoadSavedDatas();
savedDataPanel = savedDataField.transform.Find("SavedDataPanel").gameObject;
lastLoadedSavedDataPage = GameController.lastLoadedSavedDataPage;
savdDataPageCount = GameController.savdDataPageCount;
savedDatas = GameController.savedDatas;
savedDataButtons = gameController.InitList<List<Button>>(savdDataPageCount);
// InitSceneGameObject();
}
// Update is called once per frame
void Update()
{
// mosue left button click
if (Input.GetButtonDown("Fire1"))
{
if (titleContainer.activeSelf)
{
return;
}
GameObject hitUIObject = null;
if (EventSystem.current.IsPointerOverGameObject())
{
hitUIObject = GetMouseOverUIObject(mainCanvas);
Debug.Log("---- EventSystem.current.IsPointerOverGameObject ----" + hitUIObject);
}
if (null != hitUIObject && hitUIObject.tag.Trim() == "OperationButton")
{
Debug.Log("hitUIObject.name: " + hitUIObject.name);
switch (hitUIObject.name)
{
case "CloseSaveData":
isSavingGameData = false;
isLoadingSavedData = false;
SetActiveSavedDataPanel(0);
break;
}
}
if (currentLineIndex <= galgameActions.Count && IsSwitchLineAllowed())
{
if (!lineContainer.activeSelf)
{
gameController.ActiveGameObject(lineContainer);
}
if (null == hitUIObject || (null != hitUIObject && hitUIObject.tag.Trim() != "OperationButton"))
{
SwitchLine();
}
}
}
if (SettingModel.isSkipModeOn && IsSwitchLineAllowed() && (null == preSkipTime || (DateTime.Now - preSkipTime).TotalSeconds > SettingModel.skipModeLineSwitchDuration))
{
if (!lineContainer.activeSelf)
{
gameController.ActiveGameObject(lineContainer);
}
SwitchLine();
preSkipTime = DateTime.Now;
}
// mouse scroll up
if (Input.mouseScrollDelta.y > 0 && !historyField.activeSelf && gameController.inGame)
{
// TODO: Fix bug of adding double history text
ShowhistoryField();
}
}
#region Pubilc scene action
/// <summary>
/// Show history TextView
/// </summary>
public void ShowhistoryField()
{
historyField.SetActive(true);
// If `currentTextShowCoroutine` is going
if (isShowingLine)
{
ShowLineImmediately();
}
SetManualMode(true);
}
/// <summary>
/// Hide history TextView
/// </summary>
public void HideHistoryField()
{
hidehistoryFieldCoroutine = StartCoroutine(HideHistoryFieldTimeOut());
}
/// <summary>
/// Set reading mode to manual
/// </summary>
/// <param name="manual"></param>
public void SetManualMode(bool manual)
{
SettingModel.isManualModeOn = manual;
if (manual)
{
SetAutoMode(false);
SetSkipMode(false);
StopAllCoroutines();
// SettingModel.isAutoReadingModeOn = false;
// SettingModel.isSkipModeOn = false;
}
}
/// <summary>
/// Auto Reading
/// </summary>
public void AutoReading()
{
SetAutoMode(!SettingModel.isAutoReadingModeOn);
}
/// <summary>
/// Set auto mode
/// </summary>
/// <param name="auto"></param>
public void SetAutoMode(bool auto)
{
skipButton.GetComponent<Image>().color = Color.white;
SettingModel.isAutoReadingModeOn = auto;
// If SettingModel.isAutoReadingModeOn == true, call SwitchLine()
if (!auto)
{
autoPlayButton.GetComponent<Image>().color = Color.white;
}
else
{
SettingModel.isManualModeOn = false;
SettingModel.isSkipModeOn = false;
}
if (SettingModel.isAutoReadingModeOn && IsSwitchLineAllowed() && !SettingModel.isSkipModeOn && !isShowingLine)
{
autoPlayButton.GetComponent<Image>().color = Color.black;
currentLineSwitchCoroutine = StartCoroutine(SwitchLineTimeout());
}
}
/// <summary>
/// Enable/Disable skip mode
/// </summary>
public void ChangeSkipMode()
{
SetSkipMode(!SettingModel.isSkipModeOn);
}
/// <summary>
/// Set skip mode
/// </summary>
/// <param name="skip"></param>
public void SetSkipMode(bool skip)
{
autoPlayButton.GetComponent<Image>().color = Color.white;
SettingModel.isSkipModeOn = skip;
if (!skip)
{
skipButton.GetComponent<Image>().color = Color.white;
}
else
{
SettingModel.isManualModeOn = false;
SettingModel.isAutoReadingModeOn = false;
}
if (SettingModel.isSkipModeOn && IsSwitchLineAllowed())
{
skipButton.GetComponent<Image>().color = Color.black;
StopAllCoroutines();
ShowLineImmediately();
SwitchLine();
}
}
/// <summary>
/// Save game data
/// </summary>
public void SaveData()
{
isSavingGameData = true;
SetSavedDataModelButtons(0, 12);
// gameController.ShowCG();
gameController.ActiveGameObject(savedDataField);
SetManualMode(true);
Debug.Log(string.Format("Save Game Data: CurrentScript={0}, CurrentLineIndex={1}", currentScript.ChapterName, currentLineIndex));
}
/// <summary>
/// Quick save
/// </summary>
public void QuickSave()
{
Debug.Log(string.Format("Quick Save Game Data: CurrentScript={0}, CurrentLineIndex={1}", currentScript.ChapterName, currentLineIndex));
}
/// <summary>
/// Load saved data
/// </summary>
public void LoadSavedData()
{
isLoadingSavedData = true;
SetSavedDataModelButtons(0, 12);
// gameController.ShowCG();
gameController.ActiveGameObject(savedDataField);
SetManualMode(true);
Debug.Log(string.Format("Load Saved Game Data: CurrentScript={0}, CurrentLineIndex={1}", currentScript.ChapterName, currentLineIndex));
}
/// <summary>
/// Switch page display to display saved data
/// </summary>
/// <param name="step"></param>
public void SwitchSavedDataPage(int step)
{
// If out of range, do nothing
if (lastLoadedSavedDataPage + step < 0 || lastLoadedSavedDataPage + step >= savdDataPageCount) return;
// Hide previous display saved data page
Transform target = savedDataPanel.transform.Find(string.Format("SavedDataPage_{0}", lastLoadedSavedDataPage));
if (null != target)
{
target.gameObject.SetActive(false);
// gameController.DeactiveGameObject(target.gameObject);
}
lastLoadedSavedDataPage += step;
Transform nTarget = savedDataPanel.transform.Find(string.Format("SavedDataPage_{0}", lastLoadedSavedDataPage));
if (null != nTarget)
{
nTarget.gameObject.SetActive(true);
// gameController.ActiveGameObject(nTarget.gameObject);
}
// Set now display saved data page
SetSavedDataModelButtons(lastLoadedSavedDataPage);
}
/// <summary>
/// Open setting field
/// </summary>
public void OpenSetting()
{
// gameController.ShowCG();
gameController.ActiveGameObject(settingField);
SetManualMode(true);
}
/// <summary>
/// Close setting field
/// </summary>
public void CloseSetting()
{
gameController.DeactiveGameObject(settingField);
gameController.PersistSettingConfig();
GameObject lineObject = lineContainer.transform.Find("Line").gameObject;
if (SettingModel.showTextShadow)
{
if (!lineObject.GetComponent<Shadow>())
{
Shadow s = lineObject.AddComponent<Shadow>();
s.effectDistance = new Vector2(2, -1);
}
}
else
{
if (lineObject.GetComponent<Shadow>())
{
Destroy(lineObject.GetComponent<Shadow>());
}
}
}
/// <summary>
/// Reset
/// </summary>
public void ResetChapter()
{
currentLineIndex = 0;
currentLineCharIndex = 0;
currentGalgameAction = null;
}
#endregion
#region Private methods
private void InitSceneGameObject()
{
Vector2 sceneDeltaSize = mainCanvas.GetComponent<RectTransform>().sizeDelta;
historyField.GetComponent<RectTransform>().sizeDelta = sceneDeltaSize;
savedDataField.GetComponent<RectTransform>().sizeDelta = sceneDeltaSize;
}
/// <summary>
/// Whether switch line operation is allow or not
/// </summary>
/// <returns></returns>
private bool IsSwitchLineAllowed()
{
return gameController.inGame && !isMenuActive && !historyField.activeSelf && !savedDataField.activeSelf && !settingField.activeSelf && !popupWindow.activeSelf && !selectorField.activeSelf;
}
/// <summary>
/// Hide history TextView with duration
/// </summary>
private IEnumerator HideHistoryFieldTimeOut()
{
if (historyField.activeSelf)
{
historyField.SetActive(false);
if (SettingModel.isAutoReadingModeOn)
{
yield return lineSwitchWaitForSeconds;
SwitchLine();
}
}
}
/// <summary>
/// Hide history TextView with duration
/// </summary>
private IEnumerator HideSelectorFieldTimeOut()
{
if (selectorField.activeSelf)
{
selectorField.SetActive(false);
if (SettingModel.isAutoReadingModeOn)
{
yield return lineSwitchWaitForSeconds;
SwitchLine();
}
}
}
/// <summary>
/// Switch action
/// </summary>
private void SwitchAction(string actionId)
{
if (!string.IsNullOrEmpty(actionId))
{
// set current galgame action
currentGalgameAction = galgameActionsMap[actionId];
// reset line index
currentLineIndex = 0;
// build acrrent game action
BuildAAction(currentGalgameAction);
// If this action do not contains Lines and other display components
if (currentGalgameAction.Lines.Count == 0
&& string.IsNullOrEmpty(currentGalgameAction.Selector.Id)
&& null == currentGalgameAction.Bgm
&& null == currentGalgameAction.Background
&& null == currentGalgameAction.Video
)
{
// Continue immediately
SwitchAction(currentGalgameAction.NextActionId);
}
}
else
{
// this chapter is end
if (SettingModel.isSkipModeOn)
{
SetSkipMode(false);
}
if (SettingModel.isAutoReadingModeOn)
{
SetAutoMode(false);
}
// TODO: maybe consider loading another chapter?
currentScript = Resources.Load<GalgameScript>("Chapter/Chapter-01");
currentGalgameAction = null;
actorName.text = string.Empty;
currentLineIndex = 0;
currentLineCharIndex = -1;
line.text = "『つづく...』";
return;
}
}
/// <summary>
/// Show new line controller
/// </summary>
private void SwitchLine()
{
if (null == currentGalgameAction)
{
SwitchAction(currentScript.StartActionId);
}
line.text = string.Empty; // clear previous line
if (isShowingLine)
{
Debug.Log(DateTime.Now.ToString() + "准备跳过");
if (null != currentTextShowCoroutine)
{
StopCoroutine(currentTextShowCoroutine);
ShowLineImmediately(nextLine);
AddHistoryText(nextLine); // Add line to history text list
isShowingLine = false;
Debug.Log(DateTime.Now.ToString() + "已跳过");
return;
}
}
currentLineCharIndex = -1; // read from index: -1
if (isShowingSelectorOptionActionTime)
{
if (currentSelectorOptionLineIndex >= ActiveSelectorOption.Action.Lines.Count)
{
isShowingSelectorOptionActionTime = false;
}
else
{
GalgameScriptLine sLine = ActiveSelectorOption.Action.Lines[currentSelectorOptionLineIndex];
nextLine = sLine.text.Replace("\\n", "\n");
actorName.text = sLine.actor.ToString();
if (SettingModel.isSkipModeOn)
{
ShowLineImmediately();
}
else
{
currentTextShowCoroutine = StartCoroutine(ShowLineTimeOut(nextLine));
}
currentSelectorOptionLineIndex++;
}
}
else
{
if (IsSwitchLineAllowed() && currentLineIndex == currentGalgameAction.Lines.Count)
{
// reach end point of current action, switch
SwitchAction(currentGalgameAction.NextActionId);
// Debug.Log("Switch action: " + currentGalgameAction.Id + " to " + currentGalgameAction.NextActionId);
SwitchLine();
return;
}
if (currentLineIndex < currentGalgameAction.Lines.Count)
{
nextLine = currentGalgameAction.Lines[currentLineIndex].text.Replace("\\n", "\n");
if (SettingModel.isSkipModeOn)
{
ShowLineImmediately();
}
else
{
currentTextShowCoroutine = StartCoroutine(ShowLineTimeOut(nextLine));
}
// Move index to next
currentLineIndex++;
}
}
}
/// <summary>
/// Show new line controller
/// </summary>
private void SwitchLine_Old()
{
line.text = string.Empty; // clear previous line
if (isShowingLine)
{
Debug.Log(DateTime.Now.ToString() + "准备跳过");
if (null != currentTextShowCoroutine)
{
StopCoroutine(currentTextShowCoroutine);
ShowLineImmediately(nextLine);
AddHistoryText(nextLine); // Add line to history text list
isShowingLine = false;
Debug.Log(DateTime.Now.ToString() + "已跳过");
return;
}
}
currentLineCharIndex = -1; // read from index: -1
if (isShowingSelectorOptionActionTime)
{
GalgamePlainAction action = ActiveSelectorOption.Action;
if (++currentSelectorOptionLineIndex >= ActiveSelectorOption.Action.Lines.Count)
{
isShowingSelectorOptionActionTime = false;
}
}
else
{
if (currentLineIndex == galgameActions.Count)
{
// this chapter is end
if (SettingModel.isSkipModeOn)
{
SetSkipMode(false);
}
if (SettingModel.isAutoReadingModeOn)
{
SetAutoMode(false);
}
// TODO: maybe consider loading another chapter?
currentScript = Resources.Load<GalgameScript>("Chapter/Chapter-01");
actorName.text = string.Empty;
currentLineIndex = 0;
currentLineCharIndex = -1;
line.text = "『つづく...』";
return;
}
Debug.Log("galgameActions'Size: " + galgameActions.Count + " currentLineIndex: " + currentLineIndex);
currentGalgameAction = galgameActions[currentLineIndex];
BuildAAction(currentGalgameAction);
// Move index to next
currentLineIndex++;
// If is a empty line with only bgm/bg
if (string.Empty.Equals(currentGalgameAction.Line.text.Trim()) && (null != currentGalgameAction.Bgm || null != currentGalgameAction.Background))
{
// Continue immediately
SwitchLine();
}
}
}
/// <summary>
/// To build a action
/// </summary>
/// <param name="action"></param>
private void BuildAAction(GalgamePlainAction action)
{
if (null != action.Bgm)
{
// bgm
_bgmAudio.clip = action.Bgm;
_bgmAudio.Play();
}
if (null != action.Voice)
{
// voice
_voiceAudio.clip = action.Voice;
_voiceAudio.Play();
}
if (action.Actor != Actor.NULL)
{
// actor's name
// actorName.text = action.Actor.ToString();
}
else
{
actorName.text = string.Empty;
}
if (null != action.Background)
{
// current background
bgSpriteRenderer.sprite = action.Background;
}
// text-align
line.alignment = EnumMap.AlignToTextAnchor(action.Line.align);
// font
if (null != font)
{
line.font = font;
}
// font-style
if (action.Line.fstyle == FontStyle.Normal)
{
line.fontStyle = DefaultScriptProperty.fstyle;
}
else
{
line.fontStyle = action.Line.fstyle;
}
// font-size
if (action.Line.fsize != 0)
{
line.fontSize = Mathf.RoundToInt(action.Line.fsize);
}
else if (DefaultScriptProperty.fsize != 0)
{
line.fontSize = Mathf.RoundToInt(DefaultScriptProperty.fsize);
}
// line-spacing
if (action.Line.linespacing != 0)
{
line.lineSpacing = action.Line.linespacing;
}
else if (DefaultScriptProperty.linespacing != 0)
{
line.lineSpacing = DefaultScriptProperty.linespacing;
}
// font-color
if (!string.IsNullOrEmpty(action.Line.fcolor))
{
line.color = ColorUtil.HexToUnityColor(uint.Parse(action.Line.fcolor.Replace("0x", "").Substring(0, 6), System.Globalization.NumberStyles.HexNumber));
}
else if (!string.IsNullOrEmpty(DefaultScriptProperty.fcolor))
{
line.color = ColorUtil.HexToUnityColor(uint.Parse(DefaultScriptProperty.fcolor, System.Globalization.NumberStyles.HexNumber));
}
if (action.GetType().Equals(typeof(GalgameAction)))
{
// If there a selector component
if (!string.IsNullOrEmpty(((GalgameAction)action).Selector.Id))
{
BuildSelector(((GalgameAction)action).Selector);
}
// If there a adjuster component
if (!string.IsNullOrEmpty(((GalgameAction)action).GameValuesAdjuster.Id))
{
ExecuteAdjuster(((GalgameAction)action).GameValuesAdjuster);
}
// If there a events component
if (!string.IsNullOrEmpty(((GalgameAction)action).Events.Id))
{
TriggerEvents(((GalgameAction)action).Events);
}
// If there a judge component
if (!string.IsNullOrEmpty(((GalgameAction)action).Judge.Id))
{
ExecuteJudge(((GalgameAction)action).Judge);
}
}
}
/// <summary>
/// Build current scene's Selector component
/// </summary>
private void BuildSelector(PSelector selector)
{
if ((null == selector.Options || selector.Options.Count == 0) && (null == selector.Texts || selector.Texts.Count == 0)
&& (null == selector.Bgms || selector.Bgms.Count == 0) && (null == selector.Bgs || selector.Bgs.Count == 0))
return;
// Selector.Options has higher priority than Options set on properties(Texts,Bgs,Bgms) of Selector
if (null == selector.Options || selector.Options.Count == 0)
{
selector.Options = BuildSelectorOptions(selector);
// TODO: Consider update chapter
}
// Re-build Selector Container
if (null != this.selector)
{
Destroy(this.selector);
}
this.selector = new GameObject("Selector");
Image image = this.selector.AddComponent<Image>();
image.color = new Color(1.0f, 1.0f, 1.0f, 0f);
Vector2 opSize = Vector2.zero;
Vector2 opSpacing = new Vector2(10.0f, 10.0f);
int opNumber = selector.Options.Count;
Grid selectorGrid = this.selector.AddComponent<Grid>();
GridLayoutGroup selectorGroup = this.selector.AddComponent<GridLayoutGroup>();
selectorGroup.spacing = opSpacing;
// Set size of selector panel
switch (selector.Type)
{
case Model.Enum.SelectorType.HORIZONTAL:
opSize = new Vector2(200.0f, 300.0f);
selectorGroup.constraint = GridLayoutGroup.Constraint.FixedColumnCount;
selectorGroup.cellSize = opSize;
this.selector.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(opNumber * opSize.x + (opNumber - 1) * opSize.x, opSize.y);
break;
case Model.Enum.SelectorType.VERTICLE:
opSize = new Vector2(400.0f, 80.0f);
selectorGroup.constraint = GridLayoutGroup.Constraint.FixedRowCount;
selectorGroup.cellSize = opSize;
this.selector.transform.GetComponent<RectTransform>().sizeDelta = new Vector2(opSize.x, opNumber * opSize.y + (opNumber - 1) * opSpacing.y);
break;
}
selectorGroup.constraintCount = opNumber;
foreach (PSelectorOption option in selector.Options)
{
Button newEmptyOption = Instantiate(selectorOptionPrefab);
Button.ButtonClickedEvent optionClickEvent = new Button.ButtonClickedEvent();
optionClickEvent.AddListener(() =>
{
selector.IsSelected = true;
selector.SelectedItem = selector.Options.IndexOf(option);
if (null != option.Action)
{
this.ActiveSelectorOption = option;
this.isShowingSelectorOptionActionTime = true; // Hide selector panel
this.currentSelectorOptionLineIndex = 0;
nextLine = option.Action.Lines.First().text.Replace("\\n", "\n");
BuildAAction(option.Action);
}
gameController.UpdateGlobalGameValues(option.DeltaGameValues); // Update global values
StartCoroutine(HideSelectorFieldTimeOut());
SwitchLine();
Debug.Log("Global Values: \n" + GlobalGameData.GameValues.ToJSONString());
});
newEmptyOption.onClick = optionClickEvent;
if (null != option.Bg)
{
newEmptyOption.GetComponent<RawImage>().texture = option.Bg.texture;
}
if (null != option.Bgm)
{
newEmptyOption.GetComponent<AudioSource>().clip = option.Bgm;
}
Text ot = newEmptyOption.transform.Find("OptionText").GetComponent<Text>();
ot.text = option.Text.text;
ot.alignment = EnumMap.AlignToTextAnchor(option.Text.align);
// font-style
if (option.Text.fstyle == FontStyle.Normal)
{
ot.fontStyle = DefaultScriptProperty.fstyle;
}
else
{
ot.fontStyle = option.Text.fstyle;
}
// font-size
if (option.Text.fsize != 0)
{
ot.fontSize = Mathf.RoundToInt(option.Text.fsize);
}
else if (DefaultScriptProperty.fsize != 0)
{
ot.fontSize = Mathf.RoundToInt(DefaultScriptProperty.fsize);
}
// line-spacing
if (option.Text.linespacing != 0)
{
ot.lineSpacing = option.Text.linespacing;
}
else if (DefaultScriptProperty.linespacing != 0)
{
ot.lineSpacing = DefaultScriptProperty.linespacing;
}
// font-color
if (!string.IsNullOrEmpty(option.Text.fcolor))
{
ot.color = ColorUtil.HexToUnityColor(uint.Parse(option.Text.fcolor.Replace("0x", "").Substring(0, 6), System.Globalization.NumberStyles.HexNumber));
}
else if (!string.IsNullOrEmpty(DefaultScriptProperty.fcolor))
{
ot.color = ColorUtil.HexToUnityColor(uint.Parse(DefaultScriptProperty.fcolor, System.Globalization.NumberStyles.HexNumber));
}
newEmptyOption.transform.SetParent(this.selector.transform);
}
// this.selector.GetComponent<RectTransform>().position = Vector3.zero;
this.selector.transform.SetParent(selectorField.transform);
this.selector.transform.localScale = Vector3.one;
selectorField.SetActive(true);
}
/// <summary>
/// Build Selector's Options
/// </summary>
private List<PSelectorOption> BuildSelectorOptions(PSelector selector)
{
List<PSelectorOption> options = new List<PSelectorOption>();
int optionNumber = new int[] { selector.Bgms.Count, selector.Bgs.Count, selector.Texts.Count }.Max();
for (int n = 0; n < optionNumber; n++)
{
PSelectorOption o = new PSelectorOption();
o.Text = n > selector.Texts.Count - 1 ? default(PText) : selector.Texts[n];
o.Bg = n > selector.Bgs.Count - 1 ? default(Sprite) : selector.Bgs[n];
o.Bgm = n > selector.Bgms.Count - 1 ? default(AudioClip) : selector.Bgms[n];
options.Add(o);
}
return options;
}
/// <summary>
/// Execute this adjuster to change global values
/// </summary>
/// <param name="adjuster"></param>
private void ExecuteAdjuster(PGameValuesAdjuster adjuster)
{
gameController.UpdateGlobalGameValues(adjuster.DeltaGameValues); // Update global values
}
/// <summary>
/// Trigger this adjuster to change global values
/// </summary>
private void TriggerEvents(PEvents events)