125 lines
3.1 KiB
C#
125 lines
3.1 KiB
C#
using BlockSpace.Voxels;
|
|
using Photon.Pun;
|
|
using Photon.Realtime;
|
|
using Photon.VR;
|
|
using UnityEngine;
|
|
using UnityEngine.XR.Interaction.Toolkit.Locomotion.Jump;
|
|
using UnityEngine.XR.Interaction.Toolkit.Locomotion.Movement;
|
|
|
|
[DisallowMultipleComponent]
|
|
public sealed class NodePlayerLocomotionRpc : MonoBehaviourPun
|
|
{
|
|
public static NodePlayerLocomotionRpc Ensure()
|
|
{
|
|
NodePlayerLocomotionRpc nodePlayerLocomotionRpc = Object.FindAnyObjectByType<NodePlayerLocomotionRpc>();
|
|
if (nodePlayerLocomotionRpc != null)
|
|
{
|
|
return nodePlayerLocomotionRpc;
|
|
}
|
|
GameObject gameObject = ((NodeGraphManager.Instance != null) ? NodeGraphManager.Instance.gameObject : null);
|
|
if (gameObject == null || gameObject.GetComponent<PhotonView>() == null)
|
|
{
|
|
VoxelGroupPhotonSync voxelGroupPhotonSync = Object.FindAnyObjectByType<VoxelGroupPhotonSync>();
|
|
if (voxelGroupPhotonSync != null)
|
|
{
|
|
gameObject = voxelGroupPhotonSync.gameObject;
|
|
}
|
|
}
|
|
if (gameObject == null)
|
|
{
|
|
gameObject = ((PhotonVRManager.Manager != null) ? PhotonVRManager.Manager.gameObject : null);
|
|
}
|
|
if (gameObject == null)
|
|
{
|
|
return null;
|
|
}
|
|
return gameObject.GetComponent<NodePlayerLocomotionRpc>() ?? gameObject.AddComponent<NodePlayerLocomotionRpc>();
|
|
}
|
|
|
|
public bool TrySetMoveSpeed(int playerId, float speed)
|
|
{
|
|
if (playerId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (NodePlayerUtility.IsLocalPlayerId(playerId))
|
|
{
|
|
ApplyLocalMoveSpeed(speed);
|
|
return true;
|
|
}
|
|
if (!PhotonNetwork.InRoom || base.photonView == null || base.photonView.ViewID == 0)
|
|
{
|
|
return false;
|
|
}
|
|
Player player = PhotonNetwork.CurrentRoom?.GetPlayer(playerId);
|
|
if (player == null)
|
|
{
|
|
return false;
|
|
}
|
|
base.photonView.RPC("RPC_SetMoveSpeed", player, speed);
|
|
return true;
|
|
}
|
|
|
|
public bool TrySetJumpHeight(int playerId, float height)
|
|
{
|
|
if (playerId <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
if (NodePlayerUtility.IsLocalPlayerId(playerId))
|
|
{
|
|
ApplyLocalJumpHeight(height);
|
|
return true;
|
|
}
|
|
if (!PhotonNetwork.InRoom || base.photonView == null || base.photonView.ViewID == 0)
|
|
{
|
|
return false;
|
|
}
|
|
Player player = PhotonNetwork.CurrentRoom?.GetPlayer(playerId);
|
|
if (player == null)
|
|
{
|
|
return false;
|
|
}
|
|
base.photonView.RPC("RPC_SetJumpHeight", player, height);
|
|
return true;
|
|
}
|
|
|
|
private static void ApplyLocalMoveSpeed(float speed)
|
|
{
|
|
speed = Mathf.Max(0f, speed);
|
|
ContinuousMoveProvider[] array = Object.FindObjectsByType<ContinuousMoveProvider>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
if (array[i] != null)
|
|
{
|
|
array[i].moveSpeed = speed;
|
|
}
|
|
}
|
|
}
|
|
|
|
private static void ApplyLocalJumpHeight(float height)
|
|
{
|
|
height = Mathf.Max(0f, height);
|
|
JumpProvider[] array = Object.FindObjectsByType<JumpProvider>(FindObjectsInactive.Include, FindObjectsSortMode.None);
|
|
for (int i = 0; i < array.Length; i++)
|
|
{
|
|
if (array[i] != null)
|
|
{
|
|
array[i].jumpHeight = height;
|
|
}
|
|
}
|
|
}
|
|
|
|
[PunRPC]
|
|
private void RPC_SetMoveSpeed(float speed)
|
|
{
|
|
ApplyLocalMoveSpeed(speed);
|
|
}
|
|
|
|
[PunRPC]
|
|
private void RPC_SetJumpHeight(float height)
|
|
{
|
|
ApplyLocalJumpHeight(height);
|
|
}
|
|
}
|