83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.InputSystem;
|
|
|
|
public class InGameMouseHandler : UnityEngine.Object
|
|
{
|
|
private LayerMask _selectableLayerMask;
|
|
|
|
private LayerMask _walkableLayerMask;
|
|
|
|
private WaypointVisual _waypointVisual;
|
|
|
|
private Ray _ray;
|
|
private Camera _camera;
|
|
|
|
private BaseInteractableObject _selectedObject;
|
|
private bool _pressControl = false;
|
|
private bool _mousePress = false;
|
|
|
|
public InGameMouseHandler(Camera camera)
|
|
{
|
|
_camera = camera;
|
|
_selectableLayerMask = LayerMask.GetMask("Selectable");
|
|
_walkableLayerMask = LayerMask.GetMask("Walking");
|
|
|
|
var waypointPrefab = Resources.Load("WayPointSign", typeof(WaypointVisual)) as WaypointVisual;
|
|
_waypointVisual = Instantiate(waypointPrefab, GameManager.Instance.transform);
|
|
|
|
GameManager.Instance.Input.PlayerAction.PointClick.performed += ClickToMove;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
GameManager.Instance.Input.PlayerAction.PointClick.performed += ClickToMove;
|
|
}
|
|
|
|
private void ClickToMove(InputAction.CallbackContext context)
|
|
{
|
|
if (!_pressControl)
|
|
{
|
|
|
|
GameManager.Instance.UI.ClosePopupMenu();
|
|
|
|
if (_selectedObject != null)
|
|
{
|
|
_waypointVisual.SetWaypoint(_selectedObject._interactionPoint.position);
|
|
Player.Instance.Interact(_selectedObject);
|
|
}
|
|
else
|
|
{
|
|
if (Physics.Raycast(_ray, out RaycastHit hit, 100f, _walkableLayerMask))
|
|
{
|
|
_waypointVisual.SetWaypoint(hit.point);
|
|
Player.Instance.GoToPoint(_waypointVisual);
|
|
}
|
|
}
|
|
}
|
|
_pressControl = false;
|
|
}
|
|
|
|
public void Update()
|
|
{
|
|
if (Mouse.current.leftButton.wasPressedThisFrame)
|
|
{
|
|
_pressControl = !EventSystem.current.IsPointerOverGameObject();
|
|
}
|
|
_ray = _camera.ScreenPointToRay(Mouse.current.position.ReadValue());
|
|
if (Physics.Raycast(_ray, out var mouseRaycastHit, 100f, _selectableLayerMask))
|
|
{
|
|
mouseRaycastHit.transform.TryGetComponent(out _selectedObject);
|
|
if (_selectedObject != null)
|
|
{
|
|
_selectedObject.GetComponent<Hightlight>()?.ToggleHighlight(true);
|
|
return;
|
|
}
|
|
}
|
|
if (_selectedObject != null)
|
|
{
|
|
_selectedObject.GetComponent<Hightlight>()?.ToggleHighlight(false);
|
|
_selectedObject = null;
|
|
}
|
|
}
|
|
} |