162 lines
6.9 KiB
C#
162 lines
6.9 KiB
C#
using System.Collections.Generic;
|
|
using Data.ScriptableObjects;
|
|
using Types.Map;
|
|
using UnityEngine;
|
|
using Utils;
|
|
|
|
namespace Managers
|
|
{
|
|
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<string, TileGrid> _grids = new();
|
|
private GameObject _gridParent;
|
|
|
|
public static GridManager Instance { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
}
|
|
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
public TileGrid CreateGridForMap(string mapName, Vector2Int gridSize)
|
|
{
|
|
_grids[mapName] = new TileGrid(gridSize);
|
|
GameLogger.Log("New Grid created for map: " + mapName);
|
|
// move grid to correct position
|
|
return _grids[mapName];
|
|
}
|
|
|
|
public TileGrid GetGridForMap(MapSo mapSo)
|
|
{
|
|
if (_grids.TryGetValue(mapSo.sceneName, out var grid)) return grid;
|
|
|
|
GameLogger.Log($"No grid found for map {mapSo.sceneName}");
|
|
// create a new grid with default size
|
|
return CreateGridForMap(mapSo.sceneName, mapSo.gridSize);
|
|
}
|
|
|
|
public void AssignGridToMap(MapSo mapSo, TileGrid grid)
|
|
{
|
|
// Load the grid for the map
|
|
GameLogger.Log("Loading grid for map: " + mapSo.sceneName);
|
|
_grids[mapSo.sceneName] = grid;
|
|
}
|
|
|
|
// Update your other methods to take a mapName parameter and operate on the correct grid
|
|
public void LoadGrid(string currentMapSceneName, Vector3 gridPosition)
|
|
{
|
|
GameLogger.Log("GRID: Loading grid: " + currentMapSceneName);
|
|
|
|
if (_grids.TryGetValue(currentMapSceneName, out var grid))
|
|
{
|
|
GameLogger.Log("GRID: Grid found for map: " + currentMapSceneName);
|
|
GameLogger.Log("GRID: Size: " + grid.GetSize().x + ", " + grid.GetSize().y);
|
|
|
|
// Create a parent object for the grid
|
|
_gridParent = new GameObject("Grid");
|
|
// set grid parent position from Map parent
|
|
|
|
|
|
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.GetSize().x; x++)
|
|
for (var y = 0; y < grid.GetSize().y; y++)
|
|
if (grid.GetTile(x, y) == null)
|
|
{
|
|
var interactable = PrefabManager.Instance.GetRandomCropPrefabInstance();
|
|
var tile = CreateTile(new Vector2Int(x, y));
|
|
// set interactive as child of tile
|
|
interactable.transform.SetParent(tile.transform);
|
|
interactable.transform.position = tile.transform.position;
|
|
GameLogger.Log("GRID: Created tile at position: " + x + ", " + y);
|
|
}
|
|
|
|
// set tile as parent to interactable game object
|
|
|
|
/*// Load the tile at position (x, y)
|
|
GameLogger.Log("GRID: Loading tile at position: " + x + ", " + y);
|
|
|
|
var tile = ScriptableObject.CreateInstance<Tile>();
|
|
tile.Initialize(new Vector2Int(x, y));
|
|
var interactable = InteractableManager.Instance.GetRandomCropInteractable();
|
|
tile.SetInteractable(interactable);
|
|
|
|
grid.SetTile(x, y, tile);
|
|
var tilePrefab = PrefabManager.Instance.GetTilePrefab();
|
|
var cropPrefab = PrefabManager.Instance.GetRandomCropPrefabInstance();
|
|
|
|
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>();
|
|
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<InteractiveComponent>();
|
|
|
|
|
|
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<SpriteRenderer>().sprite = interactable.growthSprites[0];
|
|
// move interatable sprite child objectsuch that the bottom of the sprite is at the bottom of the tile
|
|
GameLogger.Log("GRID: Setting Interactable sprite position to bottom of tile");
|
|
var spriteRenderer = interactableComponent.GetComponent<SpriteRenderer>();
|
|
var spriteHeight = spriteRenderer.bounds.size.y;
|
|
spriteRenderer.transform.position = new Vector3(tileObject.transform.position.x,
|
|
tileObject.transform.position.y + spriteHeight / 2 - 0.5f, 0);*/
|
|
// }
|
|
GameLogger.Log("GRID: Setting grid parent position to: " + gridPosition);
|
|
_gridParent.transform.position = gridPosition;
|
|
}
|
|
else
|
|
{
|
|
GameLogger.LogError("GRID: No grid found for map: " + currentMapSceneName);
|
|
}
|
|
}
|
|
|
|
|
|
public GameObject CreateTile(Vector2Int position)
|
|
{
|
|
GameLogger.Log("GRID: Creating tile at position: " + position);
|
|
var tilePrefab = PrefabManager.Instance.GetTilePrefab();
|
|
|
|
var tileObject = Instantiate(tilePrefab, new Vector3(position.x * tileSize, -position.y * tileSize, 0),
|
|
Quaternion.identity);
|
|
|
|
tileObject.name = $"Tile_{position.x}_{position.y}";
|
|
tileObject.transform.SetParent(_gridParent.transform);
|
|
var tileComponent = tileObject.GetComponent<TileComponent>();
|
|
|
|
var tile = new Tile(new Vector2Int(position.x, position.y));
|
|
tile.Initialize(new Vector2Int(position.x, position.y));
|
|
tileComponent.Initialize(position.x, position.y, tile);
|
|
|
|
|
|
return tileObject;
|
|
}
|
|
}
|
|
} |