Scoreboard

dev-hazim
Ali Sharoz 2 weeks ago
parent 1f41843f4d
commit a8097abe35

@ -775,6 +775,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: a73f85cd904406945a2192aaecb2c310, type: 3}
m_Name:
m_EditorClassIdentifier:
serverCharacter: {fileID: 741733315856861890}
m_index: 0
playerItem: {fileID: 0}
--- !u!4 &6009713983291384766 stripped
Transform:
m_CorrespondingSourceObject: {fileID: 4600110157238723791, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3}

@ -63,7 +63,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 0, g: 0, b: 0, a: 0.31764707}
m_RaycastTarget: 1
m_RaycastTarget: 0
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
@ -173,7 +173,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastTarget: 0
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:
@ -307,7 +307,7 @@ MonoBehaviour:
m_EditorClassIdentifier:
m_Material: {fileID: 0}
m_Color: {r: 1, g: 1, b: 1, a: 1}
m_RaycastTarget: 1
m_RaycastTarget: 0
m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0}
m_Maskable: 1
m_OnCullStateChanged:

File diff suppressed because it is too large Load Diff

@ -216,34 +216,34 @@ namespace Unity.BossRoom.Gameplay.GameState
// spawn players characters with destroyWithScene = true
newPlayer.SpawnWithOwnership(clientId, true);
//ScoreboardCaller(newPlayer);
ScoreboardCallerServerRpc(newPlayer);
//ScoreboardCallerServerRpc(newPlayer);
//var playerScoreComponent = newPlayer.GetComponent<PlayerScoreComponent>();
}
void ScoreboardCaller(NetworkObject newplayer)
{
scoreboard.ScoreBoardListFiller(newplayer);
}
[ServerRpc(RequireOwnership = false)]
void ScoreboardCallerServerRpc(NetworkObject newPlayer)
{
// Call the method on the server first
scoreboard.ScoreBoardListFiller(newPlayer);
// Then invoke it on the client
ScoreboardCallerClientRpc(newPlayer);
}
[ClientRpc]
void ScoreboardCallerClientRpc(NetworkObject newPlayer)
{
// Call the method on the client
if (NetworkManager.Singleton.IsClient)
{
scoreboard.ScoreBoardListFiller(newPlayer);
}
}
//void ScoreboardCaller(NetworkObject newplayer)
//{
// scoreboard.ScoreBoardListFiller(newplayer);
//}
//[ServerRpc(RequireOwnership = false)]
//void ScoreboardCallerServerRpc(NetworkObject newPlayer)
//{
// // Call the method on the server first
// scoreboard.ScoreBoardListFiller(newPlayer);
// // Then invoke it on the client
// ScoreboardCallerClientRpc(newPlayer);
//}
//[ClientRpc]
//void ScoreboardCallerClientRpc(NetworkObject newPlayer)
//{
// // Call the method on the client
// if (NetworkManager.Singleton.IsClient)
// {
// scoreboard.ScoreBoardListFiller(newPlayer);
// }
//}
void OnLifeStateChangedEventMessage(LifeStateChangedEventMessage message)
{

@ -13,12 +13,12 @@ public class PlayerScoreComponent : NetworkBehaviour
if (IsOwner && IsClient)
{
Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}");
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId);
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId,serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
}
// For the server player (host), ensure the PlayerScoreComponent is registered properly
if (IsServer && OwnerClientId == NetworkManager.Singleton.LocalClientId)
{
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId);
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
}
serverCharacter=GetComponent<ServerCharacter>();
}

@ -9,6 +9,7 @@ public class ScoreManager : NetworkBehaviour
public static ScoreManager Instance { get; private set; }
public Dictionary<ulong, int> playerScores = new Dictionary<ulong, int>();
public Dictionary<ulong, string> playerNames = new Dictionary<ulong, string>();
private void Awake()
{
@ -29,13 +30,17 @@ public class ScoreManager : NetworkBehaviour
}
[ServerRpc(RequireOwnership = false)]
public void InitializePlayerScoreServerRpc(ulong ownerClientId)
public void InitializePlayerScoreServerRpc(ulong ownerClientId,string name)
{
if (!playerScores.ContainsKey(ownerClientId))
{
playerScores[ownerClientId] = 200;
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200.");
UpdatePlayerScoreClientRpc(ownerClientId, 200);
playerNames[ownerClientId]= name;
Scoreboard.instance.ScoreBoardItemInitializer(ownerClientId, playerScores[ownerClientId],name);
}
else
{
@ -49,6 +54,7 @@ public class ScoreManager : NetworkBehaviour
if (playerScores.ContainsKey(ownerClientId))
{
playerScores[ownerClientId] = newScore;
Scoreboard.instance.ScoreBoardUpdater(ownerClientId, newScore);
Debug.Log($"[ScoreManager] Updating Player {ownerClientId} score to {newScore}.");
UpdatePlayerScoreClientRpc(ownerClientId, newScore);
}

@ -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);
// }
// }
// }
//}

@ -1,5 +1,6 @@
using System.Collections;
using Unity.Multiplayer.Samples.Utilities;
using Unity.Netcode;
using UnityEngine;
using static TMPro.SpriteAssetUtilities.TexturePacker_JsonArray;
@ -28,8 +29,8 @@ public class TimerScript : MonoBehaviour
{
Debug.Log("Timer has ended!");
// Add logic for what happens when the timer ends
Scoreboard.instance.FinalLeaderBoard();
}
private void OnDestroy()
{
if (timer != null)

Loading…
Cancel
Save