195 lines
4.5 KiB
C#
195 lines
4.5 KiB
C#
using System;
|
|
using ExitGames.Client.Photon;
|
|
using Photon.Pun;
|
|
using Photon.Realtime;
|
|
using UFS2.Gameplay;
|
|
using UnityEngine;
|
|
|
|
public class MultiplayerFishEntity : MonoBehaviourPun, IPunObservable
|
|
{
|
|
private MultiplayerFishSpawner.NetworkedFish networkedFish;
|
|
|
|
private Transform rotationRoot;
|
|
|
|
private Transform fishVisual;
|
|
|
|
private Animator animator;
|
|
|
|
private byte[] guidSerial;
|
|
|
|
private string guidString;
|
|
|
|
private bool fishCathedByPlayer;
|
|
|
|
private bool fishDespawnedByPlayer;
|
|
|
|
private bool destroyCalled;
|
|
|
|
public void Init(MultiplayerFishSpawner.NetworkedFish networkedFish)
|
|
{
|
|
this.networkedFish = networkedFish;
|
|
networkedFish.multiplayerFishEntity = this;
|
|
rotationRoot = networkedFish.fishEntity.GetComponentInChildren<Animator>().transform;
|
|
guidSerial = networkedFish.Guid.ToByteArray();
|
|
guidString = networkedFish.guid;
|
|
SetNameAndParent();
|
|
}
|
|
|
|
private void FishEntity_OnPostSpitOutBait(FishEntity fish)
|
|
{
|
|
if (base.photonView.IsMine)
|
|
{
|
|
TryNetworkDestroy();
|
|
}
|
|
}
|
|
|
|
private void FishSpawner_OnDespawnFish(FishEntity fishEntity)
|
|
{
|
|
if (base.photonView.IsMine)
|
|
{
|
|
fishDespawnedByPlayer = true;
|
|
}
|
|
}
|
|
|
|
private void FishCatchState_OnEnter()
|
|
{
|
|
if (base.photonView.IsMine)
|
|
{
|
|
fishCathedByPlayer = true;
|
|
}
|
|
}
|
|
|
|
private void FishMultiplayerIdleState_OnEnter(FishEntity fishEntity)
|
|
{
|
|
if (base.photonView.IsMine && !(networkedFish.fishEntity != fishEntity))
|
|
{
|
|
TryNetworkDestroy();
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
FishEntity.OnPostSpitOutBait += FishEntity_OnPostSpitOutBait;
|
|
FishSpawner.OnDespawnFish += FishSpawner_OnDespawnFish;
|
|
FishCatchState.OnEnter += FishCatchState_OnEnter;
|
|
FishMultiplayerIdleState.OnEnter += FishMultiplayerIdleState_OnEnter;
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (networkedFish != null)
|
|
{
|
|
networkedFish.actorInControl = -1;
|
|
networkedFish.multiplayerFishEntity = null;
|
|
if (base.photonView.IsMine && (fishCathedByPlayer || fishDespawnedByPlayer))
|
|
{
|
|
SendFishDeleteAfterInteraction();
|
|
}
|
|
}
|
|
FishEntity.OnPostSpitOutBait -= FishEntity_OnPostSpitOutBait;
|
|
FishSpawner.OnDespawnFish -= FishSpawner_OnDespawnFish;
|
|
FishCatchState.OnEnter -= FishCatchState_OnEnter;
|
|
FishMultiplayerIdleState.OnEnter -= FishMultiplayerIdleState_OnEnter;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (base.photonView.IsMine)
|
|
{
|
|
UpdateLocal();
|
|
}
|
|
else
|
|
{
|
|
UpdateRemote();
|
|
}
|
|
if (networkedFish != null)
|
|
{
|
|
networkedFish.position = base.transform.position;
|
|
}
|
|
}
|
|
|
|
private void UpdateLocal()
|
|
{
|
|
if (networkedFish != null)
|
|
{
|
|
base.transform.SetPositionAndRotation(networkedFish.fishEntity.transform.position, rotationRoot.rotation);
|
|
}
|
|
}
|
|
|
|
private void UpdateRemote()
|
|
{
|
|
if (networkedFish == null && guidSerial != null)
|
|
{
|
|
guidString = new Guid(guidSerial).ToString();
|
|
networkedFish = MultiplayerFishSpawner.GetNetworkedFish(guidString);
|
|
if (networkedFish != null)
|
|
{
|
|
networkedFish.multiplayerFishEntity = this;
|
|
SetNameAndParent();
|
|
SpawnFish();
|
|
}
|
|
}
|
|
if (fishVisual != null && fishCathedByPlayer)
|
|
{
|
|
animator.SetFloat("speed", 0f);
|
|
if (!(networkedFish.fishWeight < 6f) && networkedFish.fishWeight < 35f)
|
|
{
|
|
animator.SetBool("fishView", value: true);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void SetNameAndParent()
|
|
{
|
|
base.transform.SetParent(networkedFish.fishEntity.transform.parent);
|
|
base.name = networkedFish.fishEntity.name + "_Networked";
|
|
}
|
|
|
|
private void SpawnFish()
|
|
{
|
|
FishSpawner fishSpawner = UnityEngine.Object.FindObjectOfType<FishSpawner>();
|
|
if (!(fishSpawner == null))
|
|
{
|
|
fishVisual = fishSpawner.InstantiateFishDummy(networkedFish.fishID, networkedFish.fishWeight, base.transform.position).transform;
|
|
animator = fishVisual.GetComponentInChildren<Animator>();
|
|
fishVisual.SetParent(base.transform);
|
|
fishVisual.transform.localPosition = Vector3.zero;
|
|
fishVisual.transform.localRotation = Quaternion.identity;
|
|
}
|
|
}
|
|
|
|
private void TryNetworkDestroy()
|
|
{
|
|
if (base.photonView.IsMine && !destroyCalled)
|
|
{
|
|
destroyCalled = true;
|
|
PhotonNetwork.Destroy(base.gameObject);
|
|
}
|
|
}
|
|
|
|
private void SendFishDeleteAfterInteraction()
|
|
{
|
|
if (base.photonView.IsMine)
|
|
{
|
|
PhotonNetwork.RaiseEvent(4, new object[1] { networkedFish.Guid.ToByteArray() }, new RaiseEventOptions
|
|
{
|
|
Receivers = ReceiverGroup.MasterClient
|
|
}, SendOptions.SendReliable);
|
|
}
|
|
}
|
|
|
|
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
|
{
|
|
if (stream.IsWriting)
|
|
{
|
|
stream.SendNext(guidSerial);
|
|
stream.SendNext(fishCathedByPlayer);
|
|
}
|
|
else
|
|
{
|
|
guidSerial = (byte[])stream.ReceiveNext();
|
|
fishCathedByPlayer = (bool)stream.ReceiveNext();
|
|
}
|
|
}
|
|
}
|