Beispiel #1
0
    void Update()
    {
        PlayerState player;

        if (GameStateManager.Instance.gameState.players.TryGetValue(subscribedPlayerId, out player))
        {
            PlayerState.TransformState interpolated = player.Interpolate(Time.unscaledTime);
            transform.position = interpolated.position;
            transform.rotation = interpolated.rotation;
        }
        else
        {
            Destroy(gameObject);
        }
    }
        private void Update()
        {
            timeElapsedSinceLastTick += Time.unscaledDeltaTime;
            if (timeElapsedSinceLastTick >= tickTime)
            {
                timeElapsedSinceLastTick -= tickTime;
                if (_client != null)
                {
                    Packet clientTransformSnapshot = new Packet(PacketType.CLIENT_TRANSFORM);
                    byte   myPlayerId = GameStateManager.Instance.currentPlayerId;
                    if (GameStateManager.Instance.gameState.players.TryGetValue(myPlayerId, out PlayerState player))
                    {
                        PlayerState.TransformState myTransform = player.GetLastTransformState();
                        clientTransformSnapshot.Write(myTransform.position);
                        clientTransformSnapshot.Write(myTransform.rotation);
                        _client.SendPacketToServer(clientTransformSnapshot);
                    }
                }

                if (_server != null)
                {
                    Packet clientTransformsSnapshot = new Packet(PacketType.CLIENTS_TRANSFORMS);
                    clientTransformsSnapshot.WritePlayersDictionary(GameStateManager.Instance.gameState.players);
                    _server.SendPacketToAllClients(clientTransformsSnapshot);
                }
            }

            if (_clientPacketHandlers.Count > 0)
            {
                lock (_clientReceivedPackets)
                {
                    while (_clientReceivedPackets.Count > 0)
                    {
                        Packet msg = _clientReceivedPackets.Dequeue();
                        if (_clientPacketHandlers.ContainsKey(msg.Type))
                        {
                            _clientPacketHandlers[msg.Type](msg, _server, _client);
                        }
                        else
                        {
                            Debug.Log($"[NetworkManager:client] Couldn't handle msg {msg.Type}");
                        }
                    }
                }
            }

            if (_serverPacketHandlers.Count > 0)
            {
                lock (_serverReceivedpackets)
                {
                    while (_serverReceivedpackets.Count > 0)
                    {
                        Packet msg = _serverReceivedpackets.Dequeue();
                        if (_serverPacketHandlers.ContainsKey(msg.Type))
                        {
                            _serverPacketHandlers[msg.Type](msg, _server, _client);
                        }
                        else
                        {
                            Debug.Log($"[NetworkManager:server] Couldn't handle msg {msg.Type}");
                        }
                    }
                }
            }
        }