Bomb.cs

Bomberman's failed bomb script
mail@pastecode.io avatar
unknown
csharp
2 years ago
2.9 kB
6
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bomb : MonoBehaviour
{
    public LayerMask blockLayer;
    public GameObject explodeSide;

    private Vector2[] cardinalDirections = { Vector2.right, Vector2.down, Vector2.left, Vector2.up };
    private Powers powers;
    private Animator animator;

    private float fuseDelay; bool isFusing = true;
    private float burnTime; bool isExploding = false;

    private void Start()
    {
       powers = GameObject.FindObjectOfType<Powers>();
        animator = GetComponent<Animator>();

        fuseDelay = powers.fuseTime;


    }

    private void Update()
    {
        Debug.DrawRay(transform.position, cardinalDirections[0], Color.green, 1);
        fusing();
    }

    void fusing()
    {
        fuseDelay -= Time.deltaTime;

        if(fuseDelay <= 0 && isFusing)
        {
            animator.SetTrigger("explode");
            Explode();
            isFusing = false;
        }

    }

    void Explode()
    {
        //Raycast to all 4 directions
        for (int i = 0; i < 4; i++)
        {
            RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y), cardinalDirections[i], powers.explodeSize, blockLayer);

            GameObject childExplodeDown = Instantiate(explodeSide, new Vector2(transform.position.x, transform.position.y), explodeSide.transform.localRotation);
            childExplodeDown.transform.parent = transform;

            //Mathf.RoundToInt(hit.distance);

            //Rotate explodeFire 90 degrees
            childExplodeDown.transform.eulerAngles = Vector3.forward * 90f * i;

            if(hit.collider != null)
            {
                //Destroy child fireExplode if too close to obstacle.
                if (hit.distance < 1)
                {
                    Destroy(childExplodeDown);
                }


                //Scale and Pose child explode based by hit distance;
                else
                {
                    childExplodeDown.transform.localScale = new Vector2(transform.localScale.x + hit.distance, transform.localScale.y);
                    childExplodeDown.transform.position = new Vector2(transform.position.x, transform.position.y) + cardinalDirections[i] * hit.distance / 2;
                }
            }

            //Scale and Pose child explode based by explodeSize;
            else
            {
                childExplodeDown.transform.localScale = new Vector2(transform.localScale.x * powers.explodeSize, transform.localScale.y);
                childExplodeDown.transform.position = new Vector2(transform.position.x, transform.position.y) + cardinalDirections[i] * (powers.explodeSize -1);
            }

        }
    }
}