69 lines
2.0 KiB
C#
69 lines
2.0 KiB
C#
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<GameObject> SpawnedSlots = new();
|
|
|
|
private void OnEnable()
|
|
{
|
|
TowerPlacementManager.OnSpawnGridRequested += SpawnSlots;
|
|
TowerPlacementManager.OnGridDeleteRequested += DeleteGrid;
|
|
}
|
|
|
|
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<SlotManager>();
|
|
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<SlotManager>();
|
|
for (int i = 0; i < slots.Length; i++)
|
|
{
|
|
Destroy(slots[i].gameObject);
|
|
}
|
|
}
|
|
}
|
|
|
|
public enum GridType
|
|
{
|
|
Primary,
|
|
Wall,
|
|
Celling
|
|
} |