|
|
|
@ -6,15 +6,17 @@ using Unity.BossRoom.Gameplay.GameState;
|
|
|
|
|
using Unity.Netcode;
|
|
|
|
|
using VContainer.Unity;
|
|
|
|
|
|
|
|
|
|
public class Scoreboard : MonoBehaviour
|
|
|
|
|
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()
|
|
|
|
|
{
|
|
|
|
@ -38,43 +40,79 @@ public class Scoreboard : MonoBehaviour
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IEnumerator Start()
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Scoreboard Start - Coroutine Started");
|
|
|
|
|
|
|
|
|
|
// Wait until coroutineStarter is set to true
|
|
|
|
|
yield return new WaitUntil(() => coroutineStarter == true);
|
|
|
|
|
|
|
|
|
|
Debug.Log("Scoreboard Start - Coroutine Finished");
|
|
|
|
|
yield return new WaitForSeconds(2);
|
|
|
|
|
|
|
|
|
|
Debug.Log("Scoreboard Start - Starter Called");
|
|
|
|
|
Starter();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ScoreBoardListFiller(NetworkObject playerObj)
|
|
|
|
|
{
|
|
|
|
|
if (!NetworkManager.Singleton.IsServer)
|
|
|
|
|
if (IsServer)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("ScoreBoardListFiller called on client, should be on server");
|
|
|
|
|
return; // Only execute this on the server
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Debug.Log("ScoreBoardListFiller called on server");
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
Instantiator(index);
|
|
|
|
|
index++;
|
|
|
|
|
// Synchronize across clients
|
|
|
|
|
InstantiatePlayerItemClientRpc(index, clientId, initialScore);
|
|
|
|
|
|
|
|
|
|
if (!coroutineStarter)
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
[ClientRpc]
|
|
|
|
|
public void InstantiatePlayerItemClientRpc(int index, ulong clientId, int currentScore)
|
|
|
|
|
{
|
|
|
|
|
coroutineStarter = true;
|
|
|
|
|
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
|
|
|
|
@ -103,119 +141,19 @@ public class Scoreboard : MonoBehaviour
|
|
|
|
|
|
|
|
|
|
IEnumerator ScoreUpdater()
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForSeconds(0.5f);
|
|
|
|
|
//yield return new WaitForSeconds(0.5f);
|
|
|
|
|
Debug.Log("Scoreboard Start - ScoreUpdater started");
|
|
|
|
|
|
|
|
|
|
yield return new WaitUntil(() => coroutineStarter == true);
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
yield return new WaitForSeconds(0.1f);
|
|
|
|
|
|
|
|
|
|
m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore));
|
|
|
|
|
|
|
|
|
|
// Update the UI on the client side only
|
|
|
|
|
if (NetworkManager.Singleton.IsClient)
|
|
|
|
|
if (IsOwner)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Scoreboard Update - UI update on client");
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Debug.LogWarning("Scoreboard Update - Trying to update UI on the server side");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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 : MonoBehaviour
|
|
|
|
|
//{
|
|
|
|
|
// public List<PlayerScoreComponent> m_PlayerScoreComponents;
|
|
|
|
|
// public List<PlayerItem> playerItems;
|
|
|
|
|
// public GameObject playerItemPrefab;
|
|
|
|
|
// public Transform Parent;
|
|
|
|
|
// int index = 0;
|
|
|
|
|
// bool coroutineStarter = false;
|
|
|
|
|
// public static Scoreboard instance;
|
|
|
|
|
// 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);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// private IEnumerator Start()
|
|
|
|
|
// {
|
|
|
|
|
// Debug.Log("Scoreboard start1");
|
|
|
|
|
// yield return new WaitUntil(() => coroutineStarter == true);
|
|
|
|
|
// Debug.Log("Scoreboard start2");
|
|
|
|
|
// yield return new WaitForSeconds(2);
|
|
|
|
|
// Debug.Log("Scoreboard start3");
|
|
|
|
|
// Starter();
|
|
|
|
|
// Debug.Log("Scoreboard start4");
|
|
|
|
|
// }
|
|
|
|
|
// public void ScoreBoardListFiller(NetworkObject playerObj)
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
// m_PlayerScoreComponents.Add(playerObj.GetComponent<PlayerScoreComponent>());
|
|
|
|
|
// m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].m_index = index;
|
|
|
|
|
|
|
|
|
|
// Instantiator(index);
|
|
|
|
|
// index++;
|
|
|
|
|
// if (!coroutineStarter)
|
|
|
|
|
// {
|
|
|
|
|
// coroutineStarter = true;
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// public void Starter()
|
|
|
|
|
// {
|
|
|
|
|
// StartCoroutine(ScoreUpdater());
|
|
|
|
|
// }
|
|
|
|
|
// public void Instantiator(int index)
|
|
|
|
|
// {
|
|
|
|
|
// 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 start5");
|
|
|
|
|
// while (true)
|
|
|
|
|
// {
|
|
|
|
|
// yield return new WaitForSeconds(0.1f);
|
|
|
|
|
// m_PlayerScoreComponents.Sort((p1, p2) => p2.CurrentScore.CompareTo(p1.CurrentScore));
|
|
|
|
|
// for (int i = 0; i < playerItems.Count; i++)
|
|
|
|
|
// {
|
|
|
|
|
// playerItems[i].transform.SetSiblingIndex(m_PlayerScoreComponents[i].m_index);
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|