blocking in some more tile stuff!
This commit is contained in:
@@ -68,8 +68,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
[HideInInspector] private GameUIController gameUI;
|
||||
#endregion
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
private void Awake() {
|
||||
RB = GetComponent<Rigidbody2D>();
|
||||
playerBehavior = this.gameObject.GetComponent<PlayerBehavior>();
|
||||
grapplingRope = playerBehavior.grapplingRope;
|
||||
@@ -80,35 +79,28 @@ public class PlayerMovement : MonoBehaviour
|
||||
trumpetAnimationObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
private void Start() {
|
||||
SetGravityScale(Data.gravityScale);
|
||||
IsFacingRight = true;
|
||||
tempFallSpeed = Data.maxFallSpeed;
|
||||
}
|
||||
|
||||
void OnMove(InputValue value)
|
||||
{
|
||||
if (playerBehavior.playerIsAlive)
|
||||
{
|
||||
void OnMove(InputValue value) {
|
||||
if (playerBehavior.playerIsAlive) {
|
||||
this._moveInput = value.Get<Vector2>();
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
this._moveInput = Vector2.zero;
|
||||
}
|
||||
}
|
||||
|
||||
void OnJump()
|
||||
{
|
||||
if (playerBehavior.playerIsAlive)
|
||||
{
|
||||
void OnJump() {
|
||||
if (playerBehavior.playerIsAlive) {
|
||||
OnJumpInput();
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
private void Update() {
|
||||
unlockedTrumpet = StateController.Instance.HasTrumpet();
|
||||
#region TIMERS
|
||||
LastOnGroundTime -= Time.deltaTime;
|
||||
@@ -117,36 +109,29 @@ public class PlayerMovement : MonoBehaviour
|
||||
#endregion
|
||||
|
||||
#region COLLISION CHECKS
|
||||
if (!IsJumping)
|
||||
{
|
||||
if (!IsJumping) {
|
||||
//Ground Check
|
||||
if (IsGrounded()) //checks if set box overlaps with ground
|
||||
{
|
||||
if (IsGrounded()) {
|
||||
//checks if set box overlaps with ground
|
||||
LastOnGroundTime = Data.coyoteTime; //if so sets the lastGrounded to coyoteTime
|
||||
if (unlockedTrumpet)
|
||||
{
|
||||
if (unlockedTrumpet) {
|
||||
trumpet = 1;
|
||||
gameUI.ToggleTrumpet(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
trumpet = -1;
|
||||
}
|
||||
wasGrappling = false;
|
||||
isRegFalling = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
// print("not jumping");
|
||||
if (!_isJumpFalling && !isRegFalling)
|
||||
{
|
||||
if (unlockedTrumpet)
|
||||
{
|
||||
if (!_isJumpFalling && !isRegFalling) {
|
||||
if (unlockedTrumpet) {
|
||||
trumpet = 1;
|
||||
gameUI.ToggleTrumpet(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
trumpet = -1;
|
||||
}
|
||||
isRegFalling = true;
|
||||
@@ -156,15 +141,13 @@ public class PlayerMovement : MonoBehaviour
|
||||
#endregion
|
||||
|
||||
#region JUMP CHECKS
|
||||
if (IsJumping && RB.velocity.y <= 0)
|
||||
{
|
||||
if (IsJumping && RB.velocity.y <= 0) {
|
||||
IsJumping = false;
|
||||
// print("isJumping " + IsJumping);
|
||||
_isJumpFalling = true;
|
||||
}
|
||||
|
||||
if (LastOnGroundTime > 0 && !IsJumping)
|
||||
{
|
||||
if (LastOnGroundTime > 0 && !IsJumping) {
|
||||
_isJumpCut = false;
|
||||
|
||||
if (!IsJumping)
|
||||
@@ -173,8 +156,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
|
||||
//Jump
|
||||
if (CanJump() && LastPressedJumpTime > 0)
|
||||
{
|
||||
if (CanJump() && LastPressedJumpTime > 0) {
|
||||
IsJumping = true;
|
||||
_isJumpCut = false;
|
||||
_isJumpFalling = false;
|
||||
@@ -185,29 +167,25 @@ public class PlayerMovement : MonoBehaviour
|
||||
Jump();
|
||||
|
||||
// determine if trumpet jump
|
||||
if (!IsGrounded() && in_range && trumpet > 0 && !inCoyoteTime)
|
||||
{
|
||||
if (!IsGrounded() && in_range && trumpet > 0 && !inCoyoteTime) {
|
||||
StartCoroutine(ActivateTrumpetSprite());
|
||||
gameObject.transform.Find("Trumpet").GetComponent<AudioSource>().Play();
|
||||
enemy.GetComponent<EnemyPatrol>().DefeatEnemy();
|
||||
enemy = null;
|
||||
in_range = false;
|
||||
}
|
||||
else if (!IsGrounded() && !in_range && trumpet > 0 && !inCoyoteTime)
|
||||
{
|
||||
else if (!IsGrounded() && !in_range && trumpet > 0 && !inCoyoteTime) {
|
||||
trumpet -= 1;
|
||||
}
|
||||
// check if double jump, play sound
|
||||
if (trumpet == 0)
|
||||
{
|
||||
if (trumpet == 0) {
|
||||
StartCoroutine(ActivateTrumpetSprite());
|
||||
gameObject.transform.Find("Trumpet").GetComponent<AudioSource>().Play();
|
||||
}
|
||||
}
|
||||
|
||||
// stop sound if needed
|
||||
if (soundPlaying && (isRegFalling || IsJumping || _isJumpFalling))
|
||||
{
|
||||
if (soundPlaying && (isRegFalling || IsJumping || _isJumpFalling)) {
|
||||
print("footsteps stop");
|
||||
audioSource.Stop();
|
||||
soundPlaying = false;
|
||||
@@ -216,8 +194,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
#region GRAPPLE CHECKS
|
||||
// set wasGrappling to true if the player starts grappling
|
||||
if (grapplingRope.isGrappling)
|
||||
{
|
||||
if (grapplingRope.isGrappling) {
|
||||
wasGrappling = true;
|
||||
}
|
||||
#endregion
|
||||
@@ -228,49 +205,41 @@ public class PlayerMovement : MonoBehaviour
|
||||
// {
|
||||
// SetGravityScale(0);
|
||||
// }
|
||||
if (RB.velocity.y < 0 && _moveInput.y < 0)
|
||||
{
|
||||
if (RB.velocity.y < 0 && _moveInput.y < 0) {
|
||||
//Much higher gravity if holding down
|
||||
SetGravityScale(Data.gravityScale * Data.fastFallGravityMult);
|
||||
//Caps maximum fall speed, so when falling over large distances we don't accelerate to insanely high speeds
|
||||
RB.velocity = new Vector2(RB.velocity.x, Mathf.Max(RB.velocity.y, -Data.maxFastFallSpeed));
|
||||
}
|
||||
else if (_isJumpCut)
|
||||
{
|
||||
else if (_isJumpCut) {
|
||||
//Higher gravity if jump button released
|
||||
SetGravityScale(Data.gravityScale * Data.jumpCutGravityMult);
|
||||
RB.velocity = new Vector2(RB.velocity.x, Mathf.Max(RB.velocity.y, -Data.maxFallSpeed));
|
||||
}
|
||||
else if ((IsJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold)
|
||||
{
|
||||
else if ((IsJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold) {
|
||||
SetGravityScale(Data.gravityScale * Data.jumpHangGravityMult);
|
||||
}
|
||||
else if (RB.velocity.y < 0)
|
||||
{
|
||||
else if (RB.velocity.y < 0) {
|
||||
//Higher gravity if falling
|
||||
SetGravityScale(Data.gravityScale * Data.fallGravityMult);
|
||||
//Caps maximum fall speed, so when falling over large distances we don't accelerate to insanely high speeds
|
||||
RB.velocity = new Vector2(RB.velocity.x, Mathf.Max(RB.velocity.y, -Data.maxFallSpeed));
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
//Default gravity if standing on a platform or moving upwards
|
||||
SetGravityScale(Data.gravityScale);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region SOUND CHECKS
|
||||
if (!IsJumping && !_isJumpFalling && !isRegFalling && _moveInput.x != 0)
|
||||
{
|
||||
if (!soundPlaying)
|
||||
{
|
||||
if (!IsJumping && !_isJumpFalling && !isRegFalling && _moveInput.x != 0) {
|
||||
if (!soundPlaying) {
|
||||
// print("footsteps PLAY");
|
||||
audioSource.Play();
|
||||
soundPlaying = true;
|
||||
}
|
||||
}
|
||||
else if (soundPlaying && audioSource.clip.name == "footsteps")
|
||||
{
|
||||
else if (soundPlaying && audioSource.clip.name == "footsteps") {
|
||||
// print("footsteps stop");
|
||||
audioSource.Stop();
|
||||
soundPlaying = false;
|
||||
@@ -278,43 +247,38 @@ public class PlayerMovement : MonoBehaviour
|
||||
#endregion
|
||||
|
||||
#region UPDATE UI
|
||||
if (trumpet == 0)
|
||||
{
|
||||
if (trumpet == 0) {
|
||||
gameUI.ToggleTrumpet(false);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
private void FixedUpdate() {
|
||||
Run(1);
|
||||
}
|
||||
|
||||
#region INPUT CALLBACKS
|
||||
//Methods which whandle input detected in Update()
|
||||
public void OnJumpInput()
|
||||
{
|
||||
public void OnJumpInput() {
|
||||
LastPressedJumpTime = Data.jumpInputBufferTime;
|
||||
}
|
||||
|
||||
public void OnJumpUpInput()
|
||||
{
|
||||
if (CanJumpCut())
|
||||
public void OnJumpUpInput() {
|
||||
if (CanJumpCut()) {
|
||||
_isJumpCut = true;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region GENERAL METHODS
|
||||
public void SetGravityScale(float scale)
|
||||
{
|
||||
public void SetGravityScale(float scale) {
|
||||
RB.gravityScale = scale;
|
||||
}
|
||||
#endregion
|
||||
|
||||
//MOVEMENT METHODS
|
||||
#region RUN METHODS
|
||||
private void Run(float lerpAmount)
|
||||
{
|
||||
private void Run(float lerpAmount) {
|
||||
//Calculate the direction we want to move in and our desired velocity
|
||||
float targetSpeed = _moveInput.x * Data.runMaxSpeed;
|
||||
//We can reduce are control using Lerp() this smooths changes to are direction and speed
|
||||
@@ -325,24 +289,20 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
//Gets an acceleration value based on if we are accelerating (includes turning)
|
||||
//or trying to decelerate (stop). As well as applying a multiplier if we're air borne.
|
||||
if (LastOnGroundTime > 0)
|
||||
{
|
||||
if (LastOnGroundTime > 0) {
|
||||
accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount : Data.runDeccelAmount;
|
||||
}
|
||||
else if (wasGrappling)
|
||||
{
|
||||
else if (wasGrappling) {
|
||||
accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount * Data.accelInAir : Data.runDeccelAmount * (Data.deccelInAir / 5);
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
accelRate = (Mathf.Abs(targetSpeed) > 0.01f) ? Data.runAccelAmount * Data.accelInAir : Data.runDeccelAmount * Data.deccelInAir;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#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 || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold)
|
||||
{
|
||||
if ((IsJumping || _isJumpFalling) && Mathf.Abs(RB.velocity.y) < Data.jumpHangTimeThreshold) {
|
||||
accelRate *= Data.jumpHangAccelerationMult;
|
||||
targetSpeed *= Data.jumpHangMaxSpeedMult;
|
||||
}
|
||||
@@ -350,8 +310,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
#region Conserve Momentum
|
||||
//We won't slow the player down if they are moving in their desired direction but at a greater speed than their maxSpeed
|
||||
if ((Data.doConserveMomentum && Mathf.Abs(RB.velocity.x) > Mathf.Abs(targetSpeed) && Mathf.Sign(RB.velocity.x) == Mathf.Sign(targetSpeed) && Mathf.Abs(targetSpeed) > 0.01f && LastOnGroundTime < 0) || grapplingRope.isGrappling)
|
||||
{
|
||||
if ((Data.doConserveMomentum && Mathf.Abs(RB.velocity.x) > Mathf.Abs(targetSpeed) && Mathf.Sign(RB.velocity.x) == Mathf.Sign(targetSpeed) && Mathf.Abs(targetSpeed) > 0.01f && LastOnGroundTime < 0) || grapplingRope.isGrappling) {
|
||||
//Prevent any deceleration from happening, or in other words conserve are current momentum
|
||||
//You could experiment with allowing for the player to slightly increae their speed whilst in this "state"
|
||||
accelRate = 0;
|
||||
@@ -374,8 +333,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
*/
|
||||
}
|
||||
|
||||
private void Turn()
|
||||
{
|
||||
private void Turn() {
|
||||
//stores scale and flips the player along the x axis,
|
||||
Vector3 scale = transform.localScale;
|
||||
scale.x *= -1;
|
||||
@@ -386,8 +344,7 @@ public class PlayerMovement : MonoBehaviour
|
||||
#endregion
|
||||
|
||||
#region JUMP METHODS
|
||||
private void Jump()
|
||||
{
|
||||
private void Jump() {
|
||||
//Ensures we can't call Jump multiple times from one press
|
||||
LastPressedJumpTime = 0;
|
||||
LastOnGroundTime = 0;
|
||||
@@ -397,9 +354,9 @@ public class PlayerMovement : MonoBehaviour
|
||||
//This means we'll always feel like we jump the same amount
|
||||
//(setting the player's Y velocity to 0 beforehand will likely work the same, but I find this more elegant :D)
|
||||
float force = Data.jumpForce;
|
||||
if (RB.velocity.y < 0)
|
||||
if (RB.velocity.y < 0) {
|
||||
force -= RB.velocity.y;
|
||||
|
||||
}
|
||||
RB.AddForce(Vector2.up * force, ForceMode2D.Impulse);
|
||||
#endregion
|
||||
}
|
||||
@@ -407,41 +364,34 @@ public class PlayerMovement : MonoBehaviour
|
||||
|
||||
|
||||
#region CHECK METHODS
|
||||
private bool CanJump()
|
||||
{
|
||||
if (!IsGrounded() && trumpet > 0)
|
||||
{
|
||||
private bool CanJump() {
|
||||
if (!IsGrounded() && trumpet > 0) {
|
||||
return true;
|
||||
}
|
||||
return LastOnGroundTime > 0 && !IsJumping;
|
||||
}
|
||||
|
||||
private bool CanJumpCut()
|
||||
{
|
||||
private bool CanJumpCut() {
|
||||
return IsJumping && RB.velocity.y > 0;
|
||||
}
|
||||
|
||||
public bool IsGrounded()
|
||||
{
|
||||
public bool IsGrounded() {
|
||||
// print(Physics2D.OverlapBox(this.transform.position, _groundCheckSize, 0, _groundLayer) && !IsJumping);
|
||||
return (Physics2D.OverlapBox(new Vector2(this.transform.position.x, this.transform.position.y - _groundCheckOffset), _groundCheckSize, 0, _groundLayer) && !IsJumping);
|
||||
}
|
||||
|
||||
public void FloatGravity(float grav)
|
||||
{
|
||||
public void FloatGravity(float grav) {
|
||||
Data.maxFallSpeed = grav;
|
||||
}
|
||||
|
||||
public void EndFloatGravity()
|
||||
{
|
||||
public void EndFloatGravity() {
|
||||
Data.maxFallSpeed = tempFallSpeed;
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
#region EDITOR METHODS
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
private void OnDrawGizmosSelected() {
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawWireCube(new Vector2(this.transform.position.x, this.transform.position.y - _groundCheckOffset), _groundCheckSize);
|
||||
Gizmos.color = Color.blue;
|
||||
@@ -451,10 +401,8 @@ public class PlayerMovement : MonoBehaviour
|
||||
#endregion
|
||||
|
||||
#region ADDITIONAL TRUMPET METHODS
|
||||
IEnumerator ActivateTrumpetSprite()
|
||||
{
|
||||
if (!trumpetActive)
|
||||
{
|
||||
IEnumerator ActivateTrumpetSprite() {
|
||||
if (!trumpetActive) {
|
||||
trumpetActive = true;
|
||||
trumpetSprite.enabled = true;
|
||||
trumpetAnimationObject.SetActive(true);
|
||||
|
||||
Reference in New Issue
Block a user