Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/bugfix #72

Merged
merged 7 commits into from
Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Assets/Prefabs/UI/PauseManager.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -44,3 +44,6 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
ui: {fileID: 0}
audioSources: []
normalAudioVolume: 1
pauseAudioVolume: 0.3
4 changes: 4 additions & 0 deletions Assets/Scripts/GameState/GameOverScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using UnityEngine.UI;


/// <summary>
/// Contains button functionality for the Game Over screen UI Canvas
/// </summary>
public class GameOverScreen : MonoBehaviour
{
public Text nameInput;
Expand All @@ -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()
Expand Down
127 changes: 65 additions & 62 deletions Assets/Scripts/GameState/LeaderboardData.cs
Original file line number Diff line number Diff line change
@@ -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<Highscore>
{
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);
}
}

Expand All @@ -20,94 +36,81 @@ public static class LeaderboardData
// Path to where highscore data is saved
private static string path = Application.persistentDataPath + "/highscores.data";

/// <summary>
/// Add a new score to the leaderboard
/// </summary>
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<Highscore> 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)
/// <summary>
/// Save all the given scores
/// </summary>
public static void SaveScores(List<Highscore> scores)
{
BinaryFormatter formatter = new BinaryFormatter();

FileStream stream = new FileStream(path, FileMode.Create);

formatter.Serialize(stream, scores);

stream.Close();
}

public static Highscores LoadScores()
/// <summary>
/// Load all saved scores
/// </summary>
/// <returns>An ordered list of <c>Highscore</c> structs.</returns>
public static List<Highscore> 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<Highscore> data;

if(data.name.Length < 10 || data.score.Length < 10)
// Check if the file is corrupted
try
{
data = formatter.Deserialize(stream) as List<Highscore>;
} 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<Highscore> highscores = new List<Highscore>
{
"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;
}
}
}
25 changes: 25 additions & 0 deletions Assets/Scripts/GameState/PauseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scripts/UI/Leaderboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ public class Leaderboard : MonoBehaviour
private void Start()
{

Highscores highscores = LeaderboardData.LoadScores();
List<Highscore> 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>().text = highscores.name[i];
entry.transform.GetChild(1).GetComponent<Text>().text = highscores.score[i].ToString();
entry.transform.GetChild(0).GetComponent<Text>().text = highscores[i].name;
entry.transform.GetChild(1).GetComponent<Text>().text = highscores[i].score.ToString();
}
}
}