From bb01a6b25c199efc5ea86b4af83304118658d4c2 Mon Sep 17 00:00:00 2001
From: melvinkj <melvinkentj@gmail.com>
Date: Fri, 10 May 2024 09:42:42 +0700
Subject: [PATCH] feat: save pet and currency feature

---
 Assets/Scenes/Main.unity                      | 26 ++----------
 Assets/Scripts/Camera/CameraFollow.cs         | 16 +------
 Assets/Scripts/Camera/CameraPosition.cs       | 27 ------------
 Assets/Scripts/Camera/CameraPosition.cs.meta  | 11 -----
 .../Scripts/DataPersistance/Data/GameData.cs  | 15 +++++--
 .../DataPersistance/DataPersistanceManager.cs | 42 ++++++++++++++++++-
 Assets/Scripts/Pets/AllyPetHealth.cs          |  2 +-
 Assets/Scripts/Pets/AttackingPetMovement.cs   | 38 ++++++++++++-----
 Assets/Scripts/Pets/HealingPetMovement.cs     | 36 +++++++++++-----
 Assets/Scripts/Player/PlayerCurrency.cs       | 12 +++++-
 Assets/Scripts/Player/PlayerPosition.cs       |  6 +--
 11 files changed, 126 insertions(+), 105 deletions(-)
 delete mode 100644 Assets/Scripts/Camera/CameraPosition.cs
 delete mode 100644 Assets/Scripts/Camera/CameraPosition.cs.meta

diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity
index 973615d..42c54ba 100644
--- a/Assets/Scenes/Main.unity
+++ b/Assets/Scenes/Main.unity
@@ -5256,17 +5256,14 @@ PrefabInstance:
     - {fileID: 8455667667674168468, guid: 4e5a0d387dc27394b81600988ad0216f, type: 3}
     - {fileID: 100008, guid: 4e5a0d387dc27394b81600988ad0216f, type: 3}
     m_AddedGameObjects: []
-    m_AddedComponents:
-    - targetCorrespondingSourceObject: {fileID: 100006, guid: 4e5a0d387dc27394b81600988ad0216f, type: 3}
-      insertIndex: -1
-      addedObject: {fileID: 2023876524}
+    m_AddedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: 4e5a0d387dc27394b81600988ad0216f, type: 3}
 --- !u!114 &1641232234 stripped
 MonoBehaviour:
   m_CorrespondingSourceObject: {fileID: 11400004, guid: 4e5a0d387dc27394b81600988ad0216f, type: 3}
   m_PrefabInstance: {fileID: 1641232233}
   m_PrefabAsset: {fileID: 0}
-  m_GameObject: {fileID: 1974427093}
+  m_GameObject: {fileID: 0}
   m_Enabled: 1
   m_EditorHideFlags: 0
   m_Script: {fileID: 11500000, guid: c32067ea884548644b13cf9baddd4573, type: 3}
@@ -6736,12 +6733,9 @@ MonoBehaviour:
   m_Script: {fileID: 11500000, guid: 615057aa5dd75e34383756f1bf0fa143, type: 3}
   m_Name: 
   m_EditorClassIdentifier: 
+  healingPet: {fileID: 7022846624846827769, guid: f86d09f168862644d8d36c6929850322, type: 3}
+  attackingPet: {fileID: 7022846624846827769, guid: 89d690d2254d9714f88745a7913a6bf3, type: 3}
   fileName: data.game
---- !u!1 &1974427093 stripped
-GameObject:
-  m_CorrespondingSourceObject: {fileID: 100006, guid: 4e5a0d387dc27394b81600988ad0216f, type: 3}
-  m_PrefabInstance: {fileID: 1641232233}
-  m_PrefabAsset: {fileID: 0}
 --- !u!1 &1979757893
 GameObject:
   m_ObjectHideFlags: 0
