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.
157 lines
4.1 KiB
157 lines
4.1 KiB
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEditor;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using static UnityEditor.Progress;
|
|
|
|
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;
|
|
|
|
[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 Item item;
|
|
[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<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);
|
|
//Player.Instance.LookAt(interactive.transform.position);
|
|
localPlayer.Movement.StopMovement();
|
|
interactable.CreateInteractions();
|
|
}
|
|
|
|
public void Deactivate()
|
|
{
|
|
if (!active) return;
|
|
|
|
interactable = null;
|
|
SetActive(false);
|
|
}
|
|
|
|
public void SetLocalPlayer(Player player)
|
|
{
|
|
if (!player.IsMine)
|
|
{
|
|
Debug.LogError("Trying to assign not owned player");
|
|
return;
|
|
}
|
|
|
|
localPlayer = player;
|
|
}
|
|
}
|
|
|