first commit
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class ResourcePickup : MonoBehaviour
|
||||
{
|
||||
public ItemData item;
|
||||
public int amount = 1;
|
||||
public float respawnSeconds = 8f;
|
||||
|
||||
private Renderer[] renderers;
|
||||
private Collider[] colliders;
|
||||
private bool available = true;
|
||||
private float respawnTimer;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
renderers = GetComponentsInChildren<Renderer>();
|
||||
colliders = GetComponentsInChildren<Collider>();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
transform.Rotate(0f, 45f * Time.deltaTime, 0f, Space.World);
|
||||
|
||||
if (available)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
respawnTimer -= Time.deltaTime;
|
||||
if (respawnTimer <= 0f)
|
||||
{
|
||||
SetAvailable(true);
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryCollect(Inventory inventory)
|
||||
{
|
||||
if (!available || inventory == null || item == null || amount <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!inventory.AddItem(item, amount))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
SetAvailable(false);
|
||||
return true;
|
||||
}
|
||||
|
||||
public string GetPickupText()
|
||||
{
|
||||
if (item == null)
|
||||
{
|
||||
return "Неизвестный ресурс";
|
||||
}
|
||||
|
||||
return item.itemName + " x" + amount;
|
||||
}
|
||||
|
||||
private void SetAvailable(bool value)
|
||||
{
|
||||
available = value;
|
||||
respawnTimer = value ? 0f : respawnSeconds;
|
||||
|
||||
for (int i = 0; i < renderers.Length; i++)
|
||||
{
|
||||
renderers[i].enabled = value;
|
||||
}
|
||||
|
||||
for (int i = 0; i < colliders.Length; i++)
|
||||
{
|
||||
colliders[i].enabled = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user