83 lines
1.7 KiB
C#
83 lines
1.7 KiB
C#
using System;
|
|
using UnityEngine;
|
|
using Vector2 = UnityEngine.Vector2;
|
|
|
|
public class PlayerMovement : MonoBehaviour
|
|
{
|
|
[Header("Config")]
|
|
[SerializeField] private float speed;
|
|
|
|
public Vector2 MoveDirection => moveDirection;
|
|
|
|
private PlayerAnimations playerAnimations;
|
|
private PlayerActions actions;
|
|
private Player player;
|
|
private Rigidbody2D rb2D;
|
|
private Vector2 moveDirection;
|
|
|
|
private bool isFrozen = false;
|
|
|
|
private void Awake()
|
|
{
|
|
player = GetComponent<Player>();
|
|
actions = new PlayerActions();
|
|
rb2D = GetComponent<Rigidbody2D>();
|
|
playerAnimations = GetComponent<PlayerAnimations>();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (isFrozen) return;
|
|
ReadMovement();
|
|
}
|
|
|
|
private void FixedUpdate()
|
|
{
|
|
if (isFrozen) return;
|
|
Move();
|
|
}
|
|
|
|
private void Move()
|
|
{
|
|
if (player.Stats.Health <= 0) return;
|
|
rb2D.MovePosition(rb2D.position + moveDirection * (speed * Time.fixedDeltaTime));
|
|
}
|
|
|
|
private void ReadMovement()
|
|
{
|
|
moveDirection = actions.Movement.Move.ReadValue<Vector2>().normalized;
|
|
|
|
if (moveDirection == Vector2.zero)
|
|
{
|
|
playerAnimations.SetMoveBoolTransition(false);
|
|
}
|
|
else
|
|
{
|
|
playerAnimations.SetMoveBoolTransition(true);
|
|
playerAnimations.SetMoveAnimation(moveDirection);
|
|
}
|
|
}
|
|
|
|
public void Freeze()
|
|
{
|
|
isFrozen = true;
|
|
moveDirection = Vector2.zero;
|
|
playerAnimations.SetMoveBoolTransition(false);
|
|
}
|
|
|
|
public void Unfreeze()
|
|
{
|
|
isFrozen = false;
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
actions.Enable();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
actions.Disable();
|
|
}
|
|
}
|