90 lines
2.2 KiB
C#
90 lines
2.2 KiB
C#
using Sirenix.OdinInspector;
|
|
using System.Collections;
|
|
using Unity.Netcode;
|
|
using UnityEngine;
|
|
using Zenject;
|
|
using ParrelSync;
|
|
|
|
public class GameManager : NetworkBehaviour
|
|
{
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
[Inject]
|
|
private PlayerComponent soloRig;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
private PlayerComponent multiplayerRig;
|
|
|
|
public PlayerComponent LocalPlayer => multiplayerRig ?? soloRig;
|
|
|
|
[SerializeField]
|
|
[ReadOnly]
|
|
[Inject]
|
|
private NetworkManager networkManager;
|
|
|
|
[SerializeField]
|
|
private bool autoConnectOrHost = true;
|
|
|
|
private void Start()
|
|
{
|
|
networkManager.OnClientConnectedCallback += OnClientConnectedCallback;
|
|
networkManager.OnClientDisconnectCallback += OnClientDisconnectCallback;
|
|
|
|
if (autoConnectOrHost)
|
|
{
|
|
if (ClonesManager.IsClone())
|
|
{
|
|
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(SwitchSoloMultiplayerRig(false));
|
|
}
|
|
}
|
|
|
|
private IEnumerator SwitchSoloMultiplayerRig(bool toSolo)
|
|
{
|
|
yield return new WaitForEndOfFrame();
|
|
|
|
if (toSolo)
|
|
{
|
|
|
|
soloRig.Toggle(toSolo);
|
|
soloRig.Teleport(multiplayerRig.Position, multiplayerRig.Position);
|
|
multiplayerRig = null;
|
|
}
|
|
else
|
|
{
|
|
soloRig.Toggle(toSolo);
|
|
|
|
var playerObject = networkManager.LocalClient.PlayerObject;
|
|
multiplayerRig = playerObject.GetComponent<PlayerComponent>();
|
|
multiplayerRig.Teleport(soloRig.Position, soloRig.Position);
|
|
multiplayerRig.ToggleAudioListener(true);
|
|
|
|
}
|
|
}
|
|
|
|
private void OnClientDisconnectCallback(ulong clientId)
|
|
{
|
|
Debug.Log($"Client-{clientId} is disconnected");
|
|
|
|
if (networkManager.LocalClientId == clientId)
|
|
{
|
|
StartCoroutine(SwitchSoloMultiplayerRig(true));
|
|
}
|
|
}
|
|
}
|