/** Network command handling */ private void HandleJoinRequest(NetCommand.JoinRequest cmd, NetPeer peer) { // TODO: Validation should occur here, if any. var playerName = cmd.PlayerSetupData.Name; Debug.Log($"{playerName} connected to the server."); var metadata = new PlayerMetadata { Name = playerName, }; // Initialize the server player model - Peer ID is used as player ID always. var existingPlayers = playerManager.GetPlayers(); var player = CreateServerPlayer(peer, metadata); // Transmit existing player state to new player and new player state to // existing clients. Separate RPCs with the same payload are used so that // the joining player can distinguish their own player ID. var joinAcceptedCmd = new NetCommand.JoinAccepted { YourPlayerState = player.ToInitialPlayerState(), ExistingPlayerStates = existingPlayers.Select(p => p.ToInitialPlayerState()).ToArray(), WorldTick = simulation.WorldTick, }; var playerJoinedCmd = new NetCommand.PlayerJoined { PlayerState = player.ToInitialPlayerState(), }; netChannel.SendCommand(peer, joinAcceptedCmd); netChannel.BroadcastCommand(playerJoinedCmd, peer); }
private void HandleOtherPlayerJoined(NetCommand.PlayerJoined cmd) { Debug.Log($"{cmd.PlayerState.Metadata.Name} joined the server."); AddPlayerFromInitialServerState(cmd.PlayerState, true); }