Exemplo n.º 1
0
    /** 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);
    }
Exemplo n.º 2
0
    private void HandleJoinAccepted(NetCommand.JoinAccepted cmd)
    {
        Debug.Log("Server join successful!");

        // Create our player object and attach client-specific components.
        Debug.Log("Local player network ID is " + cmd.YourPlayerState.NetworkObjectState.NetworkId);
        localPlayer = AddPlayerFromInitialServerState(cmd.YourPlayerState, false);
        InitializeLocalPlayer();

        // Initialize simulation.
        simulation = new ClientSimulation(
            localPlayer, playerManager, networkObjectManager, this, bestServerLatency, cmd.WorldTick);

        // Create player objects for existing clients.
        foreach (var state in cmd.ExistingPlayerStates)
        {
            AddPlayerFromInitialServerState(state, true);
        }
    }