using GatherAndDefend.Events; using System.Collections; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public abstract class UnitPlacementButton : PlacementButton { [SerializeField] protected Sprite _detectionRangeSprite; [SerializeField] protected int _wood; [SerializeField] protected int _rock; [SerializeField] protected int _food; [SerializeField] protected int _cooldownInSeconds = 3; [SerializeField] private TMP_Text _foodLabel; [SerializeField] private TMP_Text _woodLabel; [SerializeField] private TMP_Text _rockLabel; [SerializeField] protected Image _cooldownIndicator; protected bool _lockedByCooldown; protected override void Update() { base.Update(); SetTextFor(_foodLabel, _food); SetTextFor(_rockLabel, _rock); SetTextFor(_woodLabel, _wood); } void SetTextFor(TMP_Text label, int value) { label.transform.parent.gameObject.SetActive(value > 0); label.text = "" + value; } public override void OnPointerDown(PointerEventData eventData) { base.OnPointerDown(eventData); if (Placeholder is UnitPlaceholder placeHolder) { placeHolder.Rock = _rock; placeHolder.Wood = _wood; placeHolder.Food = _food; placeHolder.WasPlaced += HandleCooldown; } } private void HandleCooldown(UnitPlaceholder unitPlaceholder) { unitPlaceholder.WasPlaced -= HandleCooldown; StartCoroutine(HandleCooldownCoroutine()); IEnumerator HandleCooldownCoroutine() { var countDown = 0f; _lockedByCooldown = true; _cooldownIndicator.gameObject.SetActive(true); while (countDown < _cooldownInSeconds) { countDown += Time.deltaTime; var percentDone = countDown / _cooldownInSeconds; _cooldownIndicator.fillAmount = 1 - percentDone; yield return null; } _cooldownIndicator.gameObject.SetActive(false); _lockedByCooldown = false; } } protected override bool CanPlace() { return base.CanPlace() && !_lockedByCooldown; } }