From c78bca1e8bf60109c82172ab8b7f41195b79819d Mon Sep 17 00:00:00 2001
From: maikeljh <mjh191003@gmail.com>
Date: Wed, 8 May 2024 04:54:03 +0700
Subject: [PATCH] [New Feature] Merge shopkeeper with quest system

---
 Assets/Scenes/Level01.unity                   | 46 ++++++++-
 Assets/Scripts/Managers/LevelManager.cs       | 98 +++++++++++++++----
 .../Scripts/ShopKeeper/CollisionDetection.cs  |  1 +
 Assets/Scripts/ShopKeeper/ShopManager.cs      | 22 ++++-
 Assets/Scripts/ShopKeeper/TimerScript.cs      | 46 +++++++--
 5 files changed, 179 insertions(+), 34 deletions(-)

diff --git a/Assets/Scenes/Level01.unity b/Assets/Scenes/Level01.unity
index d5d075b..b3490e5 100644
--- a/Assets/Scenes/Level01.unity
+++ b/Assets/Scenes/Level01.unity
@@ -1984,6 +1984,18 @@ PrefabInstance:
       propertyPath: m_MatchWidthOrHeight
       value: 1
       objectReference: {fileID: 0}
+    - target: {fileID: 8102320564619976915, guid: 69053a3b6390db84692334fb21585a16, type: 3}
+      propertyPath: m_AnchorMax.y
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 8102320564619976915, guid: 69053a3b6390db84692334fb21585a16, type: 3}
+      propertyPath: m_AnchorMin.y
+      value: 0
+      objectReference: {fileID: 0}
+    - target: {fileID: 8102320564619976915, guid: 69053a3b6390db84692334fb21585a16, type: 3}
+      propertyPath: m_AnchoredPosition.y
+      value: 100
+      objectReference: {fileID: 0}
     m_RemovedComponents: []
     m_RemovedGameObjects: []
     m_AddedGameObjects: []
@@ -11563,15 +11575,15 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 5712293994674342005, guid: ce41f0f1776a2c341b1fb70e0cb568f2, type: 3}
       propertyPath: m_SizeDelta.x
-      value: 1069.7365
+      value: 30
       objectReference: {fileID: 0}
     - target: {fileID: 5712293994674342005, guid: ce41f0f1776a2c341b1fb70e0cb568f2, type: 3}
       propertyPath: m_SizeDelta.y
-      value: 323.0601
+      value: 10
       objectReference: {fileID: 0}
     - target: {fileID: 5712293994674342005, guid: ce41f0f1776a2c341b1fb70e0cb568f2, type: 3}
       propertyPath: m_AnchoredPosition.x
-      value: -547.31573
+      value: -519.37634
       objectReference: {fileID: 0}
     - target: {fileID: 5712293994674342005, guid: ce41f0f1776a2c341b1fb70e0cb568f2, type: 3}
       propertyPath: m_AnchoredPosition.y
@@ -11727,7 +11739,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 7162618932881966347, guid: ce41f0f1776a2c341b1fb70e0cb568f2, type: 3}
       propertyPath: coins
-      value: 50
+      value: 0
       objectReference: {fileID: 0}
     - target: {fileID: 7162618932881966347, guid: ce41f0f1776a2c341b1fb70e0cb568f2, type: 3}
       propertyPath: player
@@ -12172,6 +12184,7 @@ MonoBehaviour:
   - {fileID: 1836185669}
   playerInsideCollider: 0
   timerScript: {fileID: 1840906051}
+  timerObject: {fileID: 1727849421}
 --- !u!135 &1312448813
 SphereCollider:
   m_ObjectHideFlags: 0
@@ -13705,6 +13718,8 @@ MonoBehaviour:
   - Level02
   - Cutscene07
   - Cutscene08
+  currentIndex: 0
+  currentQuest: 0
   announcementText: {fileID: 0}
   enemyManager: []
   questText: {fileID: 0}
@@ -13714,6 +13729,10 @@ MonoBehaviour:
   rajaKilled: 0
   winQuest: 0
   fail: 0
+  isShopKeeper: 0
+  shopTimeRemaining: 0
+  shopKeeper: {fileID: 0}
+  timerCanvas: {fileID: 0}
 --- !u!1 &1695883712
 GameObject:
   m_ObjectHideFlags: 0
@@ -13961,6 +13980,14 @@ PrefabInstance:
       propertyPath: m_VerticalAlignment
       value: 512
       objectReference: {fileID: 0}
