129 lines
2.3 KiB
C#
129 lines
2.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using Photon.Pun;
|
|
using UnityEngine;
|
|
|
|
public class MultiplayerBoatEntity : MonoBehaviourPun, IPunObservable
|
|
{
|
|
[SerializeField]
|
|
private List<GameObject> boatVisuals;
|
|
|
|
private Vector3 remoteVelocity;
|
|
|
|
private Vector3 previousPosition;
|
|
|
|
private float lastUpdateTime;
|
|
|
|
private Boat boat;
|
|
|
|
private int enableTimer = 60;
|
|
|
|
private byte boatType;
|
|
|
|
public Boat Boat => boat;
|
|
|
|
public Vector3 RemoteVelocity => remoteVelocity;
|
|
|
|
public static event Action<MultiplayerBoatEntity> OnRemoteBoatSpawn;
|
|
|
|
public static event Action<MultiplayerBoatEntity> OnRemoteBoatDespawn;
|
|
|
|
public void Init(Boat boat)
|
|
{
|
|
this.boat = boat;
|
|
if (boat.name.Contains("Fishin Boat 4"))
|
|
{
|
|
boatType = 0;
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
boatVisuals.ForEach(delegate(GameObject element)
|
|
{
|
|
element.SetActive(value: false);
|
|
});
|
|
SetNameAndParent();
|
|
if (!base.photonView.IsMine)
|
|
{
|
|
MultiplayerBoatEntity.OnRemoteBoatSpawn?.Invoke(this);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (!base.photonView.IsMine)
|
|
{
|
|
MultiplayerBoatEntity.OnRemoteBoatDespawn?.Invoke(this);
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (base.photonView.IsMine)
|
|
{
|
|
UpdateLocal();
|
|
}
|
|
else
|
|
{
|
|
UpdateRemote();
|
|
}
|
|
}
|
|
|
|
private void SetNameAndParent()
|
|
{
|
|
base.name = "MultiplayerBoat_Actor_" + base.photonView.ControllerActorNr;
|
|
}
|
|
|
|
private void UpdateRemoteVelocity()
|
|
{
|
|
float num = Time.time - lastUpdateTime;
|
|
if (num > 0f)
|
|
{
|
|
Vector3 position = base.transform.position;
|
|
Vector3 b = (position - previousPosition) / num;
|
|
remoteVelocity = Vector3.Lerp(remoteVelocity, b, Time.deltaTime);
|
|
previousPosition = position;
|
|
lastUpdateTime = Time.time;
|
|
}
|
|
}
|
|
|
|
private void UpdateLocal()
|
|
{
|
|
if (!(boat == null))
|
|
{
|
|
base.transform.SetPositionAndRotation(boat.transform.position, boat.transform.rotation);
|
|
}
|
|
}
|
|
|
|
private void UpdateRemote()
|
|
{
|
|
if (enableTimer > 0)
|
|
{
|
|
enableTimer--;
|
|
return;
|
|
}
|
|
if (!boatVisuals[boatType].activeSelf)
|
|
{
|
|
boatVisuals.ForEach(delegate(GameObject element)
|
|
{
|
|
element.SetActive(value: false);
|
|
});
|
|
boatVisuals[boatType].SetActive(value: true);
|
|
}
|
|
UpdateRemoteVelocity();
|
|
}
|
|
|
|
public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
|
|
{
|
|
if (stream.IsWriting)
|
|
{
|
|
stream.SendNext(boatType);
|
|
}
|
|
else
|
|
{
|
|
boatType = (byte)stream.ReceiveNext();
|
|
}
|
|
}
|
|
}
|