Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform a handshake before starting a round so that we know that all clients have initialized their players #689

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Assets/Scripts/Control&Input/Peer2PeerTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
using System.Linq;
using CollectionExtensions;
using Mirror;
using Unity.Netcode;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.SceneManagement;
using NetworkClient = Mirror.NetworkClient;
using NetworkManager = Mirror.NetworkManager;

// TODO consider splitting this into match-specific state and general player metadata
public struct PlayerDetails
Expand Down Expand Up @@ -109,6 +112,10 @@ public InitializePlayerMessage(uint id, Vector3 position, Quaternion rotation)
public Quaternion rotation;
}

public struct ClientHasInitializedPlayersMessage : NetworkMessage {}

public struct StartRoundMessage : NetworkMessage {}

public enum PlayerType
{
Local,
Expand Down Expand Up @@ -179,6 +186,7 @@ public override void OnStartServer()
{
NetworkServer.RegisterHandler<PlayerConnectedMessage>(OnSpawnPlayerInput);
NetworkServer.RegisterHandler<UpdateLoadoutMessage>(OnReceiveUpdateLoadout);
NetworkServer.RegisterHandler<ClientHasInitializedPlayersMessage>(OnClientHasInitializedPlayers);

ResetState();
}
Expand All @@ -193,6 +201,7 @@ public override void OnClientConnect()
NetworkClient.RegisterHandler<InitialPlayerDetailsMessage>(OnReceivePlayerDetails);
NetworkClient.RegisterHandler<InitializePlayerMessage>(InitializeFPSPlayer);
NetworkClient.RegisterHandler<UpdatedPlayerDetailsMessage>(OnReceiveUpdatedPlayerDetails);
NetworkServer.RegisterHandler<StartRoundMessage>(OnStartRound);

// Send message for the input that we assume is registered
// TODO doesn't work if players haven't pressed a key yet
Expand Down Expand Up @@ -617,6 +626,7 @@ private IEnumerator SendSpawnRequestsAfterSceneLoad(string originalSceneName)

foreach (var p in players.Values.Where(p => p.type is PlayerType.Remote && !connectedPlayers.Contains(p.id)))
{
Debug.Log($"Spawning disconnected player {p.id}");
NetworkClient.Send(new SpawnPlayerMessage(p.id, PlayerType.Local));
}
}
Expand Down
6 changes: 5 additions & 1 deletion Assets/Scripts/Gamestate/PlayerIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public void UpdateFromDetails(PlayerDetails playerDetails, string name)
}

public override string ToString() => playerName;


#if UNITY_EDITOR
public string ToColorString() => $"<color=#{color.ToHexString()}>{playerName}</color>";
#else
public string ToColorString() => playerName;
#endif
}