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.
146 lines
4.6 KiB
C#
146 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
using Unity.Multiplayer.Samples.BossRoom;
|
|
using UnityEngine;
|
|
using Unity.Netcode;
|
|
using System.Linq;
|
|
|
|
public class Scoreboard : NetworkBehaviour
|
|
{
|
|
public List<PlayerItem> dummyPlayerItems;
|
|
public List<PlayerItem> playerItems = new();
|
|
private List<PlayerItem> playerItemsForSort = new();
|
|
public GameObject playerItemPrefab;
|
|
public Transform Parent, FinalParent;
|
|
public GameObject FinalLeaderBoardObj;
|
|
|
|
public static Scoreboard Instance { get; private set; }
|
|
|
|
private int counter = 0;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null) Instance = this;
|
|
else Destroy(gameObject);
|
|
}
|
|
private void Start()
|
|
{
|
|
counter = 1;
|
|
}
|
|
public void InitializeScoreboardItem(ulong clientId, int score, string name)
|
|
{
|
|
if (counter >= dummyPlayerItems.Count) return;
|
|
|
|
var item = (NetworkManager.Singleton.LocalClientId == clientId)
|
|
? dummyPlayerItems[0]
|
|
: dummyPlayerItems[counter++];
|
|
|
|
item.gameObject.SetActive(true);
|
|
item.PlayerName.text = name;
|
|
item.PlayerScore.text = score.ToString();
|
|
item.PlayerClientID = clientId;
|
|
playerItems.Add(item);
|
|
|
|
if (NetworkManager.Singleton.LocalClientId != clientId)
|
|
playerItemsForSort.Add(item);
|
|
}
|
|
|
|
public void UpdateScoreboard(ulong id, int score, ClientRpcParams clientRpcParams = default)
|
|
{
|
|
foreach (var item in playerItems)
|
|
{
|
|
if (item.PlayerClientID == id)
|
|
{
|
|
item.PlayerScore.text = score.ToString();
|
|
break;
|
|
}
|
|
if (NetworkManager.Singleton.LocalClientId == item.PlayerClientID)
|
|
item.PlayerScore.color = Color.green;
|
|
}
|
|
SortAndUpdateScoreboard();
|
|
//var item = playerItems.FirstOrDefault(p => p.PlayerClientID == id);
|
|
//if (item != null) item.PlayerScore.text = score.ToString();
|
|
//SortAndUpdateScoreboard();
|
|
}
|
|
|
|
public void FinalLeaderBoard()
|
|
{
|
|
if (IsServer) FinalLeaderBoardClientRPC();
|
|
}
|
|
|
|
[ClientRpc]
|
|
private void FinalLeaderBoardClientRPC()
|
|
{
|
|
Debug.Log("FinalLeaderBoardClientRPC");
|
|
playerItems.ForEach(item => Instantiate(item, FinalParent).gameObject.SetActive(true));
|
|
FinalLeaderBoardObj.SetActive(true);
|
|
}
|
|
|
|
private void SortAndUpdateScoreboard()
|
|
{
|
|
playerItemsForSort = playerItemsForSort
|
|
.OrderByDescending(p => int.Parse(p.PlayerScore.text))
|
|
.ToList();
|
|
|
|
for (int i = 0; i < playerItemsForSort.Count; i++)
|
|
playerItemsForSort[i].transform.SetSiblingIndex(i);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//using System.Collections;
|
|
//using System.Collections.Generic;
|
|
//using Unity.Multiplayer.Samples.BossRoom;
|
|
//using UnityEngine;
|
|
//using Unity.Netcode;
|
|
|
|
//public class Scoreboard : NetworkBehaviour
|
|
//{
|
|
// public List<PlayerItem> playerItems = new();
|
|
// public GameObject playerItemPrefab;
|
|
// public Transform Parent, FinalParent;
|
|
// 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)
|
|
// {
|
|
// var item = Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>();
|
|
// item.PlayerName.text = name;
|
|
// item.PlayerScore.text = score.ToString();
|
|
// item.PlayerClientID = clientId;
|
|
// playerItems.Add(item);
|
|
// }
|
|
|
|
// public void UpdateScoreboard(ulong id, int score, ClientRpcParams clientRpcParams = default)
|
|
// {
|
|
// foreach (var item in playerItems)
|
|
// {
|
|
// if (item.PlayerClientID == id)
|
|
// {
|
|
// item.PlayerScore.text = score.ToString();
|
|
// if (NetworkManager.Singleton.LocalClientId == item.PlayerClientID)
|
|
// item.PlayerScore.color = Color.green;
|
|
// break;
|
|
// }
|
|
// }
|
|
// SortAndUpdateScoreboard();
|
|
// }
|
|
// public void FinalLeaderBoard()
|
|
// {
|
|
// if (IsServer) FinalLeaderBoardClientRPC();
|
|
// }
|
|
// [ClientRpc]
|
|
// public void FinalLeaderBoardClientRPC()
|
|
// {
|
|
// playerItems.ForEach(item => Instantiate(item.gameObject, FinalParent));
|
|
// FinalLeaderBoardObj.SetActive(true);
|
|
// }
|
|
// private void SortAndUpdateScoreboard()
|
|
// {
|
|
// 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);
|
|
// }
|
|
//}
|