using System; using System.Collections.Generic; using Unity.VisualScripting; using UnityEngine; public class GridManager : MonoBehaviour { [SerializeField] private Vector2Int GridSize; [SerializeField] private Vector2 Gap; [SerializeField] private GridType gridType = GridType.Primary; [Space(10)] [SerializeField] private GameObject TowerSlotPrefab; [DoNotSerialize] public List SpawnedSlots = new(); /// /// Not implimented /// [DoNotSerialize] public string[,] GridStates; private void OnEnable() { TowerPlacementManager.OnSpawnGridRequested += SpawnSlots; TowerPlacementManager.OnGridDeleteRequested += DeleteGrid; GridStates = new string[GridSize.x, GridSize.y]; } private void OnDisable() { TowerPlacementManager.OnSpawnGridRequested -= SpawnSlots; TowerPlacementManager.OnGridDeleteRequested -= DeleteGrid; } public void SpawnSlots(TowerPlacementManager sender) { for (int x = 0; x < GridSize.x; x++) { for (int y = 0; y < GridSize.y; y++) { // Spawn slot Vector3 spawnPosition = new(x * (Gap.x + 1) + 0.5f, 0, y * (Gap.y + 1) + 0.5f); var spawned = Instantiate(TowerSlotPrefab, transform); spawned.transform.localPosition = spawnPosition; // Give the slot a ref var infoHolder = spawned.GetComponent(); infoHolder.spawnerRef = this; infoHolder.x = x; infoHolder.y = y; infoHolder.OnSlotClicked += sender.OnSlotClicked; infoHolder.OnSlotHovered += sender.OnSlotHovered; infoHolder.OnSlotUnHovered += sender.OnSlotUnHovered; SpawnedSlots.Add(spawned); } } } private void DeleteGrid(TowerPlacementManager sender) { SlotManager[] slots = GetComponentsInChildren(); for (int i = 0; i < slots.Length; i++) { Destroy(slots[i].gameObject); } } } public enum GridType { Primary, Wall, Celling }