Implement movement input handling and update UI components for treasure collection
This commit is contained in:
@@ -17,6 +17,7 @@ public class PlayerController : Character
|
||||
private InputManager _inputManager;
|
||||
private PlayerState _playerState;
|
||||
private HammerThrower _hammerThrower;
|
||||
private Vector2 _currentMovement;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
@@ -25,6 +26,7 @@ public class PlayerController : Character
|
||||
_hammerThrower = GetComponent<HammerThrower>();
|
||||
|
||||
_inputManager.OnFire += OnFireButtonPressed;
|
||||
_inputManager.OnMovementChanged += OnMovementChanged;
|
||||
base.Init();
|
||||
}
|
||||
|
||||
@@ -38,6 +40,15 @@ public class PlayerController : Character
|
||||
_inputManager.OnDisable();
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (_inputManager != null)
|
||||
{
|
||||
_inputManager.OnFire -= OnFireButtonPressed;
|
||||
_inputManager.OnMovementChanged -= OnMovementChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (_hammer == null && !_isHoldingHammer)
|
||||
@@ -46,11 +57,15 @@ public class PlayerController : Character
|
||||
_isHoldingHammer = true;
|
||||
}
|
||||
|
||||
Vector2 move = _inputManager.Movement;
|
||||
MoveTo(move.x, isAllowVertical ? move.y : 0);
|
||||
MoveTo(_currentMovement.x, isAllowVertical ? _currentMovement.y : 0);
|
||||
_hammerThrower.SetFacingDirection(_facingRight);
|
||||
}
|
||||
|
||||
private void OnMovementChanged(Vector2 movement)
|
||||
{
|
||||
_currentMovement = movement;
|
||||
}
|
||||
|
||||
private void OnFireButtonPressed()
|
||||
{
|
||||
if (_hammerThrower.CanThrow)
|
||||
|
||||
@@ -7,6 +7,7 @@ public class InputManager : MonoBehaviour
|
||||
private InputActions _actions;
|
||||
|
||||
public Vector2 Movement { get; private set; }
|
||||
public event Action<Vector2> OnMovementChanged;
|
||||
public event Action OnFire;
|
||||
|
||||
private void Awake()
|
||||
@@ -14,6 +15,7 @@ public class InputManager : MonoBehaviour
|
||||
_actions = new InputActions();
|
||||
|
||||
// Movement
|
||||
_actions.Player.Movement.started += OnMovementPerformed;
|
||||
_actions.Player.Movement.performed += OnMovementPerformed;
|
||||
_actions.Player.Movement.canceled += OnMovementCanceled;
|
||||
|
||||
@@ -31,16 +33,21 @@ public class InputManager : MonoBehaviour
|
||||
|
||||
public void OnDisable()
|
||||
{
|
||||
_actions.Disable();
|
||||
if (_actions != null)
|
||||
{
|
||||
_actions.Disable();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMovementPerformed(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Movement = ctx.ReadValue<Vector2>();
|
||||
OnMovementChanged?.Invoke(Movement);
|
||||
}
|
||||
|
||||
private void OnMovementCanceled(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Movement = Vector2.zero;
|
||||
OnMovementChanged?.Invoke(Movement);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user