88 lines
2.2 KiB
C#
88 lines
2.2 KiB
C#
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();
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
public void 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);
|
|
GridManager.Instance.LoadGrid(currentMap.sceneName);
|
|
SceneManager.LoadScene(currentMap.sceneName);
|
|
}
|
|
|
|
public Grid GetGridForMap(Map map)
|
|
{
|
|
// Get the grid for the map
|
|
Debug.Log("Getting grid for map: " + map.name);
|
|
return GridManager.Instance.GetGridForMap(map);
|
|
}
|
|
|
|
public Grid GetGridForCurrentMap()
|
|
{
|
|
// Get the grid for the current map
|
|
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))
|
|
LoadMap(map);
|
|
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);
|
|
}
|
|
}
|
|
} |