Added all upgrade buttons
This commit is contained in:
83
Assets/Scripts/UI/BasicUpgradeButton.cs
Normal file
83
Assets/Scripts/UI/BasicUpgradeButton.cs
Normal file
@@ -0,0 +1,83 @@
|
||||
using Fruitomation.Game;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace Fruitomation.UI
|
||||
{
|
||||
public class BasicUpgradeButton : MonoBehaviour
|
||||
{
|
||||
private enum UpgradeState
|
||||
{
|
||||
Unlocked,
|
||||
Unlockable,
|
||||
Viewable,
|
||||
Hidden,
|
||||
}
|
||||
|
||||
[Header("Settings")]
|
||||
[SerializeField] private BasicUpgrade Upgrade;
|
||||
[SerializeField] private int UpgradeCost;
|
||||
|
||||
[Header("References")]
|
||||
[SerializeField] private BasicUpgradeButton[] RequiredUpgrades;
|
||||
|
||||
private UpgradeState State = UpgradeState.Hidden;
|
||||
private Button AttachedButton;
|
||||
private Text AttachedText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
AttachedText = gameObject.GetComponentInChildren<Text>();
|
||||
AttachedButton = GetComponent<Button>();
|
||||
|
||||
AttachedButton.onClick.AddListener(() =>
|
||||
{
|
||||
UpgradeManager.Unlock(Upgrade);
|
||||
});
|
||||
|
||||
/* Stops null reference */
|
||||
RequiredUpgrades ??= Array.Empty<BasicUpgradeButton>();
|
||||
}
|
||||
|
||||
private bool IsUnlockable =>
|
||||
RequiredUpgrades.Length == 0 ||
|
||||
RequiredUpgrades.All(required => required.IsUnlocked);
|
||||
|
||||
private bool IsViewable =>
|
||||
RequiredUpgrades.Length == 0 ||
|
||||
RequiredUpgrades.Any(required => required.IsUnlocked);
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (UpgradeManager.Is(Upgrade))
|
||||
{
|
||||
State = UpgradeState.Unlocked;
|
||||
}
|
||||
|
||||
else if (IsUnlockable)
|
||||
{
|
||||
State = UpgradeState.Unlockable;
|
||||
}
|
||||
|
||||
else if (IsViewable)
|
||||
{
|
||||
State = UpgradeState.Viewable;
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
State = UpgradeState.Hidden;
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
AttachedText.text = State == UpgradeState.Hidden
|
||||
? "???"
|
||||
: $"{Upgrade.ToString()} [{UpgradeCost}]";
|
||||
}
|
||||
|
||||
private bool IsUnlocked => State == UpgradeState.Unlocked;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user