2020-08-25 15:10:09 +01:00
using OWML.Common ;
2022-02-25 04:53:53 -08:00
using QSB.Player ;
2021-06-23 18:35:56 +01:00
using QSB.Player.TransformSync ;
2021-11-25 15:32:34 +00:00
using QSB.RespawnSync ;
2022-08-16 16:59:45 +01:00
using QSB.ShipSync ;
2020-08-23 21:01:09 +02:00
using QSB.Utility ;
2021-11-25 15:32:34 +00:00
using QSB.WorldSync ;
2020-08-25 15:10:09 +01:00
using System.Linq ;
2020-02-28 21:32:21 +01:00
using UnityEngine ;
2022-03-02 19:46:33 -08:00
namespace QSB.DeathSync ;
public class RespawnOnDeath : MonoBehaviour
2020-02-28 21:32:21 +01:00
{
2022-03-02 19:46:33 -08:00
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 ;
private Vector3 _deathPositionRelative ;
2022-08-16 16:59:45 +01:00
private GUIStyle _deadTextStyle ;
2022-03-02 19:46:33 -08:00
public Transform DeathClosestAstroObject { get ; private set ; }
public Vector3 DeathPositionWorld
= > DeathClosestAstroObject = = null
? Vector3 . zero
: DeathClosestAstroObject . TransformPoint ( _deathPositionRelative ) ;
public Vector3 DeathPlayerUpVector { get ; private set ; }
public Vector3 DeathPlayerForwardVector { get ; private set ; }
public void Awake ( ) = > Instance = this ;
public void Init ( )
2022-02-24 22:04:54 -08:00
{
2022-03-02 19:46:33 -08:00
DebugLog . DebugWrite ( $"INIT" ) ;
var playerTransform = Locator . GetPlayerTransform ( ) ;
_playerResources = playerTransform . GetComponent < PlayerResources > ( ) ;
_spaceSuit = Locator . GetPlayerSuit ( ) ;
_playerSpawner = FindObjectOfType < PlayerSpawner > ( ) ;
_suitPickupVolumes = FindObjectsOfType < SuitPickupVolume > ( ) ;
_fluidDetector = Locator . GetPlayerCamera ( ) . GetComponentInChildren < FluidDetector > ( ) ;
_playerSpawnPoint = GetSpawnPoint ( ) ;
2022-08-16 16:59:45 +01:00
_deadTextStyle = new ( ) ;
_deadTextStyle . font = ( Font ) Resources . Load ( @"fonts\english - latin\SpaceMono-Regular_Dynamic" ) ;
_deadTextStyle . alignment = TextAnchor . MiddleCenter ;
_deadTextStyle . normal . textColor = Color . white ;
_deadTextStyle . fontSize = 20 ;
2022-03-02 19:46:33 -08:00
}
2021-02-07 19:43:09 +00:00
2022-03-02 19:46:33 -08:00
public void ResetPlayer ( )
{
DebugLog . DebugWrite ( $"RESET PLAYER" ) ;
if ( _playerSpawnPoint = = null )
2022-02-27 04:40:44 -08:00
{
2022-03-02 19:46:33 -08:00
DebugLog . ToConsole ( "Warning - _playerSpawnPoint is null!" , MessageType . Warning ) ;
Init ( ) ;
}
2021-06-23 12:06:08 +01:00
2022-03-02 19:46:33 -08:00
RespawnManager . Instance . TriggerRespawnMap ( ) ;
2021-06-23 18:35:56 +01:00
2022-03-02 19:46:33 -08:00
var inSpace = PlayerTransformSync . LocalInstance . SectorDetector . SectorList . Count = = 0 ;
2021-06-23 18:35:56 +01:00
2022-03-02 19:46:33 -08:00
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 ) ;
var closest = allAstroobjects . MinBy ( x = > Vector3 . SqrMagnitude ( x . transform . position ) ) ;
DeathClosestAstroObject = closest . transform ;
}
2021-06-23 18:35:56 +01:00
2022-03-02 19:46:33 -08:00
var deathPosition = Locator . GetPlayerTransform ( ) . position ;
_deathPositionRelative = DeathClosestAstroObject . InverseTransformPoint ( deathPosition ) ;
DeathPlayerUpVector = Locator . GetPlayerTransform ( ) . up ;
DeathPlayerForwardVector = Locator . GetPlayerTransform ( ) . forward ;
2021-02-06 21:31:12 +00:00
2022-03-02 19:46:33 -08:00
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-05-18 15:10:38 +01:00
2022-03-02 19:46:33 -08:00
_playerResources . _isSuffocating = false ;
_playerResources . DebugRefillResources ( ) ;
2022-06-07 16:41:49 -07:00
// death by oxygen turns this off, so we gotta enable it again
Delay . RunNextFrame ( ( ) = > _playerResources . enabled = true ) ;
2022-03-02 19:46:33 -08:00
_spaceSuit . RemoveSuit ( true ) ;
2022-02-27 04:40:44 -08:00
2022-03-02 19:46:33 -08:00
foreach ( var pickupVolume in _suitPickupVolumes )
{
if ( ! pickupVolume . _containsSuit & & pickupVolume . _allowSuitReturn )
2021-05-18 15:10:38 +01:00
{
2022-03-02 19:46:33 -08:00
pickupVolume . _containsSuit = true ;
pickupVolume . _interactVolume . ChangePrompt ( UITextType . SuitUpPrompt , pickupVolume . _pickupSuitCommandIndex ) ;
pickupVolume . _suitGeometry . SetActive ( true ) ;
pickupVolume . _suitOWCollider . SetActivation ( true ) ;
foreach ( var geo in pickupVolume . _toolGeometry )
2021-05-18 15:10:38 +01:00
{
2022-03-02 19:46:33 -08:00
geo . SetActive ( true ) ;
2021-05-18 15:10:38 +01:00
}
}
2022-02-27 04:40:44 -08:00
}
2020-12-02 21:23:01 +00:00
2022-03-02 19:46:33 -08:00
QSBPlayerManager . LocalPlayer . LocalFlashlight . TurnOff ( false ) ;
}
2022-02-24 22:04:54 -08:00
2022-03-02 19:46:33 -08:00
private SpawnPoint GetSpawnPoint ( )
{
var spawnList = _playerSpawner . _spawnList ;
if ( spawnList = = null )
{
DebugLog . ToConsole ( $"Warning - _spawnList was null for player spawner!" , MessageType . Warning ) ;
return null ;
2022-02-27 04:40:44 -08:00
}
2022-03-02 19:46:33 -08:00
return spawnList . FirstOrDefault ( spawnPoint = >
spawnPoint . GetSpawnLocation ( ) = = SpawnLocation . TimberHearth
& & spawnPoint . IsShipSpawn ( ) = = false ) ;
2020-12-14 21:20:53 +00:00
}
2022-08-16 16:59:45 +01:00
void OnGUI ( )
{
if ( PlayerTransformSync . LocalInstance = = null | | ShipManager . Instance . ShipCockpitUI = = null )
{
return ;
}
if ( QSBPlayerManager . LocalPlayer . IsDead )
{
GUI . contentColor = Color . white ;
var width = 200 ;
var height = 100 ;
// it is good day to be not dead
if ( ShipManager . Instance . IsShipWrecked )
{
GUI . Label ( new Rect ( ( Screen . width / 2 ) - ( width / 2 ) , ( Screen . height / 2 ) - ( height / 2 ) + ( height * 2 ) , width , height ) , $"You are dead.\nWaiting for {QSBPlayerManager.PlayerList.Count(x => !x.IsDead)} player(s) to die..." , _deadTextStyle ) ;
}
else
{
GUI . Label ( new Rect ( ( Screen . width / 2 ) - ( width / 2 ) , ( Screen . height / 2 ) - ( height / 2 ) + ( height * 2 ) , width , height ) , "You are dead.\nWaiting for someone to respawn you..." , _deadTextStyle ) ;
}
}
}
2022-02-24 22:04:54 -08:00
}