Got scores the render

This commit is contained in:
2026-01-23 11:15:27 +00:00
parent ec84295699
commit 41515e4859
5 changed files with 199 additions and 3 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using InterfaceOff.MainMenu;
using UnityEngine;
using UnityEngine.UI;
@@ -17,6 +18,11 @@ namespace InterfaceOff.WorldScene
[field: SerializeField] private Text ScoreText { get; set; }
[field: SerializeField] private Text FinalScoreText { get; set; }
[field: SerializeField] private WindowSpawner Spawner { get; set; }
[field: SerializeField] private GameObject LeaderboardObject { get; set; }
[field: SerializeField] private GameObject LeaderboardEntryPrefab { get; set; }
[field: SerializeField] private InputField PlayerTextNameInput { get; set; }
[field: SerializeField] private Button IDRKWhatToCallThis { get; set; }
private static List<PlayerScore> PlayerScores { get; } = new();
private static ScoreTracker Instance { get; set; }
@@ -35,7 +41,34 @@ namespace InterfaceOff.WorldScene
return Instance.Score;
}
private void Awake() => Instance = this;
private void RepaintLeaderboard()
{
foreach (Transform child in LeaderboardObject.transform)
{
Destroy(child.gameObject);
}
PlayerScore[] scores = PlayerScores
.OrderByDescending(playerScore => playerScore.Score)
.Take(10)
.ToArray();
for (int idx = 0; idx < scores.Length; idx++)
{
GameObject go = Instantiate(LeaderboardEntryPrefab, LeaderboardObject.transform);
RectTransform rect = go.GetComponent<RectTransform>();
Text entryText = go.GetComponent<Text>();
entryText.text = $"[{scores[idx].PlayerName}] - {scores[idx].Score}";
rect.anchoredPosition = new Vector2(0, 220 - ((idx + 1) * 40));
}
}
private void Awake()
{
RepaintLeaderboard();
Instance = this;
}
private void Update()
{
@@ -64,8 +97,12 @@ namespace InterfaceOff.WorldScene
StoredCurrentScore = true;
PlayerScores.Add(score);
RepaintLeaderboard();
PlayerTextNameInput.interactable = false;
IDRKWhatToCallThis.interactable = false;
Debug.Log($"Added score [{score.PlayerName} | {score.Score}]");
Debug.Log($"Added score [{score.PlayerName} | {score.Score:F1}]");
}
}