Added scoring. Interecepting swap and successful swap score remaining
parent
8d3809fc1d
commit
8f0a623ed5
@ -0,0 +1,34 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class PlayerScoreComponent : NetworkBehaviour
|
||||
{
|
||||
public int CurrentScore { get; private set; }
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (IsOwner && IsClient)
|
||||
{
|
||||
Debug.Log($"[PlayerScoreComponent] Requesting score initialization for Player {OwnerClientId}");
|
||||
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId);
|
||||
}
|
||||
// For the server player (host), ensure the PlayerScoreComponent is registered properly
|
||||
if (IsServer && OwnerClientId == NetworkManager.Singleton.LocalClientId)
|
||||
{
|
||||
ScoreManager.Instance?.InitializePlayerScoreServerRpc(OwnerClientId);
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateScore(int newScore)
|
||||
{
|
||||
CurrentScore = newScore;
|
||||
Debug.Log($"[PlayerScoreComponent] Player {OwnerClientId} score updated to {newScore}.");
|
||||
UpdateScoreUI();
|
||||
}
|
||||
|
||||
private void UpdateScoreUI()
|
||||
{
|
||||
// Update the player's score display in the UI.
|
||||
Debug.Log($"[PlayerScoreComponent] Updated score UI for Player {OwnerClientId}: {CurrentScore}");
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a73f85cd904406945a2192aaecb2c310
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -0,0 +1,137 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Multiplayer.Samples.BossRoom;
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class ScoreManager : NetworkBehaviour
|
||||
{
|
||||
public static ScoreManager Instance { get; private set; }
|
||||
|
||||
private Dictionary<ulong, int> playerScores = new Dictionary<ulong, int>();
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (IsServer)
|
||||
{
|
||||
StartCrowPenaltyCoroutineServerRpc();
|
||||
}
|
||||
}
|
||||
|
||||
[ServerRpc(RequireOwnership = false)]
|
||||
public void InitializePlayerScoreServerRpc(ulong ownerClientId)
|
||||
{
|
||||
if (!playerScores.ContainsKey(ownerClientId))
|
||||
{
|
||||
playerScores[ownerClientId] = 200;
|
||||
Debug.Log($"[ScoreManager] Player {ownerClientId} initialized with a starting score of 200.");
|
||||
UpdatePlayerScoreClientRpc(ownerClientId, 200);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning($"[ScoreManager] Player {ownerClientId} already initialized.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void UpdatePlayerScore(ulong ownerClientId, int newScore)
|
||||
{
|
||||
if (playerScores.ContainsKey(ownerClientId))
|
||||
{
|
||||
playerScores[ownerClientId] = newScore;
|
||||
Debug.Log($"[ScoreManager] Updating Player {ownerClientId} score to {newScore}.");
|
||||
UpdatePlayerScoreClientRpc(ownerClientId, newScore);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"[ScoreManager] No entry found for Player {ownerClientId}. Cannot update score.");
|
||||
}
|
||||
}
|
||||
|
||||
public int GetPlayerScore(ulong ownerClientId)
|
||||
{
|
||||
if (playerScores.TryGetValue(ownerClientId, out var score))
|
||||
{
|
||||
return score;
|
||||
}
|
||||
Debug.LogError($"[ScoreManager] No entry found for Player {ownerClientId}.");
|
||||
return 0; // Default score
|
||||
}
|
||||
|
||||
[ServerRpc]
|
||||
public void StartCrowPenaltyCoroutineServerRpc()
|
||||
{
|
||||
StartCoroutine(ApplyCrowPenaltyCoroutine());
|
||||
}
|
||||
|
||||
private IEnumerator ApplyCrowPenaltyCoroutine()
|
||||
{
|
||||
yield return new WaitUntil(() => PlatformManager.Instance != null && PlatformManager.Instance.AreAllPlatformsOccupied());
|
||||
while (true)
|
||||
{
|
||||
var modifications = new List<KeyValuePair<ulong, int>>();
|
||||
|
||||
foreach (var entry in playerScores)
|
||||
{
|
||||
var ownerClientId = entry.Key;
|
||||
var score = entry.Value;
|
||||
|
||||
// Check if the player is the crow
|
||||
if (CrowManager.Instance != null && CrowManager.Instance.GetCurrentCrow().OwnerClientId == ownerClientId)
|
||||
{
|
||||
var newScore = score - 2;
|
||||
modifications.Add(new KeyValuePair<ulong, int>(ownerClientId, newScore));
|
||||
Debug.Log($"[ScoreManager] Applied crow penalty to Player {ownerClientId}. New score: {newScore}");
|
||||
}
|
||||
}
|
||||
|
||||
// Apply the modifications after the iteration
|
||||
foreach (var modification in modifications)
|
||||
{
|
||||
UpdatePlayerScore(modification.Key, modification.Value);
|
||||
}
|
||||
|
||||
// Wait for the next penalty application
|
||||
yield return new WaitForSeconds(5f);
|
||||
}
|
||||
}
|
||||
|
||||
[ClientRpc]
|
||||
public void UpdatePlayerScoreClientRpc(ulong ownerClientId, int newScore)
|
||||
{
|
||||
Debug.Log($"[ScoreManager] Received score update for Player {ownerClientId}: {newScore}");
|
||||
var playerScoreComponent = FindPlayerScoreComponent(ownerClientId);
|
||||
if (playerScoreComponent != null)
|
||||
{
|
||||
playerScoreComponent.UpdateScore(newScore);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError($"[ScoreManager] Could not find PlayerScoreComponent for Player {ownerClientId}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private PlayerScoreComponent FindPlayerScoreComponent(ulong ownerClientId)
|
||||
{
|
||||
foreach (var scoreComponent in FindObjectsOfType<PlayerScoreComponent>())
|
||||
{
|
||||
if (scoreComponent.OwnerClientId == ownerClientId)
|
||||
{
|
||||
return scoreComponent;
|
||||
}
|
||||
}
|
||||
Debug.LogError($"[ScoreManager] Could not find PlayerScoreComponent for Player {ownerClientId}");
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c42f66f946855da4b99f79c5bde1d545
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Reference in New Issue