gather-and-defend/Assets/Scripts/Drag&Drop/UnitPlacementButton.cs
Felix Boucher 8adc563d47 make transition fool proof
problème :
Il y avait plusieurs manières de faire planter le loading screen en appuyant sur des boutons

changements:
- turn off buttons when loading screen is active
- turn on buttons when loading screen is not active
- add event aggregator class to project and migrate every event to it
- fix bugs and regressions
2023-10-09 22:21:06 -04:00

76 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using GatherAndDefend.Events;
using static LevelManager;
[RequireComponent(typeof(Button))]
public abstract class UnitPlacementButton : MonoBehaviour, IPointerDownHandler
{
[SerializeField]
protected Material _outlineMaterial;
[SerializeField]
protected Sprite _detectionRangeSprite;
[SerializeField]
private int _wood;
[SerializeField]
private int _rock;
[SerializeField]
private int _food;
private Button _button;
[SerializeField]
private TMP_Text _foodLabel;
[SerializeField]
private TMP_Text _woodLabel;
[SerializeField]
private TMP_Text _rockLabel;
private bool _canSpawn = false;
protected virtual void Start()
{
_button = GetComponent<Button>();
_button.enabled = false;
EventAggregator.Instance.GetEvent<LevelLoadedEvent>().Attach(OnLevelLoaded);
EventAggregator.Instance.GetEvent<ExitingLevelEvent>().Attach(DeactivateButton);
}
private void OnLevelLoaded(GatherAndDefend.LevelEditor.Level level)
{
_canSpawn = true;
EventAggregator.Instance.GetEvent<LevelLoadedEvent>().Detach(OnLevelLoaded);
}
void DeactivateButton()
{
EventAggregator.Instance.GetEvent<ExitingLevelEvent>().Detach(DeactivateButton);
_canSpawn = false;
}
protected virtual void Update()
{
_button.interactable = ResourceManager.Instance.EnoughFor(_rock, _wood, _food) && _button.enabled && _canSpawn;
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 void OnPointerDown(PointerEventData eventData)
{
if (!_button.interactable) return;
var placeholder = Place();
placeholder.Rock = _rock;
placeholder.Wood = _wood;
placeholder.Food = _food;
}
protected abstract DraggablePlaceholder Place();
}