-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement basics of high-score system
Signed-off-by: AnimaRain <[email protected]>
- Loading branch information
Showing
12 changed files
with
146 additions
and
50 deletions.
There are no files selected for viewing
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
Assets/Scripts/Game/LocalFiles.cs.meta → Assets/Scripts/Menu/LocalFiles.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters