diff --git a/Assets/Prefabs/UI/PauseManager.prefab b/Assets/Prefabs/UI/PauseManager.prefab
index 8eca4401..d8aedc3a 100644
--- a/Assets/Prefabs/UI/PauseManager.prefab
+++ b/Assets/Prefabs/UI/PauseManager.prefab
@@ -25,7 +25,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8809422691332745151}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
- m_LocalPosition: {x: -21.43265, y: 11.227003, z: -40.404335}
+ m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
@@ -44,3 +44,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ui: {fileID: 0}
+ audioSources: []
+ normalAudioVolume: 1
+ pauseAudioVolume: 0.3
diff --git a/Assets/Scripts/GameState/GameOverScreen.cs b/Assets/Scripts/GameState/GameOverScreen.cs
index 04a29e20..3aec8892 100644
--- a/Assets/Scripts/GameState/GameOverScreen.cs
+++ b/Assets/Scripts/GameState/GameOverScreen.cs
@@ -4,6 +4,9 @@
using UnityEngine.UI;
+///
+/// Contains button functionality for the Game Over screen UI Canvas
+///
public class GameOverScreen : MonoBehaviour
{
public Text nameInput;
@@ -13,6 +16,7 @@ public class GameOverScreen : MonoBehaviour
private void Start()
{
scoreText.text = UIManager.Singleton.score.ToString();
+ PauseManager.Singleton.gameObject.SetActive(false);
}
public void Restart()
diff --git a/Assets/Scripts/GameState/LeaderboardData.cs b/Assets/Scripts/GameState/LeaderboardData.cs
index cf748af8..72d1cf63 100644
--- a/Assets/Scripts/GameState/LeaderboardData.cs
+++ b/Assets/Scripts/GameState/LeaderboardData.cs
@@ -1,17 +1,33 @@
using UnityEngine;
using System.Collections.Generic;
using System.IO;
+using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
+
[System.Serializable]
-public class Highscores {
- public int[] score;
- public string[] name;
+public struct Highscore
+{
+ public int score;
+ public string name;
+
+ public Highscore(int score, string name)
+ {
+ this.score = score;
+ this.name = name;
+ }
+}
- public Highscores(int[] Score, string[] Name)
+class HighscoreComparator : IComparer
+{
+ public int Compare(Highscore x, Highscore y)
{
- score = Score;
- name = Name;
+ if (x.score == 0)
+ return y.score;
+ if (y.score == 0)
+ return x.score;
+
+ return y.score.CompareTo(x.score);
}
}
@@ -20,35 +36,27 @@ public static class LeaderboardData
// Path to where highscore data is saved
private static string path = Application.persistentDataPath + "/highscores.data";
+ ///
+ /// Add a new score to the leaderboard
+ ///
public static void AddScore(int score, string name)
{
- Highscores highscores = LoadScores();
- Highscores newScores = highscores;
-
- for(int i = 0; i < 10; i++)
- {
- if(highscores.score[i] < score)
- {
- newScores.score[i] = score;
- newScores.name[i] = name;
+ List highscores = LoadScores();
+ highscores.Add(new Highscore(score, name));
- for(int n = i + 1; n < 10; n++)
- {
- newScores.score[n] = highscores.score[n - 1];
- newScores.name[n] = highscores.name[n - 1];
- }
+ // Sort the struct using a custom comparator
+ highscores.Sort(new HighscoreComparator());
- break;
- }
- }
-
- SaveScores(newScores);
+ SaveScores(highscores);
}
- public static void SaveScores(Highscores scores)
+ ///
+ /// Save all the given scores
+ ///
+ public static void SaveScores(List scores)
{
BinaryFormatter formatter = new BinaryFormatter();
-
+
FileStream stream = new FileStream(path, FileMode.Create);
formatter.Serialize(stream, scores);
@@ -56,58 +64,53 @@ public static void SaveScores(Highscores scores)
stream.Close();
}
- public static Highscores LoadScores()
+ ///
+ /// Load all saved scores
+ ///
+ /// An ordered list of Highscore structs.
+ public static List LoadScores()
{
if (File.Exists(path))
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = new FileStream(path, FileMode.Open);
- Highscores data = formatter.Deserialize(stream) as Highscores;
- stream.Close();
+ List data;
- if(data.name.Length < 10 || data.score.Length < 10)
+ // Check if the file is corrupted
+ try
+ {
+ data = formatter.Deserialize(stream) as List;
+ } catch (SerializationException e)
{
- // Scores are missing, delete the data and reset
File.Delete(path);
- data = LoadScores();
+ Debug.LogWarning($"Highscore data was corrupted, it has been replaced.\nException message: {e.Message}");
+ return LoadScores();
+ } finally
+ {
+ stream.Close();
}
return data;
- }
- else
+ } else
{
// No leaderboard exists create a default one
- int[] defaultScores = new int[10]{
- 5000,
- 4500,
- 4000,
- 3500,
- 3000,
- 2500,
- 2000,
- 1500,
- 1000,
- 0};
-
- string[] defaultNames = new string[10]
+ List highscores = new List
{
- "The Archetype",
- "Fuereoduriko",
- "Dabble",
- "Frisk",
- "Jesper",
- "Rodrigues",
- "Zedd",
- "Grønnmerke",
- "KHTangent",
- "Endie"
+ new Highscore(5000, "The Archetype"),
+ new Highscore(4500, "Fuereoduriko"),
+ new Highscore(4000, "Dabble"),
+ new Highscore(3500, "Frisk"),
+ new Highscore(3000, "Jesper"),
+ new Highscore(2500, "Rodrigues"),
+ new Highscore(2000, "Zedd"),
+ new Highscore(1500, "Grønnmerke"),
+ new Highscore(1000, "KHTangent"),
+ new Highscore(10, "Endie"),
};
- Highscores data = new Highscores(defaultScores, defaultNames);
-
- SaveScores(data);
- return data;
+ SaveScores(highscores);
+ return highscores;
}
}
}
diff --git a/Assets/Scripts/GameState/PauseManager.cs b/Assets/Scripts/GameState/PauseManager.cs
index a821bc24..0f16957b 100644
--- a/Assets/Scripts/GameState/PauseManager.cs
+++ b/Assets/Scripts/GameState/PauseManager.cs
@@ -10,6 +10,16 @@ public class PauseManager : MonoBehaviour
[SerializeField]
public GameObject ui;
+ // An array of all audiosources in the game.
+ [SerializeField]
+ private AudioSource[] audioSources;
+
+ [SerializeField]
+ private float normalAudioVolume = 1f;
+
+ [SerializeField]
+ private float pauseAudioVolume = 0.3f;
+
public bool IsPaused { get; private set; } = false;
private float initialTimeScale;
@@ -43,9 +53,24 @@ void Start()
public void PauseGame()
{
+ if (!gameObject.activeSelf)
+ return;
+
IsPaused = !IsPaused;
Time.timeScale = IsPaused ? 0 : initialTimeScale;
ui.SetActive(IsPaused);
+
+ // Pause every audiosource in array.
+ foreach (AudioSource source in audioSources)
+ {
+ if (IsPaused)
+ source.Pause();
+ else
+ source.UnPause();
+ }
+
+ // Reduce the listener volume level
+ AudioListener.volume = IsPaused ? pauseAudioVolume : normalAudioVolume;
}
public void QuitGame()
diff --git a/Assets/Scripts/UI/Leaderboard.cs b/Assets/Scripts/UI/Leaderboard.cs
index ee3f0571..d0e87c05 100644
--- a/Assets/Scripts/UI/Leaderboard.cs
+++ b/Assets/Scripts/UI/Leaderboard.cs
@@ -16,14 +16,14 @@ public class Leaderboard : MonoBehaviour
private void Start()
{
- Highscores highscores = LeaderboardData.LoadScores();
+ List highscores = LeaderboardData.LoadScores();
entries = new GameObject[10];
for(int i = 0; i < 10; i++)
{
GameObject entry = Instantiate(highscoreEntry, leaderboardBody);
- entry.transform.GetChild(0).GetComponent().text = highscores.name[i];
- entry.transform.GetChild(1).GetComponent().text = highscores.score[i].ToString();
+ entry.transform.GetChild(0).GetComponent().text = highscores[i].name;
+ entry.transform.GetChild(1).GetComponent().text = highscores[i].score.ToString();
}
}
}