Merge branch 'dev-main' into dev-hazim

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

@ -10,15 +10,10 @@ public class PlayerScoreComponent : NetworkBehaviour
public PlayerItem playerItem;
public override void OnNetworkSpawn()
{
if (IsOwner && IsClient)
if (IsOwner)
{
Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {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, serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
ScoreManager.Instance?.InitializeAndSyncPlayerServerRpc(OwnerClientId, serverCharacter.uIStateDisplayHandler.m_UIState.playerName);
}
serverCharacter = GetComponent<ServerCharacter>();
}

@ -25,25 +25,39 @@ public class ScoreManager : NetworkBehaviour
{
if (IsServer)
{
StartCrowPenaltyCoroutineServerRpc();
StartCrowPenaltyCoroutine();
}
}
[ServerRpc(RequireOwnership = false)]
public void InitializePlayerScoreServerRpc(ulong ownerClientId, string name)
public void InitializeAndSyncPlayerServerRpc(ulong ownerClientId, string playerName)
{
if (!playerScores.ContainsKey(ownerClientId))
{
playerScores[ownerClientId] = 200;
playerNames[ownerClientId] = name;
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200 and name '{name}'.");
Scoreboard.instance.ScoreBoardItemInitializer(ownerClientId, playerScores[ownerClientId], name);
UpdatePlayerScoreClientRpc(ownerClientId, 200);
playerNames[ownerClientId] = playerName;
}
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of {playerScores[ownerClientId]} and name '{playerName}'.");
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
{
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)
@ -67,10 +81,11 @@ public class ScoreManager : NetworkBehaviour
{
string playerName = playerNames[ownerClientId];
Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId} (Name: {playerName}): {newScore}");
Scoreboard.instance.ScoreBoardUpdater(playerName, newScore);
Scoreboard.instance.UpdateScoreboard(playerName, newScore);
}
else
{
Debug.LogWarning($"[ScoreManager] Player name not found for Player {ownerClientId}. Cannot update scoreboard.");
}
}
@ -93,8 +108,7 @@ public class ScoreManager : NetworkBehaviour
}
}
[ServerRpc]
public void StartCrowPenaltyCoroutineServerRpc()
public void StartCrowPenaltyCoroutine()
{
StartCoroutine(ApplyCrowPenaltyCoroutine());
}

@ -14,12 +14,7 @@ public class Scoreboard : NetworkBehaviour
private bool coroutineStarter = false;
private void Awake() => instance = this;
public void ScoreBoardItemInitializer(ulong clientId, int score, string name)
{
if (IsServer) ScoreBoardItemInitializerClientRpc(clientId, score, name);
}
[ClientRpc]
public void ScoreBoardItemInitializerClientRpc(ulong clientId, int score, string name)
public void InitializeScoreboardItem(ulong clientId, int score, string name)
{
var item = Instantiate(playerItemPrefab, Parent).GetComponent<PlayerItem>();
item.PlayerName.text = name;
@ -27,12 +22,8 @@ public class Scoreboard : NetworkBehaviour
item.PlayerClientID = clientId;
playerItems.Add(item);
}
public void ScoreBoardUpdater(string playerName, int score)
{
if (IsServer) ScoreBoardUpdaterClientRpc(playerName, score);
}
[ClientRpc]
public void ScoreBoardUpdaterClientRpc(string playerName, int score, ClientRpcParams clientRpcParams = default)
public void UpdateScoreboard(string playerName, int score, ClientRpcParams clientRpcParams = default)
{
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)));
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