public void SendInput(NetworkBase.PlayerInput input) { if (!isServer) { GameClient.lastInput = input; } }
void Update() { if (Pauzed.IsPauzed) { return; } bool doInput = NetworkControl || !OnlineGame; if ((OnlineGame && NetworkControl) || !OnlineGame || true)//!NetworkControl || true) { if (holdingJump) { if (jumpTimer > 0) { _rigid.gravityScale = jumpGravityScale; _rigid.AddForce(Vector2.up * HoldJumpForce * Time.deltaTime); } else { if (_rigid.velocity.y > -1) { SAnimation.PlayAnimation("Jump", Pcolor, false, 5, 3); } } //_currentJumpForce *= HoldJumpDecay; } else { _rigid.gravityScale = 1.0f; } if (jumpTimer > 0) { jumpTimer -= Time.deltaTime; } } DoAnimations(); if (!NetworkControl && OnlineGame) { if (!NetManager.isServer) { if (networkPositions.Count < 2) { return; } float renderingGameTime = NetworkBase.GameTimer - timeDelay; if (networkPositions.Count > 20) { networkPositions.RemoveRange(0, networkPositions.Count - 10); } NetworkPosition minClosest = FindClosestTime(renderingGameTime, false); NetworkPosition maxClosest = FindClosestTime(renderingGameTime, true); if (minClosest.GameTime == 0 || maxClosest.GameTime == 0) { return; } float percentage = (renderingGameTime - minClosest.GameTime) / (maxClosest.GameTime - minClosest.GameTime); Vector2 newPos = minClosest.Position + ((maxClosest.Position - minClosest.Position) * percentage); Vector2 newVel = minClosest.Velocity + ((maxClosest.Velocity - minClosest.Velocity) * percentage); if (newPos.x != newPos.x || newPos.y != newPos.y) { return; //NaN } //Debug.Log(renderingGameTime + " - " + minClosest.GameTime + " - " + maxClosest.GameTime + " : " + percentage); //newVel = networkPositions[networkPositions.Count - 1].Velocity; //newPos = networkPositions[networkPositions.Count - 1].Position; _rigid.velocity = newVel; transform.localPosition = newPos; } return; } else if (OnlineGame) { if (GameClient.instance != null) { float time = NetworkBase.GameTimer - ((float)GameClient.instance.Ping / 1000f); NetworkPosition minClosest = FindClosestTime(time, false); NetworkPosition maxClosest = FindClosestTime(time, true); transform.position = minClosest.Position.magnitude == 0 ? (maxClosest.Position.magnitude == 0 ? (Vector2)transform.position : maxClosest.Position) : minClosest.Position; Debug.Log(transform.position.x + " -:- " + FindClosestTime(NetworkBase.GameTimer - GameClient.instance.Ping, false).Position.x + " : " + FindClosestTime(NetworkBase.GameTimer - GameClient.instance.Ping, true).Position.x); } } if ((NetManager.hasStarted || !OnlineGame) && doInput) { if (controls == Controls.CONTROLLER) { input = new NetworkBase.PlayerInput((int)playerID, InputManager.GetAxis("Horizontal", playerID), InputManager.GetButton("Jump", playerID), InputManager.GetButtonDown("Jump", playerID), InputManager.GetButtonUp("Jump", playerID), InputManager.GetButtonDown("Action", playerID)); } if (controls == Controls.WASD) { input = new NetworkBase.PlayerInput((int)playerID, Input.GetKey(KeyCode.A) ? -1 : (Input.GetKey(KeyCode.D) ? 1 : 0), Input.GetKey(KeyCode.W), Input.GetKeyDown(KeyCode.W), Input.GetKeyUp(KeyCode.W), Input.GetKeyDown(KeyCode.Space)); } if (controls == Controls.ARROWS) { input = new NetworkBase.PlayerInput((int)playerID, Input.GetKey(KeyCode.LeftArrow) ? -1 : (Input.GetKey(KeyCode.RightArrow) ? 1 : 0), Input.GetKey(KeyCode.UpArrow), Input.GetKeyDown(KeyCode.UpArrow), Input.GetKeyUp(KeyCode.UpArrow), Input.GetKeyDown(KeyCode.L)); } if (OnlineGame && ((input.xAxis != 0 || (oldInput != null && oldInput.xAxis != input.xAxis)) || input.Jump || input.JumpDown || input.JumpUp || input.Action) && !NetManager.isServer) { Debug.Log("sending input.."); NetManager.instance.SendInput(input); } oldInput = input; if (doInput) { SaveOldPosition(); } if (!canMove) { return; } if (StunnedTimer > 0) { StunnedTimer -= Time.deltaTime / 1.5f; return; } DoMovement(input); return; } //DoMovement(input); //return; //return; //TESTING if (!canMove) { return; } if (StunnedTimer > 0) { StunnedTimer -= Time.deltaTime / 1.5f; return; } if (doInput) { SaveOldPosition(); } bool grounded = false; float spriteXSizeHalf = gameObject.GetComponent <SpriteRenderer>().sprite.bounds.size.x / 2f; //racasts checking if player is standing on a block, casts from both edges of hitbox RaycastHit2D hit = Physics2D.Raycast((Vector2)transform.position - Vector2.up * 0.20f + new Vector2(spriteXSizeHalf - 0.05f, 0), -Vector2.up, 0.10f); //RaycastHit2D hit2 = Physics2D.Raycast((Vector2)transform.position - Vector2.up * 0.20f - new Vector2(gameObject.GetComponent<SpriteRenderer>().sprite.bounds.size.x / 2f, 0), -Vector2.up, 0.10f); //if ((hit.transform != null && hit.transform != transform) || (hit2.transform != null && hit2.transform != transform)) // grounded = true; if (hit.transform != null && hit.transform != transform) { grounded = true; } else { RaycastHit2D hit2 = Physics2D.Raycast((Vector2)transform.position - Vector2.up * 0.20f - new Vector2(spriteXSizeHalf - 0.05f, 0), -Vector2.up, 0.10f); if (hit2.transform != null && hit2.transform != transform) { grounded = true; } } if (SAnimation.GetAnimation() == "Jump") { int newFrame = _rigid.velocity.y > -1 ? (_rigid.velocity.y < 1 ? 3 : 2) : 4; if (SAnimation.GetFrame() != newFrame) { SAnimation.SetFrame(newFrame); } } //if (!Grounded && grounded) SAnimation.PlayAnimation("Idle", Pcolor); Grounded = grounded; /* * //Quickstop before input check * if ((QuickStop && grounded) || (!grounded && QuickStopAIR)) * { * _rigid.velocity = new Vector2(0, _rigid.velocity.y); * }*/ if (grounded || AirControl) { Vector2 movement = new Vector2(0, _rigid.velocity.y); //TODO change x back to 0 //if (Input.GetKey(controls == Controls.WASD ? KeyCode.A : KeyCode.LeftArrow) || InputManager.GetAxis("Horizontal", playerID) < 0) if (input.xAxis < 0) { if (doInput) { movement.x = Vector2.left.x * MoveSpeed; LastMovedRight = false; } } if (input.xAxis > 0) { if (doInput) { movement.x = Vector2.right.x * MoveSpeed; LastMovedRight = true; } } if (Grounded && !_isJumping) { if (movement.x != 0) { if (SAnimation.GetAnimation() != "Run") { SAnimation.PlayAnimation("Run", Pcolor, true, 16); } } else if (SAnimation.GetAnimation() != "Idle" && (SAnimation.GetAnimation() != "Spawn" || (SAnimation.GetAnimation() == "Spawn" && SAnimation.OnLastFrame()))) { SAnimation.PlayAnimation("Idle", Pcolor, true, 5); } } if (input.xAxis == 0 && NetworkControl && ((!QuickStopAIR && !grounded) || (!QuickStop && grounded))) { movement = _rigid.velocity; } _rigid.velocity = movement; } if (doInput) { //Debug.Log(InputManager.GetButtonDown("Jump", playerID)); if ((controls == Controls.WASD && Input.GetKeyUp(KeyCode.W)) || (controls == Controls.ARROWS && Input.GetKeyUp(KeyCode.UpArrow)) || InputManager.GetButtonUp("Jump", playerID) || (!grounded && _rigid.velocity.y < 0.1f && _rigid.velocity.y > -0.1f)) { _isJumping = false; //holdingJump = false; // _rigid.AddForce(Vector2.down * JumpENDForceDown); //_rigid.gravityScale = 1.0f; } if ((controls == Controls.WASD && Input.GetKeyDown(KeyCode.W)) || (controls == Controls.ARROWS && Input.GetKeyDown(KeyCode.UpArrow)) || InputManager.GetButtonDown("Jump", playerID)) { if (!_isJumping && (grounded || MultipleJumps)) { jumpTimer = HoldJumpMaxSec; _isJumping = true; //holdingJump = true; SAnimation.PlayAnimation("Jump", Pcolor, false, 5, 2, 0); SAnimation.SetFrame(2); //_currentJumpForce = (Physics.gravity * _rigid.mass).magnitude * 5f; _rigid.AddForce(Vector2.up * JumpForce); ScoreManager.gameData.Jumped++; } } } if (_isJumping) { if (jumpTimer > 0) { _rigid.gravityScale = jumpGravityScale; _rigid.AddForce(Vector2.up * HoldJumpForce * Time.deltaTime); } else { if (_rigid.velocity.y > -1) { SAnimation.PlayAnimation("Jump", Pcolor, false, 5, 3); } } //_currentJumpForce *= HoldJumpDecay; } else { _rigid.gravityScale = 1.0f; } if (jumpTimer > 0) { jumpTimer -= Time.deltaTime; } SpriteR.flipX = !LastMovedRight; overlay.flipX = !LastMovedRight; if ((controls == Controls.WASD && Input.GetKey(KeyCode.S)) || (controls == Controls.ARROWS && Input.GetKey(KeyCode.DownArrow)) || InputManager.GetAxis("Vertical", playerID) > 0) { if (SAnimation.GetAnimation() != "Jump") { SAnimation.PlayAnimation("Jump", Pcolor, false, 0, 7, 7); } SAnimation.SetFrame(5); } CapSpeed();//do this last in update }
public void DoMovement(NetworkBase.PlayerInput input) { if (!canMove) { return; } //if (NetworkControl) return; bool grounded = false; float spriteXSizeHalf = gameObject.GetComponent <SpriteRenderer>().sprite.bounds.size.x / 2f; //racasts checking if player is standing on a block, casts from both edges of hitbox RaycastHit2D hit = Physics2D.Raycast((Vector2)transform.position - Vector2.up * 0.20f + new Vector2(spriteXSizeHalf - 0.05f, 0), -Vector2.up, 0.10f); //RaycastHit2D hit2 = Physics2D.Raycast((Vector2)transform.position - Vector2.up * 0.20f - new Vector2(gameObject.GetComponent<SpriteRenderer>().sprite.bounds.size.x / 2f, 0), -Vector2.up, 0.10f); //if ((hit.transform != null && hit.transform != transform) || (hit2.transform != null && hit2.transform != transform)) // grounded = true; if (hit.transform != null && hit.transform != transform) { grounded = true; } else { RaycastHit2D hit2 = Physics2D.Raycast((Vector2)transform.position - Vector2.up * 0.20f - new Vector2(spriteXSizeHalf - 0.05f, 0), -Vector2.up, 0.10f); if (hit2.transform != null && hit2.transform != transform) { grounded = true; } } //if (!Grounded && grounded) SAnimation.PlayAnimation("Idle", Pcolor); Grounded = grounded; /* * //Quickstop before input check * if ((QuickStop && grounded) || (!grounded && QuickStopAIR)) * { * _rigid.velocity = new Vector2(0, _rigid.velocity.y); * }*/ if (grounded || AirControl) { Vector2 movement = new Vector2((_rigid.velocity.x < 0? Vector2.left.x : (_rigid.velocity.x > 0? Vector2.right.x : 0)) * MoveSpeed, _rigid.velocity.y); //if (Input.GetKey(controls == Controls.WASD ? KeyCode.A : KeyCode.LeftArrow) || InputManager.GetAxis("Horizontal", playerID) < 0) if (input.xAxis < 0) { movement.x = Vector2.left.x * MoveSpeed; LastMovedRight = false; } if (input.xAxis > 0) { movement.x = Vector2.right.x * MoveSpeed; LastMovedRight = true; } if (input.xAxis == 0) { movement.x = 0; } _rigid.velocity = movement; } if (input.JumpUp || (!grounded && _rigid.velocity.y < 0.1f && _rigid.velocity.y > -0.1f)) { _isJumping = false; holdingJump = false; // _rigid.AddForce(Vector2.down * JumpENDForceDown); //_rigid.gravityScale = 1.0f; } if (input.JumpDown || input.Jump) { if (!_isJumping && (grounded || MultipleJumps)) { jumpTimer = HoldJumpMaxSec; _isJumping = true; holdingJump = true; SAnimation.PlayAnimation("Jump", Pcolor, false, 5, 2, 0); SAnimation.SetFrame(2); //_currentJumpForce = (Physics.gravity * _rigid.mass).magnitude * 5f; _rigid.AddForce(Vector2.up * JumpForce); //ScoreManager.gameData.Jumped++; } } /* * if (input.Jump && _isJumping) holdingJump = true; * else holdingJump = false; */ }