public override void ProcessServerUpdate(StatePacket packet, Cell[,] maze) { if (packet.InputSequenceNumber <= LastSnapshotReceived) { return; } LastSnapshotReceived = packet.InputSequenceNumber; x = packet.X; y = packet.Y; rotation = packet.Rotation; // reconciliation for (int i = PendingInputs.Count - 1; i >= 0; i--) { InputPacket pending = PendingInputs[i]; if (pending.InputSequenceNumber <= packet.InputSequenceNumber) { PendingInputs.RemoveAt(i); } else { ApplyInput(pending, maze); } } }
public void ProcessInput(float dt, Client client, Cell[,] maze) { InputPacket?PackageInputData() { bool action = Input.Controllers[PlayerControllerIndex].IsPressed(InputKeys.Action); Vector2 movement = Input.Controllers[PlayerControllerIndex].GetDirectionVector(); if (movement.LengthSquared() == 0) { if (action) { return(new InputPacket(this.ID, this.InputSequenceNumber, 0, 0, true)); } else if (oldStateValid) { oldStateValid = false; return(new InputPacket(this.ID, this.InputSequenceNumber, 0, 0, false)); } } else { oldStateValid = true; movement *= dt; return(new InputPacket(this.ID, this.InputSequenceNumber, movement.X, movement.Y, action)); } return(null); } InputPacket?rawInputData = PackageInputData(); if (rawInputData == null) { return; } InputPacket inputData = (InputPacket)rawInputData; InputSequenceNumber++; NetDataWriter writer = inputData.Serialize(); // Send the packet client.Send(writer, SendOptions.ReliableOrdered); // Client-side prediction ApplyInput(inputData, maze); // Save the input packet for reconciliation PendingInputs.Add(inputData); }
private void ProcessInputPacket(InputPacket inputPacket) { if (world.Pjs.TryGetValue(inputPacket.CharacterID, out Pj pj)) { ((ServerPj)pj).LastProcessedInput = inputPacket.InputSequenceNumber; ((ServerPj)pj).LastProccessedInputTimestamp = (int)DateTime.UtcNow.Subtract(world.GameStartedTime).TotalMilliseconds; pj.ApplyInput(inputPacket, world.maze); if (inputPacket.Action) { if (pj.PowerUp != null && !pj.Stunned) { pj.PowerUp.Action(pj, this); RemovePowerUp(pj); } } } else { throw new System.ComponentModel.InvalidEnumArgumentException(); } }