mirror of
https://github.com/Outer-Wilds-New-Horizons/new-horizons.git
synced 2025-12-11 20:15:44 +01:00
Clear all coroutines/delays on scene unload
This commit is contained in:
parent
13c0bdeec5
commit
f05e2be30a
@ -1,5 +1,6 @@
|
|||||||
using NewHorizons.External.SerializableEnums;
|
using NewHorizons.External.SerializableEnums;
|
||||||
using NewHorizons.Handlers;
|
using NewHorizons.Handlers;
|
||||||
|
using NewHorizons.Utility;
|
||||||
using NewHorizons.Utility.OWML;
|
using NewHorizons.Utility.OWML;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -28,7 +29,7 @@ namespace NewHorizons.Components.Volumes
|
|||||||
if (hitObj.CompareTag("PlayerDetector") && enabled)
|
if (hitObj.CompareTag("PlayerDetector") && enabled)
|
||||||
{
|
{
|
||||||
// Have to run it off the mod behaviour since the game over controller disables everything
|
// Have to run it off the mod behaviour since the game over controller disables everything
|
||||||
Main.Instance.StartCoroutine(GameOver());
|
Delay.StartCoroutine(GameOver());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
using NewHorizons.Utility.OWML;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
@ -6,7 +7,7 @@ namespace NewHorizons.Handlers
|
|||||||
{
|
{
|
||||||
public static class FadeHandler
|
public static class FadeHandler
|
||||||
{
|
{
|
||||||
public static void FadeOut(float length) => Main.Instance.StartCoroutine(FadeOutCoroutine(length));
|
public static void FadeOut(float length) => Delay.StartCoroutine(FadeOutCoroutine(length));
|
||||||
|
|
||||||
private static IEnumerator FadeOutCoroutine(float length)
|
private static IEnumerator FadeOutCoroutine(float length)
|
||||||
{
|
{
|
||||||
@ -24,7 +25,7 @@ namespace NewHorizons.Handlers
|
|||||||
yield return new WaitForEndOfFrame();
|
yield return new WaitForEndOfFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void FadeThen(float length, Action action) => Main.Instance.StartCoroutine(FadeThenCoroutine(length, action));
|
public static void FadeThen(float length, Action action) => Delay.StartCoroutine(FadeThenCoroutine(length, action));
|
||||||
|
|
||||||
private static IEnumerator FadeThenCoroutine(float length, Action action)
|
private static IEnumerator FadeThenCoroutine(float length, Action action)
|
||||||
{
|
{
|
||||||
|
|||||||
@ -40,7 +40,7 @@ namespace NewHorizons.Handlers
|
|||||||
var matchInitialMotion = SearchUtilities.Find("Player_Body").GetComponent<MatchInitialMotion>();
|
var matchInitialMotion = SearchUtilities.Find("Player_Body").GetComponent<MatchInitialMotion>();
|
||||||
if (matchInitialMotion != null) UnityEngine.Object.Destroy(matchInitialMotion);
|
if (matchInitialMotion != null) UnityEngine.Object.Destroy(matchInitialMotion);
|
||||||
|
|
||||||
Main.Instance.StartCoroutine(SpawnCoroutine(2));
|
Delay.StartCoroutine(SpawnCoroutine(2));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,15 +1,49 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using UnityEngine.SceneManagement;
|
||||||
|
|
||||||
namespace NewHorizons.Utility.OWML
|
namespace NewHorizons.Utility.OWML
|
||||||
{
|
{
|
||||||
public static class Delay
|
public static class Delay
|
||||||
{
|
{
|
||||||
public static void RunWhen(Func<bool> predicate, Action action) => Main.Instance.ModHelper.Events.Unity.RunWhen(predicate, action);
|
#region OnSceneUnloaded
|
||||||
public static void FireInNUpdates(Action action, int n) => Main.Instance.ModHelper.Events.Unity.FireInNUpdates(action, n);
|
static Delay() => SceneManager.sceneUnloaded += OnSceneUnloaded;
|
||||||
public static void FireOnNextUpdate(Action action) => Main.Instance.ModHelper.Events.Unity.FireOnNextUpdate(action);
|
|
||||||
public static void RunWhenAndInNUpdates(Action action, Func<bool> predicate, int n) => Main.Instance.StartCoroutine(RunWhenOrInNUpdatesCoroutine(action, predicate, n));
|
private static void OnSceneUnloaded(Scene _) => Main.Instance.StopAllCoroutines();
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public methods
|
||||||
|
public static void StartCoroutine(IEnumerator coroutine) => Main.Instance.StartCoroutine(coroutine);
|
||||||
|
|
||||||
|
public static void RunWhen(Func<bool> predicate, Action action) => StartCoroutine(RunWhenCoroutine(action, predicate));
|
||||||
|
|
||||||
|
public static void FireInNUpdates(Action action, int n) => StartCoroutine(FireInNUpdatesCoroutine(action, n));
|
||||||
|
|
||||||
|
public static void FireOnNextUpdate(Action action) => FireInNUpdates(action, 1);
|
||||||
|
|
||||||
|
public static void RunWhenAndInNUpdates(Action action, Func<bool> predicate, int n) => Delay.StartCoroutine(RunWhenOrInNUpdatesCoroutine(action, predicate, n));
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Coroutines
|
||||||
|
private static IEnumerator RunWhenCoroutine(Action action, Func<bool> predicate)
|
||||||
|
{
|
||||||
|
while (!predicate.Invoke())
|
||||||
|
{
|
||||||
|
yield return new WaitForEndOfFrame();
|
||||||
|
}
|
||||||
|
|
||||||
|
action.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerator FireInNUpdatesCoroutine(Action action, int n)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
yield return new WaitForEndOfFrame();
|
||||||
|
}
|
||||||
|
action?.Invoke();
|
||||||
|
}
|
||||||
|
|
||||||
private static IEnumerator RunWhenOrInNUpdatesCoroutine(Action action, Func<bool> predicate, int n)
|
private static IEnumerator RunWhenOrInNUpdatesCoroutine(Action action, Func<bool> predicate, int n)
|
||||||
{
|
{
|
||||||
@ -24,5 +58,6 @@ namespace NewHorizons.Utility.OWML
|
|||||||
|
|
||||||
action.Invoke();
|
action.Invoke();
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user