This commit is contained in:
allylikesfrogs 2023-05-05 21:38:11 -07:00
commit d005a7057c

View File

@ -21,14 +21,9 @@ public class PlayerMovement : MonoBehaviour
//but can only be privately written to.
public bool IsFacingRight { get; private set; }
public bool IsJumping { get; private set; }
public bool IsWallJumping { get; private set; }
public bool IsSliding { get; private set; }
//Timers (also all fields, could be private and a method returning a bool could be used)
public float LastOnGroundTime { get; private set; }
public float LastOnWallTime { get; private set; }
public float LastOnWallRightTime { get; private set; }
public float LastOnWallLeftTime { get; private set; }
// trumpet
public int trumpet = 0;
@ -47,10 +42,6 @@ public class PlayerMovement : MonoBehaviour
private bool _isJumpFalling;
private bool isRegFalling;
//Wall Jump
private float _wallJumpStartTime;
private int _lastWallJumpDir;
private Vector2 _moveInput;
public float LastPressedJumpTime { get; private set; }
@ -121,28 +112,10 @@ public class PlayerMovement : MonoBehaviour
unlockedTrumpet = StateController.Instance.HasTrumpet();
#region TIMERS
LastOnGroundTime -= Time.deltaTime;
LastOnWallTime -= Time.deltaTime;
LastOnWallRightTime -= Time.deltaTime;
LastOnWallLeftTime -= Time.deltaTime;
LastPressedJumpTime -= Time.deltaTime;
#endregion
#region INPUT HANDLER
if (_moveInput.x != 0)
CheckDirectionToFace(_moveInput.x > 0);
#endregion
// if (!IsJumping)
// {
// print("not jumping");
// }
// else
// {
// print("jumping, " + RB.velocity.y);
// }
#region COLLISION CHECKS
if (!IsJumping)
{
@ -179,19 +152,6 @@ public class PlayerMovement : MonoBehaviour
isRegFalling = true;
}
}
//Right Wall Check
if (((Physics2D.OverlapBox(this.transform.position, _wallCheckSize, 0, _groundLayer) && IsFacingRight)
|| (Physics2D.OverlapBox(this.transform.position, _wallCheckSize, 0, _groundLayer) && !IsFacingRight)) && !IsWallJumping)
LastOnWallRightTime = Data.coyoteTime;
//Right Wall Check
if (((Physics2D.OverlapBox(this.transform.position, _wallCheckSize, 0, _groundLayer) && !IsFacingRight)
|| (Physics2D.OverlapBox(this.transform.position, _wallCheckSize, 0, _groundLayer) && IsFacingRight)) && !IsWallJumping)
LastOnWallLeftTime = Data.coyoteTime;
//Two checks needed for both left and right walls since whenever the play turns the wall checkPoints swap sides
LastOnWallTime = Mathf.Max(LastOnWallLeftTime, LastOnWallRightTime);
}
#endregion
@ -200,17 +160,10 @@ public class PlayerMovement : MonoBehaviour
{
IsJumping = false;
// print("isJumping " + IsJumping);
if (!IsWallJumping)
_isJumpFalling = true;
_isJumpFalling = true;
}
if (IsWallJumping && Time.time - _wallJumpStartTime > Data.wallJumpTime)
{
IsWallJumping = false;
}
if (LastOnGroundTime > 0 && !IsJumping && !IsWallJumping)
if (LastOnGroundTime > 0 && !IsJumping)
{
_isJumpCut = false;
@ -223,7 +176,6 @@ public class PlayerMovement : MonoBehaviour
if (CanJump() && LastPressedJumpTime > 0)
{
IsJumping = true;
IsWallJumping = false;
_isJumpCut = false;
_isJumpFalling = false;
@ -270,14 +222,6 @@ public class PlayerMovement : MonoBehaviour
}
#endregion
#region SLIDE CHECKS
if (CanSlide() && ((LastOnWallLeftTime > 0 && _moveInput.x < 0) || (LastOnWallRightTime > 0 && _moveInput.x > 0)))
IsSliding = true;
else
IsSliding = false;
#endregion
#region GRAVITY
//Higher gravity if we've released the jump input or are falling
// if (IsSliding)
@ -297,7 +241,7 @@ public class PlayerMovement : MonoBehaviour
SetGravityScale(Data.gravityScale * Data.jumpCutGravityMult);
RB.velocity = new Vector2(RB.velocity.x, Mathf.Max(RB.velocity.y, -Data.maxFallSpeed));
}
else if ((IsJumping || IsWallJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold)
else if ((IsJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold)
{
SetGravityScale(Data.gravityScale * Data.jumpHangGravityMult);
}
@ -343,15 +287,7 @@ public class PlayerMovement : MonoBehaviour
private void FixedUpdate()
{
//Handle Run
if (IsWallJumping)
Run(Data.wallJumpRunLerp);
else
Run(1);
//Handle Slide
if (IsSliding)
Slide();
Run(1);
}
#region INPUT CALLBACKS
@ -363,7 +299,7 @@ public class PlayerMovement : MonoBehaviour
public void OnJumpUpInput()
{
if (CanJumpCut() || CanWallJumpCut())
if (CanJumpCut())
_isJumpCut = true;
}
#endregion
@ -405,7 +341,7 @@ public class PlayerMovement : MonoBehaviour
#region Add Bonus Jump Apex Acceleration
//Increase are acceleration and maxSpeed when at the apex of their jump, makes the jump feel a bit more bouncy, responsive and natural
if ((IsJumping || IsWallJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold)
if ((IsJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold)
{
accelRate *= Data.jumpHangAccelerationMult;
targetSpeed *= Data.jumpHangMaxSpeedMult;
@ -467,55 +403,10 @@ public class PlayerMovement : MonoBehaviour
RB.AddForce(Vector2.up * force, ForceMode2D.Impulse);
#endregion
}
private void WallJump(int dir)
{
//Ensures we can't call Wall Jump multiple times from one press
LastPressedJumpTime = 0;
LastOnGroundTime = 0;
LastOnWallRightTime = 0;
LastOnWallLeftTime = 0;
#region Perform Wall Jump
Vector2 force = new Vector2(Data.wallJumpForce.x, Data.wallJumpForce.y);
force.x *= dir; //apply force in opposite direction of wall
if (Mathf.Sign(RB.velocity.x) != Mathf.Sign(force.x))
force.x -= RB.velocity.x;
if (RB.velocity.y < 0) //checks whether player is falling, if so we subtract the velocity.y (counteracting force of gravity). This ensures the player always reaches our desired jump force or greater
force.y -= RB.velocity.y;
//Unlike in the run we want to use the Impulse mode.
//The default mode will apply are force instantly ignoring masss
//RB.AddForce(force, ForceMode2D.Impulse);
#endregion
}
#endregion
#region OTHER MOVEMENT METHODS
private void Slide()
{
//Works the same as the Run but only in the y-axis
//THis seems to work fine, buit maybe you'll find a better way to implement a slide into this system
float speedDif = Data.slideSpeed - RB.velocity.y;
float movement = speedDif * Data.slideAccel;
//So, we clamp the movement here to prevent any over corrections (these aren't noticeable in the Run)
//The force applied can't be greater than the (negative) speedDifference * by how many times a second FixedUpdate() is called. For more info research how force are applied to rigidbodies.
movement = Mathf.Clamp(movement, -Mathf.Abs(speedDif) * (1 / Time.fixedDeltaTime), Mathf.Abs(speedDif) * (1 / Time.fixedDeltaTime));
RB.AddForce(movement * Vector2.up);
}
#endregion
#region CHECK METHODS
public void CheckDirectionToFace(bool isMovingRight)
{
// if (isMovingRight != IsFacingRight)
// Turn();
}
private bool CanJump()
{
if (!IsGrounded() && trumpet > 0)
@ -525,30 +416,11 @@ public class PlayerMovement : MonoBehaviour
return LastOnGroundTime > 0 && !IsJumping;
}
private bool CanWallJump()
{
return LastPressedJumpTime > 0 && LastOnWallTime > 0 && LastOnGroundTime <= 0 && (!IsWallJumping ||
(LastOnWallRightTime > 0 && _lastWallJumpDir == 1) || (LastOnWallLeftTime > 0 && _lastWallJumpDir == -1));
}
private bool CanJumpCut()
{
return IsJumping && RB.velocity.y > 0;
}
private bool CanWallJumpCut()
{
return IsWallJumping && RB.velocity.y > 0;
}
public bool CanSlide()
{
if (LastOnWallTime > 0 && !IsJumping && !IsWallJumping && LastOnGroundTime <= 0)
return true;
else
return false;
}
public bool IsGrounded()
{
// print(Physics2D.OverlapBox(this.transform.position, _groundCheckSize, 0, _groundLayer) && !IsJumping);