From b4b0345e653381e64cf59c53d0cdde29f5467985 Mon Sep 17 00:00:00 2001 From: Nick Date: Sat, 22 Jul 2023 03:51:10 -0400 Subject: [PATCH] Fix spawn on center of universe garbage --- .../Builder/General/SpawnPointBuilder.cs | 37 +++-------- .../Components/Ship/ShipWarpController.cs | 2 + NewHorizons/Handlers/PlayerSpawnHandler.cs | 61 +++++++++++++++---- 3 files changed, 62 insertions(+), 38 deletions(-) diff --git a/NewHorizons/Builder/General/SpawnPointBuilder.cs b/NewHorizons/Builder/General/SpawnPointBuilder.cs index 87520d52..cc7feddf 100644 --- a/NewHorizons/Builder/General/SpawnPointBuilder.cs +++ b/NewHorizons/Builder/General/SpawnPointBuilder.cs @@ -13,6 +13,9 @@ namespace NewHorizons.Builder.General public static class SpawnPointBuilder { private static bool suitUpQueued = false; + public static SpawnPoint ShipSpawn { get; private set; } + public static Vector3 ShipSpawnOffset { get; private set; } + public static SpawnPoint Make(GameObject planetGO, SpawnModule module, OWRigidbody owRigidBody) { SpawnPoint playerSpawn = null; @@ -35,41 +38,21 @@ namespace NewHorizons.Builder.General if (module.shipSpawn != null) { - GameObject spawnGO = GeneralPropBuilder.MakeNew("ShipSpawnPoint", planetGO, null, module.shipSpawn); + var spawnGO = GeneralPropBuilder.MakeNew("ShipSpawnPoint", planetGO, null, module.shipSpawn); spawnGO.SetActive(false); spawnGO.layer = Layer.PlayerSafetyCollider; - var shipSpawn = spawnGO.AddComponent(); - shipSpawn._isShipSpawn = true; - shipSpawn._attachedBody = owRigidBody; - shipSpawn._spawnLocation = SpawnLocation.None; + 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]; + ShipSpawn._triggerVolumes = new OWTriggerVolume[0]; - // TODO: this should happen elsewhere - var ship = SearchUtilities.Find("Ship_Body"); - if (ship != null) - { - ship.transform.position = spawnGO.transform.position; - ship.transform.rotation = spawnGO.transform.rotation; + ShipSpawnOffset = module.shipSpawn.offset ?? (module.shipSpawn.alignRadial.GetValueOrDefault() ? Vector3.up * 4 : Vector3.zero); - // Move it up a bit more when aligning to surface - if (module.shipSpawn.offset != null) - { - ship.transform.position += spawnGO.transform.TransformDirection(module.shipSpawn.offset); - } - else if (module.shipSpawn.alignRadial.GetValueOrDefault()) - { - ship.transform.position += ship.transform.up * 4f; - } - - ship.GetRequiredComponent().SetBodyToMatch(owRigidBody); - } spawnGO.SetActive(true); - - // Ship doesn't get activated sometimes - Delay.RunWhen(() => Main.IsSystemReady, () => ship.gameObject.SetActive(true)); } if ((Main.Instance.IsWarpingFromVessel || (!Main.Instance.IsWarpingFromShip && (module.playerSpawn?.startWithSuit ?? false))) && !suitUpQueued) diff --git a/NewHorizons/Components/Ship/ShipWarpController.cs b/NewHorizons/Components/Ship/ShipWarpController.cs index cbd09198..d1f8e62d 100644 --- a/NewHorizons/Components/Ship/ShipWarpController.cs +++ b/NewHorizons/Components/Ship/ShipWarpController.cs @@ -201,6 +201,8 @@ namespace NewHorizons.Components.Ship GlobalMessenger.FireEvent("EnterShip"); PlayerState.OnEnterShip(); + + PlayerSpawnHandler.SpawnShip(); } } } diff --git a/NewHorizons/Handlers/PlayerSpawnHandler.cs b/NewHorizons/Handlers/PlayerSpawnHandler.cs index cb1a607a..8a9eb6f8 100644 --- a/NewHorizons/Handlers/PlayerSpawnHandler.cs +++ b/NewHorizons/Handlers/PlayerSpawnHandler.cs @@ -1,3 +1,4 @@ +using NewHorizons.Builder.General; using NewHorizons.Utility; using NewHorizons.Utility.OWML; using System.Collections; @@ -45,35 +46,73 @@ namespace NewHorizons.Handlers } } + public static void SpawnShip() + { + var ship = SearchUtilities.Find("Ship_Body"); + if (ship != null) + { + ship.SetActive(true); + + var pos = SpawnPointBuilder.ShipSpawn.transform.position; + + // Move it up a bit more when aligning to surface + if (SpawnPointBuilder.ShipSpawnOffset != null) + { + pos += SpawnPointBuilder.ShipSpawn.transform.TransformDirection(SpawnPointBuilder.ShipSpawnOffset); + } + + SpawnBody(ship.GetAttachedOWRigidbody(), SpawnPointBuilder.ShipSpawn, pos); + } + } + private static IEnumerator SpawnCoroutine(int length) { for(int i = 0; i < length; i++) { - FixVelocity(); + FixPlayerVelocity(); yield return new WaitForEndOfFrame(); } InvulnerabilityHandler.MakeInvulnerable(false); + + if (!Main.Instance.IsWarpingFromShip) + { + if (SpawnPointBuilder.ShipSpawn != null) + { + NHLogger.Log("Spawning player ship"); + SpawnShip(); + } + else + { + NHLogger.Log("System has no ship spawn. Deactivating it."); + SearchUtilities.Find("Ship_Body")?.SetActive(false); + } + } } - private static void FixVelocity() + private static void FixPlayerVelocity() { var playerBody = SearchUtilities.Find("Player_Body").GetAttachedOWRigidbody(); - var spawn = GetDefaultSpawn(); var resources = playerBody.GetComponent(); - playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation); - - var spawnVelocity = spawn._attachedBody.GetVelocity(); - var spawnAngularVelocity = spawn._attachedBody.GetPointTangentialVelocity(playerBody.transform.position); - var velocity = spawnVelocity + spawnAngularVelocity; - - playerBody.SetVelocity(velocity); - NHLogger.LogVerbose($"Player spawn velocity {velocity} Player velocity {playerBody.GetVelocity()} spawn body {spawnVelocity} spawn angular vel {spawnAngularVelocity}"); + SpawnBody(playerBody, GetDefaultSpawn()); resources._currentHealth = 100f; } + public static void SpawnBody(OWRigidbody body, SpawnPoint spawn, Vector3? positionOverride = null) + { + var pos = positionOverride ?? spawn.transform.position; + + body.WarpToPositionRotation(pos, spawn.transform.rotation); + + var spawnVelocity = spawn._attachedBody.GetVelocity(); + var spawnAngularVelocity = spawn._attachedBody.GetPointTangentialVelocity(pos); + var velocity = spawnVelocity + spawnAngularVelocity; + + body.SetVelocity(velocity); + } + private static Vector3 CalculateMatchVelocity(OWRigidbody owRigidbody, OWRigidbody bodyToMatch, bool ignoreAngularVelocity) { var vector = Vector3.zero;