refactor
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class MouseInputManager : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
private LayerMask _selectableLayerMask;
|
||||
[SerializeField]
|
||||
private LayerMask _walkableLayerMask;
|
||||
[SerializeField]
|
||||
private WaypointVisual _waypointVisual;
|
||||
|
||||
|
||||
public static event EventHandler<OnSelectedObjectChangedEventArgs> OnSelectedObjectChanged;
|
||||
|
||||
public static MouseInputManager Instance { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null)
|
||||
{
|
||||
Debug.Log("There's more than one mouse Input instance");
|
||||
}
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
if (!EventSystem.current.IsPointerOverGameObject() && Physics.Raycast(ray, out var mouseRaycastHit, 100f, _selectableLayerMask))
|
||||
{
|
||||
mouseRaycastHit.transform.TryGetComponent(out BaseInteractableObject baseObject);
|
||||
if (baseObject != null)
|
||||
{
|
||||
OnSelectedObjectChanged?.Invoke(this, new OnSelectedObjectChangedEventArgs() { SelectedObject = baseObject });
|
||||
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
|
||||
{
|
||||
print($"Go to interaction point {baseObject._interactionPoint.position}");
|
||||
_waypointVisual.SetWaypoint(baseObject._interactionPoint.position);
|
||||
Player.Instance.AddTask(new PlayerTasks(Tasks.Interact, baseObject));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
OnSelectedObjectChanged?.Invoke(this, new OnSelectedObjectChangedEventArgs() { SelectedObject = null });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
OnSelectedObjectChanged?.Invoke(this, new OnSelectedObjectChangedEventArgs() { SelectedObject = null });
|
||||
if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
|
||||
{
|
||||
if (Physics.Raycast(ray, out RaycastHit hit, 100f, _walkableLayerMask))
|
||||
{
|
||||
_waypointVisual.SetWaypoint(hit.point);
|
||||
Player.Instance.AddTask(new PlayerTasks(Tasks.Move, _waypointVisual));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user