156 lines
3.8 KiB
C#
156 lines
3.8 KiB
C#
using HurricaneVR.Framework.Components;
|
|
using HurricaneVR.Framework.Core.Player;
|
|
using Meta.XR.MultiplayerBlocks.NGO;
|
|
using Sirenix.OdinInspector;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Netcode;
|
|
using Unity.Netcode.Components;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.Rendering.Universal;
|
|
|
|
public class PlayerComponent : NetworkBehaviour
|
|
{
|
|
[SerializeField]
|
|
private NetworkObject networkObject;
|
|
|
|
[SerializeField]
|
|
private HVRTeleporter teleporter;
|
|
|
|
[SerializeField]
|
|
private HVRPlayerController controller;
|
|
|
|
[SerializeField]
|
|
private CharacterController characterController;
|
|
|
|
[SerializeField]
|
|
private AudioListener audioListener;
|
|
|
|
[ReadOnly]
|
|
[SerializeField]
|
|
private float fadeDuration = 2f;
|
|
|
|
private GameObject[] dependencies => new GameObject[] {
|
|
controller.RightHand.ForceGrabber._anchor.gameObject,
|
|
controller.LeftHand.ForceGrabber._anchor.gameObject,
|
|
controller.LeftHand._anchor,
|
|
controller.RightHand._anchor
|
|
};
|
|
|
|
public Vector3 Position => controller.transform.position;
|
|
|
|
public Vector3 Rotation => controller.transform.eulerAngles;
|
|
|
|
private bool isSoloRig => !networkObject.IsPlayerObject;
|
|
private bool isMultiplayerLocalRig => networkObject.IsLocalPlayer;
|
|
|
|
private bool isMultiplayerRemoteRig => !networkObject.IsOwner && networkObject.IsPlayerObject;
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
base.OnNetworkSpawn();
|
|
|
|
if (!isSoloRig)
|
|
{
|
|
name = $"Player - {networkObject.OwnerClientId}"
|
|
+ (networkObject.IsLocalPlayer ? " (local)" : "");
|
|
}
|
|
|
|
if (isMultiplayerRemoteRig)
|
|
{
|
|
StartCoroutine(DestroyMultiplayerComponents());
|
|
}
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (!isSoloRig) return;
|
|
|
|
audioListener.enabled = true;
|
|
StartCoroutine(DestroySoloComponents());
|
|
}
|
|
|
|
private IEnumerator DestroyMultiplayerComponents()
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
DestroyDependencies();
|
|
|
|
foreach (var t in new[]
|
|
{
|
|
typeof(UniversalAdditionalCameraData),
|
|
})
|
|
{
|
|
foreach (var component in GetComponentsInChildren(t))
|
|
{
|
|
Destroy(component);
|
|
}
|
|
}
|
|
|
|
controller.RemoveMultiplayerComponents();
|
|
}
|
|
|
|
private IEnumerator DestroySoloComponents()
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
foreach (var t in new[]
|
|
{
|
|
typeof(NetworkObject),
|
|
typeof(NetworkTransform)
|
|
})
|
|
{
|
|
foreach (var component in GetComponentsInChildren(t))
|
|
{
|
|
Destroy(component);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void DestroyDependencies()
|
|
{
|
|
foreach (var d in dependencies)
|
|
{
|
|
Destroy(d);
|
|
}
|
|
}
|
|
|
|
public void ToggleAudioListener(bool enabled) => audioListener.enabled = enabled;
|
|
|
|
public void Toggle(bool active)
|
|
{
|
|
// Only toggle solo rig components, not multiplayer one
|
|
if (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);
|
|
}
|
|
|
|
public void FadeScreen(float to, float duration)
|
|
{
|
|
controller.ScreenFader.Fade(to, duration);
|
|
}
|
|
|
|
public void FadeIn()
|
|
{
|
|
controller.ScreenFader.Fade(1, fadeDuration);
|
|
}
|
|
|
|
public void FadeOut()
|
|
{
|
|
controller.ScreenFader.Fade(0, fadeDuration);
|
|
}
|
|
}
|