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.
160 lines
5.1 KiB
C#
160 lines
5.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Unity.Multiplayer.Samples.BossRoom;
|
|
using UnityEngine;
|
|
using Unity.BossRoom.Gameplay.GameState;
|
|
using Unity.Netcode;
|
|
using VContainer.Unity;
|
|
|
|
public class Scoreboard : NetworkBehaviour
|
|
{
|
|
public List<PlayerScoreComponent> m_PlayerScoreComponents;
|
|
public List<PlayerItem> playerItems;
|
|
public GameObject playerItemPrefab;
|
|
public Transform Parent;
|
|
public Transform FinalParent;
|
|
int index = 0;
|
|
bool coroutineStarter = false;
|
|
public static Scoreboard instance;
|
|
public GameObject FinalLeaderBoardObj;
|
|
|
|
private void Awake()
|
|
{
|
|
instance = this;
|
|
}
|
|
|
|
public void DestroyChecker()
|
|
{
|
|
for (int i = 0; i < m_PlayerScoreComponents.Count; i++)
|
|
{
|
|
if (m_PlayerScoreComponents[i] == null)
|
|
{
|
|
m_PlayerScoreComponents.RemoveAt(i);
|
|
}
|
|
}
|
|
for (int i = 0; i < playerItems.Count; i++)
|
|
{
|
|
if (playerItems[i] == null)
|
|
{
|
|
playerItems.RemoveAt(i);
|
|
}
|
|
}
|
|
}
|
|
public void ScoreBoardListFiller(NetworkObject playerObj)
|
|
{
|
|
if (IsServer)
|
|
{
|
|
ulong clientId = playerObj.OwnerClientId;
|
|
int initialScore = ScoreManager.Instance.GetPlayerScore(clientId);
|
|
m_PlayerScoreComponents.Add(playerObj.GetComponent<PlayerScoreComponent>());
|
|
m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].m_index = index;
|
|
|
|
// Synchronize across clients
|
|
InstantiatePlayerItemClientRpc(index, clientId, initialScore);
|
|
|
|
index++;
|
|
}
|
|
}
|
|
[ClientRpc]
|
|
public void InstantiatePlayerItemClientRpc(int index, ulong clientId, int currentScore)
|
|
{
|
|
GameObject temp = Instantiate(playerItemPrefab, Parent);
|
|
PlayerItem item = temp.GetComponent<PlayerItem>();
|
|
item.PlayerName.text = clientId.ToString();
|
|
item.PlayerScore.text = currentScore.ToString();
|
|
playerItems.Add(item);
|
|
}
|
|
[ClientRpc]
|
|
public void ScoreBoardItemInitializerClientRpc(ulong id, int score,string name)
|
|
{
|
|
playerItems.Add(Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>());
|
|
playerItems[^1].PlayerName.text = name.ToString();
|
|
playerItems[^1].PlayerScore.text = score.ToString();
|
|
}
|
|
public void ScoreBoardItemInitializer(ulong id, int score,string name)
|
|
{
|
|
if (IsServer)
|
|
{
|
|
ScoreBoardItemInitializerClientRpc(id, score,name); // Send to clients
|
|
}
|
|
}
|
|
[ClientRpc]
|
|
public void ScoreBoardUpdaterClientRpc(ulong id, int score)
|
|
{
|
|
for (int i = 0; i < playerItems.Count; i++)
|
|
{
|
|
if (playerItems[i].PlayerName.text == id.ToString())
|
|
{
|
|
playerItems[i].PlayerScore.text = score.ToString();
|
|
}
|
|
}
|
|
}
|
|
public void FinalLeaderBoard()
|
|
{
|
|
Debug.Log("FinalLeaderBoard");
|
|
if (IsServer)
|
|
{
|
|
FinalLeaderBoardClientRPC();
|
|
}
|
|
}
|
|
[ClientRpc]
|
|
public void FinalLeaderBoardClientRPC()
|
|
{
|
|
for (int i = 0; i < playerItems.Count; i++)
|
|
{
|
|
Instantiate(playerItems[i], FinalParent);
|
|
}
|
|
FinalLeaderBoardObj.SetActive(true);
|
|
}
|
|
public void ScoreBoardUpdater(ulong id, int score)
|
|
{
|
|
if (IsServer)
|
|
{
|
|
ScoreBoardUpdaterClientRpc(id, score); // Send to all clients
|
|
}
|
|
}
|
|
public void Starter()
|
|
{
|
|
// Start the coroutine for updating the scores
|
|
StartCoroutine(ScoreUpdater());
|
|
}
|
|
|
|
public void Instantiator(int index)
|
|
{
|
|
// Only instantiate UI elements on the client side
|
|
if (!NetworkManager.Singleton.IsClient)
|
|
{
|
|
Debug.LogError("Instantiator called on server, should be on client");
|
|
return;
|
|
}
|
|
|
|
Debug.Log("Instantiator called on client");
|
|
|
|
GameObject temp = Instantiate(playerItemPrefab, Parent);
|
|
PlayerItem item = temp.GetComponent<PlayerItem>();
|
|
m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].playerItem = item;
|
|
playerItems.Add(item);
|
|
|
|
playerItems[playerItems.Count - 1].PlayerScore.text = m_PlayerScoreComponents[index].CurrentScore.ToString();
|
|
playerItems[playerItems.Count - 1].PlayerName.text = m_PlayerScoreComponents[index].serverCharacter.uIStateDisplayHandler.m_UIState.playerName.ToString();
|
|
}
|
|
|
|
IEnumerator ScoreUpdater()
|
|
{
|
|
//yield return new WaitForSeconds(0.5f);
|
|
Debug.Log("Scoreboard Start - ScoreUpdater started");
|
|
yield return new WaitUntil(() => coroutineStarter == true);
|
|
while (true)
|
|
{
|
|
if (IsOwner)
|
|
{
|
|
yield return new WaitForSeconds(0.1f);
|
|
//m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore));
|
|
playerItems.Sort((p1, p2) => p2.PlayerScore.GetParsedText().CompareTo(p1.PlayerScore.GetParsedText()));
|
|
for (int i = 0; i < playerItems.Count; i++)
|
|
playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].m_index);
|
|
}
|
|
}
|
|
}
|
|
}
|