LethalCompany/Lethal Company/ExportedProject/Assets/Scripts/Assembly-CSharp/ShipTeleporter.cs
2023-12-22 19:55:14 -05:00

620 lines
21 KiB
C#

using System.Collections;
using GameNetcodeStuff;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ShipTeleporter : NetworkBehaviour
{
public bool isInverseTeleporter;
public Transform teleportOutPosition;
[Space(5f)]
public Transform teleporterPosition;
public Animator teleporterAnimator;
public Animator buttonAnimator;
public AudioSource buttonAudio;
public AudioSource shipTeleporterAudio;
public AudioClip buttonPressSFX;
public AudioClip teleporterSpinSFX;
public AudioClip teleporterBeamUpSFX;
public AudioClip beamUpPlayerBodySFX;
private Coroutine beamUpPlayerCoroutine;
public int teleporterId = 1;
private int[] playersBeingTeleported;
private float cooldownTime;
public float cooldownAmount;
public InteractTrigger buttonTrigger;
private void Awake()
{
playersBeingTeleported = new int[4] { -1, -1, -1, -1 };
buttonTrigger.interactable = false;
cooldownTime = cooldownAmount;
}
private void Update()
{
if (!buttonTrigger.interactable)
{
if (cooldownTime <= 0f)
{
buttonTrigger.interactable = true;
return;
}
buttonTrigger.disabledHoverTip = $"[Cooldown: {(int)cooldownTime} sec.]";
cooldownTime -= Time.deltaTime;
}
}
private void OnDisable()
{
for (int i = 0; i < playersBeingTeleported.Length; i++)
{
if (playersBeingTeleported[i] == teleporterId)
{
StartOfRound.Instance.allPlayerScripts[playersBeingTeleported[i]].shipTeleporterId = -1;
}
}
}
public void PressTeleportButtonOnLocalClient()
{
if (!isInverseTeleporter || (!StartOfRound.Instance.inShipPhase && SceneManager.sceneCount > 1))
{
PressTeleportButtonServerRpc();
}
}
[ServerRpc(RequireOwnership = false)]
public void PressTeleportButtonServerRpc()
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost))
{
ServerRpcParams serverRpcParams = default(ServerRpcParams);
FastBufferWriter bufferWriter = __beginSendServerRpc(389447712u, serverRpcParams, RpcDelivery.Reliable);
__endSendServerRpc(ref bufferWriter, 389447712u, serverRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Server && (networkManager.IsServer || networkManager.IsHost))
{
PressTeleportButtonClientRpc();
}
}
}
[ClientRpc]
public void PressTeleportButtonClientRpc()
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager == null || !networkManager.IsListening)
{
return;
}
if (__rpc_exec_stage != __RpcExecStage.Client && (networkManager.IsServer || networkManager.IsHost))
{
ClientRpcParams clientRpcParams = default(ClientRpcParams);
FastBufferWriter bufferWriter = __beginSendClientRpc(2773756087u, clientRpcParams, RpcDelivery.Reliable);
__endSendClientRpc(ref bufferWriter, 2773756087u, clientRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage != __RpcExecStage.Client || (!networkManager.IsClient && !networkManager.IsHost))
{
return;
}
PressButtonEffects();
if (beamUpPlayerCoroutine != null)
{
StopCoroutine(beamUpPlayerCoroutine);
}
cooldownTime = cooldownAmount;
buttonTrigger.interactable = false;
if (isInverseTeleporter)
{
if (CanUseInverseTeleporter())
{
beamUpPlayerCoroutine = StartCoroutine(beamOutPlayer());
}
}
else
{
beamUpPlayerCoroutine = StartCoroutine(beamUpPlayer());
}
}
private void PressButtonEffects()
{
buttonAnimator.SetTrigger("press");
buttonAnimator.SetBool("GlassOpen", value: false);
buttonAnimator.GetComponentInChildren<AnimatedObjectTrigger>().boolValue = false;
if (isInverseTeleporter)
{
if (CanUseInverseTeleporter())
{
teleporterAnimator.SetTrigger("useInverseTeleporter");
}
else
{
Debug.Log($"Using inverse teleporter was not allowed; {StartOfRound.Instance.inShipPhase}; {StartOfRound.Instance.currentLevel.PlanetName}");
}
}
else
{
teleporterAnimator.SetTrigger("useTeleporter");
}
buttonAudio.PlayOneShot(buttonPressSFX);
WalkieTalkie.TransmitOneShotAudio(buttonAudio, buttonPressSFX);
}
private bool CanUseInverseTeleporter()
{
if (!StartOfRound.Instance.inShipPhase)
{
return StartOfRound.Instance.currentLevel.spawnEnemiesAndScrap;
}
return false;
}
private IEnumerator beamOutPlayer()
{
if (GameNetworkManager.Instance.localPlayerController == null)
{
yield break;
}
if (StartOfRound.Instance.inShipPhase)
{
Debug.Log("Attempted using teleporter while in ship phase");
yield break;
}
shipTeleporterAudio.PlayOneShot(teleporterSpinSFX);
for (int b = 0; b < 5; b++)
{
for (int i = 0; i < StartOfRound.Instance.allPlayerObjects.Length; i++)
{
PlayerControllerB playerControllerB = StartOfRound.Instance.allPlayerScripts[i];
Vector3 position = playerControllerB.transform.position;
if (playerControllerB.deadBody != null)
{
position = playerControllerB.deadBody.bodyParts[5].transform.position;
}
if (Vector3.Distance(position, teleportOutPosition.position) > 2f)
{
if (playerControllerB.shipTeleporterId != 1)
{
if (playerControllerB.deadBody != null)
{
playerControllerB.deadBody.beamOutParticle.Stop();
playerControllerB.deadBody.bodyAudio.Stop();
}
else
{
playerControllerB.beamOutBuildupParticle.Stop();
playerControllerB.movementAudio.Stop();
}
}
continue;
}
if (playerControllerB.shipTeleporterId == 1)
{
Debug.Log($"Cancelled teleporting #{playerControllerB.playerClientId} with inverse teleporter; {playerControllerB.shipTeleporterId}");
continue;
}
SetPlayerTeleporterId(playerControllerB, 2);
if (playerControllerB.deadBody != null)
{
if (playerControllerB.deadBody.beamUpParticle == null)
{
yield break;
}
if (!playerControllerB.deadBody.beamOutParticle.isPlaying)
{
playerControllerB.deadBody.beamOutParticle.Play();
playerControllerB.deadBody.bodyAudio.PlayOneShot(beamUpPlayerBodySFX);
}
}
else if (!playerControllerB.beamOutBuildupParticle.isPlaying)
{
playerControllerB.beamOutBuildupParticle.Play();
playerControllerB.movementAudio.PlayOneShot(beamUpPlayerBodySFX);
}
}
yield return new WaitForSeconds(1f);
}
for (int j = 0; j < StartOfRound.Instance.allPlayerObjects.Length; j++)
{
PlayerControllerB playerControllerB = StartOfRound.Instance.allPlayerScripts[j];
if (playerControllerB.shipTeleporterId == 1)
{
Debug.Log($"Player #{playerControllerB.playerClientId} is in teleport 1, skipping");
continue;
}
SetPlayerTeleporterId(playerControllerB, -1);
if (playerControllerB.deadBody != null)
{
playerControllerB.deadBody.beamOutParticle.Stop();
playerControllerB.deadBody.bodyAudio.Stop();
}
else
{
playerControllerB.beamOutBuildupParticle.Stop();
playerControllerB.movementAudio.Stop();
}
if (playerControllerB != GameNetworkManager.Instance.localPlayerController || StartOfRound.Instance.inShipPhase)
{
continue;
}
Vector3 position2 = playerControllerB.transform.position;
if (playerControllerB.deadBody != null)
{
position2 = playerControllerB.deadBody.bodyParts[5].transform.position;
}
if (Vector3.Distance(position2, teleportOutPosition.position) < 2f)
{
if (RoundManager.Instance.insideAINodes.Length != 0)
{
Vector3 position3 = RoundManager.Instance.insideAINodes[Random.Range(0, RoundManager.Instance.insideAINodes.Length)].transform.position;
Debug.DrawRay(position3, Vector3.up * 1f, Color.red);
position3 = RoundManager.Instance.GetRandomNavMeshPositionInRadiusSpherical(position3);
Debug.DrawRay(position3 + Vector3.right * 0.01f, Vector3.up * 3f, Color.green);
SetPlayerTeleporterId(playerControllerB, 2);
if (playerControllerB.deadBody != null)
{
TeleportPlayerBodyOutServerRpc((int)playerControllerB.playerClientId, position3);
continue;
}
TeleportPlayerOutWithInverseTeleporter((int)playerControllerB.playerClientId, position3);
TeleportPlayerOutServerRpc((int)playerControllerB.playerClientId, position3);
}
}
else
{
Debug.Log($"Player #{playerControllerB.playerClientId} is not close enough to teleporter to beam out");
}
}
}
[ServerRpc(RequireOwnership = false)]
public void TeleportPlayerOutServerRpc(int playerObj, Vector3 teleportPos)
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost))
{
ServerRpcParams serverRpcParams = default(ServerRpcParams);
FastBufferWriter bufferWriter = __beginSendServerRpc(3033548568u, serverRpcParams, RpcDelivery.Reliable);
BytePacker.WriteValueBitPacked(bufferWriter, playerObj);
bufferWriter.WriteValueSafe(in teleportPos);
__endSendServerRpc(ref bufferWriter, 3033548568u, serverRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Server && (networkManager.IsServer || networkManager.IsHost))
{
TeleportPlayerOutClientRpc(playerObj, teleportPos);
}
}
}
[ClientRpc]
public void TeleportPlayerOutClientRpc(int playerObj, Vector3 teleportPos)
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
if (__rpc_exec_stage != __RpcExecStage.Client && (networkManager.IsServer || networkManager.IsHost))
{
ClientRpcParams clientRpcParams = default(ClientRpcParams);
FastBufferWriter bufferWriter = __beginSendClientRpc(3527914562u, clientRpcParams, RpcDelivery.Reliable);
BytePacker.WriteValueBitPacked(bufferWriter, playerObj);
bufferWriter.WriteValueSafe(in teleportPos);
__endSendClientRpc(ref bufferWriter, 3527914562u, clientRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Client && (networkManager.IsClient || networkManager.IsHost) && !StartOfRound.Instance.inShipPhase && !StartOfRound.Instance.allPlayerScripts[playerObj].IsOwner)
{
TeleportPlayerOutWithInverseTeleporter(playerObj, teleportPos);
}
}
}
private void TeleportPlayerOutWithInverseTeleporter(int playerObj, Vector3 teleportPos)
{
if (StartOfRound.Instance.allPlayerScripts[playerObj].isPlayerDead)
{
StartCoroutine(teleportBodyOut(playerObj, teleportPos));
return;
}
PlayerControllerB playerControllerB = StartOfRound.Instance.allPlayerScripts[playerObj];
SetPlayerTeleporterId(playerControllerB, -1);
playerControllerB.DropAllHeldItems();
if ((bool)Object.FindObjectOfType<AudioReverbPresets>())
{
Object.FindObjectOfType<AudioReverbPresets>().audioPresets[2].ChangeAudioReverbForPlayer(playerControllerB);
}
playerControllerB.isInElevator = false;
playerControllerB.isInHangarShipRoom = false;
playerControllerB.isInsideFactory = true;
playerControllerB.averageVelocity = 0f;
playerControllerB.velocityLastFrame = Vector3.zero;
StartOfRound.Instance.allPlayerScripts[playerObj].TeleportPlayer(teleportPos);
StartOfRound.Instance.allPlayerScripts[playerObj].beamOutParticle.Play();
shipTeleporterAudio.PlayOneShot(teleporterBeamUpSFX);
StartOfRound.Instance.allPlayerScripts[playerObj].movementAudio.PlayOneShot(teleporterBeamUpSFX);
if (playerControllerB == GameNetworkManager.Instance.localPlayerController)
{
Debug.Log("Teleporter shaking camera");
HUDManager.Instance.ShakeCamera(ScreenShakeType.Big);
}
}
[ServerRpc(RequireOwnership = false)]
public void TeleportPlayerBodyOutServerRpc(int playerObj, Vector3 teleportPos)
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost))
{
ServerRpcParams serverRpcParams = default(ServerRpcParams);
FastBufferWriter bufferWriter = __beginSendServerRpc(660932683u, serverRpcParams, RpcDelivery.Reliable);
BytePacker.WriteValueBitPacked(bufferWriter, playerObj);
bufferWriter.WriteValueSafe(in teleportPos);
__endSendServerRpc(ref bufferWriter, 660932683u, serverRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Server && (networkManager.IsServer || networkManager.IsHost))
{
TeleportPlayerBodyOutClientRpc(playerObj, teleportPos);
}
}
}
[ClientRpc]
public void TeleportPlayerBodyOutClientRpc(int playerObj, Vector3 teleportPos)
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
if (__rpc_exec_stage != __RpcExecStage.Client && (networkManager.IsServer || networkManager.IsHost))
{
ClientRpcParams clientRpcParams = default(ClientRpcParams);
FastBufferWriter bufferWriter = __beginSendClientRpc(1544539621u, clientRpcParams, RpcDelivery.Reliable);
BytePacker.WriteValueBitPacked(bufferWriter, playerObj);
bufferWriter.WriteValueSafe(in teleportPos);
__endSendClientRpc(ref bufferWriter, 1544539621u, clientRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Client && (networkManager.IsClient || networkManager.IsHost))
{
StartCoroutine(teleportBodyOut(playerObj, teleportPos));
}
}
}
private IEnumerator teleportBodyOut(int playerObj, Vector3 teleportPosition)
{
float startTime = Time.realtimeSinceStartup;
yield return new WaitUntil(() => StartOfRound.Instance.allPlayerScripts[playerObj].deadBody != null || Time.realtimeSinceStartup - startTime > 2f);
if (StartOfRound.Instance.inShipPhase || SceneManager.sceneCount <= 1)
{
yield break;
}
DeadBodyInfo deadBody = StartOfRound.Instance.allPlayerScripts[playerObj].deadBody;
SetPlayerTeleporterId(StartOfRound.Instance.allPlayerScripts[playerObj], -1);
if (deadBody != null)
{
deadBody.attachedTo = null;
deadBody.attachedLimb = null;
deadBody.secondaryAttachedLimb = null;
deadBody.secondaryAttachedTo = null;
if (deadBody.grabBodyObject != null && deadBody.grabBodyObject.isHeld && deadBody.grabBodyObject.playerHeldBy != null)
{
deadBody.grabBodyObject.playerHeldBy.DropAllHeldItems();
}
deadBody.isInShip = false;
deadBody.parentedToShip = false;
deadBody.transform.SetParent(null, worldPositionStays: true);
deadBody.SetRagdollPositionSafely(teleportPosition, disableSpecialEffects: true);
}
}
private IEnumerator beamUpPlayer()
{
shipTeleporterAudio.PlayOneShot(teleporterSpinSFX);
PlayerControllerB playerToBeamUp = StartOfRound.Instance.mapScreen.targetedPlayer;
if (playerToBeamUp == null)
{
Debug.Log("Targeted player is null");
yield break;
}
if (playerToBeamUp.redirectToEnemy != null)
{
Debug.Log($"Attemping to teleport enemy '{playerToBeamUp.redirectToEnemy.gameObject.name}' (tied to player #{playerToBeamUp.playerClientId}) to ship.");
if (StartOfRound.Instance.shipIsLeaving)
{
Debug.Log($"Ship could not teleport enemy '{playerToBeamUp.redirectToEnemy.gameObject.name}' (tied to player #{playerToBeamUp.playerClientId}) because the ship is leaving the nav mesh.");
}
playerToBeamUp.redirectToEnemy.ShipTeleportEnemy();
yield return new WaitForSeconds(3f);
shipTeleporterAudio.PlayOneShot(teleporterBeamUpSFX);
if (GameNetworkManager.Instance.localPlayerController.isInHangarShipRoom)
{
HUDManager.Instance.ShakeCamera(ScreenShakeType.Big);
}
}
SetPlayerTeleporterId(playerToBeamUp, 1);
if (playerToBeamUp.deadBody != null)
{
if (playerToBeamUp.deadBody.beamUpParticle == null)
{
yield break;
}
playerToBeamUp.deadBody.beamUpParticle.Play();
playerToBeamUp.deadBody.bodyAudio.PlayOneShot(beamUpPlayerBodySFX);
}
else
{
playerToBeamUp.beamUpParticle.Play();
playerToBeamUp.movementAudio.PlayOneShot(beamUpPlayerBodySFX);
}
Debug.Log("Teleport A");
yield return new WaitForSeconds(3f);
bool flag = false;
if (playerToBeamUp.deadBody != null)
{
if (playerToBeamUp.deadBody.grabBodyObject == null || !playerToBeamUp.deadBody.grabBodyObject.isHeldByEnemy)
{
flag = true;
playerToBeamUp.deadBody.attachedTo = null;
playerToBeamUp.deadBody.attachedLimb = null;
playerToBeamUp.deadBody.secondaryAttachedLimb = null;
playerToBeamUp.deadBody.secondaryAttachedTo = null;
playerToBeamUp.deadBody.SetRagdollPositionSafely(teleporterPosition.position, disableSpecialEffects: true);
playerToBeamUp.deadBody.transform.SetParent(StartOfRound.Instance.elevatorTransform, worldPositionStays: true);
if (playerToBeamUp.deadBody.grabBodyObject != null && playerToBeamUp.deadBody.grabBodyObject.isHeld && playerToBeamUp.deadBody.grabBodyObject.playerHeldBy != null)
{
playerToBeamUp.deadBody.grabBodyObject.playerHeldBy.DropAllHeldItems();
}
}
}
else
{
flag = true;
playerToBeamUp.DropAllHeldItems();
if ((bool)Object.FindObjectOfType<AudioReverbPresets>())
{
Object.FindObjectOfType<AudioReverbPresets>().audioPresets[3].ChangeAudioReverbForPlayer(playerToBeamUp);
}
playerToBeamUp.isInElevator = true;
playerToBeamUp.isInHangarShipRoom = true;
playerToBeamUp.isInsideFactory = false;
playerToBeamUp.averageVelocity = 0f;
playerToBeamUp.velocityLastFrame = Vector3.zero;
playerToBeamUp.TeleportPlayer(teleporterPosition.position, withRotation: true, 160f);
}
Debug.Log("Teleport B");
SetPlayerTeleporterId(playerToBeamUp, -1);
if (flag)
{
shipTeleporterAudio.PlayOneShot(teleporterBeamUpSFX);
if (GameNetworkManager.Instance.localPlayerController.isInHangarShipRoom)
{
HUDManager.Instance.ShakeCamera(ScreenShakeType.Big);
}
}
Debug.Log("Teleport C");
}
private void SetPlayerTeleporterId(PlayerControllerB playerScript, int teleporterId)
{
playerScript.shipTeleporterId = teleporterId;
playersBeingTeleported[playerScript.playerClientId] = (int)playerScript.playerClientId;
}
protected override void __initializeVariables()
{
base.__initializeVariables();
}
[RuntimeInitializeOnLoadMethod]
internal static void InitializeRPCS_ShipTeleporter()
{
NetworkManager.__rpc_func_table.Add(389447712u, __rpc_handler_389447712);
NetworkManager.__rpc_func_table.Add(2773756087u, __rpc_handler_2773756087);
NetworkManager.__rpc_func_table.Add(3033548568u, __rpc_handler_3033548568);
NetworkManager.__rpc_func_table.Add(3527914562u, __rpc_handler_3527914562);
NetworkManager.__rpc_func_table.Add(660932683u, __rpc_handler_660932683);
NetworkManager.__rpc_func_table.Add(1544539621u, __rpc_handler_1544539621);
}
private static void __rpc_handler_389447712(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
target.__rpc_exec_stage = __RpcExecStage.Server;
((ShipTeleporter)target).PressTeleportButtonServerRpc();
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_2773756087(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
target.__rpc_exec_stage = __RpcExecStage.Client;
((ShipTeleporter)target).PressTeleportButtonClientRpc();
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_3033548568(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
ByteUnpacker.ReadValueBitPacked(reader, out int value);
reader.ReadValueSafe(out Vector3 value2);
target.__rpc_exec_stage = __RpcExecStage.Server;
((ShipTeleporter)target).TeleportPlayerOutServerRpc(value, value2);
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_3527914562(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
ByteUnpacker.ReadValueBitPacked(reader, out int value);
reader.ReadValueSafe(out Vector3 value2);
target.__rpc_exec_stage = __RpcExecStage.Client;
((ShipTeleporter)target).TeleportPlayerOutClientRpc(value, value2);
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_660932683(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
ByteUnpacker.ReadValueBitPacked(reader, out int value);
reader.ReadValueSafe(out Vector3 value2);
target.__rpc_exec_stage = __RpcExecStage.Server;
((ShipTeleporter)target).TeleportPlayerBodyOutServerRpc(value, value2);
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_1544539621(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
ByteUnpacker.ReadValueBitPacked(reader, out int value);
reader.ReadValueSafe(out Vector3 value2);
target.__rpc_exec_stage = __RpcExecStage.Client;
((ShipTeleporter)target).TeleportPlayerBodyOutClientRpc(value, value2);
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
protected internal override string __getTypeName()
{
return "ShipTeleporter";
}
}