2020-08-25 14:10:09 +00:00
|
|
|
|
using OWML.Common;
|
|
|
|
|
using OWML.ModHelper.Events;
|
2020-12-14 16:24:52 +00:00
|
|
|
|
using QSB.Events;
|
2020-08-23 19:01:09 +00:00
|
|
|
|
using QSB.Utility;
|
2020-08-25 14:10:09 +00:00
|
|
|
|
using System.Linq;
|
2020-02-28 20:32:21 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
|
2020-08-13 19:46:16 +00:00
|
|
|
|
namespace QSB.DeathSync
|
2020-02-28 20:32:21 +00:00
|
|
|
|
{
|
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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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-12-14 20:41:56 +00:00
|
|
|
|
public void Awake()
|
2020-12-02 21:23:01 +00:00
|
|
|
|
{
|
|
|
|
|
Instance = this;
|
|
|
|
|
|
2020-12-14 16:24:52 +00:00
|
|
|
|
QSBCore.Helper.Events.Subscribe<PlayerResources>(OWML.Common.Events.AfterStart);
|
|
|
|
|
QSBCore.Helper.Events.Event += OnEvent;
|
2020-12-02 21:23:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnEvent(MonoBehaviour behaviour, OWML.Common.Events ev)
|
|
|
|
|
{
|
|
|
|
|
if (behaviour is PlayerResources && ev == OWML.Common.Events.AfterStart)
|
|
|
|
|
{
|
|
|
|
|
Init();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
{
|
|
|
|
|
var playerTransform = Locator.GetPlayerTransform();
|
|
|
|
|
_playerResources = playerTransform.GetComponent<PlayerResources>();
|
|
|
|
|
_spaceSuit = playerTransform.GetComponentInChildren<PlayerSpacesuit>(true);
|
|
|
|
|
_playerSpawner = FindObjectOfType<PlayerSpawner>();
|
|
|
|
|
_fluidDetector = Locator.GetPlayerCamera().GetComponentInChildren<FluidDetector>();
|
|
|
|
|
|
|
|
|
|
var shipTransform = Locator.GetShipTransform();
|
|
|
|
|
if (shipTransform == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_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()
|
|
|
|
|
{
|
|
|
|
|
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())
|
|
|
|
|
{
|
|
|
|
|
foreach (var shipComponent in _shipComponents)
|
|
|
|
|
{
|
|
|
|
|
shipComponent.SetDamaged(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invoke(nameof(ExitShip), 0.01f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ExitShip()
|
|
|
|
|
{
|
|
|
|
|
_cockpitController.Invoke("ExitFlightConsole");
|
|
|
|
|
_cockpitController.Invoke("CompleteExitFlightConsole");
|
|
|
|
|
_hatchController.SetValue("_isPlayerInShip", false);
|
|
|
|
|
_hatchController.Invoke("OpenHatch");
|
|
|
|
|
GlobalMessenger.FireEvent(EventNames.ExitShip);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ResetPlayer()
|
|
|
|
|
{
|
|
|
|
|
// Reset player position.
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// Stop suffocation sound effect.
|
|
|
|
|
_playerResources.SetValue("_isSuffocating", false);
|
|
|
|
|
|
|
|
|
|
// Reset player health and resources.
|
|
|
|
|
_playerResources.DebugRefillResources();
|
|
|
|
|
|
|
|
|
|
// Remove space suit.
|
|
|
|
|
_spaceSuit.RemoveSuit(true);
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 20:41:56 +00:00
|
|
|
|
private SpawnPoint GetSpawnPoint(bool isShip = false) =>
|
|
|
|
|
_playerSpawner
|
|
|
|
|
.GetValue<SpawnPoint[]>("_spawnList")
|
|
|
|
|
.FirstOrDefault(spawnPoint => spawnPoint.GetSpawnLocation() == SpawnLocation.TimberHearth && spawnPoint.IsShipSpawn() == isShip);
|
|
|
|
|
}
|
2020-12-03 08:28:05 +00:00
|
|
|
|
}
|