using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bomb : MonoBehaviour
{
private Player_Control pc;
private BoxCollider2D bc;
private Vector2[] explodeDirections = { Vector2.down, Vector2.left, Vector2.up, Vector2.right };
private float fuseTime = 1.5f;
private int explodeSize = 1;
public GameObject explode_center; private GameObject parentExplode;
public GameObject explode_side;
public GameObject explode_topdown;
public LayerMask blockLayer;
private void Awake()
{
pc = GameObject.FindGameObjectWithTag("Player").GetComponent<Player_Control>();
bc = GetComponent<BoxCollider2D>();
explodeSize = GameObject.FindGameObjectWithTag("Player").GetComponent<Powers>().explodeSize;
}
private void Start()
{
StartCoroutine(fusing());
}
private void OnTriggerExit2D(Collider2D collision)
{
if(collision.tag == "Player")
{
bc.isTrigger = false;
}
}
IEnumerator fusing()
{
while(fuseTime > 0)
{
fuseTime -= Time.deltaTime;
yield return null;
}
Exlode();
}
void Exlode()
{
bc.enabled = false;
parentExplode = Instantiate(explode_center, transform.position, Quaternion.identity);
//Check 4 directions for explode
for (int i = 0; i < 4; i++)
{
RaycastHit2D hit = Physics2D.Raycast(transform.position, explodeDirections[i], (float)explodeSize, blockLayer);
if(hit.collider != null)
{
if(hit.collider.tag == "Bomb")
{
hit.collider.gameObject.GetComponent<Bomb>().fuseTime = 0;
}
if(hit.collider.tag == "Break")
{
Destroy(hit.collider.gameObject);
}
if (hit.distance < 1)
{
//dont spawn anything;
}
else
{
// Explode with block distance
for (int o = 0; o < Mathf.RoundToInt(hit.distance) - 1; o++)
{
if(explodeDirections[o].y > transform.position.y || explodeDirections[o].y < transform.position.y)
{
GameObject childExplode = Instantiate(explode_topdown, new Vector2(transform.position.x, transform.position.y) + explodeDirections[i] * (o + 1), Quaternion.identity);
childExplode.transform.parent = parentExplode.transform;
}
else if (explodeDirections[o].x > transform.position.x || explodeDirections[o].x < transform.position.x)
{
GameObject childExplode = Instantiate(explode_side, new Vector2(transform.position.x, transform.position.y) + explodeDirections[i] * (o + 1), Quaternion.identity);
childExplode.transform.parent = parentExplode.transform;
}
}
}
}
else
{
//Explode with explodeSize if nothing blocks
for (int o = 0; o < explodeSize; o++)
{
if (explodeDirections[o].y > transform.position.y || explodeDirections[o].y < transform.position.y)
{
GameObject childExplode = Instantiate(explode_topdown, new Vector2(transform.position.x, transform.position.y) + explodeDirections[i] * (o + 1), Quaternion.identity);
childExplode.transform.parent = parentExplode.transform;
}
else if (explodeDirections[o].x > transform.position.x || explodeDirections[o].x < transform.position.x)
{
GameObject childExplode = Instantiate(explode_side, new Vector2(transform.position.x, transform.position.y) + explodeDirections[i] * (o + 1), Quaternion.identity);
childExplode.transform.parent = parentExplode.transform;
}
}
}
}
pc.currentBombCount--;
Destroy(gameObject);
}
}