rabidus-test/Assets/Scripts/LeaderboardController.cs

49 lines
1.2 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;
2023-08-22 15:41:12 +03:00
using UnityEngine.Events;
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-22 15:41:12 +03:00
UpdateLeaderboard();
2023-07-24 16:38:13 +03:00
}
2023-08-22 15:41:12 +03:00
private void UpdateLeaderboard()
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);
}
}
2023-08-22 15:41:12 +03:00
[ContextMenu("Debug submit")]
public void SubmitNewEntry()
{
2023-08-15 17:38:54 +03:00
PlayerSetup.Instance.SetPlayerName(_nameInputField.text);
2023-08-22 15:41:12 +03:00
UpdateLeaderboard();
}
2023-07-24 16:38:13 +03:00
}