evershade/Assets/Scripts/Managers/GridManager.cs

79 lines
2.3 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class InteractableItem
{
// Properties and methods for interactable items like rocks and trees
}
public class GridManager : MonoBehaviour
{
public int tileSize = 32;
private readonly Dictionary<string, Grid> _grids = new();
public static GridManager Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
}
else
{
Destroy(gameObject);
}
}
public Grid CreateGridForMap(string mapName, Vector2Int gridSize)
{
_grids[mapName] = ScriptableObject.CreateInstance<Grid>();
_grids[mapName].Initialize(gridSize);
Debug.Log("New Grid created for map: " + mapName);
return _grids[mapName];
}
public Grid GetGridForMap(Map map)
{
if (_grids.TryGetValue(map.sceneName, out var grid)) return grid;
Debug.LogError($"No grid found for map {map.sceneName}");
// create a new grid with default size
return CreateGridForMap(map.sceneName, map.gridSize);
}
public void AssignGridToMap(Map map, Grid grid)
{
// Load the grid for the map
Debug.Log("Loading grid for map: " + map.sceneName);
_grids[map.sceneName] = grid;
}
// Update your other methods to take a mapName parameter and operate on the correct grid
public void LoadGrid(string currentMapSceneName)
{
Debug.Log("Loading grid: " + currentMapSceneName);
if (_grids.TryGetValue(currentMapSceneName, out var grid))
{
Debug.Log("Grid found for map: " + currentMapSceneName);
// Load the grid by looping through the grid's tiles
for (var x = 0; x < grid.size.x; x++)
for (var y = 0; y < grid.size.y; y++)
{
// Load the tile at position (x, y)
Debug.Log("Loading tile at position: " + x + ", " + y);
var tile = ScriptableObject.CreateInstance<GridSpace>();
tile.Initialize(new Vector2Int(x, y));
grid.SetTile(x, y, tile);
}
}
else
{
Debug.LogError("No grid found for map: " + currentMapSceneName);
}
}
}