Implement player network spawning and position synchronization
This commit is contained in:
112
Assets/Scripts/Components/PlayerComponent.cs
Normal file
112
Assets/Scripts/Components/PlayerComponent.cs
Normal file
@@ -0,0 +1,112 @@
|
||||
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;
|
||||
using Zenject;
|
||||
using static Unity.Burst.Intrinsics.X86.Avx;
|
||||
|
||||
public class PlayerComponent : NetworkBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private NetworkObject networkObject;
|
||||
|
||||
[SerializeField]
|
||||
private HVRTeleporter teleporter;
|
||||
|
||||
[SerializeField]
|
||||
private HVRPlayerController controller;
|
||||
|
||||
[SerializeField]
|
||||
private CharacterController characterController;
|
||||
|
||||
[ReadOnly]
|
||||
[SerializeField]
|
||||
private float fadeDuration = 2f;
|
||||
|
||||
[SerializeField]
|
||||
public GameManager gameManager;
|
||||
|
||||
public Vector3 Position
|
||||
{
|
||||
get { return controller.transform.position; }
|
||||
}
|
||||
|
||||
public Vector3 Rotation
|
||||
{
|
||||
get { return controller.transform.eulerAngles; }
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
base.OnNetworkSpawn();
|
||||
|
||||
name = $"Player - {networkObject.OwnerClientId}"
|
||||
+ (networkObject.IsLocalPlayer ? " (local)" : "");
|
||||
|
||||
if (!networkObject.IsOwner)
|
||||
{
|
||||
StartCoroutine(DestroyMultiplayerComponents());
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
public void DestroyDependencies()
|
||||
{
|
||||
Destroy(controller.RightHand.ForceGrabber._anchor.gameObject);
|
||||
Destroy(controller.LeftHand.ForceGrabber._anchor.gameObject);
|
||||
Destroy(controller.LeftHand._anchor);
|
||||
Destroy(controller.RightHand._anchor);
|
||||
}
|
||||
|
||||
public void DestroyWithDependencies()
|
||||
{
|
||||
DestroyDependencies();
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/Components/PlayerComponent.cs.meta
Normal file
11
Assets/Scripts/Components/PlayerComponent.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 983bb31c4dab2d84b857006f5099d030
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user