91 lines
2.4 KiB
C#
91 lines
2.4 KiB
C#
|
|
using System.Collections;
|
||
|
|
using OTGIntegrated.Core;
|
||
|
|
using OTGIntegrated.Player;
|
||
|
|
using UnityEngine;
|
||
|
|
|
||
|
|
namespace OTGIntegrated.Items
|
||
|
|
{
|
||
|
|
[RequireComponent(typeof(Collider))]
|
||
|
|
public sealed class ResourceNode : MonoBehaviour, IInteractable
|
||
|
|
{
|
||
|
|
public ItemData itemData;
|
||
|
|
[Min(1)] public int amount = 1;
|
||
|
|
[Min(0f)] public float respawnCooldown = 0f;
|
||
|
|
public string actionLabel = "собрать";
|
||
|
|
|
||
|
|
private Collider nodeCollider;
|
||
|
|
private Renderer[] renderers;
|
||
|
|
private bool depleted;
|
||
|
|
|
||
|
|
public Transform InteractTransform => transform;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
nodeCollider = GetComponent<Collider>();
|
||
|
|
nodeCollider.isTrigger = true;
|
||
|
|
renderers = GetComponentsInChildren<Renderer>();
|
||
|
|
}
|
||
|
|
|
||
|
|
public string GetPrompt()
|
||
|
|
{
|
||
|
|
string label = itemData != null ? itemData.displayName : "Ресурс";
|
||
|
|
return $"E — {actionLabel}: {label}";
|
||
|
|
}
|
||
|
|
|
||
|
|
public bool CanInteract(GameObject interactor)
|
||
|
|
{
|
||
|
|
return !depleted && itemData != null && amount > 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Interact(GameObject interactor)
|
||
|
|
{
|
||
|
|
if (!CanInteract(interactor))
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
OTGIntegrated.Inventory.Inventory inventory = GameManager.Instance != null
|
||
|
|
? GameManager.Instance.inventory
|
||
|
|
: interactor.GetComponentInChildren<OTGIntegrated.Inventory.Inventory>();
|
||
|
|
|
||
|
|
if (inventory == null || !inventory.AddItem(itemData, amount))
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (respawnCooldown > 0f)
|
||
|
|
{
|
||
|
|
StartCoroutine(RespawnRoutine());
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
Destroy(gameObject);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private IEnumerator RespawnRoutine()
|
||
|
|
{
|
||
|
|
SetDepleted(true);
|
||
|
|
yield return new WaitForSeconds(respawnCooldown);
|
||
|
|
SetDepleted(false);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void SetDepleted(bool value)
|
||
|
|
{
|
||
|
|
depleted = value;
|
||
|
|
if (nodeCollider != null)
|
||
|
|
{
|
||
|
|
nodeCollider.enabled = !value;
|
||
|
|
}
|
||
|
|
|
||
|
|
for (int i = 0; i < renderers.Length; i++)
|
||
|
|
{
|
||
|
|
if (renderers[i] != null)
|
||
|
|
{
|
||
|
|
renderers[i].enabled = !value;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|