From b345aad26ca3e8665a3fc0c376a89f15d3c920e9 Mon Sep 17 00:00:00 2001
From: bayusamudra5502 <bayusamudra.55.02.com@gmail.com>
Date: Sun, 16 Apr 2023 13:29:21 +0700
Subject: [PATCH] fix: integrate with save and load

---
 Assets/Scripts/Managers/MainManager.cs     |  23 +-
 Assets/Scripts/Managers/MainMenuManager.cs |  14 +-
 Assets/Scripts/Player/PlayerShooting.cs    |   2 +-
 Assets/Scripts/Player/PlayerShotgun.cs     | 300 ++++++++++-----------
 Assets/Scripts/SaveLoad/SaveData.cs        |   3 +
 Assets/Scripts/SaveLoad/SaveLoadUI.cs      |   5 +
 Assets/Scripts/Shop/ShopItem.cs            |   8 +-
 7 files changed, 197 insertions(+), 158 deletions(-)

diff --git a/Assets/Scripts/Managers/MainManager.cs b/Assets/Scripts/Managers/MainManager.cs
index bfe07ad..ba9f206 100644
--- a/Assets/Scripts/Managers/MainManager.cs
+++ b/Assets/Scripts/Managers/MainManager.cs
@@ -45,6 +45,8 @@ public class MainManager : MonoBehaviour
         data.money = money;
         data.level = level;
         data.date = DateTime.Now.ToString("dd-MM-yyyy");
+        data.base_default_weapon_atk = PlayerShooting.baseDamagePerShot;
+        data.base_shotgun_atk = PlayerShotgun.baseDamagePerShot;
 
         string json = JsonUtility.ToJson(data);
         File.WriteAllText(Application.persistentDataPath + "/savefile" + num.ToString() + ".json", json);
@@ -61,7 +63,14 @@ public class MainManager : MonoBehaviour
 
             time = data.time;
             money = data.money;
-            level = data.level;
+            level = next_level(data.level);
+
+            PlayerShooting.baseDamagePerShot = data.base_default_weapon_atk;
+            PlayerShooting.damagePerShot = data.base_default_weapon_atk;
+
+            PlayerShotgun.baseDamagePerShot = data.base_shotgun_atk;
+            PlayerShotgun.damagePerShot = data.base_shotgun_atk;
+
             SceneManager.LoadScene(level);
         }
         else
@@ -70,6 +79,18 @@ public class MainManager : MonoBehaviour
         }
     }
 
