Пример #1
0
        /// <summary>
        /// A coroutine used to toggle the menu
        /// </summary>
        /// <param name="active"></param>
        /// <returns></returns>
        protected virtual IEnumerator ToggleCo(bool active)
        {
            if (_toggling)
            {
                yield break;
            }
            if (!active)
            {
                OnOpenEvent?.Invoke();
                _containerRect.gameObject.SetActive(true);
            }
            _toggling    = true;
            Active       = active;
            _newPosition = active ? _offPosition : _initialContainerPosition;
            MMTween.MoveRectTransform(this, _containerRect, _containerRect.localPosition, _newPosition, null, 0f, Data.ToggleDuration, Data.ToggleCurve, ignoreTimescale: true);
            yield return(MMCoroutine.WaitForUnscaled(Data.ToggleDuration));

            if (active)
            {
                OnCloseEvent?.Invoke();
                _containerRect.gameObject.SetActive(false);
            }
            Active    = !active;
            _toggling = false;
        }
Пример #2
0
        /// <summary>
        /// Coroutine used to make the character's sprite flicker (when hurt for example).
        /// </summary>
        public static IEnumerator Flicker(Renderer renderer, Color initialColor, Color flickerColor, float flickerSpeed, float flickerDuration)
        {
            if (renderer == null)
            {
                yield break;
            }

            if (!renderer.material.HasProperty("_Color"))
            {
                yield break;
            }

            if (initialColor == flickerColor)
            {
                yield break;
            }

            float flickerStop = Time.time + flickerDuration;

            while (Time.time < flickerStop)
            {
                renderer.material.color = flickerColor;
                yield return(MMCoroutine.WaitFor(flickerSpeed));

                renderer.material.color = initialColor;
                yield return(MMCoroutine.WaitFor(flickerSpeed));
            }

            renderer.material.color = initialColor;
        }
Пример #3
0
 /// <summary>
 /// Mutes all sounds after an optional delay
 /// </summary>
 /// <param name="delay"></param>
 /// <param name="mute"></param>
 /// <returns></returns>
 protected virtual IEnumerator MuteAllSoundsCoroutine(float delay, bool mute = true)
 {
     if (delay > 0)
     {
         yield return(MMCoroutine.WaitForUnscaled(delay));
     }
     foreach (MMSoundManagerSound sound in _sounds)
     {
         sound.Source.mute = mute;
     }
 }
        /// <summary>
        /// Waits for BeforeExitFadeDelay seconds
        /// </summary>
        /// <returns></returns>
        protected virtual IEnumerator ProcessDelayBeforeExitFade()
        {
            if (_beforeExitFadeDelay > 0f)
            {
                MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : delay before exit fade, duration : " + _beforeExitFadeDelay);
                MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.BeforeExitFade);
                OnBeforeExitFade?.Invoke();

                yield return(MMCoroutine.WaitForUnscaled(_beforeExitFadeDelay));
            }
        }
Пример #5
0
        /// <summary>
        /// Waits for the specified AfterEntryFadeDelay
        /// </summary>
        /// <returns></returns>
        protected virtual IEnumerator ProcessDelayAfterEntryFade()
        {
            if (_afterEntryFadeDelay > 0f)
            {
                MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : delay after entry fade, duration : " + _afterEntryFadeDelay);
                MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.AfterEntryFade);
                OnAfterEntryFade?.Invoke();

                yield return(MMCoroutine.WaitFor(_afterEntryFadeDelay));
            }
        }
        /// <summary>
        /// Activates the destination scene
        /// </summary>
        protected virtual IEnumerator DestinationSceneActivation()
        {
            yield return(MMCoroutine.WaitForFrames(1));

            MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : activating destination scene");
            MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.DestinationSceneActivation);
            OnDestinationSceneActivation?.Invoke();
            _loadDestinationAsyncOperation.allowSceneActivation = true;
            while (_loadDestinationAsyncOperation.progress < 1.0f)
            {
                yield return(null);
            }
        }
