public void FixedUpdate() { var c = pendingInputs.Count; NetworkingData.PlayerInputData[] datas = new NetworkingData.PlayerInputData[c]; for (int i = 0; i < c; i++) { datas[i] = pendingInputs.Dequeue(); } using (Message message = Message.Create((ushort)NetworkingData.Tags.PlayerInputs, new NetworkingData.PlayerInputDatas(datas))) { //NOTE uncomment if you want to see the last input sequence number sent by the client: Debug.Log($"last sent input sequence number: {inputData.InputSeq}"); ConnectionManager.Instance.Client.SendMessage(message, SendMode.Reliable); } }
public static Vector2 MovePlayer(NetworkingData.PlayerInputData input, Vector2 position, float timeStep) { //TODO not sure why we're getting a null reference occasionally here... punt for now, but investigate if (input.Keyinputs == null) { return(position); } Vector2 moveDirection = Vector2.Zero; bool w = input.Keyinputs[0]; bool a = input.Keyinputs[1]; bool s = input.Keyinputs[2]; bool d = input.Keyinputs[3]; bool space = input.Keyinputs[4]; if (w || a || s || d || space) { if (w) { moveDirection.Y = 1; } if (s) { moveDirection.Y = -1; } if (a) { moveDirection.X = -1; } if (d) { moveDirection.X = 1; } } position += moveDirection * timeStep * moveSpeed; return(position); }
static void UpdatePlayerPositions(Array players) { foreach (Player player in players) { uint lastProcessedInput = 0; if (player.inputBuffer.Count > 0) { int numInputs = player.inputBuffer.Count; for (int j = 0; j < numInputs; j++) { NetworkingData.PlayerInputData nextInput = player.inputBuffer.Dequeue(); //TODO we use the input's delta time but this gives the client a bit of authority //we should probably at least validate and throw invalid ones out, or verify their //total DT doesn't exceed the fixedupdate rate on the client of 1/60f player.ServerPosition = PlayerMovement.MovePlayer(nextInput, player.ServerPosition, nextInput.DeltaTime); player.LookDirection = nextInput.LookDirection; player.LastProcessedInput = nextInput.InputSeq; } } } }
public void Update() { if (isOwn) { //stuff starting here doesn't need to get synced to the server if (Input.GetKeyDown(KeyCode.Tab)) { ConsoleUtils.getInstance().toggle(); } float scrollAmt = Input.mouseScrollDelta.y; if (scrollAmt != 0) { float newSize = Camera.main.orthographicSize - scrollAmt * 2; if (MIN_CAMERA <= newSize && newSize <= MAX_CAMERA) { Camera.main.orthographicSize = newSize; } } //everything below this point needs to get synced to the server //LookDirection is 0: right, 1: down, 2: left, 3: up //animation frames is # frames for walk animation bool[] inputs = new bool[5]; inputs[0] = Input.GetKey(KeyCode.W); inputs[1] = Input.GetKey(KeyCode.A); inputs[2] = Input.GetKey(KeyCode.S); inputs[3] = Input.GetKey(KeyCode.D); inputs[4] = Input.GetKey(KeyCode.Space); byte lookDirection = 2; if (inputs[0]) { lookDirection = 3; } if (inputs[2]) { lookDirection = 1; } if (inputs[1]) { lookDirection = 2; } if (inputs[3]) { lookDirection = 0; } NetworkingData.PlayerInputData inputData = new NetworkingData.PlayerInputData(inputs, lookDirection, inputSeq, Time.deltaTime); if (inputs.Contains(true)) { //TODO can we do this differently to simplify to one list/queue pendingInputs.Enqueue(inputData); reconciliationInputs.Enqueue(inputData); var pos = PlayerMovement.MovePlayer(inputData, new System.Numerics.Vector2(transform.localPosition.x, transform.localPosition.y), Time.deltaTime); transform.localPosition = new Vector3(pos.X, pos.Y); rotateSprite(lookDirection); inputSeq++; //TODO remove me Debug.Log($"position updated to: {pos.X}, {pos.Y}"); } } }