using System.Collections; using System.Collections.Generic; using UnityEngine; using Game; using Music; using Player; using UnityEngine.InputSystem; namespace Player { /// /// This class manages player-related functionality, such as joining, leaving, and assigning colors. /// It also handles starting the game once players have joined. /// public class PlayerManager : MonoBehaviour { /// /// The singleton instance of the class. /// public static PlayerManager Instance; /// /// A list of player join cards, which represent players in the UI. /// public List cards; /// /// The input actions used by players. /// [SerializeField] private InputActionAsset playerActions; /// /// A list of colors assigned to players for identification. /// public List playerColors; /// /// The UI element used for player selection. /// public GameObject playerSelect; /// /// Indicates whether the game has started. /// private bool gameStarted = false; /// /// Initializes the singleton instance of the . /// private void Awake() { Init(); } /// /// Subscribes to player join and leave events. /// private void Start() { GetComponent().onPlayerJoined += OnPlayerJoined; GetComponent().onPlayerLeft += OnPlayerLeft; } /// /// Handles logic for when a player joins the game. /// /// The of the player who joined. private void OnPlayerJoined(PlayerInput playerInput) { // Prevent players from joining after the game has started if (gameStarted) { Destroy(playerInput.gameObject); return; } print("Player joined"); // Ensure the player object persists across scenes DontDestroyOnLoad(playerInput.gameObject); // Create a player join card for the new player if (PlayerCardCreator.Instance == null) { return; } PlayerJoinCard card = PlayerCardCreator.Instance.CreateCard(); if (card == null) { return; } // Assign a player number and add the card to the list card.playerNumber = GameManager.players.Count + 1; cards.Add(card); // Initialize the player list in the GameManager if it doesn't exist if (GameManager.players == null) { GameManager.players = new List(); } playerInput.gameObject.name = card.playerNumber.ToString(); // Add the player to the GameManager's player list and assign a color GameManager.players.Add(playerInput.gameObject); Colorize(GameManager.players.Count - 1); } /// /// Handles logic for when a player leaves the game. /// /// The of the player who left. private void OnPlayerLeft(PlayerInput playerInput) { // Remove the player from the game and destroy their object Destroy(playerInput.gameObject); GameManager.players.Remove(playerInput.gameObject); print("Player left"); } /// /// Initializes the singleton instance of the . /// private void Init() { if (Instance == null) { Instance = this; } else { Destroy(this.gameObject); } } /// /// Starts the game if at least one player has joined. /// public void StartGame() { // Prevent starting the game if no players have joined if (GameManager.players.Count == 0) { return; } gameStarted = true; // Load the selected map HubManager.Instance.LoadScene(GameManager.map); } /// /// Assigns a unique color to a player and their associated UI elements. /// /// The index of the player in the player list. private void Colorize(int index) { // Get the player object and assign a base color GameObject player = GameManager.players[index]; Color color = playerColors[(GameManager.players.Count - 1) % playerColors.Count]; // Adjust the color tint based on the number of players float tint = Mathf.Floor((GameManager.players.Count - 1) / playerColors.Count); color = (color + color + Color.white * tint) / (tint + 2); // Add the color to the GameManager's player color list GameManager.playerColors.Add(color); // Apply the color to the player and their UI preview ApplyColor(player, color); ApplyColor(cards[GameManager.players.IndexOf(player)].playerPreview, color); } /// /// Applies a color to a GameObject and its children. /// /// The GameObject to colorize. /// The color to apply. private void ApplyColor(GameObject obj, Color color) { // Apply the color to the object's SpriteRenderer, if it has one if (obj.TryGetComponent(out _)) { obj.GetComponent().color = color; } // Recursively apply the color to all child objects foreach (Transform child in obj.transform) { if (child.TryGetComponent(out _)) { ApplyColor(child.gameObject, color); } } } } }