metahunt/Assets/Scripts/Leaderboard.cs
2024-02-11 23:23:12 +05:30

69 lines
1.9 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using ExitGames.Client.Photon.StructWrapping;
using TMPro;
using UnityEngine;
public class Leaderboard : MonoBehaviour
{
public CanvasGroup canvasGroup;
public float updateInterval = 0.8f;
public LeaderboardDeathmatch DMLeaderboard;
public LeaderboardTDM TDMLeaderboard;
void OnValidate(){
if(canvasGroup == null) canvasGroup = GetComponent<CanvasGroup>();
}
void Awake(){
DMLeaderboard.gameObject.SetActive(GameManager.desiredGameMode == GameModes.Deathmatch.ToString());
TDMLeaderboard.gameObject.SetActive(GameManager.desiredGameMode == GameModes.TeamDeathmatch.ToString());
}
void Start()
{
canvasGroup.alpha=0;
Refresh();
}
void Update()
{
if(Input.GetKey(KeyCode.Tab)){
if(canvasGroup.alpha < 1){
canvasGroup.alpha += Time.deltaTime * 5;
}
}else{
if(canvasGroup.alpha >0){
canvasGroup.alpha -= Time.deltaTime * 5;
}
}
if(updateTimer < updateInterval){
updateTimer+= Time.deltaTime;
}else{
updateTimer= 0;
Refresh();
}
}
float updateTimer;
public static PlayerNetwork[] list;
public void Refresh(){
PlayerNetwork[] players = FindObjectsOfType<PlayerNetwork>();
//BUBBLE SORT!
for(int i = players.Length; i > 0; i--){
for(int ii = 0; ii < i-1; ii++){
if(players[ii].kills > players[ii + 1].kills){
PlayerNetwork temp = players[ii];
players[ii] = players[ii+1];
players[ii+1] = temp;
}
}
}
list = players;
DMLeaderboard.Populate(list);
TDMLeaderboard.Populate(list);
}
}