Implement switching between solo and multiplayer rig seamlessly
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using Zenject;
|
||||
@@ -11,28 +10,37 @@ public class GameManager : NetworkBehaviour
|
||||
[SerializeField]
|
||||
[ReadOnly]
|
||||
[Inject]
|
||||
private PlayerComponent localPlayer;
|
||||
public PlayerComponent LocalPlayer {
|
||||
get { return localPlayer; }
|
||||
}
|
||||
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 (ClonesManager.IsClone())
|
||||
if (autoConnectOrHost)
|
||||
{
|
||||
networkManager.StartClient();
|
||||
}
|
||||
else
|
||||
{
|
||||
networkManager.StartHost();
|
||||
if (ClonesManager.IsClone())
|
||||
{
|
||||
networkManager.StartClient();
|
||||
}
|
||||
else
|
||||
{
|
||||
networkManager.StartHost();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,27 +50,40 @@ public class GameManager : NetworkBehaviour
|
||||
|
||||
if (networkManager.LocalClientId == clientId)
|
||||
{
|
||||
StartCoroutine(SpawnLocalPlayer());
|
||||
StartCoroutine(SwitchSoloMultiplayerRig(false));
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator SpawnLocalPlayer()
|
||||
private IEnumerator SwitchSoloMultiplayerRig(bool toSolo)
|
||||
{
|
||||
yield return new WaitForEndOfFrame();
|
||||
|
||||
var position = localPlayer.Position;
|
||||
var rotation = localPlayer.Rotation;
|
||||
localPlayer.DestroyWithDependencies();
|
||||
if (toSolo)
|
||||
{
|
||||
|
||||
var playerObject = networkManager.LocalClient.PlayerObject;
|
||||
var player = playerObject.GetComponent<PlayerComponent>();
|
||||
player.Teleport(position, rotation);
|
||||
localPlayer = player;
|
||||
yield return null;
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user