+    private string next_level(string level)
+    {
+        int number_level = int.Parse(level.Substring(level.Length - 2));
+
+        if(number_level >= 4)
+        {
+            return level;
+        }
+
+        return $"Level 0{number_level + 1}";
+    }
+
     public void ContinueGame()
     {
         addLevel();
diff --git a/Assets/Scripts/Managers/MainMenuManager.cs b/Assets/Scripts/Managers/MainMenuManager.cs
index d138bb2..5650786 100644
--- a/Assets/Scripts/Managers/MainMenuManager.cs
+++ b/Assets/Scripts/Managers/MainMenuManager.cs
@@ -37,10 +37,20 @@ public class MainMenuManager : MonoBehaviour
 
     public void NewGame()
     {
-        SceneManager.LoadScene("Opening");
+        resetGun();
+		SceneManager.LoadScene("Opening");
     }
 
-    public void LoadGame()
+    private void resetGun()
+    {
+		PlayerShotgun.baseDamagePerShot = 30;
+		PlayerShotgun.damagePerShot = 30;
+
+        PlayerShooting.baseDamagePerShot = 20;
+        PlayerShooting.damagePerShot = 20;
+	}
+
+	public void LoadGame()
     {
         SceneManager.LoadScene("Load");
     }
diff --git a/Assets/Scripts/Player/PlayerShooting.cs b/Assets/Scripts/Player/PlayerShooting.cs
index 67b5702..3bd9cf2 100644
--- a/Assets/Scripts/Player/PlayerShooting.cs
+++ b/Assets/Scripts/Player/PlayerShooting.cs
@@ -48,7 +48,7 @@ public class PlayerShooting : MonoBehaviour
     static public void upgrade()
     {
         int multiplier = damagePerShot / baseDamagePerShot;
-        baseDamagePerShot += 10;
+        baseDamagePerShot = Mathf.RoundToInt(baseDamagePerShot * 1.3f);
         damagePerShot = baseDamagePerShot * multiplier;
     }
 
diff --git a/Assets/Scripts/Player/PlayerShotgun.cs b/Assets/Scripts/Player/PlayerShotgun.cs
index 76e8d6c..f965576 100644
--- a/Assets/Scripts/Player/PlayerShotgun.cs
+++ b/Assets/Scripts/Player/PlayerShotgun.cs
@@ -1,100 +1,100 @@
-using System.Collections;
-using UnityEngine;
-using UnityEngine.InputSystem;
-
-public class PlayerShotgun : MonoBehaviour
-{
-    public static int baseDamagePerShot = 30;
-    public static int damagePerShot = 30;                  
-    public float timeBetweenBullets = 0.5f;        
-    public float range = 6f;
-
-    public int angleInterval = 15;
-    public InputControl playerControls;      
-    private InputAction fire;
-    private bool isPlayed;
-    private bool isShooting;
-
-    float timer;                                    
-    Ray[] shootRays;                                   
-    RaycastHit[] shootHits;                            
-    int shootableMask;                             
-    ParticleSystem gunParticles;                    
-    LineRenderer[] gunLines;                           
-    AudioSource gunAudio;                           
-    Light gunLight;       
-    public Animator animator;                          
-    float effectsDisplayTime = 0.05f;    
-
-    private void OnEnable()
-    {
-        fire = playerControls.Player.Fire;
-        fire.Enable();
-        isPlayed = true;
-    }
-
-    private void OnDisable()
-    {
-        fire.Disable();
-    }
-
-    void Awake()
-    {
-        playerControls = new InputControl();
-        isPlayed = false;
-        shootableMask = LayerMask.GetMask("Shootable");
-        gunParticles = GetComponent<ParticleSystem>();
-        gunLines = GetComponentsInChildren<LineRenderer>();
-        gunAudio = GetComponent<AudioSource>();
-        gunLight = GetComponent<Light>();
-
-        shootRays = new Ray[5];
-        shootHits = new RaycastHit[5];
+using System.Collections;
+using UnityEngine;
+using UnityEngine.InputSystem;
+
+public class PlayerShotgun : MonoBehaviour
+{
+    public static int baseDamagePerShot = 30;
+    public static int damagePerShot = 30;                  
+    public float timeBetweenBullets = 0.5f;        
+    public float range = 6f;
+
+    public int angleInterval = 15;
+    public InputControl playerControls;      
+    private InputAction fire;
+    private bool isPlayed;
+    private bool isShooting;
+
+    float timer;                                    
+    Ray[] shootRays;                                   
+    RaycastHit[] shootHits;                            
+    int shootableMask;                             
+    ParticleSystem gunParticles;                    
+    LineRenderer[] gunLines;                           
+    AudioSource gunAudio;                           
+    Light gunLight;       
+    public Animator animator;                          
+    float effectsDisplayTime = 0.05f;    
+
+    private void OnEnable()
+    {
+        fire = playerControls.Player.Fire;
+        fire.Enable();
+        isPlayed = true;
+    }
+
+    private void OnDisable()
+    {
+        fire.Disable();
+    }
+
+    void Awake()
+    {
+        playerControls = new InputControl();
+        isPlayed = false;
+        shootableMask = LayerMask.GetMask("Shootable");
+        gunParticles = GetComponent<ParticleSystem>();
+        gunLines = GetComponentsInChildren<LineRenderer>();
+        gunAudio = GetComponent<AudioSource>();
+        gunLight = GetComponent<Light>();
+
+        shootRays = new Ray[5];
+        shootHits = new RaycastHit[5];
     }
 
 	static public void upgrade()
 	{
 		int multiplier = damagePerShot / baseDamagePerShot;
-        baseDamagePerShot += 10;
+        baseDamagePerShot = Mathf.RoundToInt(baseDamagePerShot * 1.3f);
 		damagePerShot = baseDamagePerShot * multiplier;
 	}
 
-	public void buffDamage(float damageMultiplier)
-    {
-        damagePerShot = (int) (damageMultiplier * baseDamagePerShot);
-    }
-
-    public void removeBuff()
-    {
-        damagePerShot = baseDamagePerShot;
-    }
- 
-    public void OnGameStop(Component sender, object data)
-    {
-        isPlayed = false;
-    }
-
-    public void OnGameStart(Component sender, object data)
-    {
-        isPlayed = true;
-    }
-
-    void Update()
-    {
-        if (isPlayed)
-        {
-            timer += Time.deltaTime;
-            if ((fire.ReadValue<float>() == 1 || isShooting) && timer >= timeBetweenBullets)
-            {
-                Shoot();
-            }
-
-            if (timer >= timeBetweenBullets * effectsDisplayTime)
-            {
-                DisableEffects();
-            }
-        }
-        
+	public void buffDamage(float damageMultiplier)
+    {
+        damagePerShot = (int) (damageMultiplier * baseDamagePerShot);
+    }
+
+    public void removeBuff()
+    {
+        damagePerShot = baseDamagePerShot;
+    }
+ 
+    public void OnGameStop(Component sender, object data)
+    {
+        isPlayed = false;
+    }
+
+    public void OnGameStart(Component sender, object data)
+    {
+        isPlayed = true;
+    }
+
+    void Update()
+    {
+        if (isPlayed)
+        {
+            timer += Time.deltaTime;
+            if ((fire.ReadValue<float>() == 1 || isShooting) && timer >= timeBetweenBullets)
+            {
+                Shoot();
+            }
+
+            if (timer >= timeBetweenBullets * effectsDisplayTime)
+            {
+                DisableEffects();
+            }
+        }
+        
     }
 
 	static public void addDamage(int damage)
@@ -104,65 +104,65 @@ public class PlayerShotgun : MonoBehaviour
 		damagePerShot = baseDamagePerShot * multiplier;
 	}
 
-	public void onShoot(Component sender, object data)
-    {
-        float time = (float)data;
-        StartCoroutine(AutoShooting(time));
-    }
-    private IEnumerator AutoShooting(float time)
-    {
-        isPlayed = true;
-        isShooting = true;
-        yield return new WaitForSeconds(time);
-        isPlayed = false;
-        isShooting = false;
-
-    }
-
-    public void DisableEffects()
-    {
-        foreach (LineRenderer gunLine in gunLines)
-        {
-            gunLine.enabled = false;
-        }
-        gunLight.enabled = false;
-    }
-
-    void Shoot()
-    {
-        timer = 0f;
-
-        gunAudio.Play();
-
-        gunLight.enabled = true;
-
-        gunParticles.Stop();
-        gunParticles.Play();
-        animator.SetTrigger("Shoot");
-
-        for (int i = 0; i < gunLines.Length; i++)
-        {
-            gunLines[i].enabled = true;
-            gunLines[i].SetPosition(0, transform.position);
-
-            shootRays[i].origin = transform.position;
-            shootRays[i].direction = Quaternion.AngleAxis(angleInterval * (i-2), Vector3.up) * transform.forward;
-
-            if (Physics.Raycast(shootRays[i], out shootHits[i], range, shootableMask))
-            {
-                EnemyHealth enemyHealth = shootHits[i].collider.GetComponent<EnemyHealth>();
-                
-                if (enemyHealth != null)
-                {
-                    enemyHealth.TakeDamage(damagePerShot - (int) (Vector3.Distance(transform.position, enemyHealth.transform.position) * 3), shootHits[i].point);
-                }
-
-                gunLines[i].SetPosition(1, shootHits[i].point);
-            }
-            else
-            {
-                gunLines[i].SetPosition(1, shootRays[i].origin + shootRays[i].direction * range);
-            }
-        }
-    }
+	public void onShoot(Component sender, object data)
+    {
+        float time = (float)data;
+        StartCoroutine(AutoShooting(time));
+    }
+    private IEnumerator AutoShooting(float time)
+    {
+        isPlayed = true;
+        isShooting = true;
+        yield return new WaitForSeconds(time);
+        isPlayed = false;
+        isShooting = false;
+
+    }
+
+    public void DisableEffects()
+    {
+        foreach (LineRenderer gunLine in gunLines)
+        {
+            gunLine.enabled = false;
+        }
+        gunLight.enabled = false;
+    }
+
+    void Shoot()
+    {
+        timer = 0f;
+
+        gunAudio.Play();
+
+        gunLight.enabled = true;
+
+        gunParticles.Stop();
+        gunParticles.Play();
+        animator.SetTrigger("Shoot");
+
+        for (int i = 0; i < gunLines.Length; i++)
+        {
+            gunLines[i].enabled = true;
+            gunLines[i].SetPosition(0, transform.position);
+
+            shootRays[i].origin = transform.position;
+            shootRays[i].direction = Quaternion.AngleAxis(angleInterval * (i-2), Vector3.up) * transform.forward;
+
+            if (Physics.Raycast(shootRays[i], out shootHits[i], range, shootableMask))
+            {
+                EnemyHealth enemyHealth = shootHits[i].collider.GetComponent<EnemyHealth>();
+                
+                if (enemyHealth != null)
+                {
+                    enemyHealth.TakeDamage(damagePerShot - (int) (Vector3.Distance(transform.position, enemyHealth.transform.position) * 3), shootHits[i].point);
+                }
+
+                gunLines[i].SetPosition(1, shootHits[i].point);
+            }
+            else
+            {
+                gunLines[i].SetPosition(1, shootRays[i].origin + shootRays[i].direction * range);
+            }
+        }
+    }
 }
\ No newline at end of file
diff --git a/Assets/Scripts/SaveLoad/SaveData.cs b/Assets/Scripts/SaveLoad/SaveData.cs
index 244ebc5..e317cec 100644
--- a/Assets/Scripts/SaveLoad/SaveData.cs
+++ b/Assets/Scripts/SaveLoad/SaveData.cs
@@ -5,4 +5,7 @@ public class SaveData
     public int money;
     public string level;
     public string date;
+
+    public int base_default_weapon_atk;
+	public int base_shotgun_atk;
 }
diff --git a/Assets/Scripts/SaveLoad/SaveLoadUI.cs b/Assets/Scripts/SaveLoad/SaveLoadUI.cs
index a588919..76f221b 100644
--- a/Assets/Scripts/SaveLoad/SaveLoadUI.cs
+++ b/Assets/Scripts/SaveLoad/SaveLoadUI.cs
@@ -31,6 +31,7 @@ public class SaveLoadUI : MonoBehaviour
             LoadMoney(data.money);
             LoadLevel(data.level);
             LoadDate(data.date);
+            LoadDefaultWeapon(data.base_default_weapon_atk);
         }
         else
         {
@@ -42,6 +43,10 @@ public class SaveLoadUI : MonoBehaviour
         }
     }
 
