rabidus-test/Assets/Scripts/LeaderboardBase.cs

77 lines
2.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class LeaderboardBase : MonoBehaviour
{
[SerializeField]
protected RectTransform _content;
[SerializeField]
protected LeaderboardEntry _leaderboardEntryPrefab;
[SerializeField]
protected LeaderboardEntry _currentPlayerLeaderboardEntry;
protected List<LeaderboardEntry> _entries = new List<LeaderboardEntry>();
protected List<Player> _players = new List<Player>();
protected LeaderboardController _leaderboardController;
public UnityEvent OnUpdated;
protected void Awake()
{
_leaderboardController = FindObjectOfType<LeaderboardController>();
if (_currentPlayerLeaderboardEntry != null)
{
if (GlobalSettings.Instance.AllMaps)
_currentPlayerLeaderboardEntry.gameObject.SetActive(true);
else
_currentPlayerLeaderboardEntry.gameObject.SetActive(false);
}
}
protected virtual void OnEnable()
{
//_leaderboardController.OnDailyInit.AddListener(InitLeaderboard);
}
protected virtual void OnDisable()
{
//_leaderboardController.OnDailyInit.RemoveListener(InitLeaderboard);
}
protected void InitLeaderboard(List<Player> players)
{
_players = players;
UpdateLeaderboard();
}
protected void UpdateLeaderboard()
{
_entries.ForEach(x => Destroy(x.gameObject));
_entries.Clear();
_entries = new List<LeaderboardEntry>();
int currentPlayerIndex = int.MaxValue;
for (int i = 0; i < _players.Count; i++)
{
var newEntry = Instantiate(_leaderboardEntryPrefab, _content);
_entries.Add(newEntry);
newEntry.Init(_players[i], i, _players[i] == PlayerSetup.Instance.CurrentPlayer);
if (_players[i] == PlayerSetup.Instance.CurrentPlayer)
currentPlayerIndex = i;
}
if (_currentPlayerLeaderboardEntry != null)
{
_currentPlayerLeaderboardEntry.Init(PlayerSetup.Instance.CurrentPlayer, currentPlayerIndex, true);
}
OnUpdated?.Invoke();
}
}