Fixes spawn death, messes up rigidbodies sometimes

This commit is contained in:
Nick 2023-07-15 16:39:19 -04:00
parent b5821e2ea9
commit 6c3c47b011
3 changed files with 36 additions and 14 deletions

View File

@ -65,6 +65,7 @@ namespace NewHorizons
public bool IsWarpingFromShip { get; private set; } = false;
public bool IsWarpingFromVessel { get; private set; } = false;
public bool IsWarpingBackToEye { get; internal set; } = false;
public bool DidWarpFromShip { get; private set; } = false;
public bool DidWarpFromVessel { get; private set; } = false;
public bool WearingSuit { get; private set; } = false;
@ -75,6 +76,10 @@ namespace NewHorizons
private string _defaultStarSystem = "SolarSystem";
internal string _currentStarSystem = "SolarSystem";
private bool _firstLoad = true;
private bool _playerAwake;
public bool PlayerSpawned { get; set; }
public ShipWarpController ShipWarpController { get; private set; }
// API events
@ -196,7 +201,6 @@ namespace NewHorizons
SceneManager.sceneUnloaded += OnSceneUnloaded;
GlobalMessenger<DeathType>.AddListener("PlayerDeath", OnDeath);
GlobalMessenger.AddListener("WakeUp", OnWakeUp);
NHAssetBundle = ModHelper.Assets.LoadBundle("Assets/newhorizons_public");
@ -245,15 +249,12 @@ namespace NewHorizons
NHLogger.Log($"Destroying NewHorizons");
SceneManager.sceneLoaded -= OnSceneLoaded;
GlobalMessenger<DeathType>.RemoveListener("PlayerDeath", OnDeath);
GlobalMessenger.RemoveListener("WakeUp", new Callback(OnWakeUp));
GlobalMessenger.RemoveListener("WakeUp", OnWakeUp);
AchievementHandler.OnDestroy();
}
private static void OnWakeUp()
{
IsSystemReady = true;
}
private void OnWakeUp() => _playerAwake = true;
private void OnSceneUnloaded(Scene scene)
{
@ -269,6 +270,8 @@ namespace NewHorizons
{
NHLogger.Log($"Scene Loaded: {scene.name} {mode} OWScene.{LoadManager.NameToScene(scene.name)}");
PlayerSpawned = false;
var isTitleScreen = scene.name == LoadManager.SceneToName(OWScene.TitleScreen);
var isSolarSystem = scene.name == LoadManager.SceneToName(OWScene.SolarSystem);
var isEyeOfTheUniverse = scene.name == LoadManager.SceneToName(OWScene.EyeOfTheUniverse);
@ -380,6 +383,7 @@ namespace NewHorizons
// EOTU fixes
if (isEyeOfTheUniverse)
{
_playerAwake = true;
EyeSceneHandler.OnSceneLoad();
}
@ -438,10 +442,10 @@ namespace NewHorizons
var shouldWarpInFromShip = IsWarpingFromShip && ShipWarpController != null;
var shouldWarpInFromVessel = IsWarpingFromVessel && VesselWarpHandler.VesselSpawnPoint != null;
Delay.RunWhen(() => IsSystemReady, () => OnSystemReady(shouldWarpInFromShip, shouldWarpInFromVessel));
IsWarpingFromShip = false;
IsWarpingFromVessel = false;
DidWarpFromShip = shouldWarpInFromShip;
DidWarpFromVessel = shouldWarpInFromVessel;
var map = FindObjectOfType<MapController>();
@ -500,16 +504,10 @@ namespace NewHorizons
}
else if (isEyeOfTheUniverse)
{
// There is no wake up in eye scene
Delay.FireOnNextUpdate(() =>
{
IsSystemReady = true;
OnSystemReady(false, false);
});
IsWarpingFromShip = false;
IsWarpingFromVessel = false;
DidWarpFromVessel = false;
DidWarpFromShip = false;
}
//Stop starfield from disappearing when there is no lights
@ -562,11 +560,16 @@ namespace NewHorizons
_currentStarSystem = _defaultStarSystem;
}
}
// Wait for player to be awake and also for frames to pass
Delay.RunWhenAndInNUpdates(() => OnSystemReady(DidWarpFromShip, DidWarpFromVessel), () => _playerAwake && PlayerSpawned, 30);
}
// Had a bunch of separate unity things firing stuff when the system is ready so I moved it all to here
private void OnSystemReady(bool shouldWarpInFromShip, bool shouldWarpInFromVessel)
{
IsSystemReady = true;
// ShipWarpController will handle the invulnerability otherwise
if (!shouldWarpInFromShip)
Delay.FireOnNextUpdate(() => InvulnerabilityHandler.MakeInvulnerable(false));

View File

@ -11,6 +11,8 @@ namespace NewHorizons.Patches.PlayerPatches
[HarmonyPatch(nameof(PlayerSpawner.SpawnPlayer))]
public static bool PlayerSpawner_SpawnPlayer(PlayerSpawner __instance)
{
Main.Instance.PlayerSpawned = true;
if (Main.Instance.IsWarpingFromVessel || Main.Instance.DidWarpFromVessel || Main.Instance.IsWarpingFromShip)
{
NHLogger.LogWarning("Abort player spawn. Vessel/Ship will handle it.");

View File

@ -1,4 +1,6 @@
using System;
using System.Collections;
using UnityEngine;
namespace NewHorizons.Utility.OWML
{
@ -7,5 +9,20 @@ namespace NewHorizons.Utility.OWML
public static void RunWhen(Func<bool> predicate, Action action) => Main.Instance.ModHelper.Events.Unity.RunWhen(predicate, action);
public static void FireInNUpdates(Action action, int n) => Main.Instance.ModHelper.Events.Unity.FireInNUpdates(action, n);
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 IEnumerator RunWhenOrInNUpdatesCoroutine(Action action, Func<bool> predicate, int n)
{
for (int i = 0; i < n; i++)
{
yield return new WaitForEndOfFrame();
}
while (!predicate.Invoke())
{
yield return new WaitForEndOfFrame();
}
action.Invoke();
}
}
}