Files
MVP1/Assets/Scripts/Economy/ResourceCollector.cs
T

99 lines
2.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using UnityEngine;
using System.Collections;
public class ResourceCollector : MonoBehaviour
{
[Header("Что производит это здание")]
public ResourceType resourceType;
[Tooltip("База награды за клик (используется для расчета множителя уровня)")]
public float amountPerClick = 10f;
[Header("Параметры хранилища")]
[Tooltip("Максимальная вместимость")]
public float maxCapacity = 100f;
[Tooltip("Текущий запас (меняется автоматически)")]
public float currentStock = 0f;
[Header("Уровень здания")]
public int level = 1;
public float perLevelMultiplier = 1.2f;
private ResourceManager _manager;
private bool isRecharging = false;
void Awake()
{
_manager = ResourceManager.Instance;
if (_manager == null)
{
Debug.LogError($"{gameObject.name}: ResourceManager не найден!");
enabled = false;
return;
}
currentStock = 0f;
}
// НОВЫЙ МЕТОД: Чтобы UI мог спрашивать: "Сколько там сейчас?"
public float GetCurrentStock()
{
return currentStock;
}
public float GetMaxCapacity()
{
return maxCapacity;
}
void OnMouseDown()
{
if (currentStock <= 0)
{
Debug.Log($"[КЛИК] {gameObject.name} пуст. Идет восстановление...");
return;
}
float finalAmount = amountPerClick * Mathf.Pow(perLevelMultiplier, level - 1);
// Отдаем всё, что накопилось
float reward = currentStock;
_manager.AddResource(resourceType, reward);
// СБРОС В НОЛЬ
currentStock = 0f;
Debug.Log($"[СБОР] Получено {reward:F1} {resourceType}. Запас сброшен в 0.");
if (!isRecharging)
{
StartCoroutine(RechargeRoutine());
}
}
System.Collections.IEnumerator RechargeRoutine()
{
if (isRecharging) yield break;
isRecharging = true;
float rechargeRate = 5f; // Единиц в секунду
while (currentStock < maxCapacity)
{
currentStock += rechargeRate * Time.deltaTime;
if (currentStock > maxCapacity) currentStock = maxCapacity;
yield return null;
}
isRecharging = false;
Debug.Log($"[ГОТОВО] {gameObject.name} полностью заполнен!");
}
public void Upgrade()
{
level++;
Debug.Log($"[АПГРЕЙД] Уровень {level}");
}
}