Fix spawn on center of universe garbage (#664)

<!-- A new module or something else important -->
## Major features
-

<!-- A new parameter added to a module, or API feature -->
## Minor features
-

<!-- Some improvement that requires no action on the part of add-on
creators i.e., improved star graphics -->
## Improvements
-

<!-- Be sure to reference the existing issue if it exists -->
## Bug fixes
- Fixes ship being broken when spawning on the center of the universe.
Resolves #648
This commit is contained in:
Nick 2023-07-22 14:42:24 -04:00 committed by GitHub
commit 0e06df1586
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 38 deletions

View File

@ -13,6 +13,9 @@ namespace NewHorizons.Builder.General
public static class SpawnPointBuilder public static class SpawnPointBuilder
{ {
private static bool suitUpQueued = false; 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) public static SpawnPoint Make(GameObject planetGO, SpawnModule module, OWRigidbody owRigidBody)
{ {
SpawnPoint playerSpawn = null; SpawnPoint playerSpawn = null;
@ -35,41 +38,21 @@ namespace NewHorizons.Builder.General
if (module.shipSpawn != null) 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.SetActive(false);
spawnGO.layer = Layer.PlayerSafetyCollider; spawnGO.layer = Layer.PlayerSafetyCollider;
var shipSpawn = spawnGO.AddComponent<SpawnPoint>(); ShipSpawn = spawnGO.AddComponent<SpawnPoint>();
shipSpawn._isShipSpawn = true; ShipSpawn._isShipSpawn = true;
shipSpawn._attachedBody = owRigidBody; ShipSpawn._attachedBody = owRigidBody;
shipSpawn._spawnLocation = SpawnLocation.None; ShipSpawn._spawnLocation = SpawnLocation.None;
// #601 we need to actually set the right trigger volumes here // #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 ShipSpawnOffset = module.shipSpawn.offset ?? (module.shipSpawn.alignRadial.GetValueOrDefault() ? Vector3.up * 4 : Vector3.zero);
var ship = SearchUtilities.Find("Ship_Body");
if (ship != null)
{
ship.transform.position = spawnGO.transform.position;
ship.transform.rotation = spawnGO.transform.rotation;
// 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<MatchInitialMotion>().SetBodyToMatch(owRigidBody);
}
spawnGO.SetActive(true); 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) if ((Main.Instance.IsWarpingFromVessel || (!Main.Instance.IsWarpingFromShip && (module.playerSpawn?.startWithSuit ?? false))) && !suitUpQueued)

View File

@ -201,6 +201,8 @@ namespace NewHorizons.Components.Ship
GlobalMessenger.FireEvent("EnterShip"); GlobalMessenger.FireEvent("EnterShip");
PlayerState.OnEnterShip(); PlayerState.OnEnterShip();
PlayerSpawnHandler.SpawnShip();
} }
} }
} }

View File

@ -1,3 +1,4 @@
using NewHorizons.Builder.General;
using NewHorizons.Utility; using NewHorizons.Utility;
using NewHorizons.Utility.OWML; using NewHorizons.Utility.OWML;
using System.Collections; 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) private static IEnumerator SpawnCoroutine(int length)
{ {
for(int i = 0; i < length; i++) for(int i = 0; i < length; i++)
{ {
FixVelocity(); FixPlayerVelocity();
yield return new WaitForEndOfFrame(); yield return new WaitForEndOfFrame();
} }
InvulnerabilityHandler.MakeInvulnerable(false); 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 playerBody = SearchUtilities.Find("Player_Body").GetAttachedOWRigidbody();
var spawn = GetDefaultSpawn();
var resources = playerBody.GetComponent<PlayerResources>(); var resources = playerBody.GetComponent<PlayerResources>();
playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation); SpawnBody(playerBody, GetDefaultSpawn());
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}");
resources._currentHealth = 100f; 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) private static Vector3 CalculateMatchVelocity(OWRigidbody owRigidbody, OWRigidbody bodyToMatch, bool ignoreAngularVelocity)
{ {
var vector = Vector3.zero; var vector = Vector3.zero;