| | 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 | | { |
| 1 | 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 | |
|
| 98 | 31 | | internal bool letInstantiation = true; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Step initialization (occurs before OnStepExecute() execution). |
| | 35 | | /// </summary> |
| | 36 | | public virtual void OnStepStart() |
| | 37 | | { |
| 15 | 38 | | tutorialController = TutorialController.i; |
| 15 | 39 | | stepAnimator = GetComponent<Animator>(); |
| | 40 | |
|
| 15 | 41 | | mouseCatcher = SceneReferences.i?.mouseCatcher; |
| | 42 | |
|
| 15 | 43 | | if (unlockCursorAtStart) |
| 10 | 44 | | mouseCatcher?.UnlockCursor(); |
| | 45 | |
|
| 15 | 46 | | if (tutorialController != null) |
| | 47 | | { |
| 15 | 48 | | tutorialController.ShowTeacher3DModel(show3DTeacherAtStart); |
| | 49 | |
|
| 15 | 50 | | if (tutorialController.configuration.teacher != null) |
| | 51 | | { |
| 0 | 52 | | if (tutorialController.currentStepIndex > 0) |
| 0 | 53 | | tutorialController.configuration.teacher.PlaySpeakSound(); |
| | 54 | | else |
| 0 | 55 | | tutorialController.configuration.teacher.PlayHappySound(1f); |
| | 56 | | } |
| | 57 | |
|
| 15 | 58 | | if (show3DTeacherAtStart && teacherPositionRef != null) |
| | 59 | | { |
| 15 | 60 | | tutorialController.SetTeacherPosition(teacherPositionRef.position); |
| | 61 | |
|
| 15 | 62 | | if (tutorialController.configuration.teacher != null && |
| | 63 | | tutorialController.configuration.teacher.isHiddenByAnAnimation) |
| 0 | 64 | | tutorialController.configuration.teacher.PlayAnimation(TutorialTeacher.TeacherAnimation.Reset); |
| | 65 | | } |
| | 66 | | } |
| | 67 | |
|
| 15 | 68 | | ConfigureSkipOptions(); |
| 15 | 69 | | } |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Executes the main flow of the step and waits for its finalization. |
| | 73 | | /// </summary> |
| | 74 | | /// <returns></returns> |
| 0 | 75 | | public virtual IEnumerator OnStepExecute() { yield break; } |
| | 76 | |
|
| | 77 | | /// <summary> |
| | 78 | | /// Executes the final animation and waits for its finalization and for any camera blending. |
| | 79 | | /// </summary> |
| | 80 | | /// <returns></returns> |
| | 81 | | public virtual IEnumerator OnStepPlayHideAnimation() |
| | 82 | | { |
| 15 | 83 | | blockSkipActions = true; |
| 15 | 84 | | OnJustAfterStepExecuted?.Invoke(); |
| 15 | 85 | | yield return PlayAndWaitForHideAnimation(); |
| 15 | 86 | | yield return null; |
| 30 | 87 | | yield return new WaitUntil(() => !CommonScriptableObjects.cameraIsBlending.Get()); |
| 15 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Step finalization (occurs after OnStepExecute() execution). |
| | 92 | | /// </summary> |
| | 93 | | public virtual void OnStepFinished() |
| | 94 | | { |
| 25 | 95 | | if (mainSection != null && |
| | 96 | | skipTutorialSection != null && |
| | 97 | | yesSkipInputAction != null && |
| | 98 | | noSkipInputAction) |
| | 99 | | { |
| 5 | 100 | | yesSkipInputAction.OnFinished -= YesSkipInputAction_OnFinished; |
| 5 | 101 | | noSkipInputAction.OnFinished -= NoSkipInputAction_OnFinished; |
| | 102 | | } |
| 25 | 103 | | } |
| | 104 | |
|
| 1 | 105 | | internal void OnShowAnimationFinish() { OnShowAnimationFinished?.Invoke(); } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Warn about the finalization of the hide animation of the step |
| | 109 | | /// </summary> |
| 0 | 110 | | internal void OnHideAnimationFinish() { hideAnimationFinished = true; } |
| | 111 | |
|
| | 112 | | private IEnumerator PlayAndWaitForHideAnimation() |
| | 113 | | { |
| 15 | 114 | | if (stepAnimator == null) |
| 14 | 115 | | yield break; |
| | 116 | |
|
| 1 | 117 | | stepAnimator.SetTrigger(STEP_FINISHED_ANIMATOR_TRIGGER); |
| 3 | 118 | | yield return new WaitUntil(() => hideAnimationFinished); |
| 1 | 119 | | } |
| | 120 | |
|
| | 121 | | private void ConfigureSkipOptions() |
| | 122 | | { |
| 15 | 123 | | if (mainSection != null && |
| | 124 | | skipTutorialSection != null && |
| | 125 | | yesSkipInputAction != null && |
| | 126 | | noSkipInputAction) |
| | 127 | | { |
| 5 | 128 | | yesSkipInputAction.OnFinished += YesSkipInputAction_OnFinished; |
| 5 | 129 | | noSkipInputAction.OnFinished += NoSkipInputAction_OnFinished; |
| | 130 | | } |
| 15 | 131 | | } |
| | 132 | |
|
| | 133 | | private void YesSkipInputAction_OnFinished(DCLAction_Hold action) |
| | 134 | | { |
| 1 | 135 | | if (skipTutorialSection.activeSelf) |
| | 136 | | { |
| 0 | 137 | | tutorialController.SkipTutorial(); |
| | 138 | | } |
| 1 | 139 | | } |
| | 140 | |
|
| | 141 | | internal void NoSkipInputAction_OnFinished(DCLAction_Hold action) |
| | 142 | | { |
| 2 | 143 | | if (blockSkipActions) |
| 0 | 144 | | return; |
| | 145 | |
|
| 2 | 146 | | if (mainSection.activeSelf) |
| | 147 | | { |
| 1 | 148 | | mainSection.SetActive(false); |
| 1 | 149 | | skipTutorialSection.SetActive(true); |
| 1 | 150 | | } |
| 1 | 151 | | else if (skipTutorialSection.activeSelf) |
| | 152 | | { |
| 1 | 153 | | mainSection.SetActive(true); |
| 1 | 154 | | skipTutorialSection.SetActive(false); |
| | 155 | | } |
| 1 | 156 | | } |
| | 157 | | } |
| | 158 | | } |