You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Scripts/AI/FSM_Template/Entities/GatherableResource.cs

53 lines
1.1 KiB
C#

1 month ago
using System;
using UnityEngine;
using UnityEngine.AI;
public class GatherableResource : MonoBehaviour
{
[SerializeField] private int _totalAvailable = 20;
private int _available;
public bool IsDepleted => _available <= 0;
private void OnEnable()
{
_available = _totalAvailable;
}
public bool Take()
{
if (_available <= 0)
return false;
_available--;
UpdateSize();
return true;
}
private void UpdateSize()
{
float scale = (float)_available / _totalAvailable;
if (scale > 0 && scale < 1f)
{
var vectorScale = Vector3.one * scale;
transform.localScale = vectorScale;
}
else if (scale <= 0)
{
gameObject.SetActive(false);
}
}
[ContextMenu("Snap")]
private void Snap()
{
if (NavMesh.SamplePosition(transform.position, out var hit, 5f, NavMesh.AllAreas))
{
transform.position = hit.position;
}
}
public void SetAvailable(int amount) => _available = amount;
}