rabidus-test/Assets/Scripts/LeaderboardController.cs

53 lines
1.3 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
2023-08-15 17:38:54 +03:00
public class LeaderboardController : MonoBehaviour
2023-07-24 16:38:13 +03:00
{
[SerializeField]
private RectTransform _content;
[SerializeField]
private LeaderboardEntry _leaderboardEntryPrefab;
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()
{
2023-08-15 17:38:54 +03:00
UpdateLeaderboard(0);
2023-07-24 16:38:13 +03:00
}
2023-08-15 17:38:54 +03:00
private void UpdateLeaderboard(float newScore)
2023-07-24 16:38:13 +03:00
{
_entries.ForEach(x => Destroy(x.gameObject));
_entries.Clear();
_entries = new List<LeaderboardEntry>();
2023-08-15 17:38:54 +03:00
var sortedList = PlayerSetup.Instance.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()
{
2023-08-15 17:38:54 +03:00
PlayerSetup.Instance.SetPlayerName(_nameInputField.text);
PlayerSetup.Instance.SavePlayer();
UpdateLeaderboard(0);
}
2023-08-15 17:38:54 +03:00
public void InitPlayerInfo(Player player)
{
2023-08-15 17:38:54 +03:00
player.OnScoreChange += UpdateLeaderboard;
}
2023-07-24 16:38:13 +03:00
}