gather-and-defend/Assets/Scripts/Drag&Drop/UnitPlacementButton.cs
Felix Boucher effe4ca2cc unit cooldown
#Le besoin addressé par le commit
les unités et bâtiments devrait avoir un temps de cooldown avant de pouvoir être spawné à nouveau

#Comment le besoin est-il addressé? (modifications)
les unités et bâtiments prennent maintenant 3 secondes avant de redevenir interactif après avoir été spawné
2024-02-18 11:11:03 -05:00

87 lines
2.3 KiB
C#

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;
}
}