97 lines
2.7 KiB
C#
97 lines
2.7 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(SphereCollider))]
|
|
public class PickupItem : MonoBehaviour
|
|
{
|
|
public ItemData item;
|
|
public int amount = 1;
|
|
public KeyCode interactKey = KeyCode.E;
|
|
public float interactionRadius = 2.25f;
|
|
public Inventory targetInventory;
|
|
|
|
private Transform player;
|
|
private bool playerInRange;
|
|
|
|
private void Awake()
|
|
{
|
|
SphereCollider trigger = GetComponent<SphereCollider>();
|
|
trigger.isTrigger = true;
|
|
trigger.radius = interactionRadius;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
if (targetInventory == null)
|
|
targetInventory = FindObjectOfType<Inventory>();
|
|
|
|
SimplePlayerController controller = FindObjectOfType<SimplePlayerController>();
|
|
if (controller != null)
|
|
player = controller.transform;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (player != null)
|
|
SetPlayerInRange(Vector3.Distance(transform.position, player.position) <= interactionRadius);
|
|
|
|
if (!playerInRange || item == null || targetInventory == null)
|
|
return;
|
|
|
|
if (DialogueManager.Instance != null && DialogueManager.Instance.IsDialogueActive)
|
|
return;
|
|
|
|
ShowHint();
|
|
|
|
if (Input.GetKeyDown(interactKey) && targetInventory.AddItem(item, amount))
|
|
{
|
|
if (DialogueHUD.Instance != null)
|
|
{
|
|
DialogueHUD.Instance.SetStatus("Получен предмет: " + item.itemName);
|
|
DialogueHUD.Instance.HideHint();
|
|
}
|
|
|
|
gameObject.SetActive(false);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
if (other.GetComponentInParent<SimplePlayerController>() != null)
|
|
{
|
|
player = other.transform;
|
|
SetPlayerInRange(true);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
if (other.GetComponentInParent<SimplePlayerController>() != null)
|
|
SetPlayerInRange(false);
|
|
}
|
|
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
Gizmos.color = new Color(0.2f, 0.8f, 1f, 0.25f);
|
|
Gizmos.DrawSphere(transform.position, interactionRadius);
|
|
Gizmos.color = new Color(0.2f, 0.8f, 1f, 1f);
|
|
Gizmos.DrawWireSphere(transform.position, interactionRadius);
|
|
}
|
|
|
|
private void SetPlayerInRange(bool inRange)
|
|
{
|
|
if (playerInRange == inRange)
|
|
return;
|
|
|
|
playerInRange = inRange;
|
|
|
|
if (!inRange && DialogueHUD.Instance != null)
|
|
DialogueHUD.Instance.HideHint();
|
|
}
|
|
|
|
private void ShowHint()
|
|
{
|
|
if (DialogueHUD.Instance != null)
|
|
DialogueHUD.Instance.ShowHint("Нажмите E, чтобы подобрать: " + item.itemName);
|
|
}
|
|
}
|