210 lines
7.1 KiB
C#
210 lines
7.1 KiB
C#
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 Utils;
|
|
// 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<string, MapSo> _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(MapSo newMapSo)
|
|
{
|
|
GameLogger.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);
|
|
// GameLogger.Log("Loading scene: " + asyncLoad.progress);
|
|
/*while (asyncLoad is { isDone: false })
|
|
{
|
|
GameLogger.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)
|
|
{
|
|
GameLogger.Log("Scene loaded: " + data.LoadedScenes.Length);
|
|
/*GameLogger.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
|
|
GameLogger.Log("GRID: Getting grid for map: " + mapSo.name);
|
|
|
|
// Check if GridManager is null
|
|
if (GridManager.Instance == null)
|
|
{
|
|
GameLogger.LogWarning("GRID: No GridManager found");
|
|
return null;
|
|
}
|
|
|
|
return GridManager.Instance.GetGridForMap(mapSo);
|
|
}
|
|
|
|
public TileGrid GetGridForCurrentMap()
|
|
{
|
|
// Get the grid for the current map
|
|
GameLogger.Log("GRID: Getting grid for current map: " + currentMapSo.name);
|
|
if (currentMapSo == null)
|
|
{
|
|
GameLogger.LogWarning("GRID: No current map found");
|
|
return null;
|
|
}
|
|
|
|
if (GridManager.Instance == null)
|
|
{
|
|
GameLogger.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)
|
|
GameLogger.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)
|
|
{
|
|
GameLogger.Log("Error unloading map: " + e.Message);
|
|
}
|
|
}
|
|
|
|
public void AddMap(MapSo mapSo)
|
|
{
|
|
if (_maps == null) _maps = new Dictionary<string, MapSo>();
|
|
|
|
_maps.Add(mapSo.name, mapSo);
|
|
}
|
|
|
|
public void GetMap(string mapName)
|
|
{
|
|
GameLogger.Log("Getting map: " + mapName);
|
|
if (_maps.TryGetValue(mapName, out var map))
|
|
{
|
|
var loadMap = LoadMap(map);
|
|
StartCoroutine(loadMap);
|
|
}
|
|
else
|
|
{
|
|
GameLogger.LogWarning("Map not found: " + mapName);
|
|
}
|
|
}
|
|
|
|
public void Initialize()
|
|
{
|
|
GameLogger.Log("Initializing items from Resources/Maps");
|
|
|
|
var items = Resources.LoadAll<MapSo>("Maps");
|
|
|
|
foreach (var item in items)
|
|
{
|
|
GameLogger.Log("Adding map: " + item.name);
|
|
AddMap(item);
|
|
}
|
|
|
|
if (currentMapSo == null)
|
|
{
|
|
GameLogger.LogWarning("No current map found");
|
|
}
|
|
else
|
|
{
|
|
GameLogger.Log("Loading current map: " + currentMapSo.name);
|
|
var loadMap = LoadMap(currentMapSo);
|
|
StartCoroutine(loadMap);
|
|
}
|
|
}
|
|
}
|
|
} |