Reimplement solo and mp rig switching
This commit is contained in:
@@ -11,9 +11,14 @@ using Unity.Netcode.Components;
|
||||
using Unity.VisualScripting;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
using Zenject;
|
||||
|
||||
public class PlayerComponent : NetworkBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
[Inject]
|
||||
private HVRInputModule inputModule;
|
||||
|
||||
[SerializeField]
|
||||
private NetworkObject networkObject;
|
||||
|
||||
@@ -45,46 +50,61 @@ public class PlayerComponent : NetworkBehaviour
|
||||
public Vector3 Rotation => controller.Camera.forward;
|
||||
|
||||
private bool isSoloRig => !networkObject.IsPlayerObject;
|
||||
private bool isMultiplayerLocalRig => networkObject.IsLocalPlayer;
|
||||
private bool isLocalRig => networkObject.IsLocalPlayer;
|
||||
|
||||
private bool isMultiplayerRemoteRig => !networkObject.IsOwner && networkObject.IsPlayerObject;
|
||||
private bool isRemoteRig => !networkObject.IsOwner && networkObject.IsPlayerObject;
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
base.OnNetworkSpawn();
|
||||
// Teleport information after player is loaded into scene
|
||||
private Vector3 teleportPosition;
|
||||
private Vector3 teleportRotation;
|
||||
private bool teleportAfterLoad;
|
||||
|
||||
if (!isSoloRig)
|
||||
{
|
||||
name = $"Player - {networkObject.OwnerClientId}"
|
||||
+ (networkObject.IsLocalPlayer ? " (local)" : "");
|
||||
}
|
||||
|
||||
if (isMultiplayerRemoteRig)
|
||||
{
|
||||
StartCoroutine(DestroyMultiplayerComponents());
|
||||
}
|
||||
}
|
||||
private bool isLoaded;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
this.Inject();
|
||||
|
||||
StartCoroutine(AddDontDestroyToDependencies());
|
||||
|
||||
if (isSoloRig || isMultiplayerLocalRig)
|
||||
if (isSoloRig)
|
||||
{
|
||||
foreach (var pointer in GetComponentsInChildren<HVRUIPointer>())
|
||||
{
|
||||
HVRInputModule.Instance.AddPointer(pointer);
|
||||
}
|
||||
AddPointersToInputModule();
|
||||
|
||||
name = "Player - SP";
|
||||
|
||||
StartCoroutine(DestroyComponentsNotNeededForSolo());
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
else if (isLocalRig)
|
||||
{
|
||||
AddPointersToInputModule();
|
||||
|
||||
name = $"Player - MP {networkObject.OwnerClientId} (local)";
|
||||
}
|
||||
else if (isRemoteRig)
|
||||
{
|
||||
name = $"Player - MP {networkObject.OwnerClientId}";
|
||||
StartCoroutine(DestroyComponentsNotNeededForRemoteRigs());
|
||||
}
|
||||
|
||||
if (!isSoloRig) return;
|
||||
isLoaded = true;
|
||||
|
||||
audioListener.enabled = true;
|
||||
StartCoroutine(DestroySoloComponents());
|
||||
DontDestroyOnLoad(gameObject);
|
||||
if (teleportAfterLoad)
|
||||
{
|
||||
Teleport(teleportPosition, teleportRotation);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator DestroyMultiplayerComponents()
|
||||
private void AddPointersToInputModule()
|
||||
{
|
||||
foreach (var pointer in GetComponentsInChildren<HVRUIPointer>())
|
||||
{
|
||||
inputModule.AddPointer(pointer);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator DestroyComponentsNotNeededForRemoteRigs()
|
||||
{
|
||||
yield return new WaitForEndOfFrame();
|
||||
|
||||
@@ -105,7 +125,7 @@ public class PlayerComponent : NetworkBehaviour
|
||||
controller.RemoveMultiplayerComponents();
|
||||
}
|
||||
|
||||
private IEnumerator DestroySoloComponents()
|
||||
private IEnumerator DestroyComponentsNotNeededForSolo()
|
||||
{
|
||||
yield return new WaitForEndOfFrame();
|
||||
|
||||
@@ -140,26 +160,18 @@ public class PlayerComponent : NetworkBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public void ToggleAudioListener(bool enabled) => audioListener.enabled = enabled;
|
||||
|
||||
public void Toggle(bool active)
|
||||
{
|
||||
// Only toggle solo rig components, not multiplayer one
|
||||
if (networkObject == null || isSoloRig)
|
||||
{
|
||||
foreach (var d in dependencies)
|
||||
{
|
||||
d.SetActive(active);
|
||||
}
|
||||
|
||||
audioListener.enabled = active;
|
||||
gameObject.SetActive(active);
|
||||
}
|
||||
}
|
||||
|
||||
public void Teleport(Vector3 position, Vector3 direction)
|
||||
{
|
||||
teleporter.Teleport(position, direction);
|
||||
if (isLoaded)
|
||||
{
|
||||
teleporter.Teleport(position, direction);
|
||||
}
|
||||
else
|
||||
{
|
||||
teleportPosition = position;
|
||||
teleportRotation = direction;
|
||||
teleportAfterLoad = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void FadeScreen(float to, float duration)
|
||||
|
||||
Reference in New Issue
Block a user