Files
2026-09-21 12:15:57 +03:00

64 lines
2.2 KiB
C#

using Code.D.Cleaning.CleanTools;
using Code.Infrastructure.Camera;
using Code.Infrastructure.Input;
using Reflex.Attributes;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Code.D.Player
{
public class PlayerInteractor : MonoBehaviour
{
private IInputService _inputService;
private LayerMask _cleanToolInteractableMask;
private Transform _cameraTransform;
private CleanToolInteractable _lastCleanToolInteractable;
[SerializeField] private float interactionDistance = 5f;
[SerializeField] private PlayerEquipment playerEquipment;
private void Awake()
{
_cleanToolInteractableMask = LayerMask.GetMask("CleanToolInteractable");
}
[Inject]
private void Construct(IInputService inputService, CameraStateMachine cameraStateMachine)
{
_cameraTransform = cameraStateMachine.Camera.transform;
_inputService = inputService;
// inputService.BindActionPerformed((int)ActionMappings.Cleaning, (int)CleaningActions.Interact, TryInteract);
}
private void OnDestroy()
{
// _inputService.UnbindActionPerformed((int)ActionMappings.Cleaning, (int)CleaningActions.Interact,
// TryInteract);
}
private void TryInteract(InputAction.CallbackContext obj)
{
if (!Physics.Raycast(
_cameraTransform.position,
_cameraTransform.forward,
out var info,
interactionDistance,
layerMask: _cleanToolInteractableMask)) return;
CleanToolInteractable interactable;
if (_lastCleanToolInteractable is not null &&
info.collider.GetEntityId() == _lastCleanToolInteractable.GetEntityId())
{
interactable = _lastCleanToolInteractable;
}
else
{
interactable = info.collider.gameObject.GetComponent<CleanToolInteractable>();
_lastCleanToolInteractable = interactable;
}
interactable.Use(playerEquipment.CurrentTool);
}
}
}