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 00:48:53 +03:00
|
|
|
private const float DefaultJumpForce = 9.81f;
|
2022-02-21 23:36:08 +03:00
|
|
|
private const float DefaultMass = 80.0f;
|
|
|
|
private const float DefaultSpeed = 500.0f;
|
2022-02-22 00:48:53 +03:00
|
|
|
private const float GravityScale = 1.0f;
|
2022-02-21 23:36:08 +03:00
|
|
|
|
2022-02-21 20:40:03 +03:00
|
|
|
private Input.PlayerInput _controls;
|
|
|
|
private Rigidbody2D _playerRigidbody2D;
|
|
|
|
private SpriteRenderer _playerSpriteRenderer;
|
2022-02-21 23:36:08 +03:00
|
|
|
private CollisionChecker _playerGroundTrigger;
|
2022-02-21 20:40:03 +03:00
|
|
|
|
|
|
|
private bool _moveKeyPressed;
|
|
|
|
private bool _jumpKeyPressed;
|
|
|
|
|
|
|
|
private bool _isOnAir;
|
|
|
|
|
|
|
|
private float _xAxisValue;
|
2022-02-22 11:49:48 +03:00
|
|
|
private PlayerInput.IPlayerControlActions _playerControlActionsImplementation;
|
2022-02-21 20:40:03 +03:00
|
|
|
|
|
|
|
private void Awake()
|
|
|
|
{
|
2022-02-22 11:49:48 +03:00
|
|
|
_playerRigidbody2D = gameObject.GetComponent<Rigidbody2D>();
|
|
|
|
_playerGroundTrigger = GameObject.Find("PlayerGroundTrigger").GetComponent<CollisionChecker>();
|
|
|
|
_playerSpriteRenderer = gameObject.GetComponent<SpriteRenderer>();
|
2022-02-21 23:36:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
{
|
2022-02-22 11:49:48 +03:00
|
|
|
_playerRigidbody2D.gravityScale = GravityScale;
|
|
|
|
_playerRigidbody2D.mass = DefaultMass;
|
2022-02-21 23:36:08 +03:00
|
|
|
BaseSpeed = DefaultSpeed;
|
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 (_moveKeyPressed)
|
|
|
|
Move(_xAxisValue);
|
|
|
|
else
|
|
|
|
_playerRigidbody2D.velocity = new Vector2(0.0f, _playerRigidbody2D.velocity.y);
|
|
|
|
|
2022-02-21 23:36:08 +03:00
|
|
|
if (_jumpKeyPressed && _playerGroundTrigger.IsCollided)
|
2022-02-21 20:40:03 +03:00
|
|
|
Jump();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
2022-02-22 11:49:48 +03:00
|
|
|
_playerRigidbody2D.simulated = IsPaused;
|
2022-02-21 20:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Resume()
|
|
|
|
{
|
2022-02-21 23:36:08 +03:00
|
|
|
IsPaused = false;
|
2022-02-22 11:49:48 +03:00
|
|
|
_playerRigidbody2D.simulated = IsPaused;
|
2022-02-21 20:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MOVE METHODS
|
|
|
|
|
|
|
|
public float BaseSpeed { get; set; }
|
|
|
|
|
|
|
|
public void Move(float value)
|
|
|
|
{
|
2022-02-22 00:48:53 +03:00
|
|
|
_playerSpriteRenderer.flipX = _xAxisValue switch
|
2022-02-21 20:40:03 +03:00
|
|
|
{
|
2022-02-22 00:48:53 +03:00
|
|
|
< 0 => true,
|
|
|
|
> 0 => false,
|
|
|
|
_ => _playerSpriteRenderer.flipX
|
|
|
|
};
|
2022-02-21 20:40:03 +03:00
|
|
|
_playerRigidbody2D.velocity = new Vector2(BaseSpeed * _xAxisValue * Time.fixedDeltaTime,
|
|
|
|
_playerRigidbody2D.velocity.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void Jump()
|
|
|
|
{
|
2022-02-21 23:36:08 +03:00
|
|
|
_playerRigidbody2D.velocity = new Vector2(_playerRigidbody2D.velocity.x, 0);
|
2022-02-22 00:48:53 +03:00
|
|
|
_playerRigidbody2D.AddForce(Vector2.up.normalized * DefaultJumpForce * DefaultMass, ForceMode2D.Impulse);
|
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;
|
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
_moveKeyPressed = true;
|
|
|
|
_xAxisValue = context.ReadValue<float>();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnJump(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
switch (context.canceled)
|
|
|
|
{
|
|
|
|
case true:
|
|
|
|
_jumpKeyPressed = false;
|
|
|
|
break;
|
|
|
|
case false:
|
|
|
|
_jumpKeyPressed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 11:49:48 +03:00
|
|
|
|
|
|
|
public void OnInteract(InputAction.CallbackContext context)
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
2022-02-21 20:40:03 +03:00
|
|
|
}
|
|
|
|
}
|