BGJ-2022.1/Assets/Scripts/Player/PlayerController.cs

307 lines
9.5 KiB
C#
Raw Normal View History

2022-02-21 20:40:03 +03:00
using System;
using Movement;
using UnityEngine;
using UnityEngine.InputSystem;
2022-02-22 11:49:48 +03:00
using PlayerInput = Input.PlayerInput;
2022-02-21 20:40:03 +03:00
namespace Player
{
2022-02-22 11:49:48 +03:00
public class PlayerController : MonoBehaviour, IMovement, PlayerInput.IPlayerControlActions
2022-02-21 20:40:03 +03:00
{
2022-02-22 23:49:06 +03:00
private const float DefaultJumpForce = 26.0f;
2022-02-22 19:43:05 +03:00
private const float DefaultMass = 80.0f;
2022-02-22 23:49:06 +03:00
private const float MaxSpeed = 700.0f;
private const float MaxAirSpeed = 500.0f;
private const float GravityScale = 6.0f;
2022-02-22 21:07:11 +03:00
private const float AccelerationScale = 40.0f;
private const float DecelerationScale = 60.0f;
private const float RespawnLimit = -60.0f;
2022-02-21 23:36:08 +03:00
2022-02-22 23:49:06 +03:00
//Took from tutorial : https://www.youtube.com/watch?v=7KiK0Aqtmzc
private const float FallMultiplier = 3.0f;
private const float LowJumpMultiplier = 40.0f;
2022-02-26 14:53:25 +03:00
2022-02-22 23:49:06 +03:00
//End of credits
2022-02-22 14:17:45 +03:00
private PlayerInput _controls;
2022-02-21 20:40:03 +03:00
private Rigidbody2D _playerRigidbody2D;
private SpriteRenderer _playerSpriteRenderer;
2022-02-26 17:39:34 +03:00
private AudioSource audioSource = null;
2022-02-22 14:17:45 +03:00
2022-02-21 23:36:08 +03:00
private CollisionChecker _playerGroundTrigger;
2022-02-22 23:49:06 +03:00
private CollisionChecker _playerWallTriggerLeft;
private CollisionChecker _playerWallTriggerRight;
2022-02-21 20:40:03 +03:00
private Orientation _orientation;
2022-02-21 20:40:03 +03:00
private bool _moveKeyPressed;
private bool _jumpKeyPressed;
private float _xAxisValue;
2022-02-22 23:49:06 +03:00
private float _speed;
2022-02-22 19:43:05 +03:00
private bool _isOnAir;
private bool _canJump;
private bool _afterMoving;
2022-02-21 20:40:03 +03:00
2022-02-26 14:53:25 +03:00
private const string IsMovingParameter = "IsMoving";
private const string IsGroundedParameter = "IsGrounded";
2022-02-26 16:48:44 +03:00
private const string IsWalledParameter = "IsWalled";
2022-02-26 14:53:25 +03:00
private Animator animator = null;
2022-02-26 18:31:19 +03:00
private Death death = null;
2022-02-26 19:00:15 +03:00
private Clop clop = null;
2022-02-26 18:31:19 +03:00
private CollisionChecker enemyTrigger = null;
2022-02-26 14:53:25 +03:00
2022-02-21 20:40:03 +03:00
private void Awake()
{
2022-02-22 14:17:45 +03:00
_playerRigidbody2D = GetComponent<Rigidbody2D>();
_playerSpriteRenderer = GetComponent<SpriteRenderer>();
2022-02-22 11:49:48 +03:00
_playerGroundTrigger = GameObject.Find("PlayerGroundTrigger").GetComponent<CollisionChecker>();
2022-02-22 23:49:06 +03:00
_playerWallTriggerLeft = GameObject.Find("PlayerWallTriggerLeft").GetComponent<CollisionChecker>();
_playerWallTriggerRight = GameObject.Find("PlayerWallTriggerRight").GetComponent<CollisionChecker>();
2022-02-21 23:36:08 +03:00
}
private void Start()
{
2022-02-22 11:49:48 +03:00
_playerRigidbody2D.mass = DefaultMass;
2022-02-22 19:43:05 +03:00
_playerRigidbody2D.gravityScale = GravityScale;
_orientation = gameObject.GetComponent<SpriteRenderer>().flipX switch
{
true => Orientation.Left,
false => Orientation.Right
};
2022-02-22 19:43:05 +03:00
_canJump = true;
2022-02-26 14:53:25 +03:00
animator = GetComponent<Animator>();
2022-02-26 17:39:34 +03:00
audioSource = GetComponent<AudioSource>();
2022-02-26 18:31:19 +03:00
death = transform.Find("Death").gameObject.GetComponent<Death>();
2022-02-26 19:00:15 +03:00
clop = transform.Find("Clop").gameObject.GetComponent<Clop>();
2022-02-26 18:31:19 +03:00
enemyTrigger = GameObject.Find("Enemy Trigger").GetComponent<CollisionChecker>();
2022-02-26 14:53:25 +03:00
2022-02-22 19:43:05 +03:00
BaseSpeed = 0.0f;
2022-02-21 20:40:03 +03:00
}
private void OnEnable()
{
if (_controls == null)
{
2022-02-22 11:49:48 +03:00
_controls = new PlayerInput();
2022-02-21 20:40:03 +03:00
_controls.PlayerControl.SetCallbacks(this);
}
_controls.PlayerControl.Enable();
}
private void OnDisable()
{
_controls.Disable();
}
private void FixedUpdate()
{
if (IsPaused)
return;
2022-02-22 23:49:06 +03:00
//Took from tutorial : https://www.youtube.com/watch?v=7KiK0Aqtmzc
switch (_playerRigidbody2D.velocity.y)
2022-02-22 14:17:45 +03:00
{
case < 0:
_playerRigidbody2D.velocity +=
Vector2.up * (Physics2D.gravity.y * (FallMultiplier - 1) * Time.fixedDeltaTime);
break;
case > 0 when !_jumpKeyPressed:
_playerRigidbody2D.velocity +=
Vector2.up * (Physics2D.gravity.y * (LowJumpMultiplier - 1) * Time.fixedDeltaTime);
break;
}
2022-02-22 23:49:06 +03:00
//End of credits
2022-02-22 14:17:45 +03:00
2022-02-21 20:40:03 +03:00
if (_moveKeyPressed)
Move(_xAxisValue);
else
2022-02-26 14:53:25 +03:00
{
2022-02-21 20:40:03 +03:00
_playerRigidbody2D.velocity = new Vector2(0.0f, _playerRigidbody2D.velocity.y);
2022-02-26 14:53:25 +03:00
animator.SetBool(IsMovingParameter, false);
}
2022-02-26 16:48:44 +03:00
2022-02-26 14:53:25 +03:00
animator.SetBool(IsGroundedParameter, _playerGroundTrigger.IsCollided);
2022-02-26 16:48:44 +03:00
animator.SetBool(IsWalledParameter, _playerWallTriggerLeft.IsCollided || _playerWallTriggerRight.IsCollided);
2022-02-21 20:40:03 +03:00
2022-02-22 23:49:06 +03:00
if (_jumpKeyPressed && _playerGroundTrigger.IsCollided && _canJump)
2022-02-21 20:40:03 +03:00
Jump();
if (_afterMoving)
DecelerationAfterMoving();
2022-02-21 20:40:03 +03:00
}
2022-02-22 14:17:45 +03:00
private void Update()
{
if (IsPaused)
return;
2022-02-22 19:43:05 +03:00
_isOnAir = !_playerGroundTrigger.IsCollided;
RespawnCheck();
2022-02-26 18:31:19 +03:00
if (enemyTrigger.IsCollided)
death.Die();
2022-02-22 19:43:05 +03:00
}
private void RespawnCheck()
{
2022-02-22 21:07:11 +03:00
if (gameObject.transform.position.y < RespawnLimit)
2022-02-26 18:31:19 +03:00
death.Die();
2022-02-22 14:17:45 +03:00
}
2022-02-21 20:40:03 +03:00
// PAUSE METHODS
2022-02-21 23:36:08 +03:00
public bool IsPaused { get; private set; }
2022-02-21 20:40:03 +03:00
public void Pause()
{
2022-02-21 23:36:08 +03:00
IsPaused = true;
_playerRigidbody2D.simulated = !IsPaused;
2022-02-21 20:40:03 +03:00
}
public void Resume()
{
2022-02-21 23:36:08 +03:00
IsPaused = false;
_playerRigidbody2D.simulated = !IsPaused;
2022-02-21 20:40:03 +03:00
}
// MOVE METHODS
2022-02-22 19:43:05 +03:00
public float BaseSpeed
{
2022-02-22 23:49:06 +03:00
get => _speed;
2022-02-22 19:43:05 +03:00
set
{
2022-02-22 23:49:06 +03:00
switch (_isOnAir)
{
2022-02-22 23:49:06 +03:00
case false when value > MaxSpeed:
_speed = MaxSpeed;
break;
case true when value > MaxAirSpeed:
_speed = MaxAirSpeed;
break;
default:
2022-02-26 14:53:25 +03:00
{
if (value < 0)
_speed = 0;
else
_speed = value;
break;
}
2022-02-22 23:49:06 +03:00
}
2022-02-22 19:43:05 +03:00
}
}
2022-02-21 20:40:03 +03:00
public void Move(float value)
{
2022-02-22 19:43:05 +03:00
switch (_xAxisValue)
2022-02-21 20:40:03 +03:00
{
2022-02-22 19:43:05 +03:00
case < 0:
_playerSpriteRenderer.flipX = true;
_orientation = Orientation.Left;
2022-02-22 23:49:06 +03:00
//prevent player from sticking to wall while moving to its direction in the air
if (!_playerGroundTrigger.IsCollided && _playerWallTriggerLeft.IsCollided)
return;
2022-02-22 19:43:05 +03:00
break;
case > 0:
_playerSpriteRenderer.flipX = false;
_orientation = Orientation.Right;
2022-02-22 23:49:06 +03:00
//prevent player from sticking to wall while moving to its direction in the air
if (!_playerGroundTrigger.IsCollided && _playerWallTriggerRight.IsCollided)
return;
2022-02-22 19:43:05 +03:00
break;
}
2022-02-22 23:49:06 +03:00
BaseSpeed += AccelerationScale;
2022-02-21 20:40:03 +03:00
_playerRigidbody2D.velocity = new Vector2(BaseSpeed * _xAxisValue * Time.fixedDeltaTime,
_playerRigidbody2D.velocity.y);
2022-02-26 14:53:25 +03:00
animator.SetBool(IsMovingParameter, true);
2022-02-21 20:40:03 +03:00
}
2022-02-22 19:43:05 +03:00
private void DecelerationAfterMoving()
{
2022-02-22 21:07:11 +03:00
BaseSpeed -= DecelerationScale;
_playerRigidbody2D.velocity = new Vector2(BaseSpeed * (int)_orientation * Time.fixedDeltaTime,
_playerRigidbody2D.velocity.y);
if (BaseSpeed == 0)
_afterMoving = false;
2022-02-22 19:43:05 +03:00
}
2022-02-21 20:40:03 +03:00
private void Jump()
{
2022-02-26 17:39:34 +03:00
audioSource.Play();
2022-02-22 23:49:06 +03:00
BaseSpeed = MaxAirSpeed;
2022-02-23 12:29:46 +03:00
_playerRigidbody2D.velocity = new Vector2(_playerRigidbody2D.velocity.x, DefaultJumpForce);
2022-02-22 19:43:05 +03:00
_canJump = false;
2022-02-21 20:40:03 +03:00
}
private void Climb()
{
throw new NotImplementedException();
}
// INPUT HANDLING
public void OnMove(InputAction.CallbackContext context)
{
switch (context.canceled)
{
case true:
_moveKeyPressed = false;
_afterMoving = true;
2022-02-21 20:40:03 +03:00
break;
case false:
_afterMoving = false;
2022-02-21 20:40:03 +03:00
_moveKeyPressed = true;
_xAxisValue = context.ReadValue<float>();
break;
}
}
public void OnJump(InputAction.CallbackContext context)
{
switch (context.canceled)
{
case true:
_jumpKeyPressed = false;
2022-02-22 19:43:05 +03:00
_canJump = true;
2022-02-21 20:40:03 +03:00
break;
case false:
2022-02-22 23:49:06 +03:00
if (_playerGroundTrigger.IsCollided)
_jumpKeyPressed = true;
2022-02-21 20:40:03 +03:00
break;
}
}
2022-02-22 11:49:48 +03:00
public void OnInteract(InputAction.CallbackContext context)
{
2022-02-22 21:58:50 +03:00
{
}
2022-02-22 11:49:48 +03:00
}
public void OnClimb(InputAction.CallbackContext context)
{
Climb();
}
private enum Orientation
{
Left = -1,
Right = 1
}
2022-02-26 19:00:15 +03:00
public void Clop() => clop.PlayClop();
public void OnPause(InputAction.CallbackContext context)
{
}
2022-02-21 20:40:03 +03:00
}
2022-02-26 14:53:25 +03:00
}