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 2 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
3 changes: 3 additions & 0 deletions Assets/Scripts/GameState/GameOverScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using UnityEngine.SceneManagement;
using UnityEngine.UI;

/// <summary>
/// Contains button functionality for the Game Over screen UI Canvas
/// </summary>
public class GameOverScreen : MonoBehaviour
{
public Text nameInput;
Expand Down
112 changes: 56 additions & 56 deletions Assets/Scripts/GameState/LeaderboardData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,48 +4,59 @@
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 Highscores(int[] Score, string[] Name)
public Highscore(int Score, string Name)
{
score = Score;
name = Name;
ddabble marked this conversation as resolved.
Show resolved Hide resolved
}
}

class HighscoreComparator : IComparer<Highscore>
{
public int Compare(Highscore x, Highscore y)
{
if (x.score == 0 || y.score == 0)
{
return 0;
}

return x.score.CompareTo(y.score);
}

}

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>
/// <param name="score"></param>
/// <param name="name"></param>
ddabble marked this conversation as resolved.
Show resolved Hide resolved
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
HighscoreComparator HC = new HighscoreComparator();
ddabble marked this conversation as resolved.
Show resolved Hide resolved

break;
}
}
highscores.Sort(HC);

SaveScores(newScores);
SaveScores(highscores);
}

public static void SaveScores(Highscores scores)
/// <summary>
/// Save all the given scores
/// </summary>
/// <param name="scores"></param>
ddabble marked this conversation as resolved.
Show resolved Hide resolved
public static void SaveScores(List<Highscore> scores)
{
BinaryFormatter formatter = new BinaryFormatter();

Expand All @@ -56,17 +67,21 @@ public static void SaveScores(Highscores scores)
stream.Close();
}

public static Highscores LoadScores()
/// <summary>
/// Load all saved scores
/// </summary>
/// <returns>Returns a ordered list of Highscore structs</returns>
ddabble marked this conversation as resolved.
Show resolved Hide resolved
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;
List<Highscore> data = formatter.Deserialize(stream) as List<Highscore>;
stream.Close();

if(data.name.Length < 10 || data.score.Length < 10)
if(data.Count < 10 || data == null)
{
// Scores are missing, delete the data and reset
File.Delete(path);
Expand All @@ -77,37 +92,22 @@ public static Highscores LoadScores()
}
else
{
List<Highscore> highscores = new List<Highscore>();

// 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]
{
"The Archetype",
"Fuereoduriko",
"Dabble",
"Frisk",
"Jesper",
"Rodrigues",
"Zedd",
"Grønnmerke",
"KHTangent",
"Endie"
};

Highscores data = new Highscores(defaultScores, defaultNames);

SaveScores(data);
return data;
highscores.Add(new Highscore(5000, "The Archetype"));
highscores.Add(new Highscore(4500, "Fuereoduriko"));
highscores.Add(new Highscore(4000, "Dabble"));
highscores.Add(new Highscore(3500, "Frisk"));
highscores.Add(new Highscore(3000, "Jesper"));
highscores.Add(new Highscore(2500, "Rodrigues"));
highscores.Add(new Highscore(2000, "Zedd"));
highscores.Add(new Highscore(1500, "Grønnmerke"));
highscores.Add(new Highscore(1000, "KHTangent"));
highscores.Add(new Highscore(0, "Endie"));

SaveScores(highscores);
return highscores;
}
}
}
16 changes: 15 additions & 1 deletion Assets/Scripts/GameState/PauseManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ public class PauseManager : MonoBehaviour
[SerializeField]
public GameObject ui;

// An array of all audiosources in the game.
public AudioSource[] audioSources;
ddabble marked this conversation as resolved.
Show resolved Hide resolved

public bool IsPaused { get; private set; } = false;

private float initialTimeScale;
Expand Down Expand Up @@ -43,8 +46,19 @@ void Start()
public void PauseGame()
{
IsPaused = !IsPaused;
Time.timeScale = IsPaused ? 0 : initialTimeScale;
Time.timeScale = IsPaused ? 0 : initialTimeScale;
ui.SetActive(IsPaused);

// Pause every audiosource in array.
foreach(AudioSource a in audioSources)
{
if (IsPaused)
a.Pause();
else
a.UnPause();
}
// Reduce the listener volume level
AudioListener.volume = IsPaused ? 0.3f : 1f;
}

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();
}
}
}