+    - target: {fileID: 2979598614730680134, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
+      propertyPath: shopCanvas
+      value: 
+      objectReference: {fileID: 264729923}
+    - target: {fileID: 2979598614730680134, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
+      propertyPath: shopInstructionCanvas
+      value: 
+      objectReference: {fileID: 929121535}
     - target: {fileID: 3013221823716727558, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
       propertyPath: m_Pivot.y
       value: 1
@@ -13969,6 +13996,10 @@ PrefabInstance:
       propertyPath: m_AnchorMax.x
       value: 0.5
       objectReference: {fileID: 0}
+    - target: {fileID: 3013221823716727558, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
+      propertyPath: m_AnchorMax.y
+      value: 1
+      objectReference: {fileID: 0}
     - target: {fileID: 3013221823716727558, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
       propertyPath: m_AnchorMin.x
       value: 0.5
@@ -13987,7 +14018,7 @@ PrefabInstance:
       objectReference: {fileID: 0}
     - target: {fileID: 3013221823716727558, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
       propertyPath: m_AnchoredPosition.y
-      value: 0
+      value: -100
       objectReference: {fileID: 0}
     - target: {fileID: 3854428197638761884, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
       propertyPath: m_Name
@@ -14098,6 +14129,11 @@ PrefabInstance:
     m_AddedGameObjects: []
     m_AddedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
+--- !u!1 &1727849421 stripped
+GameObject:
+  m_CorrespondingSourceObject: {fileID: 3854428197638761884, guid: 33c68c33f3888be4e957c37834df7765, type: 3}
+  m_PrefabInstance: {fileID: 1727849420}
+  m_PrefabAsset: {fileID: 0}
 --- !u!1 &1737175773
 GameObject:
   m_ObjectHideFlags: 0
diff --git a/Assets/Scripts/Managers/LevelManager.cs b/Assets/Scripts/Managers/LevelManager.cs
index 3c61c8e..89b2b57 100644
--- a/Assets/Scripts/Managers/LevelManager.cs
+++ b/Assets/Scripts/Managers/LevelManager.cs
@@ -11,20 +11,20 @@ namespace Nightmare
             "Cutscene01",
             "Cutscene02",
             "Cutscene03",
-            "Level01",
+            "Level01", // Quest 1
             "Cutscene04",
-            "Level01",
+            "Level01", // Shopkeeper & Quest 2 & Shopkeeper
             "Cutscene05",
-            "Level01",
+            "Level01", // Quest 3 & Shop Keeper
             "Cutscene06",
-            "Level01",
+            "Level01", // Quest 4
             "Cutscene07",
             "Cutscene08"
         };
 
         private static LevelManager instance;
-        private int currentIndex = 0;
-        private int currentQuest = 0;
+        public int currentIndex = 0;
+        public int currentQuest = 0;
         private float announcementDuration = 5f;
         public TMPro.TextMeshProUGUI announcementText;
         public EnemyManager[] enemyManager;
@@ -38,6 +38,10 @@ namespace Nightmare
         public int rajaKilled = 0;
         public bool winQuest = false;
         public bool fail = false;
+        public bool isShopKeeper = false;
+        public float shopTimeRemaining = 0f;
+        public GameObject shopKeeper;
+        public GameObject timerCanvas;
 
         public static LevelManager Instance
         {
@@ -90,7 +94,7 @@ namespace Nightmare
                 return;
             }
 
-            if (nextQuest)
+            if (nextQuest || currentQuest == 0)
             {
                 StartQuest();
                 nextQuest = false;
@@ -104,11 +108,37 @@ namespace Nightmare
                 announcementDuration -= Time.deltaTime;
             }
 
+            // Track Shopkeeper
+            if (isShopKeeper)
+            {
+                questText.text = "";
+                if (timerCanvas && !timerCanvas.activeSelf) timerCanvas.SetActive(true);
+                if (shopTimeRemaining <= 0)
+                {
+                    nextQuest = true;
+                    isShopKeeper = false;
+                    if (shopKeeper) shopKeeper.SetActive(false);
+                    if (timerCanvas) timerCanvas.SetActive(false);
+                    if (currentQuest != 1)
+                    {
+                        isCinematic = true;
+                        Next();
+                    }
+                    return;
+                }
+                else
+                {
+                    shopTimeRemaining -= Time.deltaTime;
+                    return;
+                }
+            }
+
+
             // Track Quest
             if (currentQuest == 1)
             {
                 // Quest 1
-                if (questText && announcementDuration <= 0)
+                if (questText && announcementDuration <= 0 && !isShopKeeper && !winQuest)
                 {
                     // Convert timeRemaining to TimeSpan
                     TimeSpan timeSpan = TimeSpan.FromSeconds(timeRemaining);
@@ -130,10 +160,14 @@ namespace Nightmare
                         questText.text = "";
                         winQuest = true;
                     }
+
                     if (announcementDuration <= 0)
                     {
                         winQuest = false;
                         isCinematic = true;
+                        isShopKeeper = true;
+                        if (shopKeeper) shopKeeper.SetActive(true);
+                        shopTimeRemaining = 60f;
                         Next();
                     }
                 }
@@ -145,7 +179,7 @@ namespace Nightmare
             } else if (currentQuest == 2)
             {
                 // Quest 2
-                if (questText && announcementDuration <= 0)
+                if (questText && announcementDuration <= 0 && !isShopKeeper && !winQuest)
                 {
                     string sisaKeroco = Math.Min(kerocoKilled, 5) + " / 5";
                     string sisaKepalaKeroco = Math.Min(kepalaKerocoKilled, 2) + " / 2";
@@ -167,15 +201,17 @@ namespace Nightmare
                     if (announcementDuration <= 0)
                     {
                         winQuest = false;
-                        isCinematic = true;
-                        Next();
+                        isShopKeeper = true;
+                        shopTimeRemaining = 60f;
+                        if (shopKeeper) shopKeeper.SetActive(true);
+                        if (timerCanvas) timerCanvas.SetActive(true);
                     }
                 }
 
             } else if (currentQuest == 3)
             {
                 // Quest 3
-                if (questText && announcementDuration <= 0)
+                if (questText && announcementDuration <= 0 && !isShopKeeper && !winQuest)
                 {
                     // Convert timeRemaining to TimeSpan
                     TimeSpan timeSpan = TimeSpan.FromSeconds(timeRemaining);
@@ -204,8 +240,10 @@ namespace Nightmare
                     if (announcementDuration <= 0)
                     {
                         winQuest = false;
-                        isCinematic = true;
-                        Next();
+                        if (shopKeeper) shopKeeper.SetActive(true);
+                        if (timerCanvas) timerCanvas.SetActive(true);
+                        isShopKeeper = true;
+                        shopTimeRemaining = 60f;
                     }
                 } else if (timeRemaining <= 0)
                 {
@@ -221,7 +259,7 @@ namespace Nightmare
             } else if (currentQuest == 4)
             {
                 // Quest 4
-                if (questText && announcementDuration <= 0)
+                if (questText && announcementDuration <= 0 && !isShopKeeper && !winQuest)
                 {
                     questText.text = "Quest:\n" + "Bunuh Raja!";
                 }
@@ -253,6 +291,15 @@ namespace Nightmare
             currentQuest++;
             Scene currentLevel = SceneManager.GetActiveScene();
 
+            if (shopKeeper == null)
+            {
+                shopKeeper = GameObject.FindGameObjectWithTag("ShopKeeper");
+                timerCanvas = shopKeeper.GetComponent<CollisionDetection>().timerObject;
+            }
+
+            if (shopKeeper) shopKeeper.SetActive(false);
+            if (timerCanvas) timerCanvas.SetActive(false);
+
             if (currentLevel.name.StartsWith("Level") && announcementDuration > 0)
             {
                 // Get announcement
@@ -426,7 +473,6 @@ namespace Nightmare
                     SceneManager.LoadSceneAsync(levels[currentIndex], LoadSceneMode.Single);
                 }
 
-                nextQuest = true;
                 isCinematic = false;
             }
         }
@@ -491,7 +537,7 @@ namespace Nightmare
         {
             currentIndex = 0;
             currentQuest = 0;
-            fail = false;
+            ResetAtt();
             SceneManager.LoadScene(levels[currentIndex], LoadSceneMode.Single);
         }
 
@@ -499,8 +545,24 @@ namespace Nightmare
         {
             currentIndex = 1;
             currentQuest = 0;
-            fail = false;
+            ResetAtt();
             SceneManager.LoadScene(levels[currentIndex], LoadSceneMode.Single);
         }
+
+        public void ResetAtt()
+        {
+            timeRemaining = 0f;
+            nextQuest = false;
+            isCinematic = false;
+            kerocoKilled = 0;
+            kepalaKerocoKilled = 0;
+            jendralKilled = 0;
+            rajaKilled = 0;
+            winQuest = false;
+            fail = false;
+            isShopKeeper = false;
+            shopTimeRemaining = 0f;
+            announcementDuration = 0f;
+        }
     }
 }
diff --git a/Assets/Scripts/ShopKeeper/CollisionDetection.cs b/Assets/Scripts/ShopKeeper/CollisionDetection.cs
index 1181fc7..17000e6 100644
--- a/Assets/Scripts/ShopKeeper/CollisionDetection.cs
+++ b/Assets/Scripts/ShopKeeper/CollisionDetection.cs
@@ -16,6 +16,7 @@ public class CollisionDetection : MonoBehaviour
     private float timer;
     public TimerScript timerScript;
     private Color timerColor;
+    public GameObject timerObject;
 
     public void Update()
     {
diff --git a/Assets/Scripts/ShopKeeper/ShopManager.cs b/Assets/Scripts/ShopKeeper/ShopManager.cs
index 15ac7ed..6749b24 100644
--- a/Assets/Scripts/ShopKeeper/ShopManager.cs
+++ b/Assets/Scripts/ShopKeeper/ShopManager.cs
@@ -3,10 +3,12 @@ using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 using TMPro;
+using Nightmare;
 
 public class ShopManager : MonoBehaviour, IDataPersistence
 {
-    public float coins = 50;
+    public float coins = 0;
+    public float increment = 50;
     public TMP_Text coinUI;
     public ShopItemSO[] shopItemsSO;
     public GameObject[] shopPanelsGO;
@@ -17,11 +19,14 @@ public class ShopManager : MonoBehaviour, IDataPersistence
     // Start is called before the first frame update
     void Start()
     {
+        coins = 0;
+        UpdateCoinsUI();
+
         for (int i = 0; i < shopItemsSO.Length; i++)
         {
             shopPanelsGO[i].SetActive(true);
         }
-        coinUI.text = coins.ToString();
+
         LoadPanels();
         CheckPurchaseable();
     }
@@ -88,4 +93,17 @@ public class ShopManager : MonoBehaviour, IDataPersistence
         CheckPurchaseable();
     }
 
+    void OnEnable()
+    {
+        coins += increment;
+        increment += 50;
+
+        UpdateCoinsUI();
+        CheckPurchaseable();
+    }
+
+    void UpdateCoinsUI()
+    {
+        coinUI.text = coins.ToString();
+    }
 }
diff --git a/Assets/Scripts/ShopKeeper/TimerScript.cs b/Assets/Scripts/ShopKeeper/TimerScript.cs
index f3eb1b8..d59a2fc 100644
--- a/Assets/Scripts/ShopKeeper/TimerScript.cs
+++ b/Assets/Scripts/ShopKeeper/TimerScript.cs
@@ -1,5 +1,4 @@
 using System.Collections;
-using System.Collections.Generic;
 using UnityEngine;
 using TMPro;
 
@@ -10,11 +9,23 @@ public class TimerScript : MonoBehaviour, IDataPersistence
     public Canvas shopCanvas;
     public Canvas shopInstructionCanvas;
     public Canvas timerCanvas;
-    private float timer = 120f;
+    private float timer = 60f;
 
     void Start()
     {
-        timeCoroutine =  StartCoroutine(TimerCountDown());
+        ResetTimer();
+        StartTimerCountDown();
+    }
+
+    void OnEnable()
+    {
+        ResetTimer();
+        StartTimerCountDown();
+    }
+
+    void OnDisable()
+    {
+        StopTimerCountDown();
     }
 
     public void LoadData(GameData data)
@@ -31,13 +42,9 @@ public class TimerScript : MonoBehaviour, IDataPersistence
     {
         while (timer > 0)
         {
-
-            int minutes = Mathf.FloorToInt(timer / 60);
-            int seconds = Mathf.FloorToInt(timer % 60);
-
-            timerUI.text = "Shop close in: " + string.Format("{0:00}:{1:00}", minutes, seconds);
+            UpdateTimerDisplay();
             yield return new WaitForSeconds(1f);
-            timer -=1f;
+            timer -= 1f;
         }
 
         timerUI.text = "Shop is closed";
@@ -47,6 +54,21 @@ public class TimerScript : MonoBehaviour, IDataPersistence
         StopTimerCountDown();
     }
 
+    private void UpdateTimerDisplay()
+    {
+        int minutes = Mathf.FloorToInt(timer / 60);
+        int seconds = Mathf.FloorToInt(timer % 60);
+        timerUI.text = "Shop close in: " + string.Format("{0:00}:{1:00}", minutes, seconds);
+    }
+
+    private void StartTimerCountDown()
+    {
+        if (timeCoroutine == null)
+        {
+            timeCoroutine = StartCoroutine(TimerCountDown());
+        }
+    }
+
     public void StopTimerCountDown()
     {
         if (timeCoroutine != null)
@@ -56,6 +78,12 @@ public class TimerScript : MonoBehaviour, IDataPersistence
         }
     }
 
+    private void ResetTimer()
+    {
+        timer = 60f;
+        UpdateTimerDisplay();
+    }
+
     public string GetTimerText()
     {
         return timerUI.text;
-- 
GitLab