Made price of upgrade be required

This commit is contained in:
Pasha Bibko
2026-04-28 16:22:12 +01:00
parent 631d5691ff
commit 0ccf979869
5 changed files with 161 additions and 175 deletions

View File

@@ -17,6 +17,18 @@ namespace Fruitomation.Global
Instance.InternalCurrentMoney += amount;
}
public static bool CanBuy(double amount)
{
double val = Instance.InternalCurrentMoney - amount;
if (val > 0f)
{
Instance.InternalCurrentMoney = val;
return true;
}
return false;
}
private void Awake()
{
if (Instance is not null)

View File

@@ -5,6 +5,7 @@ using UnityEngine.UI;
using UnityEngine;
using System.Linq;
using System;
using Fruitomation.Global;
using PashaBibko.Pacore.Attributes;
namespace Fruitomation.UI
@@ -101,7 +102,16 @@ namespace Fruitomation.UI
private void OnButtonClicked()
{
UpgradeManager.Unlock(Upgrade);
if (Cost == 0f)
{
Debug.LogWarning("Upgrade Cost has not been set");
return;
}
if (MoneyController.CanBuy(Cost))
{
UpgradeManager.Unlock(Upgrade);
}
}
private bool IsUnlockable =>
@@ -156,13 +166,15 @@ namespace Fruitomation.UI
AttachedButton.interactable = State == UpgradeState.Unlockable;
string cost = Cost == 0f ? "UNAVAILABLE" : $"{Cost:F1}";
// https://stackoverflow.com/questions/27040325/c-sharp-regex-to-convert-camelcase-to-sentence-case
string formatted = Regex.Replace(Upgrade.ToString(), "[A-Z]", " $0")[1..];
AttachedText.text = State switch
{
UpgradeState.Hidden => "???",
UpgradeState.Viewable => $"{formatted}",
UpgradeState.Unlockable => $"{formatted}\n{Cost:F1}",
UpgradeState.Unlockable => $"{formatted}\n{cost}",
UpgradeState.Unlocked => $"{formatted}\nUnlocked",
var _ => throw new ArgumentOutOfRangeException()
};

View File

@@ -1,5 +1,6 @@
using System.Text.RegularExpressions;
using Fruitomation.Game;
using Fruitomation.Global;
using UnityEngine.UI;
using UnityEngine;
@@ -28,16 +29,26 @@ namespace Fruitomation.UI
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:F1}";
: $"{formatted}\n{cost}";
AttachedButton.interactable = !unlocked;
}
private void OnButtonClicked()
{
UpgradeManager.Unlock(Building);
if (Cost == 0f)
{
Debug.LogWarning("Upgrade Cost has not been set");
return;
}
if (MoneyController.CanBuy(Cost))
{
UpgradeManager.Unlock(Building);
}
}
}
}