| | 1 | | using System.Collections; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | public class TutorialMusicHandler : MonoBehaviour |
| | 6 | | { |
| | 7 | | [SerializeField] AudioEvent tutorialMusic, avatarEditorMusic; |
| | 8 | |
|
| | 9 | | bool rendererIsReady = false, tutorialHasBeenEnabled = false; |
| | 10 | |
|
| | 11 | | Coroutine fadeOut; |
| | 12 | |
|
| | 13 | | private void Awake() |
| | 14 | | { |
| 39 | 15 | | CommonScriptableObjects.tutorialActive.OnChange += TutorialActive_OnChange; |
| 39 | 16 | | CommonScriptableObjects.rendererState.OnChange += OnRendererStateChange; |
| 39 | 17 | | avatarEditorMusic.OnPlay += OnAvatarEditorMusicPlay; |
| 39 | 18 | | avatarEditorMusic.OnStop += OnAvatarEditorMusicStop; |
| 39 | 19 | | } |
| | 20 | |
|
| | 21 | | private void OnDestroy() |
| | 22 | | { |
| 39 | 23 | | CommonScriptableObjects.tutorialActive.OnChange -= TutorialActive_OnChange; |
| 39 | 24 | | CommonScriptableObjects.rendererState.OnChange -= OnRendererStateChange; |
| 39 | 25 | | avatarEditorMusic.OnPlay -= OnAvatarEditorMusicPlay; |
| 39 | 26 | | avatarEditorMusic.OnStop -= OnAvatarEditorMusicStop; |
| 39 | 27 | | } |
| | 28 | |
|
| | 29 | | void OnRendererStateChange(bool current, bool previous) |
| | 30 | | { |
| 1 | 31 | | rendererIsReady = current; |
| 1 | 32 | | TryPlayingMusic(); |
| 1 | 33 | | } |
| | 34 | |
|
| | 35 | | private void TutorialActive_OnChange(bool current, bool previous) |
| | 36 | | { |
| 24 | 37 | | if (current) |
| | 38 | | { |
| 12 | 39 | | tutorialHasBeenEnabled = true; |
| 12 | 40 | | TryPlayingMusic(); |
| 12 | 41 | | } |
| | 42 | | else |
| | 43 | | { |
| 12 | 44 | | if (tutorialMusic.source.isPlaying) |
| 0 | 45 | | fadeOut = CoroutineStarter.Start(tutorialMusic.FadeOut(3f)); |
| 12 | 46 | | tutorialHasBeenEnabled = false; |
| | 47 | | } |
| 12 | 48 | | } |
| | 49 | |
|
| | 50 | | void TryPlayingMusic() |
| | 51 | | { |
| 13 | 52 | | if (DCL.Tutorial.TutorialController.i.userAlreadyDidTheTutorial) |
| 3 | 53 | | return; |
| | 54 | |
|
| 10 | 55 | | if (rendererIsReady && tutorialHasBeenEnabled && !tutorialMusic.source.isPlaying) |
| | 56 | | { |
| 0 | 57 | | if (fadeOut != null) |
| | 58 | | { |
| 0 | 59 | | CoroutineStarter.Stop(fadeOut); |
| | 60 | | } |
| 0 | 61 | | tutorialMusic.Play(); |
| | 62 | | } |
| 10 | 63 | | } |
| | 64 | |
|
| | 65 | | void OnAvatarEditorMusicPlay() |
| | 66 | | { |
| 0 | 67 | | if (tutorialMusic.source.isPlaying) |
| 0 | 68 | | fadeOut = CoroutineStarter.Start(tutorialMusic.FadeOut(1.5f, false)); |
| 0 | 69 | | } |
| | 70 | |
|
| | 71 | | void OnAvatarEditorMusicStop() |
| | 72 | | { |
| 0 | 73 | | if (tutorialMusic.source.isPlaying) |
| 0 | 74 | | CoroutineStarter.Start(tutorialMusic.FadeIn(2.5f)); |
| 0 | 75 | | } |
| | 76 | | } |