Files
Ultimate-Fishing-Simulator-…/Assets/Scripts/Assembly-CSharp/MultiplayerBoatController.cs
2026-03-04 09:37:33 +08:00

345 lines
9.5 KiB
C#

using System;
using System.Collections;
using FishNet.Connection;
using FishNet.Managing;
using FishNet.Object;
using FishNet.Object.Synchronizing;
using FishNet.Serializing;
using FishNet.Transporting;
using Obvious.Soap;
using UnityEngine;
public class MultiplayerBoatController : NetworkBehaviour
{
[SerializeField]
private Transform model;
[SerializeField]
private CapsuleCollider collisionCollider;
[SerializeField]
private BoxCollider overlapCollider;
[SerializeField]
private ScriptableEventGameObject OnTakeBoatEvent;
[SerializeField]
private LayerMask collisionDetectLayer;
[SerializeField]
private Transform boatLights;
[SerializeField]
private Transform propeller;
[SerializeField]
private AudioSource audioSource;
private BoatController boatController;
private Vector3 lastPosition;
private Vector3 velocity;
private Transform boatLightsLocal;
public readonly SyncVar<bool> isLightsOn = new SyncVar<bool>();
private bool NetworkInitialize___EarlyMultiplayerBoatControllerAssembly_002DCSharp_002Edll_Excuted;
private bool NetworkInitialize___LateMultiplayerBoatControllerAssembly_002DCSharp_002Edll_Excuted;
public static event Action<MultiplayerBoatController> OnStartLocal;
public static event Action<MultiplayerBoatController> OnStartRemote;
public static event Action<MultiplayerBoatController> OnStopLocal;
public static event Action<MultiplayerBoatController> OnStopRemote;
public override void OnStartClient()
{
base.OnStartClient();
if (base.IsOwner)
{
MultiplayerBoatController.OnStartLocal?.Invoke(this);
return;
}
OnTakeBoatEvent.OnRaised += OnTakeBoatEventNoParam_OnRaised;
StartCoroutine(DisableCollisionCoroutine());
MultiplayerBoatController.OnStartRemote?.Invoke(this);
}
public override void OnStopClient()
{
base.OnStopClient();
if (base.IsOwner)
{
MultiplayerBoatController.OnStopLocal?.Invoke(this);
return;
}
OnTakeBoatEvent.OnRaised -= OnTakeBoatEventNoParam_OnRaised;
MultiplayerBoatController.OnStopRemote?.Invoke(this);
}
private void OnTakeBoatEventNoParam_OnRaised(GameObject boat)
{
if (!base.IsOwner)
{
StopAllCoroutines();
StartCoroutine(DisableCollisionCoroutine());
}
}
public void Init(BoatController boatController)
{
if (base.IsOwner)
{
this.boatController = boatController;
model.gameObject.SetActive(value: false);
collisionCollider.gameObject.SetActive(value: false);
overlapCollider.gameObject.SetActive(value: false);
boatLightsLocal = boatController.transform.Find("boat lights root");
}
}
public void Deinit()
{
if (base.IsOwner)
{
DespawnRPC();
}
}
private void FixedUpdate()
{
DetectLargePerStepMovement();
}
private void Update()
{
if (base.IsOwner)
{
UpdateLocal();
}
else
{
UpdateRemote();
}
}
private void UpdateLocal()
{
if (!(boatController == null))
{
base.transform.SetPositionAndRotation(boatController.transform.position, boatController.transform.rotation);
bool flag = (isLightsOn.Value = boatLightsLocal != null && boatLightsLocal.gameObject.activeSelf);
UpdateSyncVars(flag);
}
}
private void UpdateRemote()
{
model.gameObject.SetActive(value: true);
boatLights.gameObject.SetActive(isLightsOn.Value);
float num = 800f;
propeller.Rotate(Vector3.forward, num * Time.deltaTime, Space.Self);
UpdateRemoteSounds();
}
private void UpdateRemoteSounds()
{
audioSource.gameObject.SetActive(value: true);
float magnitude = velocity.magnitude;
float num = 10f;
float a = 0.5f;
float b = 1f;
float t = Mathf.Clamp01(magnitude / num);
audioSource.pitch = Mathf.Lerp(a, b, t);
audioSource.volume = Mathf.Lerp(0.5f, 1f, t);
}
private bool IsColliderOverlapping()
{
Vector3 center = overlapCollider.transform.position + overlapCollider.center;
Vector3 halfExtents = overlapCollider.size * 0.5f;
Quaternion rotation = overlapCollider.transform.rotation;
return Physics.OverlapBox(center, halfExtents, rotation, collisionDetectLayer, QueryTriggerInteraction.Ignore).Length != 0;
}
private void DrawBoxDebugLines(Vector3 center, Vector3 halfExtents, Quaternion rotation, Color color)
{
Vector3[] array = new Vector3[8];
Vector3 vector = rotation * Vector3.right * halfExtents.x;
Vector3 vector2 = rotation * Vector3.up * halfExtents.y;
Vector3 vector3 = rotation * Vector3.forward * halfExtents.z;
array[0] = center + vector + vector2 + vector3;
array[1] = center + vector + vector2 - vector3;
array[2] = center + vector - vector2 + vector3;
array[3] = center + vector - vector2 - vector3;
array[4] = center - vector + vector2 + vector3;
array[5] = center - vector + vector2 - vector3;
array[6] = center - vector - vector2 + vector3;
array[7] = center - vector - vector2 - vector3;
Debug.DrawLine(array[0], array[1], color, 10f);
Debug.DrawLine(array[1], array[3], color, 10f);
Debug.DrawLine(array[3], array[2], color, 10f);
Debug.DrawLine(array[2], array[0], color, 10f);
Debug.DrawLine(array[4], array[5], color, 10f);
Debug.DrawLine(array[5], array[7], color, 10f);
Debug.DrawLine(array[7], array[6], color, 10f);
Debug.DrawLine(array[6], array[4], color, 10f);
Debug.DrawLine(array[0], array[4], color, 10f);
Debug.DrawLine(array[1], array[5], color, 10f);
Debug.DrawLine(array[2], array[6], color, 10f);
Debug.DrawLine(array[3], array[7], color, 10f);
}
private void DetectLargePerStepMovement()
{
if (!base.IsOwner)
{
Vector3 position = base.transform.position;
Vector3 vector = position - lastPosition;
float magnitude = vector.magnitude;
lastPosition = position;
if (magnitude > 2f)
{
StopAllCoroutines();
StartCoroutine(DisableCollisionCoroutine());
}
float fixedDeltaTime = Time.fixedDeltaTime;
if (fixedDeltaTime > 0f)
{
velocity = vector / fixedDeltaTime;
}
}
}
private IEnumerator DisableCollisionCoroutine()
{
collisionCollider.gameObject.SetActive(value: false);
overlapCollider.gameObject.SetActive(value: true);
yield return new WaitForSeconds(5f);
while (IsColliderOverlapping())
{
yield return new WaitForSeconds(2f);
}
collisionCollider.gameObject.SetActive(value: true);
}
[ServerRpc]
public void DespawnRPC()
{
RpcWriter___Server_DespawnRPC_2166136261();
}
[ServerRpc]
private void UpdateSyncVars(bool isLightsOn)
{
RpcWriter___Server_UpdateSyncVars_1140765316(isLightsOn);
}
public override void NetworkInitialize___Early()
{
if (!NetworkInitialize___EarlyMultiplayerBoatControllerAssembly_002DCSharp_002Edll_Excuted)
{
NetworkInitialize___EarlyMultiplayerBoatControllerAssembly_002DCSharp_002Edll_Excuted = true;
base.NetworkInitialize___Early();
isLightsOn.InitializeEarly(this, 0u, isSyncObject: false);
RegisterServerRpc(0u, RpcReader___Server_DespawnRPC_2166136261);
RegisterServerRpc(1u, RpcReader___Server_UpdateSyncVars_1140765316);
}
}
public override void NetworkInitialize___Late()
{
if (!NetworkInitialize___LateMultiplayerBoatControllerAssembly_002DCSharp_002Edll_Excuted)
{
NetworkInitialize___LateMultiplayerBoatControllerAssembly_002DCSharp_002Edll_Excuted = true;
base.NetworkInitialize___Late();
isLightsOn.InitializeLate();
}
}
public override void NetworkInitializeIfDisabled()
{
NetworkInitialize___Early();
NetworkInitialize___Late();
}
private void RpcWriter___Server_DespawnRPC_2166136261()
{
if (!base.IsClientInitialized)
{
NetworkManager networkManager = base.NetworkManager;
networkManager.LogWarning("Cannot complete action because client is not active. This may also occur if the object is not yet initialized, has deinitialized, or if it does not contain a NetworkObject component.");
return;
}
if (!base.IsOwner)
{
NetworkManager networkManager2 = base.NetworkManager;
networkManager2.LogWarning("Cannot complete action because you are not the owner of this object. .");
return;
}
Channel channel = Channel.Reliable;
PooledWriter pooledWriter = WriterPool.Retrieve();
SendServerRpc(0u, pooledWriter, channel, DataOrderType.Default);
pooledWriter.Store();
}
public void RpcLogic___DespawnRPC_2166136261()
{
Despawn();
}
private void RpcReader___Server_DespawnRPC_2166136261(PooledReader PooledReader0, Channel channel, NetworkConnection conn)
{
if (base.IsServerInitialized && OwnerMatches(conn))
{
RpcLogic___DespawnRPC_2166136261();
}
}
private void RpcWriter___Server_UpdateSyncVars_1140765316(bool isLightsOn)
{
if (!base.IsClientInitialized)
{
NetworkManager networkManager = base.NetworkManager;
networkManager.LogWarning("Cannot complete action because client is not active. This may also occur if the object is not yet initialized, has deinitialized, or if it does not contain a NetworkObject component.");
return;
}
if (!base.IsOwner)
{
NetworkManager networkManager2 = base.NetworkManager;
networkManager2.LogWarning("Cannot complete action because you are not the owner of this object. .");
return;
}
Channel channel = Channel.Reliable;
PooledWriter pooledWriter = WriterPool.Retrieve();
pooledWriter.WriteBoolean(isLightsOn);
SendServerRpc(1u, pooledWriter, channel, DataOrderType.Default);
pooledWriter.Store();
}
private void RpcLogic___UpdateSyncVars_1140765316(bool P_0)
{
isLightsOn.Value = P_0;
}
private void RpcReader___Server_UpdateSyncVars_1140765316(PooledReader PooledReader0, Channel channel, NetworkConnection conn)
{
bool flag = PooledReader0.ReadBoolean();
if (base.IsServerInitialized && OwnerMatches(conn))
{
RpcLogic___UpdateSyncVars_1140765316(flag);
}
}
public virtual void Awake()
{
NetworkInitialize___Early();
NetworkInitialize___Late();
}
}