53 lines
1.3 KiB
C#
53 lines
1.3 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class LeaderboardController : MonoBehaviour
|
|
{
|
|
[SerializeField]
|
|
private RectTransform _content;
|
|
[SerializeField]
|
|
private LeaderboardEntry _leaderboardEntryPrefab;
|
|
|
|
[SerializeField]
|
|
private InputField _nameInputField;
|
|
|
|
private List<LeaderboardEntry> _entries = new List<LeaderboardEntry>();
|
|
|
|
private void Start()
|
|
{
|
|
UpdateLeaderboard(0);
|
|
}
|
|
|
|
private void UpdateLeaderboard(float newScore)
|
|
{
|
|
_entries.ForEach(x => Destroy(x.gameObject));
|
|
_entries.Clear();
|
|
_entries = new List<LeaderboardEntry>();
|
|
|
|
var sortedList = PlayerSetup.Instance.PlayerInfo.Players.OrderByDescending(x => x.Score).ToList();
|
|
|
|
for (int i = 0; i < sortedList.Count; i++)
|
|
{
|
|
var newEntry = Instantiate(_leaderboardEntryPrefab, _content);
|
|
_entries.Add(newEntry);
|
|
newEntry.Init(sortedList[i], i);
|
|
}
|
|
}
|
|
|
|
public void SubmitNewEntry()
|
|
{
|
|
PlayerSetup.Instance.SetPlayerName(_nameInputField.text);
|
|
PlayerSetup.Instance.SavePlayer();
|
|
UpdateLeaderboard(0);
|
|
}
|
|
|
|
public void InitPlayerInfo(Player player)
|
|
{
|
|
player.OnScoreChange += UpdateLeaderboard;
|
|
}
|
|
}
|