using System.Linq; using UnityEngine; using Game; using Music; using Player; using UnityEngine.InputSystem; using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine.Events; using System.Collections; namespace Game { /// /// This class manages the hub area of the game, including loading and unloading game scenes, /// controlling the hub camera, and managing game buttons. /// public class HubManager : MonoBehaviour { /// /// A single instance of this class that can be accessed from anywhere. /// public static HubManager Instance; /// /// The camera used in the hub area. /// public GameObject hubCamera; /// /// The parent object containing all game buttons in the hub. /// public GameObject gameButtonsParent; /// /// Sets up the hub manager and ensures only one instance exists. /// private void Start() { // Ensure there is only one HubManager in the game if (Instance == null) { Instance = this; } else { Destroy(this.gameObject); } // Add an AudioListener to the hub camera if it doesn't already have one if (hubCamera.GetComponent() == null) { hubCamera.AddComponent(); } // Activate the hub camera and start the music playlist hubCamera.SetActive(true); MusicManager.Instance.StartPlaylist(); print("Game started"); } /// /// Loads a new game scene and disables the hub camera. /// /// The name of the scene to load. public void LoadScene(string sceneName) { // Unload the current game scene and disable the hub camera UnloadGameScene(); hubCamera.SetActive(false); // Load the new scene SceneManager.LoadScene(sceneName, LoadSceneMode.Additive); // Ensure the active camera has an AudioListener var activeCamera = Camera.main; if (activeCamera != null && activeCamera.GetComponent() == null) { activeCamera.gameObject.AddComponent(); } // Start the music playlist for the new scene MusicManager.Instance.StartPlaylist(); print("Loading scene: " + sceneName); } /// /// Unloads the current game scene and reactivates the hub camera. /// public void UnloadGameScene() { // Reactivate the hub camera hubCamera.SetActive(true); try { // Unload the currently loaded game scene SceneManager.UnloadSceneAsync(SceneManager.GetSceneAt(1)); } catch { // Ignore errors if no additional scene is loaded } // Disable interaction with game buttons ChangeGameButtonsInteractability(false); } /// /// Handles input and resets the game when the escape key is pressed. /// private void Update() { if (InputSystem.GetDevice().escapeKey.wasPressedThisFrame) { // Unload the current game scene and enable game buttons UnloadGameScene(); ChangeGameButtonsInteractability(true); // Remove all players and reset the game state if (GameManager.players != null) { foreach (GameObject player in GameManager.players.ToList()) { GameManager.players.Remove(player); if (player != null) { Destroy(player); } } } // Restart the music playlist for the title screen if (MusicManager.Instance != null) { MusicManager.Instance.StartPlaylist("Title Screen"); } // Disable all cameras in the scene var cameras = FindObjectsByType(FindObjectsSortMode.None); if (cameras != null) { foreach (Camera camera in cameras) { camera.enabled = false; } } // Clear player data and reset the game state GameManager.players?.Clear(); GameManager.playerColors?.Clear(); if (GameManager.Instance != null) { GameManager.Instance.gameOver = false; } // Load the title screen SceneManager.LoadScene("Title Screen"); } } /// /// Enables or disables interaction with the game buttons in the hub. /// /// True to enable interaction, false to disable it. private void ChangeGameButtonsInteractability(bool interactable) { // Show or hide the game buttons gameButtonsParent.transform.parent.gameObject.SetActive(interactable); // Enable or disable each button foreach (Transform button in gameButtonsParent.transform) { button.GetComponent