rabidus-test/Assets/Scripts/LeaderboardController.cs

55 lines
1.4 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class LeaderboardController : MonoBehaviour, IPlayerInfoRequre
{
[SerializeField]
private RectTransform _content;
[SerializeField]
private LeaderboardEntry _leaderboardEntryPrefab;
private PlayerSetup _playerSetup;
[SerializeField]
private InputField _nameInputField;
private List<LeaderboardEntry> _entries = new List<LeaderboardEntry>();
private void Start()
{
InitPlayerInfo();
UpdateLeaderboard();
}
private void UpdateLeaderboard()
{
_entries.ForEach(x => Destroy(x.gameObject));
_entries.Clear();
_entries = new List<LeaderboardEntry>();
var sortedList = _playerSetup.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.SetPlayerName(_nameInputField.text);
_playerSetup.SavePlayer();
UpdateLeaderboard();
}
public void InitPlayerInfo()
{
_playerSetup = FindObjectOfType<PlayerSetup>();
}
}