Skip to content

Commit

Permalink
Implement basics of high-score system
Browse files Browse the repository at this point in the history
Signed-off-by: AnimaRain <[email protected]>
  • Loading branch information
AnimaRain committed Jun 21, 2020
1 parent b796fbb commit b9a03e5
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 50 deletions.
Binary file added Assets/Resources/highscores-null.bytes
Binary file not shown.
7 changes: 7 additions & 0 deletions Assets/Resources/highscores-null.bytes.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion Assets/Scenes/MainMenu.unity
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ MonoBehaviour:
m_SelectedTrigger: Highlighted
m_DisabledTrigger: Disabled
m_Interactable: 1
m_TargetGraphic: {fileID: 0}
m_TargetGraphic: {fileID: 45859777}
m_OnClick:
m_PersistentCalls:
m_Calls:
Expand Down Expand Up @@ -4127,6 +4127,7 @@ GameObject:
m_Component:
- component: {fileID: 1375839745}
- component: {fileID: 1375839746}
- component: {fileID: 1375839747}
m_Layer: 5
m_Name: High-scores
m_TagString: Untagged
Expand Down Expand Up @@ -4171,6 +4172,29 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1375839744}
m_CullTransparentMesh: 0
--- !u!114 &1375839747
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1375839744}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: a8339215e52ae13d6936a04a9d067812, type: 3}
m_Name:
m_EditorClassIdentifier:
rows:
- {fileID: 748562737}
- {fileID: 377769278}
- {fileID: 1093870111}
- {fileID: 1762252980}
- {fileID: 46379384}
- {fileID: 576512188}
- {fileID: 204991251}
- {fileID: 1120473686}
- {fileID: 839775347}
- {fileID: 1469231212}
--- !u!1 &1387456275
GameObject:
m_ObjectHideFlags: 0
Expand Down
4 changes: 2 additions & 2 deletions Assets/Scripts/Game/ScoreManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ public class ScoreManager : MonoBehaviour
{
[SerializeField] private Text scoreLabel;

public int Score { get; private set; }
public long Score { get; private set; }

public static ScoreManager Create(Text scoreLabel = null, int score = 0) {
public static ScoreManager Create(Text scoreLabel = null, long score = 0) {
var o = new GameObject(nameof(ScoreManager)).AddComponent<ScoreManager>();
o.Score = score;
o.scoreLabel = scoreLabel;
Expand Down
24 changes: 24 additions & 0 deletions Assets/Scripts/Menu/HighscoreTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using UnityEngine;
using UnityEngine.UI;

namespace ShootAR.Menu {
public class HighscoreTable : MonoBehaviour {
[SerializeField] private GameObject[] rows;

private ScoreList scores;

public void OnEnable() {
if (rows == null) return;

scores = ScoreList.LoadScores();

for (int i = 0; i < ScoreList.POSITIONS; i++) {
Text[] column = rows[i].GetComponentsInChildren<Text>();

var scoreInfo = (name: scores.Get(i).Item1, points: scores.Get(i).Item2);
column[0].text = scoreInfo.name;
column[1].text = scoreInfo.points.ToString();
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Menu/HighscoreTable.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions Assets/Scripts/Menu/ScoreList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.IO;
using UnityEngine;

namespace ShootAR {
public class ScoreList {
/// <summary>Maximum number of positions in list</summary>
public const int POSITIONS = 10;

private string[] name = new string[POSITIONS];
private long[] score = new long[POSITIONS];

/// <summary>Add score to appropriate position</summary>
/// <param name="score">score to add to list</param>
/// <param name="name">name of player achieved the score</param>
/// <returns>
/// True if added to list.
/// False if <paramref name="score"/> lower than all scores.
/// </returns>
public bool AddScore(string name, long score) {
// Immediately return if score lower than all scores.
if (score < this.score[POSITIONS - 1]) return false;

for (int i = 0; i < POSITIONS; i++) {
// Find in which position the score should go
if (score > this.score[i]) {
// move the lower scores one place over (dropping the lowest),
for (int j = POSITIONS - 1; j > i; j--) {
this.score[j] = this.score[j - 1];
this.name[j] = this.name[j - 1];
}

// and replace that position.
this.score[i] = score;
this.name[i] = name;

break;
}
}

return true;
}

/// <summary>Returns the name and score on the
/// <paramref name="position"/>-th line of the list
/// </summary>
/// <param name="position">The position of the score</param>
/// <returns>A tuple with the score's information</returns>
public (string, long) Get(int position) => (name[position], score[position]);

/// <summary>Load high-score table from file.</summary>
public static ScoreList LoadScores() {
const string highscoreFile = "highscores";
string file = Path.Combine(Application.persistentDataPath, highscoreFile);

if (!File.Exists(file))
LocalFiles.CopyResourceToPersistentData("highscores-null", highscoreFile);

ScoreList scores = new ScoreList();

using (
BinaryReader reader = new BinaryReader(
new FileInfo(file).OpenRead())
) {
for (int i = 0; i < 10; i++) {
scores.AddScore(
reader.ReadString(),
reader.ReadInt64()
);
}
}

return scores;
}
}
}
File renamed without changes.
45 changes: 0 additions & 45 deletions Assets/Scripts/ScoreList.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Assets/Tests/Tests.asmdef
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Tests",
"references": ["Main"],
"references": ["Main", "Menu"],
"optionalUnityReferences": [
"TestAssemblies"
],
Expand Down

0 comments on commit b9a03e5

Please sign in to comment.