Merge branch 'dev-main' into dev-hazim

dev-hazim
Hazim Bin Ijaz 1 week ago
commit 12f2bff33d

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

@ -25,27 +25,41 @@ public class ScoreManager : NetworkBehaviour
{ {
if (IsServer) if (IsServer)
{ {
StartCrowPenaltyCoroutineServerRpc(); StartCrowPenaltyCoroutine();
} }
} }
[ServerRpc(RequireOwnership = false)] [ServerRpc(RequireOwnership = false)]
public void InitializePlayerScoreServerRpc(ulong ownerClientId, string name) public void InitializeAndSyncPlayerServerRpc(ulong ownerClientId, string playerName)
{ {
if (!playerScores.ContainsKey(ownerClientId)) if (!playerScores.ContainsKey(ownerClientId))
{ {
playerScores[ownerClientId] = 200; playerScores[ownerClientId] = 200;
playerNames[ownerClientId] = name; playerNames[ownerClientId] = playerName;
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200 and name '{name}'."); }
Scoreboard.instance.ScoreBoardItemInitializer(ownerClientId, playerScores[ownerClientId], name); Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of {playerScores[ownerClientId]} and name '{playerName}'.");
UpdatePlayerScoreClientRpc(ownerClientId, 200); SyncPlayerDataClientRpc(ownerClientId, playerName, playerScores[ownerClientId]);
}
[ClientRpc]
public void SyncPlayerDataClientRpc(ulong ownerClientId, string playerName, int score)
{
// Update local data
if (!playerScores.ContainsKey(ownerClientId))
{
playerScores[ownerClientId] = score;
playerNames[ownerClientId] = playerName;
} }
else else
{ {
Debug.LogWarning($"[ScoreManager] Player {ownerClientId} already initialized."); playerScores[ownerClientId] = score;
} }
}
Debug.Log($"[ScoreManager] Synced Player {ownerClientId} data: Name = {playerName}, Score = {score}");
// Update the scoreboard
Scoreboard.instance.InitializeScoreboardItem(ownerClientId, score, playerName);
}
public void UpdatePlayerScore(ulong ownerClientId, int newScore) public void UpdatePlayerScore(ulong ownerClientId, int newScore)
{ {
if (playerScores.ContainsKey(ownerClientId)) if (playerScores.ContainsKey(ownerClientId))
@ -67,10 +81,11 @@ public class ScoreManager : NetworkBehaviour
{ {
string playerName = playerNames[ownerClientId]; string playerName = playerNames[ownerClientId];
Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}"); Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}");
Scoreboard.instance.ScoreBoardUpdater(playerName, newScore); Scoreboard.instance.UpdateScoreboard(playerName, newScore);
} }
else else
{ {
Debug.LogWarning($"[ScoreManager] Player name not found for Player {ownerClientId}. Cannot update scoreboard."); Debug.LogWarning($"[ScoreManager] Player name not found for Player {ownerClientId}. Cannot update scoreboard.");
} }
} }
@ -93,8 +108,7 @@ public class ScoreManager : NetworkBehaviour
} }
} }
[ServerRpc] public void StartCrowPenaltyCoroutine()
public void StartCrowPenaltyCoroutineServerRpc()
{ {
StartCoroutine(ApplyCrowPenaltyCoroutine()); StartCoroutine(ApplyCrowPenaltyCoroutine());
} }

@ -14,12 +14,7 @@ public class Scoreboard : NetworkBehaviour
private bool coroutineStarter = false; private bool coroutineStarter = false;
private void Awake() => instance = this; private void Awake() => instance = this;
public void ScoreBoardItemInitializer(ulong clientId, int score, string name) public void InitializeScoreboardItem(ulong clientId, int score, string name)
{
if (IsServer) ScoreBoardItemInitializerClientRpc(clientId, score, name);
}
[ClientRpc]
public void ScoreBoardItemInitializerClientRpc(ulong clientId, int score, string name)
{ {
var item = Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>(); var item = Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>();
item.PlayerName.text = name; item.PlayerName.text = name;
@ -27,12 +22,8 @@ public class Scoreboard : NetworkBehaviour
item.PlayerClientID = clientId; item.PlayerClientID = clientId;
playerItems.Add(item); playerItems.Add(item);
} }
public void ScoreBoardUpdater(string playerName, int score)
{ public void UpdateScoreboard(string playerName, int score, ClientRpcParams clientRpcParams = default)
if (IsServer) ScoreBoardUpdaterClientRpc(playerName, score);
}
[ClientRpc]
public void ScoreBoardUpdaterClientRpc(string playerName, int score, ClientRpcParams clientRpcParams = default)
{ {
foreach (var item in playerItems) foreach (var item in playerItems)
{ {
@ -61,21 +52,5 @@ public class Scoreboard : NetworkBehaviour
playerItems.Sort((p1, p2) => int.Parse(p2.PlayerScore.text).CompareTo(int.Parse(p1.PlayerScore.text))); 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); for (int i = 0; i < playerItems.Count; i++) playerItems[i].transform.SetSiblingIndex(i);
} }
public void StartScoreUpdater()
{
if (!coroutineStarter)
{
coroutineStarter = true;
StartCoroutine(ScoreUpdater());
}
}
private IEnumerator ScoreUpdater()
{
yield return new WaitUntil(() => coroutineStarter);
while (true)
{
SortAndUpdateScoreboard();
yield return new WaitForSeconds(0.5f);
}
}
} }

Loading…
Cancel
Save