Leaderboard.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace JTSystems
  5. {
  6. public class Leaderboard : MonoBehaviour
  7. {
  8. public static Leaderboard instance;
  9. public enum ComparisonType { ZPosition }
  10. [SerializeField] private ComparisonType comparisonType;
  11. [Header(" Elements ")]
  12. [SerializeField] private List<LeaderboardCharacter> characters;
  13. private void Awake()
  14. {
  15. if (instance == null)
  16. instance = this;
  17. else
  18. Destroy(gameObject);
  19. }
  20. void Update()
  21. {
  22. SortCharacters();
  23. }
  24. public string GetPlayerPositionString()
  25. {
  26. int playerPositionIndex = GetPlayerPosition() + 1;
  27. if (playerPositionIndex == 1)
  28. return "1st";
  29. else if (playerPositionIndex == 2)
  30. return "2nd";
  31. else if (playerPositionIndex == 3)
  32. return "3rd";
  33. return playerPositionIndex.ToString() + "th";
  34. }
  35. public ComparisonType GetComparisonType()
  36. {
  37. return comparisonType;
  38. }
  39. private int GetPlayerPosition()
  40. {
  41. for (int i = 0; i < characters.Count; i++)
  42. if (characters[i].IsPlayer())
  43. return i;
  44. return 0;
  45. }
  46. private void SortCharacters()
  47. {
  48. characters.Sort();
  49. }
  50. }
  51. }