gather-and-defend/Assets/Scripts/Drag&Drop/UnitPlacementButton.cs
Felix Boucher b54627196c population mechanic with art
- added art for house and UI
- put population in ResourceManager
- create house prefab
- added code for adding and removing pop depending on entity
- refactor harvesters so they inherit from ally
- modify placeholders and buttons so that only units cost population
- add events for population and resources changing
- add population relative configs to global config
- add start resources values to Levels
- add debug feature for generating resources for free
2023-10-29 19:12:32 -04:00

78 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;
[RequireComponent(typeof(Button))]
public abstract class UnitPlacementButton : MonoBehaviour, IPointerDownHandler
{
[SerializeField]
protected Material _outlineMaterial;
[SerializeField]
protected Sprite _detectionRangeSprite;
[SerializeField]
protected int _wood;
[SerializeField]
protected int _rock;
[SerializeField]
protected int _food;
protected Button _button;
[SerializeField]
private TMP_Text _foodLabel;
[SerializeField]
private TMP_Text _woodLabel;
[SerializeField]
private TMP_Text _rockLabel;
protected 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 = CanPlace();
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();
protected abstract bool CanPlace();
}