Refactor connection handling to game manager

This commit is contained in:
2024-08-25 21:57:37 +02:00
parent b05cab8288
commit c71f2023df
7 changed files with 50 additions and 91 deletions

View File

@@ -9,8 +9,6 @@ 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
{
@@ -30,9 +28,6 @@ public class PlayerComponent : NetworkBehaviour
[SerializeField]
private float fadeDuration = 2f;
[SerializeField]
public GameManager gameManager;
public Vector3 Position
{
get { return controller.transform.position; }

View File

@@ -1,54 +0,0 @@
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using Zenject;
public class ConnectionManager : NetworkBehaviour
{
[SerializeField]
private NetworkManager networkManager;
[Inject]
[SerializeField]
[ReadOnly]
private GameManager gameManager;
private void Start()
{
networkManager.OnClientConnectedCallback += OnClientConnectedCallback;
networkManager.OnClientDisconnectCallback += OnClientDisconnectCallback;
}
private void OnClientConnectedCallback(ulong clientId)
{
Debug.Log($"Client-{clientId} is connected and can spawn {nameof(NetworkObject)}s.");
if (networkManager.LocalClientId == clientId)
{
StartCoroutine(SpawnLocalPlayer());
}
}
private IEnumerator SpawnLocalPlayer()
{
yield return new WaitForEndOfFrame();
var playerObject = networkManager.LocalClient.PlayerObject;
var player = playerObject.GetComponent<PlayerComponent>();
player.gameManager = gameManager;
gameManager.PlayerSpawnedOnNetwork(player);
yield return null;
}
private void OnClientDisconnectCallback(ulong clientId)
{
Debug.Log($"Client-{clientId} is disconnected and can spawn {nameof(NetworkObject)}s.");
if (networkManager.LocalClientId == clientId)
{
}
}
}

View File

@@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: cb16de57aad3ebe42bb3df246dc58d5f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using Zenject;
using ParrelSync;
public class GameManager : NetworkBehaviour
{
@@ -15,13 +16,53 @@ public class GameManager : NetworkBehaviour
get { return localPlayer; }
}
public void PlayerSpawnedOnNetwork(PlayerComponent player)
[SerializeField]
[ReadOnly]
[Inject]
private NetworkManager networkManager;
private void Start()
{
if (player.IsLocalPlayer)
networkManager.OnClientConnectedCallback += OnClientConnectedCallback;
networkManager.OnClientDisconnectCallback += OnClientDisconnectCallback;
if (ClonesManager.IsClone())
{
player.Teleport(localPlayer.Position, localPlayer.Rotation);
localPlayer.DestroyWithDependencies();
localPlayer = player;
networkManager.StartClient();
}
else
{
networkManager.StartHost();
}
}
private void OnClientConnectedCallback(ulong clientId)
{
Debug.Log($"Client-{clientId} is connected and can spawn {nameof(NetworkObject)}s.");
if (networkManager.LocalClientId == clientId)
{
StartCoroutine(SpawnLocalPlayer());
}
}
private IEnumerator SpawnLocalPlayer()
{
yield return new WaitForEndOfFrame();
var position = localPlayer.Position;
var rotation = localPlayer.Rotation;
localPlayer.DestroyWithDependencies();
var playerObject = networkManager.LocalClient.PlayerObject;
var player = playerObject.GetComponent<PlayerComponent>();
player.Teleport(position, rotation);
localPlayer = player;
yield return null;
}
private void OnClientDisconnectCallback(ulong clientId)
{
Debug.Log($"Client-{clientId} is disconnected");
}
}

View File

@@ -3,10 +3,11 @@ using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Unity.Netcode;
using UnityEngine;
using Zenject;
public class LevelManager : MonoBehaviour
public class LevelManager : NetworkBehaviour
{
[SerializeField]
[ReadOnly]

View File

@@ -2,11 +2,12 @@ using HurricaneVR.Framework.Core.Player;
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.SceneManagement;
using Zenject;
public class SceneManager : MonoBehaviour
public class SceneManager : NetworkBehaviour
{
enum Scene
{