Example #1
0
 public void SetPlayerState(PlayerUpdatePacket packet)
 {
     PlayerState.Position = new Vector2(packet.XPosition, packet.YPosition);
     PlayerState.Rotation = packet.Rotation;
     PlayerState.Speed    = packet.Speed;
     // VELOCITY????/
 }
        public void ProcessInput(GameTime gameTime, InputInformation inputInfo)
        {
            // Is it time to send outgoing network packets?
            bool sendPacketThisFrame = false;

            framesSinceLastSend++;

            if (framesSinceLastSend >= Application.CLIENT_UPDATE_RATE)
            {
                sendPacketThisFrame = true;
                framesSinceLastSend = 0;
            }

            // Process and fetch input from local player
            KeyboardMovementInput condensedInput = ProcessInputForLocalPlayer(gameTime, inputInfo);

            // Build an update packet from the input and player values
            PlayerUpdatePacket packet = _localPlayer.BuildUpdatePacket();

            packet.DeltaTime      = (float)gameTime.ElapsedGameTime.TotalSeconds;
            packet.Input          = condensedInput;
            packet.SequenceNumber = _packetNumber++;

            // Add it to the queue
            _updatePackets.Enqueue(packet);

            if (sendPacketThisFrame)
            {
                // Send the packet to the server
                SendMessageToTheServer(packet, MessageType.GI_ClientSend_PlayerUpdate);
            }
        }
Example #3
0
        public PlayerUpdatePacket MakePlayerUpdatePacket(float xPosition, float yPosition, float speed, float rotation)
        {
            PlayerUpdatePacket packet = new PlayerUpdatePacket
            {
                SendDate  = DateTime.UtcNow,
                XPosition = xPosition,
                YPosition = yPosition,
                Speed     = speed,
                Rotation  = rotation
            };

            return(packet);
        }
        public void SetUpdatePacket(PlayerUpdatePacket updatePacket)
        {
            _updatePacket            = updatePacket;
            simulationState.Rotation = _updatePacket.Rotation;
            simulationState.Position = new Vector2(_updatePacket.XPosition, _updatePacket.YPosition);
            simulationState.Speed    = _updatePacket.Speed;

            if (Application.APPLY_ENTITY_INTERPOLATION)
            {
                // Start a new smoothing interpolation from our current
                // state toward this new state we just received.
                previousState     = PlayerState;
                _currentSmoothing = 1;
            }
            else
            {
#pragma warning disable CS0162
                _currentSmoothing = 0;
#pragma warning restore CS0162
            }

            // TODO: APPLY PREDICTION
            //ApplyPrediction(gameTime, latency, packetSendTime);
        }
        private void OnRecievedPlayerUpdatePacket(BasePacket packet)
        {
            PlayerUpdatePacket serverUpdate = (PlayerUpdatePacket)packet;

            if (Application.APPLY_SERVER_RECONCILLIATION &&
                serverUpdate.PlayerID == _localPlayer.NetworkID && serverUpdate.SequenceNumber >= 0 &&
                _updatePackets.Count > 0)
            {
                PlayerUpdatePacket localUpdate = GetUpdateAtSequenceNumber(serverUpdate.SequenceNumber);

                if (localUpdate.XPosition != serverUpdate.XPosition ||
                    localUpdate.YPosition != serverUpdate.YPosition ||
                    localUpdate.Rotation != serverUpdate.Rotation ||
                    localUpdate.Speed != serverUpdate.Speed)
                {
                    // Create a new queue with 'serverUpdate' as the first update
                    var newQueue   = new Queue <PlayerUpdatePacket>();
                    var updateList = new List <PlayerUpdatePacket>();

                    PlayerUpdatePacket removedPacket = _updatePackets.Dequeue(); // Remove the first one which we are replacing with the serverUpdate

                    serverUpdate.DeltaTime = removedPacket.DeltaTime;
                    newQueue.Enqueue(serverUpdate);
                    updateList.Add(serverUpdate);

                    while (_updatePackets.Count > 0)
                    {
                        PlayerUpdatePacket updatePacket = _updatePackets.Dequeue();
                        newQueue.Enqueue(updatePacket);
                        updateList.Add(updatePacket);
                    }

                    _updatePackets = newQueue; // Set the new queue

                    if (updateList.Count == 0)
                    {
                        return;
                    }

                    _localPlayer.SetPlayerState(updateList[0]);
                    _localPlayer.Update(updateList[0].DeltaTime);

                    if (updateList.Count == 1)
                    {
                        return;
                    }

                    // Now we must perform the previous inputs again
                    for (int i = 1; i < updateList.Count; i++)
                    {
                        _localPlayer.ApplyInputToPlayer(updateList[i].Input, updateList[i].DeltaTime);
                        _localPlayer.Update(updateList[i].DeltaTime);
                    }
                }
            }
            else
            {
                RemotePlayer remotePlayer = _players[serverUpdate.PlayerID] as RemotePlayer;
                remotePlayer.SetUpdatePacket(serverUpdate);
            }
        }