You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
4.9 KiB
C#
185 lines
4.9 KiB
C#
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<ServerCharacter> players = new List<ServerCharacter>();
|
|
|
|
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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Populates the players list from active ServerCharacters in the scene.
|
|
/// </summary>
|
|
private void PopulatePlayersList()
|
|
{
|
|
players.Clear();
|
|
players = FindObjectsOfType<ServerCharacter>().ToList();
|
|
Debug.Log("CrowManager: Players list populated.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Determines which player should be the crow.
|
|
/// </summary>
|
|
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();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Assigns a player as the Crow.
|
|
/// </summary>
|
|
/// <param name="player">The ServerCharacter to assign as Crow.</param>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clears the current Crow if no player is unoccupied.
|
|
/// </summary>
|
|
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
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// RPC to notify clients about the Crow change.
|
|
/// </summary>
|
|
/// <param name="crowClientId">The Client ID of the new crow, or 0 if no crow.</param>
|
|
[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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Adds a character to the players list.
|
|
/// </summary>
|
|
/// <param name="character">The ServerCharacter to add.</param>
|
|
public void AddCharacterToList(ServerCharacter character)
|
|
{
|
|
if (!players.Contains(character))
|
|
{
|
|
players.Add(character);
|
|
Debug.Log($"Player {character.name} added to CrowManager.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a character from the players list.
|
|
/// </summary>
|
|
/// <param name="character">The ServerCharacter to remove.</param>
|
|
public void RemoveCharacterFromList(ServerCharacter character)
|
|
{
|
|
if (players.Contains(character))
|
|
{
|
|
players.Remove(character);
|
|
Debug.Log($"Player {character.name} removed from CrowManager.");
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ensures the players list is updated when a player is spawned or despawned.
|
|
/// </summary>
|
|
/// <param name="character">The ServerCharacter to track.</param>
|
|
public void OnPlayerSpawned(ServerCharacter character)
|
|
{
|
|
AddCharacterToList(character);
|
|
}
|
|
|
|
public void OnPlayerDespawned(ServerCharacter character)
|
|
{
|
|
RemoveCharacterFromList(character);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves the current Crow.
|
|
/// </summary>
|
|
/// <returns>The current Crow player.</returns>
|
|
public ServerCharacter GetCurrentCrow()
|
|
{
|
|
return currentCrow;
|
|
}
|
|
}
|