You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

149 lines
3.9 KiB

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
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;
}
[Header("Layers")]
[SerializeField] private LayerMask clickableLayers;
[SerializeField] private LayerMask groundLayers;
[SerializeField] private LayerMask interactableLayers;
[Header("Interaction")]
[SerializeField] private List<string> blockReasons;
[SerializeField, ReadOnly] private bool active;
[SerializeField, ReadOnly] private Interactable interactable;
public static Interactable Interactable => Instance.interactable;
public static Player LocalPlayer => GameManager.LocalPlayer;
public bool IsBlocked => blockReasons.Count > 0;
public bool IsActive => active;
public void AddBlockReason(string reason) => AddBlockReason(reason, true);
public void AddBlockReason(string reason, bool stopMovement)
{
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<Interactable>();
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 InteractTest()
{
if (interactable)
{
interactable.Interactions[0].Action();
}
}
}