quantum-space-buddies/QSB/DeathSync/RespawnOnDeath.cs

134 lines
4.4 KiB
C#
Raw Normal View History

2020-08-25 14:10:09 +00:00
using OWML.Common;
2020-12-20 10:56:15 +00:00
using OWML.Utils;
using QSB.Utility;
2020-08-25 14:10:09 +00:00
using System.Linq;
using UnityEngine;
2020-08-13 19:46:16 +00:00
namespace QSB.DeathSync
{
2020-12-02 21:23:01 +00:00
public class RespawnOnDeath : MonoBehaviour
{
public static RespawnOnDeath Instance;
public readonly DeathType[] AllowedDeathTypes = {
DeathType.BigBang,
DeathType.Supernova,
DeathType.TimeLoop
};
2021-02-06 21:31:12 +00:00
private readonly Vector3 ShipContainerOffset = new Vector3(-16.45f, -52.67f, 227.39f);
private readonly Quaternion ShipContainerRotation = Quaternion.Euler(-76.937f, 1.062f, -185.066f);
2020-12-02 21:23:01 +00:00
private SpawnPoint _shipSpawnPoint;
private SpawnPoint _playerSpawnPoint;
private OWRigidbody _shipBody;
private PlayerSpawner _playerSpawner;
private FluidDetector _fluidDetector;
private PlayerResources _playerResources;
private ShipComponent[] _shipComponents;
private HatchController _hatchController;
private ShipCockpitController _cockpitController;
private PlayerSpacesuit _spaceSuit;
2021-02-07 18:48:46 +00:00
private ShipTractorBeamSwitch _shipTractorBeam;
2020-12-02 21:23:01 +00:00
2020-12-19 21:47:19 +00:00
public void Awake() => Instance = this;
2020-12-02 21:23:01 +00:00
public void Init()
{
var playerTransform = Locator.GetPlayerTransform();
_playerResources = playerTransform.GetComponent<PlayerResources>();
2021-02-06 21:31:12 +00:00
_spaceSuit = Locator.GetPlayerSuit();
2020-12-02 21:23:01 +00:00
_playerSpawner = FindObjectOfType<PlayerSpawner>();
2021-02-07 18:48:46 +00:00
_shipTractorBeam = FindObjectOfType<ShipTractorBeamSwitch>();
2020-12-02 21:23:01 +00:00
_fluidDetector = Locator.GetPlayerCamera().GetComponentInChildren<FluidDetector>();
2021-02-06 21:31:12 +00:00
_playerSpawnPoint = GetSpawnPoint();
_shipSpawnPoint = GetSpawnPoint(true);
2020-12-02 21:23:01 +00:00
var shipTransform = Locator.GetShipTransform();
if (shipTransform == null)
{
2021-02-07 11:26:13 +00:00
DebugLog.ToConsole($"Warning - Init() ran when ship was null?", MessageType.Warning);
2020-12-02 21:23:01 +00:00
return;
}
_shipComponents = shipTransform.GetComponentsInChildren<ShipComponent>();
_hatchController = shipTransform.GetComponentInChildren<HatchController>();
_cockpitController = shipTransform.GetComponentInChildren<ShipCockpitController>();
_shipBody = Locator.GetShipBody();
2021-02-06 21:31:12 +00:00
if (_shipSpawnPoint == null)
{
DebugLog.ToConsole("Warning - _shipSpawnPoint is null in Init()!", MessageType.Warning);
return;
}
// Move debug spawn point to initial ship position (so ship doesnt spawn in space!)
var timberHearth = Locator.GetAstroObject(AstroObject.Name.TimberHearth).transform;
_shipSpawnPoint.transform.SetParent(timberHearth);
_shipSpawnPoint.transform.localPosition = ShipContainerOffset;
_shipSpawnPoint.transform.localRotation = ShipContainerRotation;
}
public void ResetPlayer()
{
if (_playerSpawnPoint == null)
{
DebugLog.ToConsole("Warning - _playerSpawnPoint is null!", MessageType.Warning);
Init();
}
2021-02-07 19:43:09 +00:00
2021-02-06 21:31:12 +00:00
// Cant use _playerSpawner.DebugWarp because that will warp the ship if the player is in it
var playerBody = Locator.GetPlayerBody();
playerBody.WarpToPositionRotation(_playerSpawnPoint.transform.position, _playerSpawnPoint.transform.rotation);
playerBody.SetVelocity(_playerSpawnPoint.GetPointVelocity());
_playerSpawnPoint.AddObjectToTriggerVolumes(Locator.GetPlayerDetector().gameObject);
_playerSpawnPoint.AddObjectToTriggerVolumes(_fluidDetector.gameObject);
_playerSpawnPoint.OnSpawnPlayer();
_playerResources.SetValue("_isSuffocating", false);
_playerResources.DebugRefillResources();
_spaceSuit.RemoveSuit(true);
2020-12-02 21:23:01 +00:00
}
public void ResetShip()
{
2020-12-19 21:58:14 +00:00
if (_shipSpawnPoint == null)
2020-12-02 21:23:01 +00:00
{
2020-12-19 22:03:00 +00:00
DebugLog.ToConsole("Warning - _shipSpawnPoint is null!", MessageType.Warning);
2020-12-19 21:58:14 +00:00
Init();
2020-12-02 21:23:01 +00:00
}
2020-12-19 21:58:14 +00:00
if (_shipBody == null)
2020-12-02 21:23:01 +00:00
{
2021-02-07 11:26:13 +00:00
DebugLog.ToConsole($"Warning - Tried to reset ship, but the ship is null!", MessageType.Warning);
2020-12-02 21:23:01 +00:00
return;
}
2020-12-20 09:45:50 +00:00
2020-12-02 21:23:01 +00:00
_shipBody.SetVelocity(_shipSpawnPoint.GetPointVelocity());
_shipBody.WarpToPositionRotation(_shipSpawnPoint.transform.position, _shipSpawnPoint.transform.rotation);
2021-02-06 21:31:12 +00:00
foreach (var shipComponent in _shipComponents)
2020-12-02 21:23:01 +00:00
{
2021-02-06 21:31:12 +00:00
shipComponent.SetDamaged(false);
2020-12-02 21:23:01 +00:00
}
Invoke(nameof(ExitShip), 0.01f);
}
private void ExitShip()
{
_cockpitController.Invoke("ExitFlightConsole");
_cockpitController.Invoke("CompleteExitFlightConsole");
_hatchController.SetValue("_isPlayerInShip", false);
_hatchController.Invoke("OpenHatch");
2021-02-07 18:48:46 +00:00
_shipTractorBeam.ActivateTractorBeam();
2020-12-02 21:23:01 +00:00
}
2021-02-09 09:11:35 +00:00
private SpawnPoint GetSpawnPoint(bool isShip = false)
2021-02-06 21:31:12 +00:00
=> _playerSpawner
2020-12-14 21:20:53 +00:00
.GetValue<SpawnPoint[]>("_spawnList")
2021-02-06 21:31:12 +00:00
.FirstOrDefault(spawnPoint =>
2021-02-09 09:11:35 +00:00
spawnPoint.GetSpawnLocation() == SpawnLocation.TimberHearth
2021-02-06 21:31:12 +00:00
&& spawnPoint.IsShipSpawn() == isShip);
2020-12-14 21:20:53 +00:00
}
2020-12-03 08:28:05 +00:00
}