Refactored Sound script & removed an animation bug

This commit is contained in:
MarcUs7 2024-12-28 21:49:19 +01:00
parent a90f5fe421
commit e51c6f4650
3 changed files with 14 additions and 14 deletions

View file

@ -20,7 +20,7 @@ public class PlayerSaving : MonoBehaviour
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() private static void LoadPlayer()
{ {
var data = SaveSystem.LoadPlayer(); var data = SaveSystem.LoadPlayer();

View file

@ -17,7 +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"); private static readonly int IsCrouchingAnimationID = Animator.StringToHash("IsCrouching");
public void IsPauseing() public void IsPauseing()
{ {
@ -58,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(IsCrouchingAnimationID, false);
} }
} }
} }

View file

@ -6,10 +6,10 @@ public class SoundBar : MonoBehaviour
{ {
public static float SoundVolume = 0.75f; public static float SoundVolume = 0.75f;
public AudioSource BackgroundMusic; public AudioSource BackgroundMusic;
public static float MusicTime = 0f; // Static variable to store the music time private static float _musicTime; // Static variable to store the music time
public static bool SceneReloaded = false; // Static variable to track if scene is reloaded public static bool SceneReloaded; // Static variable to track if scene is reloaded
void Start() private void Start()
{ {
LoadSoundVolume(); LoadSoundVolume();
ApplySoundVolume(); ApplySoundVolume();
@ -17,38 +17,38 @@ public class SoundBar : MonoBehaviour
if (SceneReloaded) if (SceneReloaded)
{ {
// If the scene has been reloaded before, continue playing from where it stopped // If the scene has been reloaded before, continue playing from where it stopped
BackgroundMusic.time = MusicTime; BackgroundMusic.time = _musicTime;
} }
SceneReloaded = false; SceneReloaded = false;
} }
void Update() private void Update()
{ {
if (SoundVolume != BackgroundMusic.volume) if (!Mathf.Approximately(SoundVolume, BackgroundMusic.volume))
{ {
SoundVolume = BackgroundMusic.volume; SoundVolume = BackgroundMusic.volume;
SaveSoundVolume(); SaveSoundVolume();
} }
MusicTime = BackgroundMusic.time; _musicTime = BackgroundMusic.time;
} }
public static void SaveSoundVolume() private static void SaveSoundVolume()
{ {
PlayerPrefs.SetFloat("SoundVolume", SoundVolume); PlayerPrefs.SetFloat("SoundVolume", SoundVolume);
PlayerPrefs.Save(); PlayerPrefs.Save();
} }
void LoadSoundVolume() private static void LoadSoundVolume()
{ {
if (PlayerPrefs.HasKey("SoundVolume")) if (PlayerPrefs.HasKey("SoundVolume"))
{ {
float savedVolume = PlayerPrefs.GetFloat("SoundVolume"); var savedVolume = PlayerPrefs.GetFloat("SoundVolume");
SoundVolume = savedVolume; SoundVolume = savedVolume;
} }
} }
void ApplySoundVolume() private void ApplySoundVolume()
{ {
BackgroundMusic.volume = SoundVolume; BackgroundMusic.volume = SoundVolume;
} }