+    private void LoadDefaultWeapon(int base_damage) {
+        PlayerShooting.baseDamagePerShot = base_damage;
+    }
+
     private void LoadTime(int time)
     {
         int minute = time / 60000;
diff --git a/Assets/Scripts/Shop/ShopItem.cs b/Assets/Scripts/Shop/ShopItem.cs
index 691fa3b..dcb4b3f 100644
--- a/Assets/Scripts/Shop/ShopItem.cs
+++ b/Assets/Scripts/Shop/ShopItem.cs
@@ -34,10 +34,10 @@ public class ShopItem : MonoBehaviour
             case ItemType.Weapon_Shotgun:   return 100;
             case ItemType.Weapon_Sword:     return 50;
 
-            case ItemType.Weapon_Default_Upgrade: return 50;
-			case ItemType.Weapon_Bow_Upgrade: return 250;
-            case ItemType.Weapon_Shotgun_Upgrade: return 150;
-            case ItemType.Weapon_Sword_Upgrade: return 100;
+            case ItemType.Weapon_Default_Upgrade: return 125;
+			case ItemType.Weapon_Bow_Upgrade: return 350;
+            case ItemType.Weapon_Shotgun_Upgrade: return 225;
+            case ItemType.Weapon_Sword_Upgrade: return 200;
 		}
 	}
 }
-- 
GitLab