Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix spawn on center of universe garbage #664

Merged
merged 1 commit into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions NewHorizons/Builder/General/SpawnPointBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,41 +38,21 @@ public static SpawnPoint Make(GameObject planetGO, SpawnModule module, OWRigidbo

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<SpawnPoint>();
shipSpawn._isShipSpawn = true;
shipSpawn._attachedBody = owRigidBody;
shipSpawn._spawnLocation = SpawnLocation.None;
ShipSpawn = spawnGO.AddComponent<SpawnPoint>();
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<MatchInitialMotion>().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)
Expand Down
2 changes: 2 additions & 0 deletions NewHorizons/Components/Ship/ShipWarpController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@ public void FinishWarpIn()

GlobalMessenger.FireEvent("EnterShip");
PlayerState.OnEnterShip();

PlayerSpawnHandler.SpawnShip();
}
}
}
57 changes: 48 additions & 9 deletions NewHorizons/Handlers/PlayerSpawnHandler.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using NewHorizons.Builder.General;
using NewHorizons.Utility;
using NewHorizons.Utility.OWML;
using System.Collections;
Expand Down Expand Up @@ -45,33 +46,71 @@ public static void OnSystemReady(bool shouldWarpInFromShip, bool shouldWarpInFro
}
}

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<PlayerResources>();

playerBody.WarpToPositionRotation(spawn.transform.position, spawn.transform.rotation);
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(playerBody.transform.position);
var spawnAngularVelocity = spawn._attachedBody.GetPointTangentialVelocity(pos);
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;
body.SetVelocity(velocity);
}

private static Vector3 CalculateMatchVelocity(OWRigidbody owRigidbody, OWRigidbody bodyToMatch, bool ignoreAngularVelocity)
Expand Down