55 lines
1.5 KiB
C#
55 lines
1.5 KiB
C#
using System.Text.RegularExpressions;
|
|
using Fruitomation.Game;
|
|
using Fruitomation.Global;
|
|
using UnityEngine.UI;
|
|
using UnityEngine;
|
|
|
|
namespace Fruitomation.UI
|
|
{
|
|
public class BuildingUnlockButton : MonoBehaviour
|
|
{
|
|
[Header("Settings")]
|
|
[SerializeField] private BuildingUnlock Building;
|
|
[SerializeField] private double Cost;
|
|
|
|
[Header("References")]
|
|
[SerializeField] private Button AttachedButton;
|
|
[SerializeField] private Text AttachedText;
|
|
|
|
private void Awake()
|
|
{
|
|
string formatted = Regex.Replace(Building.ToString(), "[A-Z]", " $0")[1..];
|
|
AttachedText.text = formatted;
|
|
|
|
AttachedButton.onClick.AddListener(OnButtonClicked);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
string formatted = Regex.Replace(Building.ToString(), "[A-Z]", " $0")[1..];
|
|
bool unlocked = UpgradeManager.Is(Building);
|
|
|
|
string cost = Cost == 0f ? "UNAVAILABLE" : $"{Cost:F1}";
|
|
AttachedText.text = unlocked
|
|
? $"{formatted}\nUnlocked"
|
|
: $"{formatted}\n{cost}";
|
|
|
|
AttachedButton.interactable = !unlocked;
|
|
}
|
|
|
|
private void OnButtonClicked()
|
|
{
|
|
if (Cost == 0f)
|
|
{
|
|
Debug.LogWarning("Upgrade Cost has not been set");
|
|
return;
|
|
}
|
|
|
|
if (MoneyController.CanBuy(Cost))
|
|
{
|
|
UpgradeManager.Unlock(Building);
|
|
}
|
|
}
|
|
}
|
|
}
|