evershade/Assets/Scripts/Managers/MapManager.cs

197 lines
6.1 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using FishNet.Managing;
using FishNet.Managing.Scened;
using UnityEngine;
// using UnityEngine.SceneManagement as UnitySceneManager; // for SceneManager
using UnitySceneManager = UnityEngine.SceneManagement.SceneManager;
public class MapManager : MonoBehaviour
{
public Map currentMap;
private Dictionary<string, Map> _maps;
private NetworkManager _networkManager;
public static MapManager Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
_networkManager = FindObjectOfType<NetworkManager>();
_networkManager.SceneManager.OnLoadEnd += OnLoadEnd;
Initialize();
// create grid manager if it doesn't exist
if (GridManager.Instance == null)
{
var gridManager = new GameObject("GridManager");
gridManager.AddComponent<GridManager>();
}
}
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(Map newMap)
{
Debug.Log("Loading map: " + newMap.name);
// Unload the current map, if any
if (currentMap != null) UnloadMap(currentMap);
// Load the new map
currentMap = newMap;
// load grid
var grid = GetGridForMap(currentMap);
GridManager.Instance.AssignGridToMap(currentMap, grid);
// SceneManager.LoadScene(currentMap.sceneName);
// var nob = new NetworkObject();
var sceneLoadData = new SceneLoadData(SceneLookupData.CreateData(currentMap.sceneName));
// sceneLoadData.MovedNetworkObjects = new NetworkObject[] { nob };
sceneLoadData.ReplaceScenes = ReplaceOption.All;
sceneLoadData.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[0].name);
// GridManager.Instance.LoadGrid(currentMap.sceneName, currentMap.gridPosition);
}
public TileGrid GetGridForMap(Map map)
{
// Get the grid for the map
Debug.Log("GRID: Getting grid for map: " + map.name);
// Check if GridManager is null
if (GridManager.Instance == null)
{
Debug.LogWarning("GRID: No GridManager found");
return null;
}
return GridManager.Instance.GetGridForMap(map);
}
public TileGrid GetGridForCurrentMap()
{
// Get the grid for the current map
Debug.Log("GRID: Getting grid for current map: " + currentMap.name);
if (currentMap == 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(currentMap);
}
public void UnloadMap(Map map)
{
// Clean up the map (e.g., destroy all items and crops)
Debug.Log("Unloading map: " + map.name);
try
{
// _networkManager.SceneManager.UnloadConnectionScenes(_networkManager.ClientManager.Connection,
// new SceneUnloadData(SceneLookupData.CreateData(currentMap.sceneName)));
UnitySceneManager.UnloadSceneAsync(currentMap.sceneName);
}
catch (Exception e)
{
Debug.Log("Error unloading map: " + e.Message);
}
}
public void AddMap(Map map)
{
if (_maps == null) _maps = new Dictionary<string, Map>();
_maps.Add(map.name, map);
}
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<Map>("Maps");
foreach (var item in items)
{
Debug.Log("Adding map: " + item.name);
AddMap(item);
}
if (currentMap == null)
{
Debug.LogWarning("No current map found");
}
else
{
Debug.Log("Loading current map: " + currentMap.name);
var loadMap = LoadMap(currentMap);
StartCoroutine(loadMap);
}
}
}