| | 1 | | using System; |
| | 2 | | using System.Collections; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Tutorial |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Class that represents one of the steps included in the onboarding tutorial. |
| | 9 | | /// </summary> |
| | 10 | | public class TutorialStep : MonoBehaviour |
| | 11 | | { |
| 0 | 12 | | protected static int STEP_FINISHED_ANIMATOR_TRIGGER = Animator.StringToHash("StepFinished"); |
| | 13 | |
|
| | 14 | | internal event Action OnShowAnimationFinished; |
| | 15 | | internal event Action OnJustAfterStepExecuted; |
| | 16 | |
|
| | 17 | | [SerializeField] internal bool unlockCursorAtStart = false; |
| | 18 | | [SerializeField] internal bool show3DTeacherAtStart = false; |
| | 19 | | [SerializeField] internal protected RectTransform teacherPositionRef; |
| | 20 | | [SerializeField] internal GameObject mainSection; |
| | 21 | | [SerializeField] internal GameObject skipTutorialSection; |
| | 22 | | [SerializeField] internal InputAction_Hold yesSkipInputAction; |
| | 23 | | [SerializeField] internal InputAction_Hold noSkipInputAction; |
| | 24 | |
|
| | 25 | | protected TutorialController tutorialController; |
| | 26 | | internal Animator stepAnimator; |
| | 27 | | internal MouseCatcher mouseCatcher; |
| | 28 | | protected bool hideAnimationFinished = false; |
| | 29 | | internal bool blockSkipActions = false; |
| | 30 | |
|
| 0 | 31 | | internal bool letInstantiation = true; |
| | 32 | | private HUDCanvasCameraModeController hudCanvasCameraMode; |
| | 33 | |
|
| | 34 | | private void Awake() |
| | 35 | | { |
| 0 | 36 | | var canvas = GetComponentInChildren<Canvas>(); |
| 0 | 37 | | if (canvas != null) |
| 0 | 38 | | hudCanvasCameraMode = new HUDCanvasCameraModeController(canvas, DataStore.i.camera.hudsCamera); |
| 0 | 39 | | } |
| | 40 | |
|
| 0 | 41 | | private void OnDestroy() { hudCanvasCameraMode?.Dispose(); } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Step initialization (occurs before OnStepExecute() execution). |
| | 45 | | /// </summary> |
| | 46 | | public virtual void OnStepStart() |
| | 47 | | { |
| 0 | 48 | | tutorialController = TutorialController.i; |
| 0 | 49 | | stepAnimator = GetComponent<Animator>(); |
| | 50 | |
|
| 0 | 51 | | mouseCatcher = SceneReferences.i?.mouseCatcher; |
| | 52 | |
|
| 0 | 53 | | if (unlockCursorAtStart) |
| 0 | 54 | | mouseCatcher?.UnlockCursor(); |
| | 55 | |
|
| 0 | 56 | | if (tutorialController != null) |
| | 57 | | { |
| 0 | 58 | | tutorialController.ShowTeacher3DModel(show3DTeacherAtStart); |
| | 59 | |
|
| 0 | 60 | | if (tutorialController.configuration.teacher != null) |
| | 61 | | { |
| 0 | 62 | | if (tutorialController.currentStepIndex > 0) |
| 0 | 63 | | tutorialController.configuration.teacher.PlaySpeakSound(); |
| | 64 | | else |
| 0 | 65 | | tutorialController.configuration.teacher.PlayHappySound(1f); |
| | 66 | | } |
| | 67 | |
|
| 0 | 68 | | if (show3DTeacherAtStart && teacherPositionRef != null) |
| | 69 | | { |
| 0 | 70 | | tutorialController.SetTeacherPosition(teacherPositionRef.position); |
| | 71 | |
|
| 0 | 72 | | if (tutorialController.configuration.teacher != null && |
| | 73 | | tutorialController.configuration.teacher.IsHiddenByAnimation) |
| 0 | 74 | | tutorialController.configuration.teacher.PlayAnimation(TutorialTeacher.TeacherAnimation.Reset); |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 0 | 78 | | ConfigureSkipOptions(); |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Executes the main flow of the step and waits for its finalization. |
| | 83 | | /// </summary> |
| | 84 | | /// <returns></returns> |
| 0 | 85 | | public virtual IEnumerator OnStepExecute() { yield break; } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Executes the final animation and waits for its finalization and for any camera blending. |
| | 89 | | /// </summary> |
| | 90 | | /// <returns></returns> |
| | 91 | | public virtual IEnumerator OnStepPlayHideAnimation() |
| | 92 | | { |
| 0 | 93 | | blockSkipActions = true; |
| 0 | 94 | | OnJustAfterStepExecuted?.Invoke(); |
| 0 | 95 | | yield return PlayAndWaitForHideAnimation(); |
| 0 | 96 | | yield return null; |
| 0 | 97 | | yield return new WaitUntil(() => !CommonScriptableObjects.cameraIsBlending.Get()); |
| 0 | 98 | | } |
| | 99 | |
|
| | 100 | | /// <summary> |
| | 101 | | /// Step finalization (occurs after OnStepExecute() execution). |
| | 102 | | /// </summary> |
| | 103 | | public virtual void OnStepFinished() |
| | 104 | | { |
| 0 | 105 | | if (mainSection != null && |
| | 106 | | skipTutorialSection != null && |
| | 107 | | yesSkipInputAction != null && |
| | 108 | | noSkipInputAction) |
| | 109 | | { |
| 0 | 110 | | yesSkipInputAction.OnFinished -= YesSkipInputAction_OnFinished; |
| 0 | 111 | | noSkipInputAction.OnFinished -= NoSkipInputAction_OnFinished; |
| | 112 | | } |
| 0 | 113 | | } |
| | 114 | |
|
| 0 | 115 | | internal void OnShowAnimationFinish() { OnShowAnimationFinished?.Invoke(); } |
| | 116 | |
|
| | 117 | | /// <summary> |
| | 118 | | /// Warn about the finalization of the hide animation of the step |
| | 119 | | /// </summary> |
| 0 | 120 | | internal void OnHideAnimationFinish() { hideAnimationFinished = true; } |
| | 121 | |
|
| | 122 | | private IEnumerator PlayAndWaitForHideAnimation() |
| | 123 | | { |
| 0 | 124 | | if (stepAnimator == null) |
| 0 | 125 | | yield break; |
| | 126 | |
|
| 0 | 127 | | stepAnimator.SetTrigger(STEP_FINISHED_ANIMATOR_TRIGGER); |
| 0 | 128 | | yield return new WaitUntil(() => hideAnimationFinished); |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | private void ConfigureSkipOptions() |
| | 132 | | { |
| 0 | 133 | | if (mainSection != null && |
| | 134 | | skipTutorialSection != null && |
| | 135 | | yesSkipInputAction != null && |
| | 136 | | noSkipInputAction) |
| | 137 | | { |
| 0 | 138 | | yesSkipInputAction.OnFinished += YesSkipInputAction_OnFinished; |
| 0 | 139 | | noSkipInputAction.OnFinished += NoSkipInputAction_OnFinished; |
| | 140 | | } |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | private void YesSkipInputAction_OnFinished(DCLAction_Hold action) |
| | 144 | | { |
| 0 | 145 | | if (skipTutorialSection.activeSelf) |
| | 146 | | { |
| 0 | 147 | | tutorialController.SkipTutorial(); |
| | 148 | | } |
| 0 | 149 | | } |
| | 150 | |
|
| | 151 | | internal void NoSkipInputAction_OnFinished(DCLAction_Hold action) |
| | 152 | | { |
| 0 | 153 | | if (blockSkipActions) |
| 0 | 154 | | return; |
| | 155 | |
|
| 0 | 156 | | if (mainSection.activeSelf) |
| | 157 | | { |
| 0 | 158 | | mainSection.SetActive(false); |
| 0 | 159 | | skipTutorialSection.SetActive(true); |
| 0 | 160 | | } |
| 0 | 161 | | else if (skipTutorialSection.activeSelf) |
| | 162 | | { |
| 0 | 163 | | mainSection.SetActive(true); |
| 0 | 164 | | skipTutorialSection.SetActive(false); |
| | 165 | | } |
| 0 | 166 | | } |
| | 167 | | } |
| | 168 | | } |