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

144 lines
4.8 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;
2021-07-31 08:44:56 +00:00
using QSB.Events;
using QSB.Player;
2021-06-23 17:35:56 +00:00
using QSB.Player.TransformSync;
2021-11-25 15:32:34 +00:00
using QSB.RespawnSync;
using QSB.Utility;
2021-11-25 15:32:34 +00:00
using QSB.WorldSync;
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
};
private SpawnPoint _playerSpawnPoint;
private PlayerSpawner _playerSpawner;
private FluidDetector _fluidDetector;
private PlayerResources _playerResources;
private PlayerSpacesuit _spaceSuit;
private SuitPickupVolume[] _suitPickupVolumes;
2021-06-23 17:35:56 +00:00
private Vector3 _deathPositionRelative;
public Transform DeathClosestAstroObject { get; private set; }
2021-08-22 15:54:48 +00:00
public Vector3 DeathPositionWorld
=> DeathClosestAstroObject == null
? Vector3.zero
: DeathClosestAstroObject.TransformPoint(_deathPositionRelative);
2021-06-23 17:35:56 +00:00
public Vector3 DeathPlayerUpVector { get; private set; }
public Vector3 DeathPlayerForwardVector { get; private set; }
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>();
_suitPickupVolumes = FindObjectsOfType<SuitPickupVolume>();
2020-12-02 21:23:01 +00:00
_fluidDetector = Locator.GetPlayerCamera().GetComponentInChildren<FluidDetector>();
2021-02-06 21:31:12 +00:00
_playerSpawnPoint = GetSpawnPoint();
}
public void ResetPlayer()
{
2021-07-31 08:44:56 +00:00
DebugLog.DebugWrite($"RESET PLAYER");
2021-02-06 21:31:12 +00:00
if (_playerSpawnPoint == null)
{
DebugLog.ToConsole("Warning - _playerSpawnPoint is null!", MessageType.Warning);
Init();
}
2021-02-07 19:43:09 +00:00
2021-07-31 08:44:56 +00:00
var deadPlayersCount = QSBPlayerManager.PlayerList.Count(x => x.IsDead);
if (deadPlayersCount == QSBPlayerManager.PlayerList.Count)
{
QSBEventManager.FireEvent(EventNames.QSBEndLoop, EndLoopReason.AllPlayersDead);
return;
}
2021-06-23 11:06:08 +00:00
RespawnManager.Instance.TriggerRespawnMap();
2021-06-23 17:35:56 +00:00
var inSpace = PlayerTransformSync.LocalInstance.SectorSync.SectorList.Count == 0;
if (inSpace)
{
DeathClosestAstroObject = Locator.GetAstroObject(AstroObject.Name.Sun).transform;
}
else
{
var allAstroobjects = QSBWorldSync.GetUnityObjects<AstroObject>().Where(x => x.GetAstroObjectName() != AstroObject.Name.None && x.GetAstroObjectType() != AstroObject.Type.Satellite);
2021-06-23 17:35:56 +00:00
var ordered = allAstroobjects.OrderBy(x => Vector3.SqrMagnitude(x.transform.position));
DeathClosestAstroObject = ordered.First().transform;
}
var deathPosition = Locator.GetPlayerTransform().position;
_deathPositionRelative = DeathClosestAstroObject.InverseTransformPoint(deathPosition);
DeathPlayerUpVector = Locator.GetPlayerTransform().up;
DeathPlayerForwardVector = Locator.GetPlayerTransform().forward;
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();
2021-02-06 21:31:12 +00:00
_playerResources.SetValue("_isSuffocating", false);
_playerResources.DebugRefillResources();
_spaceSuit.RemoveSuit(true);
foreach (var pickupVolume in _suitPickupVolumes)
{
var containsSuit = pickupVolume.GetValue<bool>("_containsSuit");
var allowReturn = pickupVolume.GetValue<bool>("_allowSuitReturn");
if (!containsSuit && allowReturn)
{
var interactVolume = pickupVolume.GetValue<MultipleInteractionVolume>("_interactVolume");
var pickupSuitIndex = pickupVolume.GetValue<int>("_pickupSuitCommandIndex");
pickupVolume.SetValue("_containsSuit", true);
interactVolume.ChangePrompt(UITextType.SuitUpPrompt, pickupSuitIndex);
var suitGeometry = pickupVolume.GetValue<GameObject>("_suitGeometry");
var suitCollider = pickupVolume.GetValue<OWCollider>("_suitOWCollider");
var toolGeometries = pickupVolume.GetValue<GameObject[]>("_toolGeometry");
suitGeometry.SetActive(true);
suitCollider.SetActivation(true);
foreach (var geo in toolGeometries)
{
geo.SetActive(true);
}
}
}
2020-12-02 21:23:01 +00:00
}
2021-06-20 12:39:34 +00:00
private SpawnPoint GetSpawnPoint()
2021-03-31 14:43:55 +00:00
{
var spawnList = _playerSpawner.GetValue<SpawnPoint[]>("_spawnList");
if (spawnList == null)
{
DebugLog.ToConsole($"Warning - _spawnList was null for player spawner!", MessageType.Warning);
return null;
}
2021-06-18 21:38:32 +00:00
2021-03-31 14:43:55 +00:00
return spawnList.FirstOrDefault(spawnPoint =>
2021-02-09 09:11:35 +00:00
spawnPoint.GetSpawnLocation() == SpawnLocation.TimberHearth
2021-06-20 12:39:34 +00:00
&& spawnPoint.IsShipSpawn() == false);
2021-03-31 14:43:55 +00:00
}
2020-12-14 21:20:53 +00:00
}
}