/** * \brief Update the status of the inputs of all registered players. */ public virtual void UpdateInputStatus() { for (int i = 0; i < _playerInputList.Count; i++) { RollbackInputBaseActions actionsValue = GetCurrentActionsValue(i); _playerInputList[i].value = actionsValue; } }
/** * \brief Called when changing Unity play mode state. */ private void LogPlayModeState(PlayModeStateChange playModeStateChange) { switch (playModeStateChange) { case PlayModeStateChange.EnteredPlayMode: _rollbackManager = RollbackManager.Instance; _rbBaseInput = new RollbackInputBaseActions(1 + _numOfInputs / 8); break; } }
public IEnumerator CheckMultipleInputCorrection() { //Setup RollbackManager with the input manager if (RollbackManager.Instance.GetComponent <SampleRollbackInputManager>() == null) { RollbackManager.Instance.gameObject.AddComponent <SampleRollbackInputManager>(); RollbackManager.Instance.ResetRbInputManagerEvents(); } RollbackManager.Instance.bufferSize = -1; RollbackManager.Instance.bufferRestriction = false; RollbackManager.Instance.registerFrames = false; RollbackManager.Instance.ClearRollbackManager(); Assert.True(RollbackManager.Instance.GetDisplayedFrameNum() == 0); Assert.True(RollbackManager.Instance.GetMaxFramesNum() == 0); int playerId = RollbackManager.rbInputManager.AddPlayer(); yield return(new WaitForSeconds(0.1f)); RollbackManager.Instance.Simulate(NumFramesToSimulate); Assert.True(RollbackManager.Instance.GetDisplayedFrameNum() == NumFramesToSimulate); yield return(new WaitForSeconds(0.1f)); //Correct input of a certain frame number RollbackElementRollbackInputBaseActions playerInputHistory = RollbackManager.rbInputManager.GetPlayerInputHistory(playerId); RollbackInputBaseActions rbInput = new RollbackInputBaseActions(5); rbInput.SetHorizontalAxis(1.0f); rbInput.SetVerticalAxis(1.0f); rbInput.SetBit(3); for (int i = 1; i < RollbackManager.Instance.GetDisplayedFrameNum(); i++) { Assert.True(playerInputHistory.CorrectValue(rbInput, i)); } //Resimulate frames yield return(new WaitForSeconds(0.1f)); RollbackManager.Instance.ReSimulate(NumFramesToSimulate); yield return(new WaitForSeconds(0.1f)); //Get corrected input for (int i = 1; i < RollbackManager.Instance.GetDisplayedFrameNum(); i++) { RollbackInputBaseActions rbCorrectedInput = RollbackManager.rbInputManager.GetPlayerInputHistory(playerId).GetValue(i); Assert.True(rbCorrectedInput.Equals(rbInput)); Assert.True(Math.Abs(rbCorrectedInput.GetHorizontalAxis() - 1.0f) < 0.001f); } }
protected override RollbackInputBaseActions GetCurrentActionsValue(int controllerId) { //Gather local inputs and execute them RollbackInputBaseActions actionsValue = new RollbackInputBaseActions((int)ActionsCode.LENGTH); SetBitFromKeycode((int)ActionsCode.UP, KeyCode.W, ref actionsValue); SetBitFromKeycode((int)ActionsCode.RIGHT, KeyCode.D, ref actionsValue); SetBitFromKeycode((int)ActionsCode.DOWN, KeyCode.S, ref actionsValue); SetBitFromKeycode((int)ActionsCode.LEFT, KeyCode.A, ref actionsValue); actionsValue.SetOrClearBit((int)ActionsCode.SHOOT, Input.GetMouseButton(1)); actionsValue.SetHorizontalAxis(Input.GetAxisRaw("Horizontal")); actionsValue.SetVerticalAxis(Input.GetAxisRaw("Vertical")); return(actionsValue); }
/// <summary>Creates the "custom content" Hashtable that is sent as position update.</summary> /// <remarks> /// As with event codes, the content of this event is arbitrary and "made up" for this demo. /// Your game (e.g.) could use floats as positions or you send a height and actions or state info. /// It makes sense to use numbers (best: bytes) as Hashtable key type, cause they are use less space. /// But this is not a requirement as you see in WriteEvColor. /// /// The position can only go up to 128 in this demo, so a byte[] technically is the best (leanest) /// choice here. /// </remarks> /// <returns>Hashtable for event "move" to update others</returns> public Hashtable WriteEvInput() { Hashtable evContent = new Hashtable(); int currentFrameNum = RollbackManager.Instance.GetIndexFrameNumFromStart(); int numFramesToSend = CustomConstants.NetworkBufferSize; if (numFramesToSend >= currentFrameNum) { numFramesToSend = currentFrameNum - 1; } evContent[0] = numFramesToSend; // Last x frames to pass through the evContent[1] = currentFrameNum; // Current frame number RollbackElementRollbackInputBaseActions playerInputHistory = RollbackManager.rbInputManager.GetPlayerInputHistory(ActorNumber - 1); for (int i = 0; i < numFramesToSend; i++) { RollbackInputBaseActions rollbackInputBaseActions = playerInputHistory.GetValue(currentFrameNum - i); evContent[2 + i] = rollbackInputBaseActions.PackBits(); } return(evContent); }
protected override RollbackInputBaseActions GetCurrentActionsValue(int controllerId) { if (controllerToPlayers.Count <= controllerId) { return(new RollbackInputBaseActions()); } InputDevice currentDevice = controllerToPlayers[controllerId].device; RollbackInputBaseActions actionsValue = new RollbackInputBaseActions(5); SetBitFromAction(InputActionManager.InputType.LEFT, ref actionsValue, currentDevice); SetBitFromAction(InputActionManager.InputType.RIGHT, ref actionsValue, currentDevice); SetBitFromAction(InputActionManager.InputType.UP, ref actionsValue, currentDevice); SetBitFromAction(InputActionManager.InputType.DOWN, ref actionsValue, currentDevice); SetBitFromAction(InputActionManager.InputType.SHOOT, ref actionsValue, currentDevice); actionsValue.SetHorizontalAxis(InputActionManager.GetAxis(InputActionManager.AxisType.HORIZONTAL, currentDevice)); actionsValue.SetVerticalAxis(InputActionManager.GetAxis(InputActionManager.AxisType.VERTICAL, currentDevice)); return(actionsValue); }
protected override RollbackInputBaseActions GetCurrentActionsValue(int controllerId) { if (controllerId == localPlayerId) { //Gather local inputs and execute them RollbackInputBaseActions actionsValue = new RollbackInputBaseActions((int)ActionsCode.LENGTH); SetBitFromKeycode((int)ActionsCode.UP, KeyCode.W, ref actionsValue); SetBitFromKeycode((int)ActionsCode.RIGHT, KeyCode.D, ref actionsValue); SetBitFromKeycode((int)ActionsCode.DOWN, KeyCode.S, ref actionsValue); SetBitFromKeycode((int)ActionsCode.LEFT, KeyCode.A, ref actionsValue); actionsValue.SetOrClearBit((int)ActionsCode.SHOOT, Input.GetMouseButton(1)); actionsValue.SetHorizontalAxis(Input.GetAxisRaw("Horizontal")); actionsValue.SetVerticalAxis(Input.GetAxisRaw("Vertical")); return(actionsValue); } else { //Predict the next input by copying the last input return(_playerInputList[controllerId].value); } }
// Start is called before the first frame update void Start() { _input1 = new RollbackInputBaseActions(5); _input2 = new RollbackInputBaseActions(5); }
/** * \brief Set and save the value of the player. * \param playerId ID of the player to add an input. * \param rbInputBaseActions inputs to save. */ public void AddInput(int playerId, RollbackInputBaseActions rbInputBaseActions) { _playerInputList[playerId].SetAndSaveValue(rbInputBaseActions); }
void SetBitFromKeycode(int inputIndex, KeyCode keyCode, ref RollbackInputBaseActions actionsValue) { actionsValue.SetOrClearBit(inputIndex, Input.GetKey(keyCode)); }
/// <summary>Reads the "custom content" Hashtable that is sent as position update.</summary> /// <returns>Hashtable for event "move" to update others</returns> public void ReadEvInput(Hashtable evContent) { int numFramesReceived = 0; if (evContent.ContainsKey((int)0)) { numFramesReceived = (int)evContent[0]; } if (numFramesReceived <= 0) { return; } int sentAtFrameNumber = 0; int currentFrame = RollbackManager.Instance.GetIndexFrameNumFromStart(); if (evContent.ContainsKey((int)1)) { sentAtFrameNumber = (int)evContent[1]; } int numDiffFramesWithPresent = currentFrame - sentAtFrameNumber; int applicableFrame = RollbackManager.Instance.GetMaxFramesNum() - numDiffFramesWithPresent; RollbackElementRollbackInputBaseActions playerInputHistory = RollbackManager.rbInputManager.GetPlayerInputHistory(ActorNumber - 1); //Correct inputs int backtrackNumFrames = -1; for (int i = 0; i < numFramesReceived; i++) { if (LastConfirmedFrame >= sentAtFrameNumber - i) { break; } RollbackInputBaseActions baseActions = new RollbackInputBaseActions(); baseActions.UnpackBits((byte[])evContent[2 + i]); //If return true, that means the correction was done if (playerInputHistory.CorrectValue(baseActions, applicableFrame - i)) { backtrackNumFrames = i + 1; } } // If at least a frame changed, we make a simulate if (backtrackNumFrames > -1) { //Predict new inputs from difference of receiving RollbackInputBaseActions lastInput = new RollbackInputBaseActions(); lastInput.UnpackBits((byte[])evContent[2]); // The latest frame is always on 2 for (int i = 0; i < numDiffFramesWithPresent; i++) { playerInputHistory.CorrectValue(lastInput, applicableFrame + i); } //Resimulate actions depending RollbackManager.Instance.ReSimulate(backtrackNumFrames + numDiffFramesWithPresent + 5); } this.LastConfirmedFrame = sentAtFrameNumber; this.LastUpdateFrame = GameLogic.Timestamp; }
void SetBitFromAction(InputActionManager.InputType inputType, ref RollbackInputBaseActions actionsValue, InputDevice inputDevice) { actionsValue.SetOrClearBit((int)inputType, InputActionManager.GetInput(inputType, inputDevice)); }
private void DisplayInputSimulation() { GUILayout.Label("Input simulations options", EditorStyles.boldLabel); EditorGUILayout.IntField("ControllerId : ", _controllerId); //Vertical axis input float verticalValue = _rbBaseInput.GetVerticalAxis(); verticalValue = EditorGUILayout.Slider("Vertical axis", verticalValue, -1f, 1f); _rbBaseInput.SetVerticalAxis(verticalValue); //Vertical axis input float horizontalValue = _rbBaseInput.GetHorizontalAxis(); horizontalValue = EditorGUILayout.Slider("Horizontal axis", horizontalValue, -1f, 1f); _rbBaseInput.SetHorizontalAxis(horizontalValue); //Button inputs GUILayout.Label("Buttons press options : ", EditorStyles.boldLabel); int oldNumOfInputs = _numOfInputs; _numOfInputs = EditorGUILayout.IntField("NumOfInputs : ", _numOfInputs); if (_numOfInputs != oldNumOfInputs) { _rbBaseInput = new RollbackInputBaseActions(1 + _numOfInputs / 8); } for (int i = 0; i < _numOfInputs; i++) { EditorGUILayout.BeginHorizontal(); bool initValue = _rbBaseInput.GetValueBit(i); initValue = EditorGUILayout.Toggle(_rollbackManager.GetRBInputManager().GetActionName(i), initValue); _rbBaseInput.SetOrClearBit(i, initValue); EditorGUILayout.EndHorizontal(); } //Correction of inputs if (GUILayout.Button("Correct Inputs")) { if (_rollbackManager != null) { RollbackInputBaseActions[] rbInputs = new RollbackInputBaseActions[_numFramesToSimulate]; for (int i = 0; i < _numFramesToSimulate; i++) { rbInputs[i] = _rbBaseInput; } DateTime currentTime = System.DateTime.Now; _rollbackManager.GetRBInputManager().CorrectInputs(_controllerId, _numFramesToSimulate, rbInputs); _rollbackManager.ReSimulate(_numFramesToSimulate); _spentTimeToResimulate = System.DateTime.Now - currentTime; } } if (_spentTimeToResimulate != null) { GUILayout.Label("Time to resimulate " + _numFramesToSimulate + " frames : " + _spentTimeToResimulate.TotalMilliseconds + "ms."); } }