using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class BombDestroyer : MonoBehaviour
{
public Tilemap tilemap;
private Tile tile;
public TileBase wallTile;
public TileBase destructibleTile;
private void Start()
{
if(tilemap == null)
tilemap = GameObject.FindObjectOfType<Tilemap>();
}
public GameObject prefab_explosion;
public void Explode(Vector2 worldPosition)
{
Vector3Int originalCell = tilemap.WorldToCell(worldPosition);
ExplodeCell(originalCell);
//Right
if (ExplodeCell(originalCell + new Vector3Int(1, 0, 0)))
{
ExplodeCell(originalCell + new Vector3Int(2, 0, 0));
}
//Up
if (ExplodeCell(originalCell + new Vector3Int(0, 1, 0)))
{
ExplodeCell(originalCell + new Vector3Int(0, 2, 0));
}
//Left
if (ExplodeCell(originalCell + new Vector3Int(-1, 0, 0)))
{
ExplodeCell(originalCell + new Vector3Int(-2, 0, 0));
}
//Down
if (ExplodeCell(originalCell + new Vector3Int(0, -1, 0)))
{
ExplodeCell(originalCell + new Vector3Int(0, -2, 0));
}
}
bool ExplodeCell(Vector3Int cell)
{
//Tile tile = tilemap.GetTile<TileBase>(cell);
TileBase clickedTile = tilemap.GetTile(cell);
if (clickedTile == wallTile)
{
return false;
}
if(clickedTile == destructibleTile)
{
//Remove this tile
tilemap.SetTile(cell, null);
}
//Create an explosion here
Vector3 pos = tilemap.GetCellCenterWorld(cell);
if (prefab_explosion) { Instantiate(prefab_explosion, pos, Quaternion.identity); }
return true;
}
}