LethalCompany/Lethal Company/ExportedProject/Assets/Scripts/Assembly-CSharp/Shovel.cs

355 lines
12 KiB
C#
Raw Normal View History

2023-12-23 00:55:14 +00:00
using System;
2023-12-22 22:51:17 +00:00
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using GameNetcodeStuff;
using Unity.Netcode;
using UnityEngine;
public class Shovel : GrabbableObject
{
public int shovelHitForce = 1;
public bool reelingUp;
public bool isHoldingButton;
private RaycastHit rayHit;
private Coroutine reelingUpCoroutine;
private RaycastHit[] objectsHitByShovel;
private List<RaycastHit> objectsHitByShovelList = new List<RaycastHit>();
public AudioClip reelUp;
public AudioClip swing;
public AudioClip[] hitSFX;
public AudioSource shovelAudio;
private PlayerControllerB previousPlayerHeldBy;
private int shovelMask = 11012424;
public override void ItemActivate(bool used, bool buttonDown = true)
{
if (playerHeldBy == null)
{
return;
}
Debug.Log($"Is player pressing down button?: {buttonDown}");
isHoldingButton = buttonDown;
Debug.Log("PLAYER ACTIVATED ITEM TO HIT WITH SHOVEL. Who sent this log: " + GameNetworkManager.Instance.localPlayerController.gameObject.name);
if (!reelingUp && buttonDown)
{
reelingUp = true;
previousPlayerHeldBy = playerHeldBy;
Debug.Log($"Set previousPlayerHeldBy: {previousPlayerHeldBy}");
if (reelingUpCoroutine != null)
{
StopCoroutine(reelingUpCoroutine);
}
reelingUpCoroutine = StartCoroutine(reelUpShovel());
}
}
private IEnumerator reelUpShovel()
{
playerHeldBy.activatingItem = true;
playerHeldBy.twoHanded = true;
playerHeldBy.playerBodyAnimator.ResetTrigger("shovelHit");
playerHeldBy.playerBodyAnimator.SetBool("reelingUp", value: true);
shovelAudio.PlayOneShot(reelUp);
ReelUpSFXServerRpc();
yield return new WaitForSeconds(0.35f);
yield return new WaitUntil(() => !isHoldingButton || !isHeld);
SwingShovel(!isHeld);
yield return new WaitForSeconds(0.13f);
2023-12-23 02:13:19 +00:00
yield return new WaitForEndOfFrame();
2023-12-22 22:51:17 +00:00
HitShovel(!isHeld);
yield return new WaitForSeconds(0.3f);
reelingUp = false;
reelingUpCoroutine = null;
}
[ServerRpc]
public void ReelUpSFXServerRpc()
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager == null || !networkManager.IsListening)
{
return;
}
if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost))
{
if (base.OwnerClientId != networkManager.LocalClientId)
{
if (networkManager.LogLevel <= LogLevel.Normal)
{
Debug.LogError("Only the owner can invoke a ServerRpc that requires ownership!");
}
return;
}
ServerRpcParams serverRpcParams = default(ServerRpcParams);
FastBufferWriter bufferWriter = __beginSendServerRpc(4113335123u, serverRpcParams, RpcDelivery.Reliable);
__endSendServerRpc(ref bufferWriter, 4113335123u, serverRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Server && (networkManager.IsServer || networkManager.IsHost))
{
ReelUpSFXClientRpc();
}
}
[ClientRpc]
public void ReelUpSFXClientRpc()
{
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(2042054613u, clientRpcParams, RpcDelivery.Reliable);
__endSendClientRpc(ref bufferWriter, 2042054613u, clientRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Client && (networkManager.IsClient || networkManager.IsHost) && !base.IsOwner)
{
shovelAudio.PlayOneShot(reelUp);
}
}
}
public override void DiscardItem()
{
2023-12-23 00:55:14 +00:00
if (playerHeldBy != null)
{
playerHeldBy.activatingItem = false;
}
2023-12-22 22:51:17 +00:00
base.DiscardItem();
}
public void SwingShovel(bool cancel = false)
{
previousPlayerHeldBy.playerBodyAnimator.SetBool("reelingUp", value: false);
if (!cancel)
{
shovelAudio.PlayOneShot(swing);
previousPlayerHeldBy.UpdateSpecialAnimationValue(specialAnimation: true, (short)previousPlayerHeldBy.transform.localEulerAngles.y, 0.4f);
}
}
public void HitShovel(bool cancel = false)
{
if (previousPlayerHeldBy == null)
{
Debug.LogError("Previousplayerheldby is null on this client when HitShovel is called.");
return;
}
previousPlayerHeldBy.activatingItem = false;
bool flag = false;
2023-12-23 02:13:19 +00:00
bool flag2 = false;
int num = -1;
2023-12-22 22:51:17 +00:00
if (!cancel)
{
previousPlayerHeldBy.twoHanded = false;
2023-12-23 02:13:19 +00:00
objectsHitByShovel = Physics.SphereCastAll(previousPlayerHeldBy.gameplayCamera.transform.position + previousPlayerHeldBy.gameplayCamera.transform.right * -0.35f, 0.8f, previousPlayerHeldBy.gameplayCamera.transform.forward, 1.4f, shovelMask, QueryTriggerInteraction.Collide);
2023-12-22 22:51:17 +00:00
objectsHitByShovelList = objectsHitByShovel.OrderBy((RaycastHit x) => x.distance).ToList();
for (int i = 0; i < objectsHitByShovelList.Count; i++)
{
IHittable component;
RaycastHit hitInfo;
if (objectsHitByShovelList[i].transform.gameObject.layer == 8 || objectsHitByShovelList[i].transform.gameObject.layer == 11)
{
flag = true;
string text = objectsHitByShovelList[i].collider.gameObject.tag;
for (int j = 0; j < StartOfRound.Instance.footstepSurfaces.Length; j++)
{
if (StartOfRound.Instance.footstepSurfaces[j].surfaceTag == text)
{
2023-12-23 02:13:19 +00:00
num = j;
2023-12-22 22:51:17 +00:00
break;
}
}
}
2023-12-23 02:13:19 +00:00
else if (objectsHitByShovelList[i].transform.TryGetComponent<IHittable>(out component) && !(objectsHitByShovelList[i].transform == previousPlayerHeldBy.transform) && (objectsHitByShovelList[i].point == Vector3.zero || !Physics.Linecast(previousPlayerHeldBy.gameplayCamera.transform.position, objectsHitByShovelList[i].point, out hitInfo, StartOfRound.Instance.collidersAndRoomMaskAndDefault)))
2023-12-22 22:51:17 +00:00
{
flag = true;
Vector3 forward = previousPlayerHeldBy.gameplayCamera.transform.forward;
2023-12-23 02:13:19 +00:00
Debug.DrawRay(objectsHitByShovelList[i].point, Vector3.up * 0.25f, Color.green, 5f);
2023-12-23 00:55:14 +00:00
try
{
component.Hit(shovelHitForce, forward, previousPlayerHeldBy, playHitSFX: true);
2023-12-23 02:13:19 +00:00
flag2 = true;
2023-12-23 00:55:14 +00:00
}
catch (Exception arg)
{
Debug.Log($"Exception caught when hitting object with shovel from player #{previousPlayerHeldBy.playerClientId}: {arg}");
}
2023-12-22 22:51:17 +00:00
}
}
}
if (flag)
{
RoundManager.PlayRandomClip(shovelAudio, hitSFX);
2023-12-23 00:55:14 +00:00
UnityEngine.Object.FindObjectOfType<RoundManager>().PlayAudibleNoise(base.transform.position, 17f, 0.8f);
2023-12-23 02:13:19 +00:00
if (!flag2 && num != -1)
{
shovelAudio.PlayOneShot(StartOfRound.Instance.footstepSurfaces[num].hitSurfaceSFX);
WalkieTalkie.TransmitOneShotAudio(shovelAudio, StartOfRound.Instance.footstepSurfaces[num].hitSurfaceSFX);
}
2023-12-22 22:51:17 +00:00
playerHeldBy.playerBodyAnimator.SetTrigger("shovelHit");
2023-12-23 02:13:19 +00:00
HitShovelServerRpc(num);
2023-12-22 22:51:17 +00:00
}
}
[ServerRpc]
public void HitShovelServerRpc(int hitSurfaceID)
{
NetworkManager networkManager = base.NetworkManager;
if ((object)networkManager == null || !networkManager.IsListening)
{
return;
}
if (__rpc_exec_stage != __RpcExecStage.Server && (networkManager.IsClient || networkManager.IsHost))
{
if (base.OwnerClientId != networkManager.LocalClientId)
{
if (networkManager.LogLevel <= LogLevel.Normal)
{
Debug.LogError("Only the owner can invoke a ServerRpc that requires ownership!");
}
return;
}
ServerRpcParams serverRpcParams = default(ServerRpcParams);
FastBufferWriter bufferWriter = __beginSendServerRpc(2096026133u, serverRpcParams, RpcDelivery.Reliable);
BytePacker.WriteValueBitPacked(bufferWriter, hitSurfaceID);
__endSendServerRpc(ref bufferWriter, 2096026133u, serverRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Server && (networkManager.IsServer || networkManager.IsHost))
{
HitShovelClientRpc(hitSurfaceID);
}
}
[ClientRpc]
public void HitShovelClientRpc(int hitSurfaceID)
{
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(275435223u, clientRpcParams, RpcDelivery.Reliable);
BytePacker.WriteValueBitPacked(bufferWriter, hitSurfaceID);
__endSendClientRpc(ref bufferWriter, 275435223u, clientRpcParams, RpcDelivery.Reliable);
}
if (__rpc_exec_stage == __RpcExecStage.Client && (networkManager.IsClient || networkManager.IsHost) && !base.IsOwner)
{
RoundManager.PlayRandomClip(shovelAudio, hitSFX);
if (hitSurfaceID != -1)
{
HitSurfaceWithShovel(hitSurfaceID);
}
}
}
private void HitSurfaceWithShovel(int hitSurfaceID)
{
shovelAudio.PlayOneShot(StartOfRound.Instance.footstepSurfaces[hitSurfaceID].hitSurfaceSFX);
WalkieTalkie.TransmitOneShotAudio(shovelAudio, StartOfRound.Instance.footstepSurfaces[hitSurfaceID].hitSurfaceSFX);
}
protected override void __initializeVariables()
{
base.__initializeVariables();
}
[RuntimeInitializeOnLoadMethod]
internal static void InitializeRPCS_Shovel()
{
NetworkManager.__rpc_func_table.Add(4113335123u, __rpc_handler_4113335123);
NetworkManager.__rpc_func_table.Add(2042054613u, __rpc_handler_2042054613);
NetworkManager.__rpc_func_table.Add(2096026133u, __rpc_handler_2096026133);
NetworkManager.__rpc_func_table.Add(275435223u, __rpc_handler_275435223);
}
private static void __rpc_handler_4113335123(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager == null || !networkManager.IsListening)
{
return;
}
if (rpcParams.Server.Receive.SenderClientId != target.OwnerClientId)
{
if (networkManager.LogLevel <= LogLevel.Normal)
{
Debug.LogError("Only the owner can invoke a ServerRpc that requires ownership!");
}
}
else
{
target.__rpc_exec_stage = __RpcExecStage.Server;
((Shovel)target).ReelUpSFXServerRpc();
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_2042054613(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
target.__rpc_exec_stage = __RpcExecStage.Client;
((Shovel)target).ReelUpSFXClientRpc();
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_2096026133(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager == null || !networkManager.IsListening)
{
return;
}
if (rpcParams.Server.Receive.SenderClientId != target.OwnerClientId)
{
if (networkManager.LogLevel <= LogLevel.Normal)
{
Debug.LogError("Only the owner can invoke a ServerRpc that requires ownership!");
}
}
else
{
ByteUnpacker.ReadValueBitPacked(reader, out int value);
target.__rpc_exec_stage = __RpcExecStage.Server;
((Shovel)target).HitShovelServerRpc(value);
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
private static void __rpc_handler_275435223(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
{
NetworkManager networkManager = target.NetworkManager;
if ((object)networkManager != null && networkManager.IsListening)
{
ByteUnpacker.ReadValueBitPacked(reader, out int value);
target.__rpc_exec_stage = __RpcExecStage.Client;
((Shovel)target).HitShovelClientRpc(value);
target.__rpc_exec_stage = __RpcExecStage.None;
}
}
protected internal override string __getTypeName()
{
return "Shovel";
}
}