using System; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class InteractionManager : MonoBehaviour { public static InteractionManager Instance { get; private set; } private Camera mainCamera; private void Awake() { if (Instance == null) Instance = this; mainCamera = Camera.main; } [SerializeField] private Player localPlayer; [SerializeField] private CameraController cameraController; [Header("Layers")] [SerializeField] private LayerMask clickableLayers; [SerializeField] private LayerMask groundLayers; [SerializeField] private LayerMask interactableLayers; [Header("Interaction")] [SerializeField] private List blockReasons; [SerializeField, ReadOnly] private bool active; [SerializeField, ReadOnly] private Interactable interactable; public static Interactable Interactable => Instance.interactable; public static Player LocalPlayer => Instance.localPlayer; public bool IsBlocked => blockReasons.Count > 0; public bool IsActive => active; public void AddBlockReason(string reason, bool stopMovement = false) { if (blockReasons.Contains(reason)) return; blockReasons.Add(reason); if (stopMovement) localPlayer.Movement.StopMovement(); } public void RemoveBlockReason(string reason) { StartCoroutine(RemoveBlockReasonDelay(reason)); } private IEnumerator RemoveBlockReasonDelay(string reason) { yield return null; blockReasons.Remove(reason); } private void Update() { if (IsBlocked || !Input.GetMouseButtonDown(0) || EventSystem.current.IsPointerOverGameObject()) return; Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100, clickableLayers)) { if (!hit.collider || hit.collider.gameObject.IsInLayerMask(groundLayers)) { if (active) { Deactivate(); } else { localPlayer.Movement.MoveTo(hit.point); } return; } if (!active && hit.collider.gameObject.IsInLayerMask(groundLayers)) { localPlayer.Movement.MoveTo(hit.point); return; } Interactable interactable = hit.collider.GetComponent(); if (interactable) // && interactable.ActiveInteractionsCount > 0) { if (this.interactable != interactable) { Debug.Log("Activate New"); Activate(interactable); } else Debug.Log("Same. Skip"); return; } //if (!hit.collider.CompareTag("InteractionButton")) //{ // Deactivate(); //} } } public void MoveLocalPlayerTo(Vector3 point, Action action) { localPlayer.Movement.MoveTo(point, action); } private void ClearButtons() { //foreach (InteractionButton button in interactionButtons) //{ // button.SetActive(false); //} } private void SetActive(bool setActive) { Debug.Log($"Interaction: {setActive}"); ClearButtons(); active = setActive; } private void Activate(Interactable interactable) { this.interactable = interactable; SetActive(true); localPlayer.Movement.StopMovement(); interactable.CreateInteractions(); } public void Deactivate() { if (!active) return; interactable = null; SetActive(false); } public void SetLocalPlayer(Player player) { if (!player.IsOwner) { Debug.LogError("Trying to assign not owned player"); return; } localPlayer = player; cameraController.SetTarget(player.transform); } public void InteractTest() { if (interactable) { interactable.Interactions[0].Action(); } } }