57 lines
1.6 KiB
C#
57 lines
1.6 KiB
C#
using System;
|
|
using Code.Game.OfficeGame.Entities;
|
|
using Code.Infrastructure.Input;
|
|
using Reflex.Attributes;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
namespace Code.OfficeGame.Player
|
|
{
|
|
public class PlayerInteractor : MonoBehaviour
|
|
{
|
|
private LayerMask _layerMask;
|
|
private IInputService _inputService;
|
|
|
|
private void Awake()
|
|
{
|
|
_layerMask = LayerMask.GetMask("Interactable");
|
|
}
|
|
|
|
[Inject]
|
|
private void Construct(IInputService inputService)
|
|
{
|
|
_inputService = inputService;
|
|
_inputService.BindActionPerformed(
|
|
(int)ActionMappings.OpenMovement,
|
|
(int)OpenMovementActions.Interact,
|
|
Interact);
|
|
}
|
|
|
|
private RaycastHit[] _hitBuffer = new RaycastHit[3];
|
|
[SerializeField] private float _maxDistance = 2f;
|
|
|
|
private void Interact(InputAction.CallbackContext obj)
|
|
{
|
|
var count = Physics.RaycastNonAlloc(
|
|
transform.position,
|
|
transform.forward,
|
|
_hitBuffer,
|
|
_maxDistance,
|
|
_layerMask);
|
|
|
|
if (count == 0) return;
|
|
|
|
var hit = _hitBuffer[0].transform.GetComponent<InteractableEntity>();
|
|
|
|
hit.Interact();
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
_inputService.UnbindActionPerformed(
|
|
(int)ActionMappings.OpenMovement,
|
|
(int)OpenMovementActions.Interact,
|
|
Interact);
|
|
}
|
|
}
|
|
} |