@@ -6833,18 +6827,6 @@ Animator:
   m_CorrespondingSourceObject: {fileID: 9500000, guid: 4e5a0d387dc27394b81600988ad0216f, type: 3}
   m_PrefabInstance: {fileID: 1641232233}
   m_PrefabAsset: {fileID: 0}
---- !u!114 &2023876524
-MonoBehaviour:
-  m_ObjectHideFlags: 0
-  m_CorrespondingSourceObject: {fileID: 0}
-  m_PrefabInstance: {fileID: 0}
-  m_PrefabAsset: {fileID: 0}
-  m_GameObject: {fileID: 1974427093}
-  m_Enabled: 1
-  m_EditorHideFlags: 0
-  m_Script: {fileID: 11500000, guid: 472ec104c593ad9439f5dbb57bd16de6, type: 3}
-  m_Name: 
-  m_EditorClassIdentifier: 
 --- !u!1 &2025282751
 GameObject:
   m_ObjectHideFlags: 0
diff --git a/Assets/Scripts/Camera/CameraFollow.cs b/Assets/Scripts/Camera/CameraFollow.cs
index 5e3ebe8..e02e4fa 100644
--- a/Assets/Scripts/Camera/CameraFollow.cs
+++ b/Assets/Scripts/Camera/CameraFollow.cs
@@ -3,7 +3,7 @@ using System.Collections;
 
 namespace Nightmare
 {
-    public class CameraFollow : MonoBehaviour, IDataPersistance
+    public class CameraFollow : MonoBehaviour
     {
         public Transform target;            // The position that that camera will be following.
         public float smoothing = 5f;        // The speed with which the camera will be following.
@@ -24,19 +24,5 @@ namespace Nightmare
             // Smoothly interpolate between the camera's current position and it's target position.
             transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);
         }
-
-        public void LoadData(GameData data)
-        {
-            if (data.cameraPosition != Vector3.zero)
-            {
-                this.transform.position = data.cameraPosition;
-            }
-        }
-
-        public void SaveData(ref GameData data)
-        {
-            data.cameraPosition = this.transform.position;
-        }
-
     }
 }
