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