291 lines
8.0 KiB
C#
291 lines
8.0 KiB
C#
using System;
|
|
using System.Linq;
|
|
using DG.Tweening;
|
|
using FishNet.Component.Transforming;
|
|
using FishNet.Connection;
|
|
using FishNet.Managing;
|
|
using FishNet.Object;
|
|
using FishNet.Object.Synchronizing;
|
|
using FishNet.Serializing;
|
|
using FishNet.Transporting;
|
|
using SoapCustomVariable;
|
|
using UnityEngine;
|
|
|
|
public class MultiplayerFishController : NetworkBehaviour
|
|
{
|
|
[SerializeField]
|
|
private FishDatabase fishDatabase;
|
|
|
|
[SerializeField]
|
|
private NetworkTransform networkTransform;
|
|
|
|
[SerializeField]
|
|
private Rigidbody rb;
|
|
|
|
[SerializeField]
|
|
private ConfigurableJoint joint;
|
|
|
|
public readonly SyncVar<int> fishID = new SyncVar<int>(-1);
|
|
|
|
public readonly SyncVar<float> fishWeight = new SyncVar<float>(-1f);
|
|
|
|
private FishController localFishController;
|
|
|
|
private GameObject remoteFishModel;
|
|
|
|
private FishModelBindings remoteFishModelBindings;
|
|
|
|
private MultiplayerAvatarControler remoteAvatarControler;
|
|
|
|
private bool NetworkInitialize___EarlyMultiplayerFishControllerAssembly_002DCSharp_002Edll_Excuted;
|
|
|
|
private bool NetworkInitialize___LateMultiplayerFishControllerAssembly_002DCSharp_002Edll_Excuted;
|
|
|
|
public static event Action<MultiplayerFishController> OnStartLocal;
|
|
|
|
public override void OnStartClient()
|
|
{
|
|
if (base.IsOwner)
|
|
{
|
|
base.OnStartClient();
|
|
MultiplayerFishController.OnStartLocal?.Invoke(this);
|
|
}
|
|
}
|
|
|
|
public override void OnStopClient()
|
|
{
|
|
base.OnStopClient();
|
|
CleanupRemoteFishModel();
|
|
}
|
|
|
|
public void Init(FishController fishController)
|
|
{
|
|
if (base.IsOwner)
|
|
{
|
|
localFishController = fishController;
|
|
}
|
|
}
|
|
|
|
public void Deinit(float delay = 0f)
|
|
{
|
|
if (!base.IsOwner)
|
|
{
|
|
return;
|
|
}
|
|
DOTween.Kill(base.gameObject);
|
|
if (delay <= 0f)
|
|
{
|
|
DespawnRPC();
|
|
return;
|
|
}
|
|
DOVirtual.DelayedCall(delay, delegate
|
|
{
|
|
if ((bool)this)
|
|
{
|
|
DespawnRPC();
|
|
}
|
|
}).SetTarget(base.gameObject).SetLink(base.gameObject);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (base.IsOwner)
|
|
{
|
|
UpdateLocal();
|
|
}
|
|
else
|
|
{
|
|
UpdateRemote();
|
|
}
|
|
}
|
|
|
|
private void UpdateLocal()
|
|
{
|
|
if (!(localFishController == null) && !(localFishController.FishData == null))
|
|
{
|
|
base.transform.SetPositionAndRotation(localFishController.transform.position, localFishController.transform.rotation);
|
|
UpdateSyncVars(localFishController.FishData.ID, localFishController.FishData.Weight);
|
|
}
|
|
}
|
|
|
|
private void UpdateRemote()
|
|
{
|
|
UpdateRemoteFishModel();
|
|
FishCatchState();
|
|
}
|
|
|
|
private void UpdateRemoteFishModel()
|
|
{
|
|
if (!(remoteFishModel != null) && fishID.Value >= 0 && !(fishWeight.Value <= 0f))
|
|
{
|
|
FishData newInstanceByID = fishDatabase.GetNewInstanceByID(fishID.Value);
|
|
remoteFishModel = UnityEngine.Object.Instantiate(newInstanceByID.GetModel(), base.transform);
|
|
float num = newInstanceByID.LWR.Evaluate(fishWeight.Value) * 1.73f;
|
|
remoteFishModel.transform.localScale = new Vector3(num, num, num);
|
|
remoteFishModel.transform.localPosition = Vector3.zero;
|
|
remoteFishModelBindings = remoteFishModel.GetComponent<FishModelBindings>();
|
|
remoteFishModelBindings.TailSetEnable(setEnable: false);
|
|
UnityEngine.Object.Destroy(newInstanceByID);
|
|
}
|
|
}
|
|
|
|
private void FishCatchState()
|
|
{
|
|
if (remoteAvatarControler == null || !remoteAvatarControler.IsSpawned)
|
|
{
|
|
remoteAvatarControler = base.Owner.Objects.FirstOrDefault((NetworkObject element) => element.GetComponent<MultiplayerAvatarControler>() != null)?.GetComponent<MultiplayerAvatarControler>();
|
|
}
|
|
if (remoteAvatarControler != null && remoteAvatarControler.IsSpawned && remoteAvatarControler.State == State.collectFish)
|
|
{
|
|
networkTransform.SetSynchronizePosition(value: false);
|
|
networkTransform.SetSynchronizeRotation(value: false);
|
|
remoteAvatarControler.Waga.SetText(fishWeight.Value);
|
|
if (rb.isKinematic)
|
|
{
|
|
rb.isKinematic = false;
|
|
Vector3 anchor = remoteFishModel.transform.localPosition + remoteFishModelBindings.Jaw.localPosition * remoteFishModel.transform.localScale.x;
|
|
joint.anchor = anchor;
|
|
joint.connectedBody = remoteAvatarControler.Waga.hookRbody;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
networkTransform.SetSynchronizePosition(value: true);
|
|
networkTransform.SetSynchronizeRotation(value: true);
|
|
if (!rb.isKinematic)
|
|
{
|
|
rb.isKinematic = true;
|
|
joint.connectedBody = null;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void CleanupRemoteFishModel()
|
|
{
|
|
if (remoteFishModel != null)
|
|
{
|
|
UnityEngine.Object.Destroy(remoteFishModel);
|
|
remoteFishModel = null;
|
|
}
|
|
}
|
|
|
|
[ServerRpc]
|
|
public void UpdateSyncVars(int fishID, float fishWeight)
|
|
{
|
|
RpcWriter___Server_UpdateSyncVars_1293284375(fishID, fishWeight);
|
|
}
|
|
|
|
[ServerRpc]
|
|
public void DespawnRPC()
|
|
{
|
|
RpcWriter___Server_DespawnRPC_2166136261();
|
|
}
|
|
|
|
public override void NetworkInitialize___Early()
|
|
{
|
|
if (!NetworkInitialize___EarlyMultiplayerFishControllerAssembly_002DCSharp_002Edll_Excuted)
|
|
{
|
|
NetworkInitialize___EarlyMultiplayerFishControllerAssembly_002DCSharp_002Edll_Excuted = true;
|
|
base.NetworkInitialize___Early();
|
|
fishWeight.InitializeEarly(this, 1u, isSyncObject: false);
|
|
fishID.InitializeEarly(this, 0u, isSyncObject: false);
|
|
RegisterServerRpc(0u, RpcReader___Server_UpdateSyncVars_1293284375);
|
|
RegisterServerRpc(1u, RpcReader___Server_DespawnRPC_2166136261);
|
|
}
|
|
}
|
|
|
|
public override void NetworkInitialize___Late()
|
|
{
|
|
if (!NetworkInitialize___LateMultiplayerFishControllerAssembly_002DCSharp_002Edll_Excuted)
|
|
{
|
|
NetworkInitialize___LateMultiplayerFishControllerAssembly_002DCSharp_002Edll_Excuted = true;
|
|
base.NetworkInitialize___Late();
|
|
fishWeight.InitializeLate();
|
|
fishID.InitializeLate();
|
|
}
|
|
}
|
|
|
|
public override void NetworkInitializeIfDisabled()
|
|
{
|
|
NetworkInitialize___Early();
|
|
NetworkInitialize___Late();
|
|
}
|
|
|
|
private void RpcWriter___Server_UpdateSyncVars_1293284375(int fishID, float fishWeight)
|
|
{
|
|
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.WriteInt32(fishID);
|
|
pooledWriter.WriteSingle(fishWeight);
|
|
SendServerRpc(0u, pooledWriter, channel, DataOrderType.Default);
|
|
pooledWriter.Store();
|
|
}
|
|
|
|
public void RpcLogic___UpdateSyncVars_1293284375(int P_0, float P_1)
|
|
{
|
|
fishID.Value = P_0;
|
|
fishWeight.Value = P_1;
|
|
}
|
|
|
|
private void RpcReader___Server_UpdateSyncVars_1293284375(PooledReader PooledReader0, Channel channel, NetworkConnection conn)
|
|
{
|
|
int num = PooledReader0.ReadInt32();
|
|
float num2 = PooledReader0.ReadSingle();
|
|
if (base.IsServerInitialized && OwnerMatches(conn))
|
|
{
|
|
RpcLogic___UpdateSyncVars_1293284375(num, num2);
|
|
}
|
|
}
|
|
|
|
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(1u, 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();
|
|
}
|
|
}
|
|
|
|
public virtual void Awake()
|
|
{
|
|
NetworkInitialize___Early();
|
|
NetworkInitialize___Late();
|
|
}
|
|
}
|