using Code.Game.Gameplay.Player; using Code.Infrastructure.Camera; using Code.Infrastructure.Input; using Reflex.Attributes; using UnityEngine; using UnityEngine.InputSystem; namespace Code.Game.Cleaning.Player { public class PlayerControlsProvider : MonoBehaviour { [SerializeField] private MovementController movementController; private IInputService _inputService; private Vector2 _movementInput; private Transform _cameraTransform; [Inject] private void Construct(IInputService inputService, CameraStateMachine cameraStateMachine) { _cameraTransform = cameraStateMachine.Camera.transform; _inputService = inputService; _inputService.BindActionPerformed((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Movement, MovementPerformed); _inputService.BindActionCancelled((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Movement, MovementCancelled); } private void MovementCancelled(InputAction.CallbackContext obj) => _movementInput = Vector2.zero; private void MovementPerformed(InputAction.CallbackContext obj) => _movementInput = obj.ReadValue(); private void Update() { if (!_cameraTransform) return; var inputs = new PlayerCharacterInputs() { MoveAxisForward = _movementInput.y, MoveAxisRight = _movementInput.x, CameraRotation = _cameraTransform.rotation }; movementController.SetInputs(ref inputs); } private void OnDestroy() { _inputService.UnbindActionPerformed((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Movement, MovementPerformed); _inputService.UnbindActionCancelled((int)ActionMappings.OpenMovement, (int)OpenMovementActions.Movement, MovementCancelled); } } }