Пример #7
0
 /// <summary>
 /// Waits and triggers state change and events
 /// </summary>
 protected virtual IEnumerator TimedActivationSequence()
 {
     if (DelayMode == DelayModes.Time)
     {
         yield return(MMCoroutine.WaitFor(TimeBeforeStateChange));
     }
     else
     {
         yield return(StartCoroutine(MMCoroutine.WaitForFrames(FrameCount)));
     }
     StateChange();
     Activate();
 }
        /// <summary>
        /// Destroys the object after TimeBeforeDestruction seconds
        /// </summary>
        protected virtual IEnumerator Destruction()
        {
            yield return(MMCoroutine.WaitFor(TimeBeforeDestruction));

            if (TimeDestructionMode == TimedDestructionModes.Destroy)
            {
                Destroy(gameObject);
            }
            else
            {
                gameObject.SetActive(false);
            }
        }
Пример #9
0
        /// <summary>
        /// Mutes all sounds on the specified track after an optional delay
        /// </summary>
        /// <param name="track"></param>
        /// <param name="mute"></param>
        /// <param name="delay"></param>
        /// <returns></returns>
        protected virtual IEnumerator MuteSoundsOnTrackCoroutine(MMSoundManagerTracks track, bool mute, float delay)
        {
            if (delay > 0)
            {
                yield return(MMCoroutine.WaitForUnscaled(delay));
            }

            foreach (MMSoundManagerSound sound in _sounds)
            {
                if (sound.Track == track)
                {
                    sound.Source.mute = mute;
                }
            }
        }
        /// <summary>
        /// Disables an audio source after it's done playing
        /// </summary>
        /// <param name="duration"></param>
        /// <param name="targetObject"></param>
        /// <returns></returns>
        public virtual IEnumerator AutoDisableAudioSource(float duration, AudioSource source, AudioClip clip, bool doNotAutoRecycleIfNotDonePlaying)
        {
            yield return(MMCoroutine.WaitFor(duration));

            if (source.clip != clip)
            {
                yield break;
            }
            if (doNotAutoRecycleIfNotDonePlaying)
            {
                while (source.time < source.clip.length)
                {
                    yield return(null);
                }
            }
            source.gameObject.SetActive(false);
        }
        /// <summary>
        /// Requests a fade on exit
        /// </summary>
        /// <returns></returns>
        protected virtual IEnumerator ExitFade()
        {
            SetAudioListener(false);
            if (_exitFadeDuration > 0f)
            {
                MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : exit fade, duration : " + _exitFadeDuration);
                MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.ExitFade);
                OnExitFade?.Invoke();

                if (_fadeMode == FadeModes.FadeOutThenIn)
                {
                    MMFadeInEvent.Trigger(_exitFadeDuration, _exitFadeTween, FaderID, true);
                }
                else
                {
                    MMFadeOutEvent.Trigger(_exitFadeDuration, _exitFadeTween, FaderID, true);
                }
                yield return(MMCoroutine.WaitForUnscaled(_exitFadeDuration));
            }
        }
Пример #12
0
        /// <summary>
        /// Enables the camera, either via enabled state or priority
        /// </summary>
        /// <param name="state"></param>
        /// <param name="frames"></param>
        /// <returns></returns>
        protected virtual IEnumerator EnableCamera(bool state, int frames)
        {
            if (VirtualCamera == null)
            {
                yield break;
            }

            if (frames > 0)
            {
                yield return(MMCoroutine.WaitForFrames(frames));
            }

            if (Mode == Modes.Enable)
            {
                VirtualCamera.enabled = state;
            }
            else if (Mode == Modes.Priority)
            {
                VirtualCamera.Priority = state ? EnabledPriority : DisabledPriority;
            }
        }
        /// <summary>
        /// Hides the bar when it reaches zero
        /// </summary>
        /// <returns>The hide bar.</returns>
        protected virtual IEnumerator FinalHideBar()
        {
            _finalHideStarted = true;
            if (InstantiatedOnDeath != null)
            {
                Instantiate(InstantiatedOnDeath, this.transform.position + HealthBarOffset, this.transform.rotation);
            }
            if (HideBarAtZeroDelay == 0)
            {
                _showBar = false;
                _progressBar.gameObject.SetActive(false);
                yield return(null);
            }
            else
            {
                yield return(MMCoroutine.WaitFor(HideBarAtZeroDelay));

                _showBar = false;
                _progressBar.gameObject.SetActive(false);
            }
        }
