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 m_PlayerScoreComponents; public List 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 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) { Debug.LogError("ScoreBoardListFiller called on client, should be on server"); return; // Only execute this on the server } Debug.Log("ScoreBoardListFiller called on server"); m_PlayerScoreComponents.Add(playerObj.GetComponent()); m_PlayerScoreComponents[m_PlayerScoreComponents.Count - 1].m_index = index; Instantiator(index); index++; if (!coroutineStarter) { coroutineStarter = true; } } 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(); 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"); 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) { Debug.Log("Scoreboard Update - UI update on client"); 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 m_PlayerScoreComponents; // public List 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()); // 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(); // 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); // } // } // } //}