142 lines
3.7 KiB
C#
142 lines
3.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public class MapManager : MonoBehaviour
|
|
{
|
|
public Map currentMap;
|
|
private Dictionary<string, Map> _maps;
|
|
public static MapManager Instance { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
Initialize();
|
|
// create grid manager if it doesn't exist
|
|
if (GridManager.Instance == null)
|
|
{
|
|
var gridManager = new GameObject("GridManager");
|
|
gridManager.AddComponent<GridManager>();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
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 asyncLoad = SceneManager.LoadSceneAsync(currentMap.sceneName);
|
|
|
|
while (!asyncLoad.isDone)
|
|
{
|
|
Debug.Log("Loading scene: " + asyncLoad.progress);
|
|
yield return null;
|
|
}
|
|
|
|
// todo: wait till after new scene is loaded then load grid
|
|
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("TODO: Unloading map: " + map.name);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
} |