41 lines
1.9 KiB
C#
41 lines
1.9 KiB
C#
using Code.Infrastructure.Input;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.Example.Race.Racer.Player
|
|
{
|
|
public class PlayerCarInput : MonoBehaviour
|
|
{
|
|
private IInputService _inputService;
|
|
public Vector2 MovementInput { get; private set; }
|
|
public bool Brake { get; private set; }
|
|
|
|
[Inject]
|
|
public void Construct(IInputService inputService)
|
|
{
|
|
_inputService = inputService;
|
|
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
|
|
_inputService.BindActionCancel((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
|
|
_inputService.BindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakePerformed);
|
|
_inputService.BindActionCancel((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
|
|
}
|
|
|
|
private void MovementPerformed(InputAction.CallbackContext context) => MovementInput = context.ReadValue<Vector2>();
|
|
|
|
private void MovementEnded(InputAction.CallbackContext context) => MovementInput = Vector2.zero;
|
|
|
|
private void BrakePerformed(InputAction.CallbackContext context) => Brake = true;
|
|
|
|
private void BrakeEnded(InputAction.CallbackContext context) => Brake = false;
|
|
|
|
private void OnDestroy()
|
|
{
|
|
if (_inputService == null) return;
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementPerformed);
|
|
_inputService.UnbindActionCanceled((int)ActionMappings.Gameplay, (int)GameplayActions.Move, MovementEnded);
|
|
_inputService.UnbindActionPerformed((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakePerformed);
|
|
_inputService.UnbindActionCanceled((int)ActionMappings.Gameplay, (int)GameplayActions.Brake, BrakeEnded);
|
|
}
|
|
}
|
|
} |