private void OnMessageReceived(byte[] message) { if (message[0] == MessageId.MATCH_END) { DIContainer.Logger.Debug("Match end message received, switching to MatchEndState"); this.matchStateMachine.ChangeMatchState(new EndMatchState()); return; } if (message[0] == MessageId.UNIT_STATE) { UnitStateMessage unitStateMessage = new UnitStateMessage(message); /*DIContainer.Logger.Debug(string.Format( * "Received unit state message = UnitId: '{0}' XPosition: '{1}' YPosition: '{2}' Rotation: '{3}' Frame: '{4}'", * unitStateMessage.UnitId, unitStateMessage.XPosition, unitStateMessage.YPosition, unitStateMessage.Rotation, unitStateMessage.Frame));*/ unitStateMessageBuffer[bufferCursor].Add(unitStateMessage); } if (message[0] == MessageId.POSITION_CONFIRMATION) { PositionConfirmationMessage positionConfirmationMessage = new PositionConfirmationMessage(message); PCMBuffer[bufferCursor].Add(positionConfirmationMessage); } if (message[0] == MessageId.UNIT_ABILITY_ACTIVATION) { UnitAbilityActivationMessage unitAbilityActivationMessage = new UnitAbilityActivationMessage(message); unitAbilityMessageBuffer[bufferCursor].Add(unitAbilityActivationMessage); } if (message[0] == MessageId.UNIT_SPAWN) { UnitSpawnMessage unitSpawnMessage = new UnitSpawnMessage(message); unitSpawnMessageBuffer[bufferCursor].Add(unitSpawnMessage); } }
private byte UpdateLocalPlayerState(List <PositionConfirmationMessage> receivedPositionConfirmationMessagesSinceLastFrame, byte currentTimebasedFrame) { if (localPlayer == null) { return(currentTimebasedFrame); } // so combined translation is max 1, so diagonal movement isn't faster. float[] cappedTranslations = MathHelper.GetCappedTranslations(inputProvider.XTranslation, inputProvider.YTranslation); byte inputFrame = (byte)MathHelper.Modulo(currentSimulationFrame + 1, byte.MaxValue); byte rotation = inputProvider.AbilityInputReceived ? inputProvider.GetSimulationAimingRotation() : inputProvider.GetSimulationRotation(); localPlayer.SetLocalFrameInput((int)Math.Round(playerMaxFrameSpeed * cappedTranslations[0]), (int)Math.Round(playerMaxFrameSpeed * cappedTranslations[1]), rotation, inputFrame); byte frameToProcess = inputFrame; // this means we skipped a frame, we need to create buffer entries for all frames though while (frameToProcess != currentTimebasedFrame) { frameToProcess = (byte)MathHelper.Modulo(frameToProcess + 1, byte.MaxValue); localPlayer.SetLocalFrameInput(0, 0, rotation, frameToProcess); } // sort by oldest frame to newest frame receivedPositionConfirmationMessagesSinceLastFrame.Sort((message1, message2) => { return(message1.Frame == message2.Frame ? 0 : MatchSimulationUnit.IsFrameInFuture(message1.Frame, message2.Frame) ? 1 : -1); }); for (int i = 0; i < receivedPositionConfirmationMessagesSinceLastFrame.Count; i++) { PositionConfirmationMessage positionConfirmationMessage = receivedPositionConfirmationMessagesSinceLastFrame[i]; if (positionConfirmationMessage.UnitId == localPlayer.UnitId) { localPlayer.SetConfirmedState(positionConfirmationMessage.XPosition, positionConfirmationMessage.YPosition, 0, 0, positionConfirmationMessage.Frame); } } if (inputProvider.AbilityInputReceived) { byte activationFrame = (byte)MathHelper.Modulo(inputFrame + 10, byte.MaxValue); localPlayer.AbilityActivationSubject.OnNext(new AbilityActivation { Rotation = inputProvider.AimingRotation, StartFrame = inputFrame, ActivationFrame = activationFrame }); } else if (inputProvider.AimingInputReceived) { localPlayer.LocalAimingSubject.OnNext(inputProvider.AimingRotation); } return(inputFrame); }