rabidus-test/Assets/Scripts/LeaderboardController.cs

49 lines
1.2 KiB
C#
Raw Permalink 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]
2023-10-05 17:56:59 +03:00
protected RectTransform _content;
2023-07-24 16:38:13 +03:00
[SerializeField]
2023-10-05 17:56:59 +03:00
protected LeaderboardEntry _leaderboardEntryPrefab;
2023-07-24 16:38:13 +03:00
[SerializeField]
private InputField _nameInputField;
2023-10-05 17:56:59 +03:00
protected List<LeaderboardEntry> _entries = new List<LeaderboardEntry>();
2023-07-24 16:38:13 +03:00
2023-10-05 17:56:59 +03:00
protected virtual void Start()
2023-07-24 16:38:13 +03:00
{
2023-08-22 15:41:12 +03:00
UpdateLeaderboard();
2023-07-24 16:38:13 +03:00
}
2023-10-05 17:56:59 +03:00
protected virtual void UpdateLeaderboard()
2023-07-24 16:38:13 +03:00
{
_entries.ForEach(x => Destroy(x.gameObject));
_entries.Clear();
_entries = new List<LeaderboardEntry>();
2023-10-05 17:56:59 +03:00
var sortedList = PlayerSetup.Instance.AllPlayers.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
}