evershade/Assets/Scripts/Managers/MapManager.cs

71 lines
1.6 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MapManager : MonoBehaviour
{
public Map currentMap;
public 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)
{
// Unload the current map, if any
if (currentMap != null) UnloadMap(currentMap);
// Load the new map
currentMap = newMap;
SceneManager.LoadScene(currentMap.sceneName);
}
public void UnloadMap(Map map)
{
// Clean up the map (e.g., destroy all items and crops)
// map.Clear();
// Destroy(map);
Debug.Log("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);
}
}
}