Пример #14
0
        /// <summary>
        /// Starts a fade
        /// </summary>
        /// <param name="fadingIn"></param>
        /// <param name="duration"></param>
        /// <param name="curve"></param>
        /// <param name="id"></param>
        /// <param name="ignoreTimeScale"></param>
        /// <param name="worldPosition"></param>
        protected virtual IEnumerator StartFading(bool fadingIn, float duration, MMTweenType curve, int id,
                                                  bool ignoreTimeScale, Vector3 worldPosition)
        {
            if (id != ID)
            {
                yield break;
            }

            if (InitialDelay > 0f)
            {
                yield return(MMCoroutine.WaitFor(InitialDelay));
            }

            if (!_initialized)
            {
                Initialization();
            }

            if (curve == null)
            {
                curve = DefaultTween;
            }

            IgnoreTimescale = ignoreTimeScale;
            EnableFader();
            _fading = true;

            _fadeStartedAt   = IgnoreTimescale ? Time.unscaledTime : Time.time;
            _currentCurve    = curve;
            _currentDuration = duration;

            _fromPosition = _rectTransform.anchoredPosition;
            _toPosition   = fadingIn ? _initialPosition : ExitPosition();

            _newPosition = MMTween.Tween(0f, 0f, duration, _fromPosition, _toPosition, _currentCurve);
            _rectTransform.anchoredPosition = _newPosition;
        }
Пример #15
0
        /// <summary>
        /// Calls a fader on entry
        /// </summary>
        /// <returns></returns>
        protected virtual IEnumerator EntryFade()
        {
            if (_entryFadeDuration > 0f)
            {
                MMLoadingSceneDebug("MMLoadingSceneManagerAdditive : entry fade, duration : " + _entryFadeDuration);
                MMSceneLoadingManager.LoadingSceneEvent.Trigger(_sceneToLoadName, MMSceneLoadingManager.LoadingStatus.EntryFade);
                OnEntryFade?.Invoke();

                if (_fadeMode == FadeModes.FadeOutThenIn)
                {
                    yield return(null);

                    MMFadeOutEvent.Trigger(_entryFadeDuration, _entryFadeTween, FaderID, true);
                }
                else
                {
                    yield return(null);

                    MMFadeInEvent.Trigger(_entryFadeDuration, _entryFadeTween, FaderID, true);
                }

                yield return(MMCoroutine.WaitFor(_entryFadeDuration));
            }
        }
Пример #16
0
        /// <summary>
        /// Plays a new song in the playlist, and stops / fades the previous one
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        protected virtual IEnumerator PlaySong(int index)
        {
            // if we don't have a song, we stop
            if (Songs.Count == 0)
            {
                yield break;
            }

            // if we've played all our songs, we stop
            if (!Endless && (_songsPlayedThisCycle > Songs.Count))
            {
                yield break;
            }

            if (_coroutine != null)
            {
                StopCoroutine(_coroutine);
            }

            // we stop our current song
            if ((PlaylistState.CurrentState == PlaylistStates.Playing) && (index >= 0 && index < Songs.Count))
            {
                StartCoroutine(Fade(CurrentlyPlayingIndex,
                                    Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y),
                                    Songs[CurrentlyPlayingIndex].Volume.y * VolumeMultiplier,
                                    Songs[CurrentlyPlayingIndex].Volume.x * VolumeMultiplier,
                                    true));
            }

            // we stop all other coroutines
            if ((CurrentlyPlayingIndex >= 0) && (Songs.Count > CurrentlyPlayingIndex))
            {
                foreach (MMPlaylistSong song in Songs)
                {
                    if (song != Songs[CurrentlyPlayingIndex])
                    {
                        song.Fading = false;
                    }
                }
            }

            if (index < 0 || index >= Songs.Count)
            {
                yield break;
            }

            // initial delay
            yield return(MMCoroutine.WaitFor(Random.Range(Songs[index].InitialDelay.x, Songs[index].InitialDelay.y)));

            if (Songs[index].TargetAudioSource == null)
            {
                Debug.LogError(this.name + " : the playlist song you're trying to play is null");
                yield break;
            }

            Songs[index].TargetAudioSource.pitch        = Random.Range(Songs[index].Pitch.x, Songs[index].Pitch.y);
            Songs[index].TargetAudioSource.panStereo    = Songs[index].StereoPan;
            Songs[index].TargetAudioSource.spatialBlend = Songs[index].SpatialBlend;
            Songs[index].TargetAudioSource.loop         = Songs[index].Loop;

            // fades the new song's volume
            StartCoroutine(Fade(index,
                                Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y),
                                Songs[index].Volume.x * VolumeMultiplier,
                                Songs[index].Volume.y * VolumeMultiplier,
                                false));

            // starts the new song
            Songs[index].TargetAudioSource.Play();

            // updates our state
            CurrentSongName = Songs[index].TargetAudioSource.clip.name;
            PlaylistState.ChangeState(PlaylistStates.Playing);
            Songs[index].Playing  = true;
            CurrentlyPlayingIndex = index;
            _songsPlayedSoFar++;
            _songsPlayedThisCycle++;

            while (Songs[index].TargetAudioSource.isPlaying)
            {
                yield return(null);
            }

            if (PlaylistState.CurrentState != PlaylistStates.Playing)
            {
                yield break;
            }

            if (_songsPlayedSoFar < Songs.Count)
            {
                _coroutine = StartCoroutine(PlaySong(PickNextIndex()));
            }
            else
            {
                if (Endless)
                {
                    _coroutine = StartCoroutine(PlaySong(PickNextIndex()));
                }
                else
                {
                    PlaylistState.ChangeState(PlaylistStates.Idle);
                }
            }
        }
