Untitled

 avatar
unknown
csharp
3 years ago
1.6 kB
6
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;

public class BombDestroyer : MonoBehaviour
{
    public Tilemap tilemap;
    private Stats stats;
    //private Tile tile;

    public TileBase wallTile;
    public TileBase destructibleTile;

    private void Start()
    {
        stats = GameObject.FindObjectOfType<Stats>();

        if(tilemap == null)
        tilemap = GameObject.FindObjectOfType<Tilemap>();
    }

    public GameObject prefab_explosion;

    public void Explode(Vector2 worldPosition)
    {
        Vector3Int originalCell = tilemap.WorldToCell(worldPosition);
        ExplodeCell(originalCell);

        for (int i = 0; i < stats.bombExplodeSize; i++)
        {
            if (ExplodeCell(originalCell + new Vector3Int(1, 0, 0)))
            {
                ExplodeCell(originalCell + new Vector3Int(1 + i, 0, 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);
            return false;
        }

        //Create an explosion here
       Vector3 pos = tilemap.GetCellCenterWorld(cell);
       if (prefab_explosion) { Instantiate(prefab_explosion, pos, Quaternion.identity); }

        return true;
    }
}
Editor is loading...