added camera, better animations, first platform
This commit is contained in:
1 parent
0090d6d774
commit
00fbe12dba
17 files changed
+1794
-174
No files matched your search
@@ -5,24 +5,23 @@ namespace Code.Gameplay.Player
|
||||
{
|
||||
public class PlayerMovement : MonoBehaviour
|
||||
{
|
||||
[Header("References")]
|
||||
[SerializeField] private Rigidbody2D _rb;
|
||||
[SerializeField] private Transform _groundCheck;
|
||||
[SerializeField] private LayerMask _groundLayer;
|
||||
[SerializeField] private Animator _animator;
|
||||
|
||||
[Header("Movement")]
|
||||
[SerializeField] private float _moveSpeed = 5f;
|
||||
[SerializeField] private float _sprintSpeedMultiplier = 1.8f;
|
||||
[SerializeField] private float _jumpForce = 12f;
|
||||
|
||||
[Header("Ground Check")]
|
||||
[SerializeField] private float _groundCheckRadius = 0.2f;
|
||||
[SerializeField] private float _jumpDuration = 0.5f;
|
||||
|
||||
private IInputService _input;
|
||||
private bool _isGrounded;
|
||||
private bool _jumpRequested;
|
||||
private bool _isJumping;
|
||||
private float _jumpTimer;
|
||||
|
||||
public bool IsGrounded => _isGrounded;
|
||||
public bool isGrounded { get; private set; }
|
||||
|
||||
public void Construct(IInputService input)
|
||||
{
|
||||
@@ -31,8 +30,8 @@ namespace Code.Gameplay.Player
|
||||
|
||||
private void Update()
|
||||
{
|
||||
HandleInput();
|
||||
CheckGround();
|
||||
HandleInput();
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
@@ -42,6 +41,28 @@ namespace Code.Gameplay.Player
|
||||
}
|
||||
|
||||
private void HandleInput()
|
||||
{
|
||||
switch (_input.JumpPressed)
|
||||
{
|
||||
case true when isGrounded:
|
||||
_isJumping = true;
|
||||
_jumpTimer = 0f;
|
||||
_rb.linearVelocity = new Vector2(_rb.linearVelocity.x, _jumpForce);
|
||||
break;
|
||||
case false when _isJumping:
|
||||
{
|
||||
_isJumping = false;
|
||||
if (_rb.linearVelocity.y > 0)
|
||||
_rb.linearVelocity = new Vector2(_rb.linearVelocity.x, _rb.linearVelocity.y * 0.5f);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
UpdateFacingDirection();
|
||||
UpdateAnimations();
|
||||
}
|
||||
|
||||
private void UpdateFacingDirection()
|
||||
{
|
||||
transform.rotation = _input.MoveInput switch
|
||||
{
|
||||
@@ -49,42 +70,51 @@ namespace Code.Gameplay.Player
|
||||
< 0 => Quaternion.Euler(0, -180, 0),
|
||||
_ => transform.rotation
|
||||
};
|
||||
|
||||
if (_input.JumpPressed && !_jumpRequested)
|
||||
{
|
||||
_jumpRequested = true;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateAnimations()
|
||||
{
|
||||
var isMoving = Mathf.Abs(_input.MoveInput) > 0.1f;
|
||||
var isJumping = !isGrounded;
|
||||
|
||||
if (isJumping)
|
||||
_animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("Animations/penguin_jump_01");
|
||||
else if (isMoving)
|
||||
_animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("Animations/penguin_walk_01");
|
||||
else
|
||||
_animator.runtimeAnimatorController = Resources.Load<RuntimeAnimatorController>("Animations/penguin_idle_01");
|
||||
}
|
||||
|
||||
private void CheckGround()
|
||||
{
|
||||
_isGrounded = Physics2D.OverlapCircle(_groundCheck.position, _groundCheckRadius, _groundLayer);
|
||||
isGrounded = Physics2D.OverlapCircle(_groundCheck.position, _groundCheckRadius, _groundLayer);
|
||||
}
|
||||
|
||||
private void Move()
|
||||
{
|
||||
float moveInput = _input.MoveInput;
|
||||
float speed = _input.SprintHeld ? _moveSpeed * _sprintSpeedMultiplier : _moveSpeed;
|
||||
float targetVelocityX = moveInput * speed;
|
||||
var moveInput = _input.MoveInput;
|
||||
var speed = _input.SprintHeld ? _moveSpeed * _sprintSpeedMultiplier : _moveSpeed;
|
||||
var targetVelocityX = moveInput * speed;
|
||||
|
||||
// Плавное изменение скорости (опционально, можно и напрямую)
|
||||
_rb.linearVelocity = new Vector2(targetVelocityX, _rb.linearVelocity.y);
|
||||
}
|
||||
|
||||
private void HandleJump()
|
||||
{
|
||||
if (_jumpRequested && _isGrounded)
|
||||
if (!_isJumping || !_input.JumpPressed) return;
|
||||
|
||||
_jumpTimer += Time.fixedDeltaTime;
|
||||
if (!(_jumpTimer < _jumpDuration && _rb.linearVelocity.y > 0))
|
||||
{
|
||||
_rb.linearVelocity = new Vector2(_rb.linearVelocity.x, _jumpForce);
|
||||
_jumpRequested = false;
|
||||
_isJumping = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Для отладки
|
||||
|
||||
private void OnDrawGizmosSelected()
|
||||
{
|
||||
if (_groundCheck != null)
|
||||
Gizmos.DrawWireSphere(_groundCheck.position, _groundCheckRadius);
|
||||
if (_groundCheck == null) return;
|
||||
Gizmos.color = isGrounded ? Color.green : Color.red;
|
||||
Gizmos.DrawWireSphere(_groundCheck.position, _groundCheckRadius);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user