rabidus-test/Assets/Scripts/LeaderboardController.cs

55 lines
1.4 KiB
C#
Raw Normal View History

2023-07-24 16:38:13 +03:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
2023-07-24 16:38:13 +03:00
public class LeaderboardController : MonoBehaviour, IPlayerInfoRequre
2023-07-24 16:38:13 +03:00
{
[SerializeField]
private RectTransform _content;
[SerializeField]
private LeaderboardEntry _leaderboardEntryPrefab;
private PlayerSetup _playerSetup;
2023-07-24 16:38:13 +03:00
[SerializeField]
private InputField _nameInputField;
private List<LeaderboardEntry> _entries = new List<LeaderboardEntry>();
2023-07-24 16:38:13 +03:00
private void Start()
{
InitPlayerInfo();
2023-07-24 16:38:13 +03:00
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();
2023-07-24 16:38:13 +03:00
for (int i = 0; i < sortedList.Count; i++)
{
var newEntry = Instantiate(_leaderboardEntryPrefab, _content);
_entries.Add(newEntry);
2023-07-24 16:38:13 +03:00
newEntry.Init(sortedList[i], i);
}
}
public void SubmitNewEntry()
{
_playerSetup.SetPlayerName(_nameInputField.text);
_playerSetup.SavePlayer();
UpdateLeaderboard();
}
public void InitPlayerInfo()
{
_playerSetup = FindObjectOfType<PlayerSetup>();
}
2023-07-24 16:38:13 +03:00
}