From f354f991079501f37e87268b4ab530b23784f5a2 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 1 Jul 2023 14:44:00 -0400 Subject: [PATCH] Player spawning cleanup, doesn't work in main system + kills player on first spawn sometimes --- .../Builder/General/SpawnPointBuilder.cs | 40 +++++++++---------- .../Components/Ship/ShipWarpController.cs | 5 ++- NewHorizons/Handlers/PlanetCreationHandler.cs | 7 +++- NewHorizons/Handlers/VesselWarpHandler.cs | 1 + NewHorizons/Main.cs | 24 +++++++++-- .../PlayerPatches/PlayerSpawnerPatches.cs | 13 ++---- 6 files changed, 52 insertions(+), 38 deletions(-) diff --git a/NewHorizons/Builder/General/SpawnPointBuilder.cs b/NewHorizons/Builder/General/SpawnPointBuilder.cs index bac09f22..2fdd64e9 100644 --- a/NewHorizons/Builder/General/SpawnPointBuilder.cs +++ b/NewHorizons/Builder/General/SpawnPointBuilder.cs @@ -6,6 +6,7 @@ using NewHorizons.Utility.OuterWilds; using System; using System.Reflection; using UnityEngine; +using Steamworks; namespace NewHorizons.Builder.General { @@ -16,25 +17,37 @@ namespace NewHorizons.Builder.General { SpawnPoint playerSpawn = null; - if (!Main.Instance.IsWarpingFromVessel && !Main.Instance.IsWarpingFromShip && module.playerSpawn != null) + // Make the spawn point even if it won't be used this loop + if (module.playerSpawn != null) { GameObject spawnGO = GeneralPropBuilder.MakeNew("PlayerSpawnPoint", planetGO, null, module.playerSpawn); + spawnGO.SetActive(false); spawnGO.layer = Layer.PlayerSafetyCollider; playerSpawn = spawnGO.AddComponent(); + playerSpawn._attachedBody = owRigidBody; + playerSpawn._spawnLocation = SpawnLocation.None; + // #601 we need to actually set the right trigger volumes here playerSpawn._triggerVolumes = new OWTriggerVolume[0]; - spawnGO.transform.position += spawnGO.transform.up * 4f; + spawnGO.SetActive(true); } + if (module.shipSpawn != null) { GameObject spawnGO = GeneralPropBuilder.MakeNew("ShipSpawnPoint", planetGO, null, module.shipSpawn); + spawnGO.SetActive(false); spawnGO.layer = Layer.PlayerSafetyCollider; - var spawnPoint = spawnGO.AddComponent(); - spawnPoint._isShipSpawn = true; - spawnPoint._triggerVolumes = new OWTriggerVolume[0]; + var shipSpawn = spawnGO.AddComponent(); + shipSpawn._isShipSpawn = true; + shipSpawn._attachedBody = owRigidBody; + shipSpawn._spawnLocation = SpawnLocation.None; + // #601 we need to actually set the right trigger volumes here + shipSpawn._triggerVolumes = new OWTriggerVolume[0]; + + // TODO: this should happen elsewhere var ship = SearchUtilities.Find("Ship_Body"); if (ship != null) { @@ -48,27 +61,14 @@ namespace NewHorizons.Builder.General } ship.GetRequiredComponent().SetBodyToMatch(owRigidBody); - - if (Main.Instance.IsWarpingFromShip) - { - NHLogger.LogVerbose("Overriding player spawn to be inside ship"); - GameObject playerSpawnGO = new GameObject("PlayerSpawnPoint"); - playerSpawnGO.transform.parent = ship.transform; - playerSpawnGO.layer = Layer.PlayerSafetyCollider; - - playerSpawnGO.transform.localPosition = new Vector3(0, 0, 0); - - playerSpawn = playerSpawnGO.AddComponent(); - playerSpawn._triggerVolumes = new OWTriggerVolume[0]; - playerSpawnGO.transform.localRotation = Quaternion.Euler(0, 0, 0); - } } + spawnGO.SetActive(true); } if ((Main.Instance.IsWarpingFromVessel || (!Main.Instance.IsWarpingFromShip && (module.playerSpawn?.startWithSuit ?? false))) && !suitUpQueued) { suitUpQueued = true; - Delay.RunWhen(() => Main.IsSystemReady, () => SuitUp()); + Delay.RunWhen(() => Main.IsSystemReady, SuitUp); } NHLogger.Log($"Made spawnpoint on [{planetGO.name}]"); diff --git a/NewHorizons/Components/Ship/ShipWarpController.cs b/NewHorizons/Components/Ship/ShipWarpController.cs index 3d951631..8917ddb0 100644 --- a/NewHorizons/Components/Ship/ShipWarpController.cs +++ b/NewHorizons/Components/Ship/ShipWarpController.cs @@ -134,7 +134,7 @@ namespace NewHorizons.Components.Ship { if (_isWarpingIn && LateInitializerManager.isDoneInitializing) { - Delay.FireInNUpdates(() => StartWarpInEffect(), 1); + Delay.FireOnNextUpdate(StartWarpInEffect); _isWarpingIn = false; } @@ -167,7 +167,7 @@ namespace NewHorizons.Components.Ship NHLogger.LogVerbose("Starting warp-in effect"); _oneShotSource.PlayOneShot(AudioType.VesselSingularityCollapse, 1f); Locator.GetDeathManager()._invincible = true; - if (Main.Instance.CurrentStarSystem.Equals("SolarSystem")) TeleportToShip(); + TeleportToShip(); _whitehole.Create(); _waitingToBeSeated = true; if (_wearingSuit && !Locator.GetPlayerController()._isWearingSuit) @@ -179,6 +179,7 @@ namespace NewHorizons.Components.Ship private void TeleportToShip() { var playerSpawner = FindObjectOfType(); + NHLogger.LogVerbose("Debug warping into ship"); playerSpawner.DebugWarp(playerSpawner.GetSpawnPoint(SpawnLocation.Ship)); } diff --git a/NewHorizons/Handlers/PlanetCreationHandler.cs b/NewHorizons/Handlers/PlanetCreationHandler.cs index 8d0eceee..ecb465a1 100644 --- a/NewHorizons/Handlers/PlanetCreationHandler.cs +++ b/NewHorizons/Handlers/PlanetCreationHandler.cs @@ -456,9 +456,12 @@ namespace NewHorizons.Handlers // Spawning on other planets is a bit hacky so we do it last if (body.Config.Spawn != null) { - NHLogger.LogVerbose("Making spawn point"); + NHLogger.LogVerbose($"Making spawn point on {body.Config.name}"); var spawnPoint = SpawnPointBuilder.Make(go, body.Config.Spawn, owRigidBody); - if (Main.SystemDict[body.Config.starSystem].SpawnPoint == null || (body.Config.Spawn.playerSpawn?.isDefault ?? false)) + var isVanillaSystem = body.Config.starSystem == "SolarSystem" || body.Config.starSystem == "EyeOfTheUniverse"; + var needsSpawnPoint = Main.SystemDict[body.Config.starSystem].SpawnPoint == null || isVanillaSystem; + var isDefaultSpawn = body.Config.Spawn.playerSpawn?.isDefault ?? true; // Backwards compat + if (needsSpawnPoint || isDefaultSpawn) { Main.SystemDict[body.Config.starSystem].SpawnPoint = spawnPoint; } diff --git a/NewHorizons/Handlers/VesselWarpHandler.cs b/NewHorizons/Handlers/VesselWarpHandler.cs index 9cc279ce..07277776 100644 --- a/NewHorizons/Handlers/VesselWarpHandler.cs +++ b/NewHorizons/Handlers/VesselWarpHandler.cs @@ -62,6 +62,7 @@ namespace NewHorizons.Handlers public static void TeleportToVessel() { var playerSpawner = Object.FindObjectOfType(); + NHLogger.LogVerbose("Debug warping into vessel"); playerSpawner.DebugWarp(_vesselSpawnPoint); Builder.General.SpawnPointBuilder.SuitUp(); diff --git a/NewHorizons/Main.cs b/NewHorizons/Main.cs index 1b431a0e..5dbc822a 100644 --- a/NewHorizons/Main.cs +++ b/NewHorizons/Main.cs @@ -564,11 +564,27 @@ namespace NewHorizons Locator.GetPlayerBody().gameObject.AddComponent(); Locator.GetPlayerBody().gameObject.AddComponent(); Locator.GetPlayerBody().gameObject.AddComponent(); - // DebugArrow.CreateArrow(Locator.GetPlayerBody().gameObject); // This is for NH devs mostly. It shouldn't be active in debug mode for now. Someone should make a dev tools submenu for it though. - if (shouldWarpInFromShip) _shipWarpController.WarpIn(WearingSuit); - else if (shouldWarpInFromVessel) VesselWarpHandler.TeleportToVessel(); - else FindObjectOfType().DebugWarp(SystemDict[_currentStarSystem].SpawnPoint); + if (shouldWarpInFromShip) + { + _shipWarpController.WarpIn(WearingSuit); + } + else if (shouldWarpInFromVessel) + { + VesselWarpHandler.TeleportToVessel(); + } + else + { + var spawnPoint = SystemDict[CurrentStarSystem].SpawnPoint; + if (spawnPoint != null) + { + Delay.FireOnNextUpdate(() => GameObject.FindObjectOfType().DebugWarp(spawnPoint)); + } + else + { + NHLogger.Log($"No NH spawn point for {CurrentStarSystem}"); + } + } VesselCoordinatePromptHandler.RegisterPrompts(SystemDict.Where(system => system.Value.Config.Vessel?.coords != null).Select(x => x.Value).ToList()); } diff --git a/NewHorizons/Patches/PlayerPatches/PlayerSpawnerPatches.cs b/NewHorizons/Patches/PlayerPatches/PlayerSpawnerPatches.cs index f3017482..70099cce 100644 --- a/NewHorizons/Patches/PlayerPatches/PlayerSpawnerPatches.cs +++ b/NewHorizons/Patches/PlayerPatches/PlayerSpawnerPatches.cs @@ -11,19 +11,12 @@ namespace NewHorizons.Patches.PlayerPatches [HarmonyPatch(nameof(PlayerSpawner.SpawnPlayer))] public static bool PlayerSpawner_SpawnPlayer(PlayerSpawner __instance) { - if (Main.Instance.IsWarpingFromVessel || Main.Instance.DidWarpFromVessel) + if (Main.Instance.IsWarpingFromVessel || Main.Instance.DidWarpFromVessel || Main.Instance.IsWarpingFromShip) { - NHLogger.LogWarning("Abort player spawn. Vessel will handle it."); + NHLogger.LogWarning("Abort player spawn. Vessel/Ship will handle it."); return false; } - else if (Main.SystemDict[Main.Instance.CurrentStarSystem].SpawnPoint != null) - { - NHLogger.LogVerbose($"Player spawning at {Main.SystemDict[Main.Instance.CurrentStarSystem].SpawnPoint.transform.GetPath()}"); - __instance.SetInitialSpawnPoint(Main.SystemDict[Main.Instance.CurrentStarSystem].SpawnPoint); - } else if (Main.Instance.CurrentStarSystem != "SolarSystem" && Main.Instance.CurrentStarSystem != "EyeOfTheUniverse") - { - NHLogger.LogWarning("No player spawn point set."); - } + return true; } }