From c2be0bbf53ce6a650b3d709b985b4e1b7a21d49f Mon Sep 17 00:00:00 2001
From: Daffa Romyz Aufa <80190953+DaffaRomyz@users.noreply.github.com>
Date: Sun, 16 Apr 2023 08:31:26 +0700
Subject: [PATCH] feat : add scoreboard when game complete

---
 Assets/Scenes/Level_01.unity        |  4 ++
 Assets/Scripts/Quest/QuestDetail.cs | 88 +++++++++++++++++++----------
 2 files changed, 62 insertions(+), 30 deletions(-)

diff --git a/Assets/Scenes/Level_01.unity b/Assets/Scenes/Level_01.unity
index d14941b5..d75da5ad 100644
--- a/Assets/Scenes/Level_01.unity
+++ b/Assets/Scenes/Level_01.unity
@@ -9750,6 +9750,10 @@ PrefabInstance:
       propertyPath: ShopKeeper
       value: 
       objectReference: {fileID: 442418676}
+    - target: {fileID: 3355275491469237930, guid: 46ca049bb430ca14fa9ca0e300f0a849, type: 3}
+      propertyPath: timePlayed
+      value: 
+      objectReference: {fileID: 1772748160}
     - target: {fileID: 3355275491469237930, guid: 46ca049bb430ca14fa9ca0e300f0a849, type: 3}
       propertyPath: QuestStatus
       value: 
diff --git a/Assets/Scripts/Quest/QuestDetail.cs b/Assets/Scripts/Quest/QuestDetail.cs
index 6adbdb45..20462d2a 100644
--- a/Assets/Scripts/Quest/QuestDetail.cs
+++ b/Assets/Scripts/Quest/QuestDetail.cs
@@ -19,16 +19,16 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
     public GameObject ShopKeeper;
     public GameObject Monolog;
     public GameObject Epilog;
+    public TimePlayed timePlayed;
 
     public static int zombearKilled;
     public static int zombunnyKilled;
     public static int hellephantKilled;
-    public static bool isQuestFinished=false;
+    public static bool isQuestFinished = false;
 
     bool lastQuest;
-    float interval=5.0f;
-    float timer=5.0f;
-    
+    float interval = 5.0f;
+    float timer = 5.0f;
     // Start is called before the first frame update
     void Start()
     {
@@ -48,14 +48,14 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
 
     // Update is called once per frame
     void Update()
-    {   
+    {
         timer += Time.deltaTime;
-        if(timer >= interval)
+        if (timer >= interval)
         {
             QuestingMechanishm();
         }
-        
-        if(onQuest == true && Panel.activeSelf == false)
+
+        if (onQuest == true && Panel.activeSelf == false)
         {
             OpenQuestStatus();
         }
@@ -64,15 +64,18 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
 
     }
 
-    public void questDone (){
+    public void questDone()
+    {
 
-        QuestDescription.message= "\nCongrats! You have completed the quest";
+        QuestDescription.message = "\nCongrats! You have completed the quest";
         onQuest = false;
-        timer=0.0f;
-        isQuestFinished=true;
+        timer = 0.0f;
+        isQuestFinished = true;
 
         if (lastQuest)
         {
+            Debug.Log("LAST QUEST COMPLETED");
+            AddScore(timePlayed.timePlayed);
             EpilogStory();
         }
         else
@@ -81,13 +84,15 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
         }
     }
 
-    public void OpenPanel(){
+    public void OpenPanel()
+    {
         ShopKeeper.SetActive(false);
         Panel.SetActive(true);
         QuestStatus.SetActive(false);
     }
 
-    public void OpenQuestStatus(){
+    public void OpenQuestStatus()
+    {
         QuestStatus.SetActive(true);
     }
 
@@ -101,9 +106,23 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
         Epilog.SetActive(true);
     }
 
-    public void QuestingMechanishm(){
-        if(onQuest== false && questLevel == 1) {
-            QuestDescription.message= "\nYou Got a new Quest\n\nkill 4 zombear";
+    void AddScore(float timePlayed)
+    {
+        Debug.Log("SCOREBOARD ADD");
+        var playername = PlayerPrefs.GetString("playername", "Player");
+        var jsonin = PlayerPrefs.GetString("scores", "{}");
+        var sd = JsonUtility.FromJson<ScoreData>(jsonin);
+        sd.scores.Add(new Score(playername, timePlayed));
+        var jsonout = JsonUtility.ToJson(sd);
+        PlayerPrefs.SetString("scores", jsonout);
+        Debug.Log("SCOREBOARD ADDED");
+    }
+
+    public void QuestingMechanishm()
+    {
+        if (onQuest == false && questLevel == 1)
+        {
+            QuestDescription.message = "\nYou Got a new Quest\n\nkill 4 zombear";
             zombearKillNeeded = 4;
             zombunnyKillNeeded = 0;
             hellephantKillNeeded = 0;
@@ -117,8 +136,9 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
 
         }
 
-        if(onQuest== false && questLevel == 3) {
-            QuestDescription.message= "\nYou Got a new Quest\n\nkill 4 zombunny";
+        if (onQuest == false && questLevel == 3)
+        {
+            QuestDescription.message = "\nYou Got a new Quest\n\nkill 4 zombunny";
             zombunnyKillNeeded = 4;
             zombearKillNeeded = 0;
             hellephantKillNeeded = 0;
@@ -131,8 +151,9 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
             isQuestFinished = false;
         }
 
-        if(onQuest== false && questLevel == 6) {
-            QuestDescription.message= "\nYou Got a new Quest\n\nkill 4 hellephant";
+        if (onQuest == false && questLevel == 6)
+        {
+            QuestDescription.message = "\nYou Got a new Quest\n\nkill 4 hellephant";
             hellephantKillNeeded = 4;
             zombearKillNeeded = 0;
             zombunnyKillNeeded = 0;
@@ -146,8 +167,9 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
 
         }
 
-        if(onQuest== false && questLevel == 8) {
-            QuestDescription.message= "\nYou Got a new Quest\n\nkill 4 zombear\nkill 4 zombunny\nkill 4 hellephant";
+        if (onQuest == false && questLevel == 8)
+        {
+            QuestDescription.message = "\nYou Got a new Quest\n\nkill 4 zombear\nkill 4 zombunny\nkill 4 hellephant";
             zombearKillNeeded = 4;
             zombunnyKillNeeded = 4;
             hellephantKillNeeded = 4;
@@ -161,8 +183,10 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
             isQuestFinished = false;
         }
 
-        if(onQuest == true){
-            if(zombearKilled >= zombearKillNeeded && zombunnyKilled >= zombunnyKillNeeded && hellephantKilled >= hellephantKillNeeded){
+        if (onQuest == true)
+        {
+            if (zombearKilled >= zombearKillNeeded && zombunnyKilled >= zombunnyKillNeeded && hellephantKilled >= hellephantKillNeeded)
+            {
                 zombearKilled = 0;
                 zombunnyKilled = 0;
                 hellephantKilled = 0;
@@ -176,15 +200,19 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
         }
     }
 
-    public void levelling(){
-        if(!onQuest){
-            if(questExp >= 80){
+    public void levelling()
+    {
+        if (!onQuest)
+        {
+            if (questExp >= 80)
+            {
                 levelUp();
             }
         }
     }
 
-    public void levelUp(){
+    public void levelUp()
+    {
         questLevel++;
         questExp = 0;
     }
@@ -202,5 +230,5 @@ public class QuestDetail : MonoBehaviour, IDataPersistence
     }
 }
 
-    
+
 
-- 
GitLab