using System.Collections.Generic; using System.Linq; using Unity.BossRoom.Gameplay.GameplayObjects.Character; using UnityEngine; using Unity.Netcode; public class CrowManager : NetworkBehaviour { public static CrowManager Instance { get; private set; } private List players = new List(); private ServerCharacter currentCrow; private void Awake() { if (Instance != null && Instance != this) { Destroy(this); return; } Instance = this; } public override void OnNetworkSpawn() { if (!IsServer) { enabled = false; // Disable CrowManager on clients return; } } private void Update() { if (!IsServer) return; // Ensure logic only runs on the server DetermineCrow(); } /// /// Populates the players list from active ServerCharacters in the scene. /// private void PopulatePlayersList() { players.Clear(); players = FindObjectsOfType().ToList(); Debug.Log("CrowManager: Players list populated."); } /// /// Determines which player should be the crow. /// private void DetermineCrow() { int unoccupiedPlayers = 0; ServerCharacter potentialCrow = null; foreach (var player in players) { if (!player.IsOnAPlatform) { unoccupiedPlayers++; potentialCrow = player; } } if (unoccupiedPlayers == 1) { AssignCrow(potentialCrow); } else if (unoccupiedPlayers == 0 && currentCrow != null) { ClearCrow(); } } /// /// Assigns a player as the Crow. /// /// The ServerCharacter to assign as Crow. private void AssignCrow(ServerCharacter player) { if (currentCrow == player) return; if (currentCrow != null) { currentCrow.SetAsCrow(false); // Clear old crow } currentCrow = player; currentCrow.SetAsCrow(true); Debug.Log($"{currentCrow.name} is now the Crow."); // Notify all clients about the new crow NotifyCrowChangeClientRpc(currentCrow.OwnerClientId); } /// /// Clears the current Crow if no player is unoccupied. /// private void ClearCrow() { if (currentCrow != null) { currentCrow.SetAsCrow(false); Debug.Log($"{currentCrow.name} is no longer the Crow."); currentCrow = null; // Notify all clients about the crow being cleared NotifyCrowChangeClientRpc(0); // 0 indicates no crow } } /// /// RPC to notify clients about the Crow change. /// /// The Client ID of the new crow, or 0 if no crow. [Rpc(SendTo.Everyone)] private void NotifyCrowChangeClientRpc(ulong crowClientId) { foreach (var player in players) { if (player.OwnerClientId == crowClientId) { player.SetAsCrow(true); currentCrow = player; Debug.Log($"Client: {player.name} is the new Crow."); } else { player.SetAsCrow(false); } } } /// /// Adds a character to the players list. /// /// The ServerCharacter to add. public void AddCharacterToList(ServerCharacter character) { if (!players.Contains(character)) { players.Add(character); Debug.Log($"Player {character.name} added to CrowManager."); } } /// /// Removes a character from the players list. /// /// The ServerCharacter to remove. public void RemoveCharacterFromList(ServerCharacter character) { if (players.Contains(character)) { players.Remove(character); Debug.Log($"Player {character.name} removed from CrowManager."); } } /// /// Ensures the players list is updated when a player is spawned or despawned. /// /// The ServerCharacter to track. public void OnPlayerSpawned(ServerCharacter character) { AddCharacterToList(character); } public void OnPlayerDespawned(ServerCharacter character) { RemoveCharacterFromList(character); } /// /// Retrieves the current Crow. /// /// The current Crow player. public ServerCharacter GetCurrentCrow() { return currentCrow; } }