Fix spawn death (#638)

## Bug fixes
- Fixes dying on spawn in Evacuation
This commit is contained in:
Will Corby 2023-07-16 00:21:43 -07:00 committed by GitHub
commit 32f74e8be8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 37 deletions

View File

@ -1,6 +1,5 @@
using NewHorizons.Utility; using NewHorizons.Utility;
using NewHorizons.Utility.OWML; using NewHorizons.Utility.OWML;
using System;
using System.Collections; using System.Collections;
using UnityEngine; using UnityEngine;
@ -8,10 +7,6 @@ namespace NewHorizons.Handlers
{ {
public static class PlayerSpawnHandler public static class PlayerSpawnHandler
{ {
private static bool _wasInvincible;
private static bool _wasDeathManagerInvincible;
private static float _impactDeathSpeed;
public static void SetUpPlayerSpawn() public static void SetUpPlayerSpawn()
{ {
var spawnPoint = Main.SystemDict[Main.Instance.CurrentStarSystem].SpawnPoint; var spawnPoint = Main.SystemDict[Main.Instance.CurrentStarSystem].SpawnPoint;
@ -41,22 +36,18 @@ namespace NewHorizons.Handlers
{ {
InvulnerabilityHandler.MakeInvulnerable(true); InvulnerabilityHandler.MakeInvulnerable(true);
var player = SearchUtilities.Find("Player_Body").GetAttachedOWRigidbody();
var spawn = GetDefaultSpawn();
// Idk why but these just don't work? // Idk why but these just don't work?
var matchInitialMotion = player.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(player, spawn, 5)); Main.Instance.StartCoroutine(SpawnCoroutine(2));
} }
} }
private static IEnumerator SpawnCoroutine(OWRigidbody playerBody, SpawnPoint spawn, int length) private static IEnumerator SpawnCoroutine(int length)
{ {
for(int i = 0; i < length; i++) for(int i = 0; i < length; i++)
{ {
playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation);
FixVelocity(); FixVelocity();
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
} }
@ -66,16 +57,12 @@ namespace NewHorizons.Handlers
private static void FixVelocity() private static void FixVelocity()
{ {
var player = SearchUtilities.Find("Player_Body"); var playerBody = SearchUtilities.Find("Player_Body").GetAttachedOWRigidbody();
var playerBody = player.GetAttachedOWRigidbody();
var spawn = GetDefaultSpawn(); var spawn = GetDefaultSpawn();
var resources = playerBody.GetComponent<PlayerResources>();
playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation); playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation);
// Player dies during the teleport sometimes so we prevent that
var resources = player.GetComponent<PlayerResources>();
var deathManager = Locator.GetDeathManager();
var spawnVelocity = spawn._attachedBody.GetVelocity(); var spawnVelocity = spawn._attachedBody.GetVelocity();
var spawnAngularVelocity = spawn._attachedBody.GetPointTangentialVelocity(playerBody.transform.position); var spawnAngularVelocity = spawn._attachedBody.GetPointTangentialVelocity(playerBody.transform.position);
var velocity = spawnVelocity + spawnAngularVelocity; var velocity = spawnVelocity + spawnAngularVelocity;
@ -84,10 +71,6 @@ namespace NewHorizons.Handlers
NHLogger.LogVerbose($"Player spawn velocity {velocity} Player velocity {playerBody.GetVelocity()} spawn body {spawnVelocity} spawn angular vel {spawnAngularVelocity}"); NHLogger.LogVerbose($"Player spawn velocity {velocity} Player velocity {playerBody.GetVelocity()} spawn body {spawnVelocity} spawn angular vel {spawnAngularVelocity}");
resources._currentHealth = 100f; resources._currentHealth = 100f;
resources._invincible = _wasInvincible;
deathManager._invincible = _wasDeathManagerInvincible;
deathManager._impactDeathSpeed = _impactDeathSpeed;
} }
private static Vector3 CalculateMatchVelocity(OWRigidbody owRigidbody, OWRigidbody bodyToMatch, bool ignoreAngularVelocity) private static Vector3 CalculateMatchVelocity(OWRigidbody owRigidbody, OWRigidbody bodyToMatch, bool ignoreAngularVelocity)

View File

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

View File

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

View File

@ -1,4 +1,6 @@
using System; using System;
using System.Collections;
using UnityEngine;
namespace NewHorizons.Utility.OWML 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 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 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 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();
}
} }
} }

View File

@ -4,7 +4,7 @@
"author": "xen, Bwc9876, clay, MegaPiggy, John, Trifid, Hawkbar, Book", "author": "xen, Bwc9876, clay, MegaPiggy, John, Trifid, Hawkbar, Book",
"name": "New Horizons", "name": "New Horizons",
"uniqueName": "xen.NewHorizons", "uniqueName": "xen.NewHorizons",
"version": "1.12.2", "version": "1.12.3",
"owmlVersion": "2.9.3", "owmlVersion": "2.9.3",
"dependencies": [ "JohnCorby.VanillaFix", "_nebula.MenuFramework", "xen.CommonCameraUtility", "dgarro.CustomShipLogModes" ], "dependencies": [ "JohnCorby.VanillaFix", "_nebula.MenuFramework", "xen.CommonCameraUtility", "dgarro.CustomShipLogModes" ],
"conflicts": [ "Raicuparta.QuantumSpaceBuddies", "PacificEngine.OW_CommonResources" ], "conflicts": [ "Raicuparta.QuantumSpaceBuddies", "PacificEngine.OW_CommonResources" ],