added leaderboard
This commit is contained in:
1 parent
51b5659f4a
commit
8a9fe611e0
9 files changed
+1606
-25
No files matched your search
@@ -1,19 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Code.Saves
|
||||
{
|
||||
[Serializable]
|
||||
public class LeaderboardEntry
|
||||
{
|
||||
public string playerName;
|
||||
public int score;
|
||||
|
||||
public LeaderboardEntry(string name, int score)
|
||||
{
|
||||
playerName = name;
|
||||
this.score = score;
|
||||
}
|
||||
}
|
||||
|
||||
[Serializable]
|
||||
public class GameData
|
||||
{
|
||||
public int bodyMaterialIndex;
|
||||
public int faceMaterialIndex;
|
||||
public int hatIndex;
|
||||
|
||||
public Color platformColor = Color.white;
|
||||
public Color bonusColor = Color.white;
|
||||
|
||||
public string playerName = "Player";
|
||||
public int highScore;
|
||||
|
||||
public List<LeaderboardEntry> leaderboard = new List<LeaderboardEntry>();
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using Code.Saves;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.SceneManagement;
|
||||
@@ -76,10 +77,21 @@ namespace Code.UI
|
||||
|
||||
private void SaveScore()
|
||||
{
|
||||
var playerName = nameInputField.text;
|
||||
if (string.IsNullOrEmpty(playerName))
|
||||
playerName = "Player";
|
||||
Debug.Log($"Saved: {playerName} - {_coins}");
|
||||
var data = SaveManager.Load();
|
||||
var currentName = string.IsNullOrEmpty(nameInputField.text) ? "Player" : nameInputField.text;
|
||||
|
||||
data.playerName = currentName;
|
||||
|
||||
data.leaderboard.Add(new Saves.LeaderboardEntry(currentName, _coins));
|
||||
|
||||
data.leaderboard.Sort((a, b) => b.score.CompareTo(a.score));
|
||||
|
||||
if (data.leaderboard.Count > 10)
|
||||
{
|
||||
data.leaderboard.RemoveRange(10, data.leaderboard.Count - 10);
|
||||
}
|
||||
|
||||
SaveManager.Save(data);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using Code.Saves;
|
||||
using System.Text;
|
||||
using UnityEngine.SceneManagement;
|
||||
|
||||
namespace Code.UI
|
||||
{
|
||||
public class LeaderboardController : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private TMP_Text leaderboardText;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
DisplayLeaderboard();
|
||||
}
|
||||
|
||||
private void DisplayLeaderboard()
|
||||
{
|
||||
var data = SaveManager.Load();
|
||||
|
||||
if (data.leaderboard == null || data.leaderboard.Count == 0)
|
||||
{
|
||||
leaderboardText.text = "No records yet!";
|
||||
return;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
for (var i = 0; i < data.leaderboard.Count; i++)
|
||||
{
|
||||
var entry = data.leaderboard[i];
|
||||
sb.AppendLine($"{i + 1}. {entry.playerName} - {entry.score} coins");
|
||||
}
|
||||
|
||||
leaderboardText.text = sb.ToString();
|
||||
}
|
||||
|
||||
public void LoadMenu()
|
||||
{
|
||||
SceneManager.LoadScene("MainMenu");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 982ad72e6a8749cc9b04213879b95957
|
||||
timeCreated: 1772995731
|
||||
@@ -29,7 +29,7 @@ namespace Code.UI
|
||||
{
|
||||
playButton.onClick.AddListener(() => LoadScene("Gameplay"));
|
||||
customizeButton.onClick.AddListener(() => LoadScene("Customization"));
|
||||
leaderboardButton.onClick.AddListener(() => Debug.Log("Leaderboard"));
|
||||
leaderboardButton.onClick.AddListener(() => LoadScene("Leaderboard"));
|
||||
exitButton.onClick.AddListener(Application.Quit);
|
||||
|
||||
AnimateMenu();
|
||||
|
||||
Reference in new issue
Block a user