From 67da27438d22f3bfb4e0b66c6a82ffd044e272fc Mon Sep 17 00:00:00 2001
From: Adrian Paschkowski <git@wasdennnoch.me>
Date: Mon, 12 Apr 2021 21:39:18 +0200
Subject: [PATCH] StatsManager: Public upgrade info, add CanModifyMana

---
 Assets/Scripts/StatsManager.cs | 62 +++++++++++++++++++++-------------
 1 file changed, 39 insertions(+), 23 deletions(-)

diff --git a/Assets/Scripts/StatsManager.cs b/Assets/Scripts/StatsManager.cs
index ee00be5..ea985e2 100644
--- a/Assets/Scripts/StatsManager.cs
+++ b/Assets/Scripts/StatsManager.cs
@@ -1,3 +1,4 @@
+using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
@@ -10,20 +11,41 @@ public enum UpgradeTypes
     MANA_COST,
 }
 
-class UpgradeData
+public struct EnemyStats
+{
+    public float healthMultiplier;
+    public float damageMultiplier;
+    public float armorMultiplier;
+}
+
+public struct PlayerStats
+{
+    public float healthMultiplier;
+    public float damageMultiplier;
+    public float armorMultiplier;
+}
+
+public class UpgradeData
 {
     public Upgrade upgrade;
     public int count;
 
-    public int GetUnadjustedNextUpgradeCost()
+    // How much it costs to purchase another one of this upgrade
+    public int GetNextUpgradeCost()
     {
-        return (int)(upgrade.baseCost * (1 + count * upgrade.costMultiplier));
+        return StatsManager.instance.AdjustManaCost(GetUnadjustedNextUpgradeCost());
     }
 
     public float GetTotalEffectMultiplier()
     {
         return 1 + count * upgrade.effectMultiplier;
     }
+
+    // You probably shouldn't call this outside this file.
+    public int GetUnadjustedNextUpgradeCost()
+    {
+        return (int)(upgrade.baseCost * (1 + count * upgrade.costMultiplier));
+    }
 }
 
 public class StatsManager : MonoBehaviour
@@ -36,7 +58,9 @@ public class StatsManager : MonoBehaviour
     [SerializeField] int currentMana;
 
     [SerializeField] List<Upgrade> availableUpgrades;
-    Dictionary<UpgradeTypes, UpgradeData> upgrades = new Dictionary<UpgradeTypes, UpgradeData>();
+
+    // Information about currently available and purchased upgrades. Should only be read, not modified.
+    public Dictionary<UpgradeTypes, UpgradeData> upgrades = new Dictionary<UpgradeTypes, UpgradeData>();
 
     public EnemyStats GetEnemyStats()
     {
@@ -55,20 +79,20 @@ public class StatsManager : MonoBehaviour
     }
 
     // Adjusts the given Mana cost based on the amount of Mana cost upgrades currently bought.
-    public int GetAdjustedManaCost(int cost)
+    public int AdjustManaCost(int cost)
     {
         return (int)(cost * upgrades[UpgradeTypes.MANA_COST].GetTotalEffectMultiplier());
     }
 
     public int GetNextUpgradeCost(UpgradeTypes type)
     {
-        return GetAdjustedManaCost(upgrades[type].GetUnadjustedNextUpgradeCost());
+        return upgrades[type].GetNextUpgradeCost();
     }
 
     // Buys an upgrade, spending the required amount of Mana in the process. Returns false if not enough Mana is available.
     public bool BuyUpgrade(UpgradeTypes type)
     {
-        if (!ModifyMana(GetNextUpgradeCost(type)))
+        if (!ModifyMana(-GetNextUpgradeCost(type)))
         {
             return false;
         }
@@ -77,10 +101,10 @@ public class StatsManager : MonoBehaviour
         return true;
     }
 
-    // Add (positive value) or spend (negative value) Mana.
+    // Add (positive value) or spend (negative value) Mana. Returns false if not enough Mana is available to spend.
     public bool ModifyMana(int delta)
     {
-        if (currentMana + delta < 0)
+        if (!CanModifyMana(delta))
         {
             return false;
         }
@@ -88,6 +112,12 @@ public class StatsManager : MonoBehaviour
         return true;
     }
 
+    // If `delta` is negative, returns this amount of Mana can be spent. If `delta` is positive, this will always return `true`.
+    public bool CanModifyMana(int delta)
+    {
+        return currentMana + delta >= 0;
+    }
+
     void RecalculateStatMultipliers()
     {
         currentEnemyStats.healthMultiplier = upgrades[UpgradeTypes.ENEMY_HEALTH].GetTotalEffectMultiplier();
@@ -121,17 +151,3 @@ public class StatsManager : MonoBehaviour
         currentPlayerStats.armorMultiplier = 1f;
     }
 }
-
-public struct EnemyStats
-{
-    public float healthMultiplier;
-    public float damageMultiplier;
-    public float armorMultiplier;
-}
-
-public struct PlayerStats
-{
-    public float healthMultiplier;
-    public float damageMultiplier;
-    public float armorMultiplier;
-}
-- 
GitLab