using System; using System.Collections; using System.Collections.Generic; using Data.ScriptableObjects; using FishNet.Managing; using FishNet.Managing.Scened; using Types.Map; using UnityEngine; using UnityEngine.Serialization; // using UnityEngine.SceneManagement as UnitySceneManager; // for SceneManager using UnitySceneManager = UnityEngine.SceneManagement.SceneManager; namespace Managers { public class MapManager : MonoBehaviour { [FormerlySerializedAs("currentMap")] public MapSo currentMapSo; private Dictionary _maps; private NetworkManager _networkManager; public static MapManager Instance { get; private set; } private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); _networkManager = FindObjectOfType(); _networkManager.SceneManager.OnLoadEnd += OnLoadEnd; Initialize(); // create grid manager if it doesn't exist if (GridManager.Instance == null) { var gridManager = new GameObject("GridManager"); gridManager.AddComponent(); } } else { Destroy(gameObject); } } private void OnDestroy() { if (_networkManager != null) _networkManager.SceneManager.OnLoadEnd -= OnLoadEnd; } /* public void UnloadCurrentMap() { if (currentMap != null) _networkManager.SceneManager.UnloadConnectionScenes(_networkManager.ClientManager.Connection, new SceneUnloadData(SceneLookupData.CreateData(currentMap.sceneName))); // UnloadMap(currentMap); }*/ public IEnumerator LoadMap(MapSo newMapSo) { Debug.Log("Loading map: " + newMapSo.name); // Unload the current map, if any if (currentMapSo != null) UnloadMap(currentMapSo); // Load the new map currentMapSo = newMapSo; // load grid var grid = GetGridForMap(currentMapSo); GridManager.Instance.AssignGridToMap(currentMapSo, grid); // SceneManager.LoadScene(currentMap.sceneName); // var nob = new NetworkObject(); var sceneLoadData = new SceneLoadData(SceneLookupData.CreateData(currentMapSo.sceneName)) { // sceneLoadData.MovedNetworkObjects = new NetworkObject[] { nob }; ReplaceScenes = ReplaceOption.All, Options = { AllowStacking = false } }; // InstanceFinder.SceneManager.LoadConnectionScenes(nob.Owner, sceneLoadData); // var sceneProcessor = (DefaultSceneProcessor)_networkManager.SceneManager.GetSceneProcessor(); // sceneProcessor.BeginLoadAsync(currentMap.sceneName, // new LoadSceneParameters(LoadSceneMode.Single, LocalPhysicsMode.Physics2D)); /*_networkManager.SceneManager.LoadGlobalScenes( new SceneLoadData(SceneLookupData.CreateData(currentMap.sceneName)));*/ // var asyncLoad = SceneManager.LoadSceneAsync(currentMap.sceneName); // var sceneProcessor = _networkManager.SceneManager.GetSceneProcessor(); // var asyncLoad = UnitySceneManager.LoadSceneAsync(currentMap.sceneName); // Debug.Log("Loading scene: " + asyncLoad.progress); /*while (asyncLoad is { isDone: false }) { Debug.Log("Loading scene: " + asyncLoad.progress); yield return null; }*/ // todo: wait till after new scene is loaded then load grid yield return null; } public void OnLoadEnd(SceneLoadEndEventArgs data) { Debug.Log("Scene loaded: " + data.LoadedScenes.Length); /*Debug.Log("Scene loaded: " + data.LoadedScenes[0].name);*/ // GridManager.Instance.LoadGrid(currentMap.sceneName, currentMap.gridPosition); } public TileGrid GetGridForMap(MapSo mapSo) { // Get the grid for the map Debug.Log("GRID: Getting grid for map: " + mapSo.name); // Check if GridManager is null if (GridManager.Instance == null) { Debug.LogWarning("GRID: No GridManager found"); return null; } return GridManager.Instance.GetGridForMap(mapSo); } public TileGrid GetGridForCurrentMap() { // Get the grid for the current map Debug.Log("GRID: Getting grid for current map: " + currentMapSo.name); if (currentMapSo == null) { Debug.LogWarning("GRID: No current map found"); return null; } if (GridManager.Instance == null) { Debug.LogWarning("GRID: No GridManager found"); return null; } return GridManager.Instance.GetGridForMap(currentMapSo); } public void UnloadMap(MapSo mapSo) { // Clean up the map (e.g., destroy all items and crops) Debug.Log("Unloading map: " + mapSo.name); try { // _networkManager.SceneManager.UnloadConnectionScenes(_networkManager.ClientManager.Connection, // new SceneUnloadData(SceneLookupData.CreateData(currentMap.sceneName))); UnitySceneManager.UnloadSceneAsync(currentMapSo.sceneName); } catch (Exception e) { Debug.Log("Error unloading map: " + e.Message); } } public void AddMap(MapSo mapSo) { if (_maps == null) _maps = new Dictionary(); _maps.Add(mapSo.name, mapSo); } public void GetMap(string mapName) { Debug.Log("Getting map: " + mapName); if (_maps.TryGetValue(mapName, out var map)) { var loadMap = LoadMap(map); StartCoroutine(loadMap); } else { Debug.LogWarning("Map not found: " + mapName); } } public void Initialize() { Debug.Log("Initializing items from Resources/Maps"); var items = Resources.LoadAll("Maps"); foreach (var item in items) { Debug.Log("Adding map: " + item.name); AddMap(item); } if (currentMapSo == null) { Debug.LogWarning("No current map found"); } else { Debug.Log("Loading current map: " + currentMapSo.name); var loadMap = LoadMap(currentMapSo); StartCoroutine(loadMap); } } } }