You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleNetcode/Assets/Scripts/Gameplay/Scoreboard.cs

57 lines
1.9 KiB
C#

1 month ago
using System.Collections;
using System.Collections.Generic;
using Unity.Multiplayer.Samples.BossRoom;
using UnityEngine;
using Unity.Netcode;
1 month ago
public class Scoreboard : NetworkBehaviour
1 month ago
{
public List<PlayerItem> playerItems = new();
1 month ago
public GameObject playerItemPrefab;
public Transform Parent, FinalParent;
1 month ago
public GameObject FinalLeaderBoardObj;
public static Scoreboard instance;
private bool coroutineStarter = false;
private void Awake() => instance = this;
public void InitializeScoreboardItem(ulong clientId, int score, string name)
1 month ago
{
var item = Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>();
item.PlayerName.text = name;
item.PlayerScore.text = score.ToString();
item.PlayerClientID = clientId;
1 month ago
playerItems.Add(item);
}
public void UpdateScoreboard(string playerName, int score, ClientRpcParams clientRpcParams = default)
{
foreach (var item in playerItems)
{
if (item.PlayerName.text == playerName)
{
item.PlayerScore.text = score.ToString();
if (NetworkManager.Singleton.LocalClientId == item.PlayerClientID)
item.PlayerScore.color = Color.green;
break;
}
}
SortAndUpdateScoreboard();
1 month ago
}
public void FinalLeaderBoard()
{
if (IsServer) FinalLeaderBoardClientRPC();
1 month ago
}
[ClientRpc]
public void FinalLeaderBoardClientRPC()
{
playerItems.ForEach(item => Instantiate(item.gameObject, FinalParent));
1 month ago
FinalLeaderBoardObj.SetActive(true);
}
private void SortAndUpdateScoreboard()
1 month ago
{
playerItems.Sort((p1, p2) => int.Parse(p2.PlayerScore.text).CompareTo(int.Parse(p1.PlayerScore.text)));
for (int i = 0; i < playerItems.Count; i++) playerItems[i].transform.SetSiblingIndex(i);
1 month ago
}
1 month ago
}