\ No newline at end of file
diff --git a/Assets/Scripts/Camera/CameraPosition.cs b/Assets/Scripts/Camera/CameraPosition.cs
deleted file mode 100644
index d663007..0000000
--- a/Assets/Scripts/Camera/CameraPosition.cs
+++ /dev/null
@@ -1,27 +0,0 @@
-using System.Collections;
-using System.Collections.Generic;
-using UnityEngine;
-
-public class CameraPosition : MonoBehaviour
-{
-    // Start is called before the first frame update
-    void Start()
-    {
-
-    }
-
-    // Update is called once per frame
-    void Update()
-    {
-
-    }
-    //public void LoadData(GameData data)
-    //{
-    //    this.transform.position = data.cameraPosition;
-    //}
-
-    //public void SaveData(ref GameData data)
-    //{
-    //    data.cameraPosition = this.transform.position;
-    //}
-}
diff --git a/Assets/Scripts/Camera/CameraPosition.cs.meta b/Assets/Scripts/Camera/CameraPosition.cs.meta
deleted file mode 100644
index 96dd079..0000000
--- a/Assets/Scripts/Camera/CameraPosition.cs.meta
+++ /dev/null
@@ -1,11 +0,0 @@
-fileFormatVersion: 2
-guid: 0bb2d3419e085b341a2beb50ff4e439c
-MonoImporter:
-  externalObjects: {}
-  serializedVersion: 2
-  defaultReferences: []
-  executionOrder: 0
-  icon: {instanceID: 0}
-  userData: 
-  assetBundleName: 
-  assetBundleVariant: 
diff --git a/Assets/Scripts/DataPersistance/Data/GameData.cs b/Assets/Scripts/DataPersistance/Data/GameData.cs
index ad5baf5..6edb162 100644
--- a/Assets/Scripts/DataPersistance/Data/GameData.cs
+++ b/Assets/Scripts/DataPersistance/Data/GameData.cs
@@ -8,10 +8,14 @@ public class GameData
 {
     // public int timer;
     public int playerHealth;
+    public Vector3 playerPosition;
+    public int balance;
+
     public float damagePercent;
 
-    public Vector3 playerPosition;
-    public Vector3 cameraPosition;
+    public List<int> healingPetHealths;
+    public List<int> attackingPetHealths;
+
 
     public int score;
     public int shotsFired;
@@ -29,7 +33,12 @@ public class GameData
         this.playerHealth = 100;
         this.damagePercent = 1;
         this.playerPosition = Vector3.zero;
-        this.cameraPosition = Vector3.zero;
+        this.balance = 0;
+
+
+
+        this.healingPetHealths = new List<int>();
+        this.attackingPetHealths = new List<int>();
 
         // Statistics
         this.score = 0;
diff --git a/Assets/Scripts/DataPersistance/DataPersistanceManager.cs b/Assets/Scripts/DataPersistance/DataPersistanceManager.cs
index 9944b64..480938e 100644
--- a/Assets/Scripts/DataPersistance/DataPersistanceManager.cs
+++ b/Assets/Scripts/DataPersistance/DataPersistanceManager.cs
@@ -2,9 +2,17 @@ using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
+using Nightmare;
+
 
 public class DataPersistanceManager : MonoBehaviour
 {
+    [Header("Pet Prefabs")]
+
+    [SerializeField] public GameObject healingPet;
+    [SerializeField] public GameObject attackingPet;
+
+
     [Header("File Storage Config")]
     [SerializeField] private string fileName;
 
@@ -29,7 +37,6 @@ public class DataPersistanceManager : MonoBehaviour
     private void Start()
     {
         this.dataHandler = new FileDataHandler(Application.persistentDataPath, fileName);
-        this.dataPersistanceObjects = FindAllDataPersistanceObjects();
         LoadGame();
     }
 
@@ -49,6 +56,37 @@ public class DataPersistanceManager : MonoBehaviour
         {
             NewGame();
         }
+        else
+        {
+            if (this.gameData.healingPetHealths.Count != 0)
+            {
+                foreach (int healingPetHealth in this.gameData.healingPetHealths)
+                {
+                    GameObject pet = Instantiate(healingPet, gameData.playerPosition, Quaternion.identity);
+                    AllyPetHealth petHealthScript = pet.GetComponent<AllyPetHealth>();
+                    if (petHealthScript != null)
+                    {
+                        petHealthScript.currentHealth = healingPetHealth;
+                    }
+                }
+                this.gameData.healingPetHealths.Clear();
+            }
+            if (this.gameData.attackingPetHealths.Count != 0)
+            {
+                foreach (int attackingPetHealth in this.gameData.attackingPetHealths)
+                {
+                    GameObject pet = Instantiate(attackingPet, gameData.playerPosition, Quaternion.identity);
+                    AllyPetHealth petHealthScript = pet.GetComponent<AllyPetHealth>();
+                    if (petHealthScript != null)
+                    {
+                        petHealthScript.currentHealth = attackingPetHealth;
+                    }
+                }
+                this.gameData.attackingPetHealths.Clear();
+            }
+        }
+
+        this.dataPersistanceObjects = FindAllDataPersistanceObjects();
 
         // push the loaded data to all other scipts that need it
         foreach (IDataPersistance dataPersistanceObj in dataPersistanceObjects)
@@ -59,6 +97,8 @@ public class DataPersistanceManager : MonoBehaviour
     }
     public void SaveGame()
     {
+        this.dataPersistanceObjects = FindAllDataPersistanceObjects();
+
         // pass the data to other scripts so they can update it 
         foreach (IDataPersistance dataPersistanceObj in dataPersistanceObjects)
         {
diff --git a/Assets/Scripts/Pets/AllyPetHealth.cs b/Assets/Scripts/Pets/AllyPetHealth.cs
index a294a6b..eeb2703 100644
--- a/Assets/Scripts/Pets/AllyPetHealth.cs
+++ b/Assets/Scripts/Pets/AllyPetHealth.cs
@@ -10,7 +10,7 @@ namespace Nightmare
 
         Animator anim;
         CapsuleCollider capsuleCollider;
-        int currentHealth;
+        public int currentHealth;
 
         // Cheat Full HP Pet
         public bool isCheatFullHPPet = false;
diff --git a/Assets/Scripts/Pets/AttackingPetMovement.cs b/Assets/Scripts/Pets/AttackingPetMovement.cs
index 64ca5da..99b5210 100644
--- a/Assets/Scripts/Pets/AttackingPetMovement.cs
+++ b/Assets/Scripts/Pets/AttackingPetMovement.cs
@@ -5,29 +5,32 @@ using UnityEngine.Events;
 
 namespace Nightmare
 {
-    public class AttackingPetMovement : PausibleObject
+    public class AttackingPetMovement : PausibleObject, IDataPersistance
     {
         public float visionRange = 10f;
         public float wanderDistance = 10f;
         public Vector2 idleTimeRange;
-        [Range(0f,1f)]
+        [Range(0f, 1f)]
 
-        float currentVision; 
+        float currentVision;
         Transform player;
         PlayerHealth playerHealth;
 
-        Animator anim;        
+        Animator anim;
         NavMeshAgent nav;
         public float timer = 0f;
 
         float attackTimer = 0f;
 
-        void Awake ()
+        private AllyPetHealth allyPetHealthScript;
+
+        void Awake()
         {
-            player = GameObject.FindGameObjectWithTag ("Player").transform;
-            playerHealth = player.GetComponent <PlayerHealth> ();
+            player = GameObject.FindGameObjectWithTag("Player").transform;
+            playerHealth = player.GetComponent<PlayerHealth>();
             nav = GetComponent<NavMeshAgent>();
             anim = GetComponent<Animator>();
+            allyPetHealthScript = GetComponent<AllyPetHealth>();
             StartPausible();
         }
 
@@ -46,7 +49,7 @@ namespace Nightmare
                 nav.ResetPath();
         }
 
-        void Update ()
+        void Update()
         {
             if (!isPaused)
             {
@@ -95,7 +98,7 @@ namespace Nightmare
                 nav.isStopped = false;
         }
 
-        private GameObject getNearestEnemy() 
+        private GameObject getNearestEnemy()
         {
             GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
             float nearestDistance = Mathf.Infinity;
@@ -140,7 +143,7 @@ namespace Nightmare
             timer = -1f;
             // if (!enemyHealth.IsDead()) TODO: verify pet health
             // {
-                SetDestination(position);
+            SetDestination(position);
             // }
         }
 
@@ -174,7 +177,7 @@ namespace Nightmare
 
         private Vector3 GetRandomPoint(float distance, int layermask)
         {
-            Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position;;
+            Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ;
 
             NavMeshHit navHit;
             NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask);
@@ -195,5 +198,18 @@ namespace Nightmare
             return navHit.mask;
         }
 
+        public void LoadData(GameData data)
+        {
+            // Do Nothing
+        }
+
+        public void SaveData(ref GameData data)
+        {
+            if (allyPetHealthScript.CurrentHealth() > 0)
+            {
+                data.attackingPetHealths.Add(allyPetHealthScript.CurrentHealth());
+            }
+        }
+
     }
 }
\ No newline at end of file
diff --git a/Assets/Scripts/Pets/HealingPetMovement.cs b/Assets/Scripts/Pets/HealingPetMovement.cs
index 587250f..ba2f490 100644
--- a/Assets/Scripts/Pets/HealingPetMovement.cs
+++ b/Assets/Scripts/Pets/HealingPetMovement.cs
@@ -5,29 +5,32 @@ using UnityEngine.Events;
 
 namespace Nightmare
 {
-    public class HealingPetMovement : PausibleObject
+    public class HealingPetMovement : PausibleObject, IDataPersistance
     {
         public float visionRange = 10f;
         public float hearingRange = 20f;
         public float wanderDistance = 10f;
         public Vector2 idleTimeRange;
-        [Range(0f,1f)]
+        [Range(0f, 1f)]
         public float psychicLevels = 0.2f;
 
-        float currentVision; 
+        float currentVision;
         Transform player;
         PlayerHealth playerHealth;
 
-        Animator anim; 
+        Animator anim;
 
         NavMeshAgent nav;
         public float timer = 0f;
-        void Awake ()
+
+        private AllyPetHealth allyPetHealthScript;
+        void Awake()
         {
             anim = GetComponent<Animator>();
-            player = GameObject.FindGameObjectWithTag ("Player").transform;
-            playerHealth = player.GetComponent <PlayerHealth> ();
+            player = GameObject.FindGameObjectWithTag("Player").transform;
+            playerHealth = player.GetComponent<PlayerHealth>();
             nav = GetComponent<NavMeshAgent>();
+            allyPetHealthScript = GetComponent<AllyPetHealth>();
             StartPausible();
         }
 
@@ -46,7 +49,7 @@ namespace Nightmare
                 nav.ResetPath();
         }
 
-        void Update ()
+        void Update()
         {
             if (!isPaused)
             {
@@ -120,7 +123,7 @@ namespace Nightmare
         {
             timer = -1f;
             SetDestination(position);
-   
+
         }
 
         private void SetDestination(Vector3 position)
@@ -153,7 +156,7 @@ namespace Nightmare
 
         private Vector3 GetRandomPoint(float distance, int layermask)
         {
-            Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position;;
+            Vector3 randomPoint = UnityEngine.Random.insideUnitSphere * distance + this.transform.position; ;
 
             NavMeshHit navHit;
             NavMesh.SamplePosition(randomPoint, out navHit, distance, layermask);
@@ -174,5 +177,18 @@ namespace Nightmare
             return navHit.mask;
         }
 
+        public void LoadData(GameData data)
+        {
+            // Do nothing
+        }
+
+        public void SaveData(ref GameData data)
+        {
+            if (allyPetHealthScript.CurrentHealth() > 0)
+            {
+                data.healingPetHealths.Add(allyPetHealthScript.CurrentHealth());
+            }
+        }
+
     }
 }
\ No newline at end of file
diff --git a/Assets/Scripts/Player/PlayerCurrency.cs b/Assets/Scripts/Player/PlayerCurrency.cs
index bc6cc57..101037d 100644
--- a/Assets/Scripts/Player/PlayerCurrency.cs
+++ b/Assets/Scripts/Player/PlayerCurrency.cs
@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 
-public class PlayerCurrency : MonoBehaviour
+public class PlayerCurrency : MonoBehaviour, IDataPersistance
 {
     public int balance = 0;
     int prevBalance;
@@ -40,4 +40,14 @@ public class PlayerCurrency : MonoBehaviour
     {
         balance = prevBalance;
     }
+
+    public void LoadData(GameData data)
+    {
+        this.balance = data.balance;
+    }
+
+    public void SaveData(ref GameData data)
+    {
+        data.balance = this.balance;
+    }
 }
diff --git a/Assets/Scripts/Player/PlayerPosition.cs b/Assets/Scripts/Player/PlayerPosition.cs
index 5562906..ad95698 100644
--- a/Assets/Scripts/Player/PlayerPosition.cs
+++ b/Assets/Scripts/Player/PlayerPosition.cs
@@ -7,18 +7,18 @@ public class PlayerPosition : MonoBehaviour, IDataPersistance
     // Start is called before the first frame update
     void Start()
     {
-        
+
     }
 
     // Update is called once per frame
     void Update()
     {
-        
+
     }
 
     public void LoadData(GameData data)
     {
-        this.transform.position = data.playerPosition;
+        // this.transform.position = data.playerPosition;
     }
 
     public void SaveData(ref GameData data)
-- 
GitLab