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

View file

@ -36,7 +36,7 @@ public class minusCounter : MonoBehaviour
for (int i = 0; i < levelRanges.Length; i++)
{
// 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])
{

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -39,7 +39,7 @@ public class CheckDeleted : MonoBehaviour
}
count++;
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);
@ -62,7 +62,7 @@ public class CheckDeleted : MonoBehaviour
PlayerSaving.LoadPlayer();
count++;
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);
Debug.Log("Done Stage: " + count);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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