Fade out before changing star system, move invulnerability handling to a class

This commit is contained in:
Nick 2023-07-15 15:39:49 -04:00
parent b792f73bb0
commit d34b1f44de
7 changed files with 153 additions and 77 deletions

View File

@ -1,4 +1,5 @@
using NewHorizons.Builder.General; using NewHorizons.Builder.General;
using NewHorizons.Handlers;
using NewHorizons.Utility; using NewHorizons.Utility;
using NewHorizons.Utility.OWML; using NewHorizons.Utility.OWML;
using UnityEngine; using UnityEngine;
@ -113,10 +114,8 @@ namespace NewHorizons.Components.Ship
public void WarpIn(bool wearingSuit) public void WarpIn(bool wearingSuit)
{ {
NHLogger.LogVerbose("Starting warp-in"); NHLogger.LogVerbose("Starting warp-in");
// Trying really hard to stop the player from dying while warping in
_impactDeathSpeed = Locator.GetDeathManager()._impactDeathSpeed; InvulnerabilityHandler.MakeInvulnerable(true);
Locator.GetDeathManager()._impactDeathSpeed = Mathf.Infinity;
Locator.GetDeathManager()._invincible = true;
_isWarpingIn = true; _isWarpingIn = true;
_wearingSuit = wearingSuit; _wearingSuit = wearingSuit;
@ -191,10 +190,7 @@ namespace NewHorizons.Components.Ship
Delay.FireInNUpdates(() => _whitehole.Collapse(), 30); Delay.FireInNUpdates(() => _whitehole.Collapse(), 30);
var resources = Locator.GetPlayerTransform().GetComponent<PlayerResources>(); var resources = Locator.GetPlayerTransform().GetComponent<PlayerResources>();
InvulnerabilityHandler.MakeInvulnerable(false);
Locator.GetDeathManager()._impactDeathSpeed = _impactDeathSpeed;
resources._currentHealth = 100f;
Locator.GetDeathManager()._invincible = false;
// For some reason warping into the ship makes you suffocate while in the ship // For some reason warping into the ship makes you suffocate while in the ship
if (_wearingSuit) resources.OnSuitUp(); if (_wearingSuit) resources.OnSuitUp();

View File

@ -0,0 +1,36 @@
using System;
using System.Collections;
using UnityEngine;
namespace NewHorizons.Handlers
{
public static class FadeHandler
{
public static void FadeOut(float length) => Main.Instance.StartCoroutine(FadeOutCoroutine(length));
private static IEnumerator FadeOutCoroutine(float length)
{
LoadManager.s_instance._fadeCanvas.enabled = true;
float startTime = Time.unscaledTime;
float endTime = Time.unscaledTime + length;
while (Time.unscaledTime < endTime)
{
LoadManager.s_instance._fadeImage.color = Color.Lerp(Color.clear, Color.black, (Time.unscaledTime - startTime) / length);
yield return new WaitForEndOfFrame();
}
LoadManager.s_instance._fadeImage.color = Color.black;
yield return new WaitForEndOfFrame();
}
public static void FadeThen(float length, Action action) => Main.Instance.StartCoroutine(FadeThenCoroutine(length, action));
private static IEnumerator FadeThenCoroutine(float length, Action action)
{
yield return FadeOutCoroutine(length);
action?.Invoke();
}
}
}

View File

@ -0,0 +1,36 @@
using NewHorizons.Utility.OWML;
using UnityEngine;
namespace NewHorizons.Handlers
{
internal class InvulnerabilityHandler
{
private static float _defaultImpactDeathSpeed = -1f;
public static void MakeInvulnerable(bool invulnerable)
{
NHLogger.Log($"Toggling immortality: {invulnerable}");
var deathManager = GetDeathManager();
var resources = GetPlayerResouces();
if (invulnerable)
{
if (_defaultImpactDeathSpeed == -1f)
_defaultImpactDeathSpeed = deathManager._impactDeathSpeed;
deathManager._impactDeathSpeed = Mathf.Infinity;
deathManager._invincible = true;
}
else
{
deathManager._impactDeathSpeed = _defaultImpactDeathSpeed;
resources._currentHealth = 100f;
deathManager._invincible = false;
}
}
private static DeathManager GetDeathManager() => GameObject.FindObjectOfType<DeathManager>();
private static PlayerResources GetPlayerResouces() => GameObject.FindObjectOfType<PlayerResources>();
}
}

View File

@ -246,7 +246,14 @@ namespace NewHorizons.Handlers
{ {
NHLogger.Log($"Creating [{body.Config.name}]"); NHLogger.Log($"Creating [{body.Config.name}]");
var planetObject = GenerateBody(body, defaultPrimaryToSun); var planetObject = GenerateBody(body, defaultPrimaryToSun);
planetObject?.SetActive(true); try
{
planetObject?.SetActive(true);
}
catch (Exception e)
{
NHLogger.LogError($"Error when activating new planet [{body.Config.name}] - {e}");
}
if (planetObject == null) if (planetObject == null)
{ {
body.UnloadCache(); body.UnloadCache();

View File

@ -86,7 +86,7 @@ namespace NewHorizons.Handlers
if (ao.GetAstroObjectName() == AstroObject.Name.RingWorld) if (ao.GetAstroObjectName() == AstroObject.Name.RingWorld)
{ {
CloakHandler.FlagStrangerDisabled = true; CloakHandler.FlagStrangerDisabled = true;
if (Locator._cloakFieldController.GetComponentInParent<AstroObject>() == ao) Locator._cloakFieldController = null; if (Locator._cloakFieldController?.GetComponentInParent<AstroObject>() == ao) Locator._cloakFieldController = null;
} }
if (ao.gameObject == null || !ao.gameObject.activeInHierarchy) if (ao.gameObject == null || !ao.gameObject.activeInHierarchy)

View File

@ -1,6 +1,7 @@
using NewHorizons.Utility; using NewHorizons.Utility;
using NewHorizons.Utility.OWML; using NewHorizons.Utility.OWML;
using System; using System;
using System.Collections;
using UnityEngine; using UnityEngine;
namespace NewHorizons.Handlers namespace NewHorizons.Handlers
@ -27,6 +28,7 @@ namespace NewHorizons.Handlers
public static void OnSystemReady(bool shouldWarpInFromShip, bool shouldWarpInFromVessel) public static void OnSystemReady(bool shouldWarpInFromShip, bool shouldWarpInFromVessel)
{ {
NHLogger.Log($"OnSystemReady {shouldWarpInFromVessel}, {shouldWarpInFromShip}, {UsingCustomSpawn()}");
if (shouldWarpInFromShip) if (shouldWarpInFromShip)
{ {
Main.Instance.ShipWarpController.WarpIn(Main.Instance.WearingSuit); Main.Instance.ShipWarpController.WarpIn(Main.Instance.WearingSuit);
@ -37,42 +39,31 @@ namespace NewHorizons.Handlers
} }
else if (UsingCustomSpawn()) else if (UsingCustomSpawn())
{ {
var player = SearchUtilities.Find("Player_Body"); InvulnerabilityHandler.MakeInvulnerable(true);
var playerBody = player.GetAttachedOWRigidbody();
var player = SearchUtilities.Find("Player_Body").GetAttachedOWRigidbody();
var spawn = GetDefaultSpawn(); var spawn = GetDefaultSpawn();
// Player dies during the teleport sometimes so we prevent that // Idk why but these just don't work?
var resources = player.GetComponent<PlayerResources>(); var matchInitialMotion = player.GetComponent<MatchInitialMotion>();
var deathManager = Locator.GetDeathManager(); if (matchInitialMotion != null) UnityEngine.Object.Destroy(matchInitialMotion);
_wasInvincible = resources._invincible; Main.Instance.StartCoroutine(SpawnCoroutine(player, spawn, 5));
_wasDeathManagerInvincible = deathManager._invincible;
_impactDeathSpeed = deathManager._impactDeathSpeed;
resources._invincible = true;
deathManager._invincible = true;
deathManager._impactDeathSpeed = float.MaxValue;
Delay.FireOnNextUpdate(() =>
{
var matchInitialMotion = playerBody.GetComponent<MatchInitialMotion>();
playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation);
if (matchInitialMotion != null)
{
// Idk why but these just don't work?
UnityEngine.Object.Destroy(matchInitialMotion);
Delay.FireOnNextUpdate(FixVelocity);
}
else
{
FixVelocity();
}
});
} }
} }
private static IEnumerator SpawnCoroutine(OWRigidbody playerBody, SpawnPoint spawn, int length)
{
for(int i = 0; i < length; i++)
{
playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation);
FixVelocity();
yield return new WaitForEndOfFrame();
}
InvulnerabilityHandler.MakeInvulnerable(false);
}
private static void FixVelocity() private static void FixVelocity()
{ {
var player = SearchUtilities.Find("Player_Body"); var player = SearchUtilities.Find("Player_Body");

View File

@ -385,6 +385,9 @@ namespace NewHorizons
if (isSolarSystem || isEyeOfTheUniverse) if (isSolarSystem || isEyeOfTheUniverse)
{ {
// Stop dying while spawning please
InvulnerabilityHandler.MakeInvulnerable(true);
IsSystemReady = false; IsSystemReady = false;
NewHorizonsData.Load(); NewHorizonsData.Load();
@ -564,6 +567,10 @@ namespace NewHorizons
// 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)
{ {
// ShipWarpController will handle the invulnerability otherwise
if (!shouldWarpInFromShip)
Delay.FireOnNextUpdate(() => InvulnerabilityHandler.MakeInvulnerable(false));
Locator.GetPlayerBody().gameObject.AddComponent<DebugRaycaster>(); Locator.GetPlayerBody().gameObject.AddComponent<DebugRaycaster>();
Locator.GetPlayerBody().gameObject.AddComponent<DebugPropPlacer>(); Locator.GetPlayerBody().gameObject.AddComponent<DebugPropPlacer>();
Locator.GetPlayerBody().gameObject.AddComponent<DebugMenu>(); Locator.GetPlayerBody().gameObject.AddComponent<DebugMenu>();
@ -814,47 +821,50 @@ namespace NewHorizons
return; return;
} }
if (LoadManager.GetCurrentScene() == OWScene.SolarSystem || LoadManager.GetCurrentScene() == OWScene.EyeOfTheUniverse)
{
// Slide reel unloading is tied to being removed from the sector, so we do that here to prevent a softlock
Locator.GetPlayerSectorDetector().RemoveFromAllSectors();
}
if (IsChangingStarSystem) return; if (IsChangingStarSystem) return;
IsWarpingFromShip = warp; if (LoadManager.GetCurrentScene() == OWScene.SolarSystem || LoadManager.GetCurrentScene() == OWScene.EyeOfTheUniverse)
IsWarpingFromVessel = vessel;
DidWarpFromVessel = false;
OnChangeStarSystem?.Invoke(newStarSystem);
NHLogger.Log($"Warping to {newStarSystem}");
if (warp && ShipWarpController) ShipWarpController.WarpOut();
IsChangingStarSystem = true;
WearingSuit = PlayerState.IsWearingSuit();
OWScene sceneToLoad;
if (newStarSystem == "EyeOfTheUniverse")
{ {
PlayerData.SaveWarpedToTheEye(TimeLoopUtilities.GetVanillaSecondsRemaining()); IsWarpingFromShip = warp;
sceneToLoad = OWScene.EyeOfTheUniverse; IsWarpingFromVessel = vessel;
DidWarpFromVessel = false;
OnChangeStarSystem?.Invoke(newStarSystem);
NHLogger.Log($"Warping to {newStarSystem}");
if (warp && ShipWarpController) ShipWarpController.WarpOut();
IsChangingStarSystem = true;
WearingSuit = PlayerState.IsWearingSuit();
OWScene sceneToLoad;
if (newStarSystem == "EyeOfTheUniverse")
{
PlayerData.SaveWarpedToTheEye(TimeLoopUtilities.GetVanillaSecondsRemaining());
sceneToLoad = OWScene.EyeOfTheUniverse;
}
else
{
PlayerData.SaveEyeCompletion(); // So that the title screen doesn't keep warping you back to eye
if (SystemDict[_currentStarSystem].Config.enableTimeLoop) SecondsElapsedInLoop = TimeLoop.GetSecondsElapsed();
else SecondsElapsedInLoop = -1;
sceneToLoad = OWScene.SolarSystem;
}
_currentStarSystem = newStarSystem;
// Freeze player inputs
OWInput.ChangeInputMode(InputMode.None);
// Hide unloading
FadeHandler.FadeThen(1f, () =>
{
// Slide reel unloading is tied to being removed from the sector, so we do that here to prevent a softlock
Locator.GetPlayerSectorDetector().RemoveFromAllSectors();
LoadManager.LoadSceneImmediate(sceneToLoad);
});
} }
else
{
PlayerData.SaveEyeCompletion(); // So that the title screen doesn't keep warping you back to eye
if (SystemDict[_currentStarSystem].Config.enableTimeLoop) SecondsElapsedInLoop = TimeLoop.GetSecondsElapsed();
else SecondsElapsedInLoop = -1;
sceneToLoad = OWScene.SolarSystem;
}
_currentStarSystem = newStarSystem;
// Freeze player inputs
OWInput.ChangeInputMode(InputMode.None);
LoadManager.LoadSceneAsync(sceneToLoad, !vessel, LoadManager.FadeType.ToBlack, vessel ? 1 : 0.1f, true);
} }
void OnDeath(DeathType _) void OnDeath(DeathType _)