Пример #17
0
        /// <summary>
        /// Disables an audio source after it's done playing
        /// </summary>
        /// <param name="duration"></param>
        /// <param name="targetObject"></param>
        /// <returns></returns>
        public virtual IEnumerator AutoDisableAudioSource(float duration, GameObject targetObject)
        {
            yield return(MMCoroutine.WaitFor(duration));

            targetObject.SetActive(false);
        }
Пример #18
0
        /// <summary>
        /// An internal coroutine used to handle the disabling of the progress bar after a delay
        /// </summary>
        /// <param name="delay"></param>
        /// <returns></returns>
        protected virtual IEnumerator HideBarCo(float delay)
        {
            yield return(MMCoroutine.WaitFor(delay));

            this.gameObject.SetActive(false);
        }
Пример #19
0
        /// <summary>
        /// Plays a new track in the playlist, and stops / fades the previous one
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        protected virtual IEnumerator PlayTrack(int index)
        {
            // if we don't have a song, we stop
            if (Songs.Count == 0)
            {
                yield break;
            }

            // if we've played all our songs, we stop
            if (!Endless && (_songsPlayedThisCycle > Songs.Count))
            {
                yield break;
            }

            // we stop our current track
            if (PlaylistState.CurrentState == PlaylistStates.Playing)
            {
                StartCoroutine(Fade(CurrentlyPlayingIndex,
                                    Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y),
                                    Songs[CurrentlyPlayingIndex].Volume.y,
                                    Songs[CurrentlyPlayingIndex].Volume.x,
                                    true));
            }

            // we stop all other coroutines
            if (CurrentlyPlayingIndex >= 0)
            {
                foreach (MMPlaylistSong song in Songs)
                {
                    if (song != Songs[CurrentlyPlayingIndex])
                    {
                        song.Fading = false;
                    }
                }
            }

            // initial delay
            yield return(MMCoroutine.WaitFor(Random.Range(Songs[index].InitialDelay.x, Songs[index].InitialDelay.y)));

            if (Songs[index].TargetAudioSource == null)
            {
                Debug.LogError(this.name + " : the playlist song you're trying to play is null");
                yield break;
            }

            Songs[index].TargetAudioSource.pitch        = Random.Range(Songs[index].Pitch.x, Songs[index].Pitch.y);
            Songs[index].TargetAudioSource.panStereo    = Songs[index].StereoPan;
            Songs[index].TargetAudioSource.spatialBlend = Songs[index].SpatialBlend;
            Songs[index].TargetAudioSource.loop         = Songs[index].Loop;

            // fades the new track's volume
            StartCoroutine(Fade(index,
                                Random.Range(Songs[index].CrossFadeDuration.x, Songs[index].CrossFadeDuration.y),
                                Songs[index].Volume.x,
                                Songs[index].Volume.y,
                                false));

            // starts the new track
            Songs[index].TargetAudioSource.Play();

            // updates our state
            CurrentTrackName = Songs[index].TargetAudioSource.clip.name;
            PlaylistState.ChangeState(PlaylistStates.Playing);
            Songs[index].Playing  = true;
            CurrentlyPlayingIndex = index;
            _songsPlayedSoFar++;
            _songsPlayedThisCycle++;
        }