Пример #1
0
    // ===========================================================================================================
    // ===========================================================================================================

    // ============================================================
    // Connection
    // ============================================================

    private void ConnectPlayer(int playerId)
    {
        PlayerNetworkView player;

        if (_players.TryGetValue(playerId, out player))
        {
            DisconnectPlayer(player); // Disconnect player if already connected, and reconnect it.
        }

        var playerGameObject = Instantiate(PlayerPrefab);

        playerGameObject.name = string.Format("Player {0}", playerId);

        player          = playerGameObject.GetComponent <PlayerNetworkView>();
        player.PlayerId = playerId;
        PlayerPublic    = player;

        // Check if the connected player is the own player
        if (playerId.Equals(PlayerId))
        {
            _connected                       = true;
            _ownPlayerController             = playerGameObject.AddComponent <PlayerController>();
            _ownPlayerController.PlayerInput = new PlayerInput();
        }
        _players.Add(playerId, player);
    }
Пример #2
0
    public void ProcessPlayerDisconnected(PlayerDisconnectedMessage message)
    {
        PlayerNetworkView player = GetPlayerWithId(message.PlayerId);

        if (player != null)
        {
            DisconnectPlayer(player);
        }
    }
Пример #3
0
    public void ProcessPlayerDisconnected(PlayerDisconnectedMessage playerDisconnectedMessage)
    {
        int playerId             = playerDisconnectedMessage.PlayerId;
        PlayerNetworkView player = GetPlayerWithId(playerId);

        if (player != null)
        {
            players.Remove(player);
            Destroy(player);
        }
    }
Пример #4
0
    public void ProcessSnapshot(SnapshotMessage snapshotMessage)
    {
        List <PlayerData> playersData = snapshotMessage.GameSnapshot.Players;

        for (int i = 0; i < playersData.Count; i++)
        {
            PlayerData        playerData        = playersData[i];
            PlayerNetworkView playerNetworkView = GetPlayerWithId(playerData.PlayerId);
            if (playerNetworkView != null)
            {
                playerNetworkView.Load(playerData);
            }
        }
    }
Пример #5
0
    public void ProcessPlayerConnected(PlayerConnectedMessage playerConnectedMessage)
    {
        int        playerId = playerConnectedMessage.PlayerId;
        GameObject playerGO = Instantiate(playerPrefab) as GameObject;

        playerGO.name = "Player Network View " + playerId;
        PlayerNetworkView player = playerGO.GetComponent <PlayerNetworkView> ();

        player.Id = playerId;
        if (playerId == this.playerId)
        {
            localPlayerController = playerGO.AddComponent <PlayerController> ();
        }
        players.Add(player);
    }
Пример #6
0
    private void ConnectPlayer(int _playerId)
    {
        PlayerNetworkView player = GetPlayerWithId(_playerId);

        if (player != null)
        {
            DisconnectPlayer(player);
        }

        GameObject playerGO = Instantiate(playerPrefab) as GameObject;

        playerGO.name = PLAYER + " " + _playerId;
        player        = playerGO.GetComponent <PlayerNetworkView>();
        player.id     = _playerId;

        if (_playerId.Equals(playerId))
        {
            isConnected           = true;
            ownPlayer             = playerGO.AddComponent <ClientPlayerController>();
            ownPlayer.playerInput = new PlayerInput();
        }
        players.Add(player);
    }
Пример #7
0
    public void Interpolate()
    {
        UpdateSpeed();
        simTime += Time.deltaTime * simSpeed;
        RemoveOldSnapshots();
        UpdateSpeed();

        if (snapshots.Count > 1)
        {
            GameData start;
            GameData end;
            if (maxDiffTime < snapshots[snapshots.Count - 1].Time - simIniTime - simTime)
            {
                start   = snapshots [snapshots.Count - 1];
                end     = snapshots[snapshots.Count - 1];
                simTime = end.Time - simIniTime;
            }
            else
            {
                start = snapshots[0];
                end   = snapshots[1];
            }

            GameData          interpolated = InterpolateData(start, end);
            List <PlayerData> playersData  = interpolated.Players;
            foreach (PlayerData data in playersData)
            {
                int playerId             = data.PlayerId;
                PlayerNetworkView player = GetPlayerWithId(playerId);
                if (player == null)
                {
                    ConnectPlayer(playerId);
                }
                player.UpdatePosition(data.Position);
            }
        }
    }
Пример #8
0
 private void DisconnectPlayer(PlayerNetworkView player)
 {
     Destroy(player.gameObject);
     _players.Remove(player.PlayerId);
 }