From 1aca14bcd5716326e894d54edf8876fffdc53863 Mon Sep 17 00:00:00 2001
From: Adrian Paschkowski <git@wasdennnoch.me>
Date: Sat, 17 Apr 2021 13:58:24 +0200
Subject: [PATCH] Reset Mana on round loss

---
 Assets/Scripts/StatsManager.cs               | 23 +++++++++++++++++++-
 Assets/Scripts/Store/EnemySpawnController.cs |  2 +-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/Assets/Scripts/StatsManager.cs b/Assets/Scripts/StatsManager.cs
index e27103e..835fb64 100644
--- a/Assets/Scripts/StatsManager.cs
+++ b/Assets/Scripts/StatsManager.cs
@@ -50,12 +50,13 @@ public interface IManaChangeCallback
     void OnManaChanged(int oldValue, int newValue);
 }
 
-public class StatsManager : MonoBehaviour
+public class StatsManager : MonoBehaviour, IRoundCallback
 {
 
     public static StatsManager instance { get; private set; }
 
     [SerializeField] int currentMana;
+    int manaAtRoundStart;
     [SerializeField, Tooltip("When the AntiPlayer receives damage, how much of that damage is converted to Mana")]
     float antiPlayerDamageManaMultiplier = 100f;
     [SerializeField, Tooltip("When the AntiPlayer dies, how much mana to give")]
@@ -186,4 +187,24 @@ public class StatsManager : MonoBehaviour
             armorMultiplier = 1f,
         });
     }
+
+    void Start()
+    {
+        RoundController.instance.roundCallbacks.Add(this);
+    }
+
+    public void OnRoundStart()
+    {
+        manaAtRoundStart = currentMana;
+    }
+
+    public void OnRoundEnd(bool won)
+    {
+        if (!won)
+        {
+            // Reset current Mana back to start value if round was lost
+            var gainedMana = currentMana - manaAtRoundStart;
+            ModifyMana(-gainedMana);
+        }
+    }
 }
diff --git a/Assets/Scripts/Store/EnemySpawnController.cs b/Assets/Scripts/Store/EnemySpawnController.cs
index 9869563..1166d7e 100644
--- a/Assets/Scripts/Store/EnemySpawnController.cs
+++ b/Assets/Scripts/Store/EnemySpawnController.cs
@@ -69,9 +69,9 @@ public class EnemySpawnController : MonoBehaviour
 
     public void OnEnemyMouseDown(string enemyName)
     {
+        SetEnemyToSpawnObject(enemyName);
         if (!RoundController.instance.roundRunning && StatsManager.instance.ModifyMana(-1 * (int)activeSpawn.enemyCost))
         {
-            SetEnemyToSpawnObject(enemyName);
             dragObject.GetComponent<Image>().sprite = activeSpawn.EnemySprite;
             enemyImage = Instantiate(dragObject, GetPreviewWorldPosition(), Quaternion.identity, transform);
         }
-- 
GitLab