Refactored Save scripts

This commit is contained in:
MarcUs7 2024-12-28 19:24:28 +01:00
parent 3655968bfc
commit b26343b261
14 changed files with 55 additions and 56 deletions

View file

@ -15,7 +15,7 @@ public class SC_2DCoin : MonoBehaviour
{ {
//Make Collider2D as trigger //Make Collider2D as trigger
GetComponent<Collider2D>().isTrigger = true; GetComponent<Collider2D>().isTrigger = true;
TotalCoins = PlayerSaving.coins; TotalCoins = PlayerSaving.Coins;
} }
private void OnTriggerEnter2D(Collider2D c2d) private void OnTriggerEnter2D(Collider2D c2d)
@ -42,9 +42,9 @@ public class SC_2DCoin : MonoBehaviour
private void Update() private void Update()
{ {
if (TotalCoins != PlayerSaving.coins) if (TotalCoins != PlayerSaving.Coins)
{ {
PlayerSaving.coins = TotalCoins; PlayerSaving.Coins = TotalCoins;
PlayerSaving.SavePlayer(); PlayerSaving.SavePlayer();
} }
} }

View file

@ -36,7 +36,7 @@ public class minusCounter : MonoBehaviour
for (int i = 0; i < levelRanges.Length; i++) for (int i = 0; i < levelRanges.Length; i++)
{ {
// Check if player's level is within the range for the current coin value // Check if player's level is within the range for the current coin value
if (PlayerSaving.level <= levelRanges[i] && (i == 0 || PlayerSaving.level > levelRanges[i - 1])) if (PlayerSaving.Level <= levelRanges[i] && (i == 0 || PlayerSaving.Level > levelRanges[i - 1]))
{ {
if (SC_2DCoin.TotalCoins + levelRanges[i] < levelRanges[i]) if (SC_2DCoin.TotalCoins + levelRanges[i] < levelRanges[i])
{ {

View file

@ -13,7 +13,7 @@ public class Endlevel : MonoBehaviour
{ {
//Make Collider2D as trigger //Make Collider2D as trigger
GetComponent<Collider2D>().isTrigger = true; GetComponent<Collider2D>().isTrigger = true;
level = PlayerSaving.level; level = PlayerSaving.Level;
SetLevel = GetValueForSetLevel(); SetLevel = GetValueForSetLevel();
} }
@ -27,11 +27,11 @@ public class Endlevel : MonoBehaviour
void Update() void Update()
{ {
if (level != PlayerSaving.level) if (level != PlayerSaving.Level)
{ {
if (PlayerSaving.level < level) if (PlayerSaving.Level < level)
{ {
PlayerSaving.level = level; PlayerSaving.Level = level;
PlayerSaving.SavePlayer(); PlayerSaving.SavePlayer();
//Debug.Log("Saved " + PlayerSaving.levels + " Level"); //Debug.Log("Saved " + PlayerSaving.levels + " Level");
} }

View file

@ -5,16 +5,16 @@ using UnityEngine;
[System.Serializable] [System.Serializable]
public class PlayerData public class PlayerData
{ {
public int savedLevels = 0; public int savedLevels;
public int savedCoins = 0; public int savedCoins;
public bool tutorial = false; public bool tutorial;
public bool cloudsMove = true; public bool cloudsMove;
public PlayerData () public PlayerData()
{ {
savedLevels = PlayerSaving.level; savedLevels = PlayerSaving.Level;
savedCoins = PlayerSaving.coins; savedCoins = PlayerSaving.Coins;
tutorial = PlayerSaving.hasCompletedTutorial; tutorial = PlayerSaving.HasCompletedTutorial;
cloudsMove = PlayerSaving.movingClouds; cloudsMove = PlayerSaving.MovingClouds;
} }
} }

View file

@ -4,12 +4,12 @@ using UnityEngine;
public class PlayerSaving : MonoBehaviour public class PlayerSaving : MonoBehaviour
{ {
public static int level = 0; public static int Level;
public static int coins = 0; public static int Coins;
public static bool hasCompletedTutorial = false; public static bool HasCompletedTutorial;
public static bool movingClouds = true; public static bool MovingClouds = true;
void Awake() private void Awake()
{ {
LoadPlayer(); LoadPlayer();
} }
@ -17,29 +17,29 @@ public class PlayerSaving : MonoBehaviour
public static void SavePlayer() public static void SavePlayer()
{ {
SaveSystem.SavePlayer(); SaveSystem.SavePlayer();
Debug.Log($"Saved level: {level}, coins: {coins}, tutorial: {hasCompletedTutorial}, cloudsMove: {movingClouds}"); Debug.Log($"Saved level: {Level}, coins: {Coins}, tutorial: {HasCompletedTutorial}, cloudsMove: {MovingClouds}");
} }
public static void LoadPlayer() public static void LoadPlayer()
{ {
PlayerData data = SaveSystem.LoadPlayer(); var data = SaveSystem.LoadPlayer();
level = data.savedLevels; Level = data.savedLevels;
coins = data.savedCoins; Coins = data.savedCoins;
hasCompletedTutorial = data.tutorial; HasCompletedTutorial = data.tutorial;
movingClouds = data.cloudsMove; MovingClouds = data.cloudsMove;
Debug.Log($"Loaded level: {level}, coins: {coins}, tutorial: {hasCompletedTutorial}, cloudsMove: {movingClouds}"); Debug.Log($"Loaded level: {Level}, coins: {Coins}, tutorial: {HasCompletedTutorial}, cloudsMove: {MovingClouds}");
SC_2DCoin.TotalCoins = coins; SC_2DCoin.TotalCoins = Coins;
Endlevel.level = level; Endlevel.level = Level;
} }
public static void DeletePlayer() public static void DeletePlayer()
{ {
level = 0; Level = 0;
coins = 0; Coins = 0;
hasCompletedTutorial = false; HasCompletedTutorial = false;
movingClouds = true; MovingClouds = true;
// For MusicToggle.cs // For MusicToggle.cs
PlayerPrefs.SetInt("MusicToggled", 1); PlayerPrefs.SetInt("MusicToggled", 1);

View file

@ -4,7 +4,7 @@ using System.Runtime.Serialization.Formatters.Binary;
public static class SaveSystem public static class SaveSystem
{ {
public static void SavePlayer () public static void SavePlayer()
{ {
BinaryFormatter formatter = new BinaryFormatter(); BinaryFormatter formatter = new BinaryFormatter();
string path = Application.persistentDataPath + "/player.save"; string path = Application.persistentDataPath + "/player.save";
@ -16,7 +16,7 @@ public static class SaveSystem
stream.Close(); stream.Close();
} }
public static PlayerData LoadPlayer () public static PlayerData LoadPlayer()
{ {
string path = Application.persistentDataPath + "/player.save"; string path = Application.persistentDataPath + "/player.save";
if (File.Exists(path)) if (File.Exists(path))
@ -29,13 +29,11 @@ public static class SaveSystem
return data; return data;
} }
else
{ Debug.LogError("SaveFile not found in " + path);
Debug.LogError("SaveFile not found in " + path); Debug.Log("Creating new save file...");
Debug.Log("Creating new save file..."); SavePlayer();
SavePlayer(); return LoadPlayer();
return LoadPlayer();
}
} }
} }

View file

@ -14,7 +14,7 @@ public class CloudsToggle : MonoBehaviour
{ {
changingStates = true; changingStates = true;
cloudsCanMove = PlayerSaving.movingClouds; cloudsCanMove = PlayerSaving.MovingClouds;
cloudsToggle.isOn = cloudsCanMove; cloudsToggle.isOn = cloudsCanMove;
previousCloudsCanMoveState = cloudsCanMove; previousCloudsCanMoveState = cloudsCanMove;
@ -32,7 +32,7 @@ public class CloudsToggle : MonoBehaviour
if (cloudsCanMove != previousCloudsCanMoveState) if (cloudsCanMove != previousCloudsCanMoveState)
{ {
//Debug.Log("State changed. Saving Clouds state: " + cloudsCanMove); //Debug.Log("State changed. Saving Clouds state: " + cloudsCanMove);
PlayerSaving.movingClouds = cloudsCanMove; PlayerSaving.MovingClouds = cloudsCanMove;
PlayerSaving.SavePlayer(); PlayerSaving.SavePlayer();
previousCloudsCanMoveState = cloudsCanMove; previousCloudsCanMoveState = cloudsCanMove;
} }

View file

@ -39,7 +39,7 @@ public class CheckDeleted : MonoBehaviour
} }
count++; count++;
Exit = 0; Exit = 0;
if (PlayerSaving.level == 0 && PlayerSaving.coins == 0 && !PlayerSaving.hasCompletedTutorial && PlayerSaving.movingClouds) if (PlayerSaving.Level == 0 && PlayerSaving.Coins == 0 && !PlayerSaving.HasCompletedTutorial && PlayerSaving.MovingClouds)
{ {
Deleted.SetActive(true); Deleted.SetActive(true);
@ -62,7 +62,7 @@ public class CheckDeleted : MonoBehaviour
PlayerSaving.LoadPlayer(); PlayerSaving.LoadPlayer();
count++; count++;
Delete = 0; Delete = 0;
if (PlayerSaving.level == 0 && PlayerSaving.coins == 0 && !PlayerSaving.hasCompletedTutorial && PlayerSaving.movingClouds) if (PlayerSaving.Level == 0 && PlayerSaving.Coins == 0 && !PlayerSaving.HasCompletedTutorial && PlayerSaving.MovingClouds)
{ {
Deleted.SetActive(true); Deleted.SetActive(true);
Debug.Log("Done Stage: " + count); Debug.Log("Done Stage: " + count);

View file

@ -35,8 +35,8 @@ public class ClearData : MonoBehaviour
int[] defaultInts = { 0, 0 }; int[] defaultInts = { 0, 0 };
bool[] defaultBools = { false, true }; bool[] defaultBools = { false, true };
int[] checkInts = { PlayerSaving.level, PlayerSaving.coins }; int[] checkInts = { PlayerSaving.Level, PlayerSaving.Coins };
bool[] checkBools = { PlayerSaving.hasCompletedTutorial, PlayerSaving.movingClouds }; bool[] checkBools = { PlayerSaving.HasCompletedTutorial, PlayerSaving.MovingClouds };
for (int i = 0; i < defaultInts.Length; i++) for (int i = 0; i < defaultInts.Length; i++)
{ {

View file

@ -17,6 +17,7 @@ public class Pause : MonoBehaviour
public GameObject Fire; public GameObject Fire;
public GameObject Image; public GameObject Image;
private static readonly int IsCrouching = Animator.StringToHash("IsCrouchingAnimationID");
public void IsPauseing() public void IsPauseing()
{ {
@ -57,7 +58,7 @@ public class Pause : MonoBehaviour
if(!IsPause) // if going back to the game if(!IsPause) // if going back to the game
{ {
PlayerMovement.Crouch = false; PlayerMovement.Crouch = false;
animator.SetBool("IsCrouching", false); animator.SetBool(IsCrouching, false);
} }
} }
} }

View file

@ -49,7 +49,7 @@ public class Tutorial : MonoBehaviour
IEnumerator StartGame() IEnumerator StartGame()
{ {
PlayerSaving.hasCompletedTutorial = true; PlayerSaving.HasCompletedTutorial = true;
PlayerSaving.SavePlayer(); PlayerSaving.SavePlayer();
yield return new WaitForSeconds(0.5f); yield return new WaitForSeconds(0.5f);
mainMenu.StartLevel(1); mainMenu.StartLevel(1);

View file

@ -21,7 +21,7 @@ public class LevelButtons : MonoBehaviour
} }
mainMenu = FindObjectOfType<MainMenu>().GetComponent<MainMenu>(); mainMenu = FindObjectOfType<MainMenu>().GetComponent<MainMenu>();
level = PlayerSaving.level; level = PlayerSaving.Level;
InitializeButtonListeners(); InitializeButtonListeners();
UpdateButtons(); UpdateButtons();

View file

@ -26,7 +26,7 @@ public class MainMenu : MonoBehaviour
public void StartLevel(int level) public void StartLevel(int level)
{ {
bool tutorial = PlayerSaving.hasCompletedTutorial; bool tutorial = PlayerSaving.HasCompletedTutorial;
if (level == 1 && !tutorial) if (level == 1 && !tutorial)
{ {
sceneFader.FadeTo(scenes[3]); sceneFader.FadeTo(scenes[3]);

View file

@ -55,7 +55,7 @@ public class PlayerHealth : MonoBehaviour
if (SC_2DCoin.TotalCoins > coinDeductions[i] && health <= 0) if (SC_2DCoin.TotalCoins > coinDeductions[i] && health <= 0)
{ {
// Subtract coins based on the level // Subtract coins based on the level
if (PlayerSaving.level <= coinDeductions[i]) if (PlayerSaving.Level <= coinDeductions[i])
{ {
SC_2DCoin.TotalCoins -= coinDeductions[i]; SC_2DCoin.TotalCoins -= coinDeductions[i];
Minus = true; Minus = true;
@ -68,7 +68,7 @@ public class PlayerHealth : MonoBehaviour
} }
// Save the updated coin count // Save the updated coin count
PlayerSaving.coins = SC_2DCoin.TotalCoins; PlayerSaving.Coins = SC_2DCoin.TotalCoins;
PlayerSaving.SavePlayer(); PlayerSaving.SavePlayer();
} }
} }