170 lines
5.0 KiB
C#
170 lines
5.0 KiB
C#
using System;
|
|
using Movement;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
using PlayerInput = Input.PlayerInput;
|
|
|
|
namespace Player
|
|
{
|
|
public class PlayerController : MonoBehaviour, IMovement, PlayerInput.IPlayerControlActions
|
|
{
|
|
private const float DefaultJumpForce = 10.0f;
|
|
private const float DefaultMass = 40.0f;
|
|
private const float DefaultSpeed = 500.0f;
|
|
|
|
private const float FallMultiplier = 3f;
|
|
private const float LowJumpMultiplier = 3f;
|
|
|
|
private PlayerInput _controls;
|
|
private Rigidbody2D _playerRigidbody2D;
|
|
private SpriteRenderer _playerSpriteRenderer;
|
|
|
|
private CollisionChecker _playerGroundTrigger;
|
|
private CollisionChecker _playerInteractableTrigger;
|
|
|
|
private bool _moveKeyPressed;
|
|
private bool _jumpKeyPressed;
|
|
|
|
private bool _isOnAir;
|
|
|
|
private float _xAxisValue;
|
|
|
|
private void Awake()
|
|
{
|
|
_playerRigidbody2D = GetComponent<Rigidbody2D>();
|
|
_playerSpriteRenderer = GetComponent<SpriteRenderer>();
|
|
_playerGroundTrigger = GameObject.Find("PlayerGroundTrigger").GetComponent<CollisionChecker>();
|
|
_playerInteractableTrigger = GameObject.Find("PlayerInteractableTrigger").GetComponent<CollisionChecker>();
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
_playerRigidbody2D.mass = DefaultMass;
|
|
BaseSpeed = DefaultSpeed;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
if (_controls == null)
|
|
{
|
|
_controls = new PlayerInput();
|
|
_controls.PlayerControl.SetCallbacks(this);
|
|
}
|
|
|
|
_controls.PlayerControl.Enable();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
_controls.Disable();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
switch (_playerRigidbody2D.velocity.y)
|
|
{
|
|
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;
|
|
}
|
|
|
|
if (_moveKeyPressed)
|
|
Move(_xAxisValue);
|
|
else
|
|
_playerRigidbody2D.velocity = new Vector2(0.0f, _playerRigidbody2D.velocity.y);
|
|
|
|
//TODO prevent jumping while keep pressing the button
|
|
if (_jumpKeyPressed && _playerGroundTrigger.IsCollided)
|
|
Jump();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
//TODO calculate is on air based on wall and ground triggers
|
|
}
|
|
|
|
// PAUSE METHODS
|
|
|
|
public bool IsPaused { get; private set; }
|
|
|
|
public void Pause()
|
|
{
|
|
IsPaused = true;
|
|
_playerRigidbody2D.simulated = IsPaused;
|
|
}
|
|
|
|
public void Resume()
|
|
{
|
|
IsPaused = false;
|
|
_playerRigidbody2D.simulated = IsPaused;
|
|
}
|
|
|
|
// MOVE METHODS
|
|
|
|
public float BaseSpeed { get; set; }
|
|
|
|
public void Move(float value)
|
|
{
|
|
_playerSpriteRenderer.flipX = _xAxisValue switch
|
|
{
|
|
< 0 => true,
|
|
> 0 => false,
|
|
_ => _playerSpriteRenderer.flipX
|
|
};
|
|
_playerRigidbody2D.velocity = new Vector2(BaseSpeed * _xAxisValue * Time.fixedDeltaTime,
|
|
_playerRigidbody2D.velocity.y);
|
|
}
|
|
|
|
private void Jump()
|
|
{
|
|
//_playerRigidbody2D.velocity = new Vector2(_playerRigidbody2D.velocity.x, 0);
|
|
//_playerRigidbody2D.AddForce(Vector2.up.normalized * DefaultJumpForce * DefaultMass, ForceMode2D.Impulse);
|
|
_playerRigidbody2D.velocity = Vector2.up * DefaultJumpForce;
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
public void OnInteract(InputAction.CallbackContext context)
|
|
{
|
|
if (_playerInteractableTrigger.IsCollided)
|
|
throw new NotImplementedException();
|
|
}
|
|
}
|
|
} |