using System.Collections.Generic; using Components; using Unity.VisualScripting; using UnityEngine; public class GridManager : MonoBehaviour { public int tileSize = 1; // my grid is set to 32x32 and so that is 1 in unity, ok huh. private readonly Dictionary _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(); _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("GRID: Loading grid: " + currentMapSceneName); if (_grids.TryGetValue(currentMapSceneName, out var grid)) { Debug.Log("GRID: Grid found for map: " + currentMapSceneName); Debug.Log("GRID: Size: " + grid.size.x + ", " + grid.size.y); // Create a parent object for the grid var gridParent = new GameObject("Grid"); // gridParent.transform.position = Vector3.zero; var mapParent = GameObject.FindGameObjectWithTag("Map"); if (mapParent != null) gridParent.transform.SetParent(mapParent.transform); Instantiate(gridParent, new Vector3(0, 0, 0), Quaternion.identity); // 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("GRID: Loading tile at position: " + x + ", " + y); var tile = ScriptableObject.CreateInstance(); tile.Initialize(new Vector2Int(x, y)); var interactable = InteractableManager.Instance.GetRandomInteractable(); tile.SetInteractable(interactable); grid.SetTile(x, y, tile); var tilePrefab = PrefabManager.Instance.GetTilePrefab(); var tileObject = Instantiate(tilePrefab, new Vector3(x * tileSize, -y * tileSize, 0), Quaternion.identity); tileObject.name = $"Tile_{x}_{y}"; tileObject.transform.SetParent(gridParent.transform); var tileComponent = tileObject.GetComponent(); tileComponent.Initialize(x, y, tile); // check if it's a crop var interactablePrefab = PrefabManager.Instance.GetInteractablePrefab(); var interactableObject = Instantiate(interactablePrefab, tileObject.transform.position, Quaternion.identity); var interactableComponent = interactableObject.GetComponent(); interactableComponent.SetInteractable(tile.Interactable); interactableObject.name = $"Interactable{tile.Interactable.objectName}_{x}_{y}"; // interactableObject.transform.SetParent(tileObject.transform); // get Crop prefab and instantiate it at the center of its new parent tile interactableComponent.transform.SetParent(tileObject.transform); interactableComponent.GetComponent().sprite = tile.Interactable.display; // move interatable sprite such that the bottom of the sprite is at the bottom of the tile if (tile.Interactable.display) { var tileBottom = tileObject.transform.position.y - tileObject.GetComponent().bounds.size.y; interactableComponent.transform.localPosition = new Vector3(0, tileBottom, 0); } } } else { Debug.LogError("GRID: No grid found for map: " + currentMapSceneName); } } }