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

140 lines
5.0 KiB
C#
Raw Normal View History

2020-08-25 14:10:09 +00:00
using OWML.Common;
using OWML.ModHelper.Events;
2020-11-03 21:42:14 +00:00
using QSB.EventsCore;
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
{
/// <summary>
/// Client-only-side component for managing respawning after death.
/// </summary>
2020-02-29 13:02:09 +00:00
public class RespawnOnDeath : MonoBehaviour
{
2020-08-13 19:46:16 +00:00
public static RespawnOnDeath Instance;
2020-02-29 13:02:09 +00:00
2020-08-13 19:46:16 +00:00
public readonly DeathType[] AllowedDeathTypes = {
DeathType.BigBang,
DeathType.Supernova,
DeathType.TimeLoop
};
2020-02-29 13:02:09 +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;
2020-02-29 13:02:09 +00:00
private void Awake()
{
2020-08-13 19:46:16 +00:00
Instance = this;
QSB.Helper.Events.Subscribe<PlayerResources>(OWML.Common.Events.AfterStart);
2020-08-10 16:17:54 +00:00
QSB.Helper.Events.Event += OnEvent;
}
private void OnEvent(MonoBehaviour behaviour, OWML.Common.Events ev)
{
2020-07-28 13:59:24 +00:00
if (behaviour is PlayerResources && ev == OWML.Common.Events.AfterStart)
{
Init();
}
}
2020-08-23 11:48:31 +00:00
public void Init()
{
var playerTransform = Locator.GetPlayerTransform();
2020-02-29 13:02:09 +00:00
_playerResources = playerTransform.GetComponent<PlayerResources>();
_spaceSuit = playerTransform.GetComponentInChildren<PlayerSpacesuit>(true);
_playerSpawner = FindObjectOfType<PlayerSpawner>();
_fluidDetector = Locator.GetPlayerCamera().GetComponentInChildren<FluidDetector>();
var shipTransform = Locator.GetShipTransform();
2020-08-10 16:17:54 +00:00
if (shipTransform == null)
{
2020-08-10 16:17:54 +00:00
return;
}
2020-08-10 16:17:54 +00:00
_shipComponents = shipTransform.GetComponentsInChildren<ShipComponent>();
_hatchController = shipTransform.GetComponentInChildren<HatchController>();
_cockpitController = shipTransform.GetComponentInChildren<ShipCockpitController>();
_shipBody = Locator.GetShipBody();
_shipSpawnPoint = GetSpawnPoint(true);
// Move debug spawn point to initial ship position.
_playerSpawnPoint = GetSpawnPoint();
_shipSpawnPoint.transform.position = shipTransform.position;
_shipSpawnPoint.transform.rotation = shipTransform.rotation;
}
public void ResetShip()
{
2020-02-29 13:02:09 +00:00
if (_shipBody == null)
{
return;
}
// Reset ship position.
if (_shipSpawnPoint == null)
{
DebugLog.ToConsole("_shipSpawnPoint is null!", MessageType.Warning);
return;
}
_shipBody.SetVelocity(_shipSpawnPoint.GetPointVelocity());
_shipBody.WarpToPositionRotation(_shipSpawnPoint.transform.position, _shipSpawnPoint.transform.rotation);
// Reset ship damage.
if (Locator.GetShipTransform())
{
2020-02-29 13:02:09 +00:00
foreach (var shipComponent in _shipComponents)
{
2020-02-29 13:02:09 +00:00
shipComponent.SetDamaged(false);
}
}
Invoke(nameof(ExitShip), 0.01f);
}
2020-02-29 13:02:09 +00:00
private void ExitShip()
{
_cockpitController.Invoke("ExitFlightConsole");
_cockpitController.Invoke("CompleteExitFlightConsole");
_hatchController.SetValue("_isPlayerInShip", false);
_hatchController.Invoke("OpenHatch");
2020-08-10 17:24:28 +00:00
GlobalMessenger.FireEvent(EventNames.ExitShip);
}
public void ResetPlayer()
{
// Reset player position.
2020-08-10 16:17:54 +00:00
var playerBody = Locator.GetPlayerBody();
2020-08-23 11:56:58 +00:00
playerBody.WarpToPositionRotation(_playerSpawnPoint.transform.position, _playerSpawnPoint.transform.rotation);
playerBody.SetVelocity(_playerSpawnPoint.GetPointVelocity());
_playerSpawnPoint.AddObjectToTriggerVolumes(Locator.GetPlayerDetector().gameObject);
_playerSpawnPoint.AddObjectToTriggerVolumes(_fluidDetector.gameObject);
_playerSpawnPoint.OnSpawnPlayer();
// Stop suffocation sound effect.
2020-08-23 11:56:58 +00:00
_playerResources.SetValue("_isSuffocating", false);
// Reset player health and resources.
2020-08-23 11:56:58 +00:00
_playerResources.DebugRefillResources();
// Remove space suit.
2020-08-23 11:56:58 +00:00
_spaceSuit.RemoveSuit(true);
}
2020-02-29 13:02:09 +00:00
private SpawnPoint GetSpawnPoint(bool isShip = false)
{
return _playerSpawner
.GetValue<SpawnPoint[]>("_spawnList")
.FirstOrDefault(spawnPoint => spawnPoint.GetSpawnLocation() == SpawnLocation.TimberHearth && spawnPoint.IsShipSpawn() == isShip);
}
}
}