Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
5.6 kB
0
Indexable
Never
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class testiPommi : MonoBehaviour
{
    public GameObject explodeSide;

    private int explodeStep = 1;
    private int explodeSize = 3;
    public LayerMask blockLayer;

    // Start is called before the first frame update
    void Start()
    {
        for(int i = 0; i < 4; i++)
        {
            Explode();
            explodeStep++;
        }
    }

  

    void Explode()
    {
        switch(explodeStep)
        {
            //Explode to RIGHT
            case 4:
                GameObject childExplodeRight = Instantiate(explodeSide, new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
                childExplodeRight.transform.parent = transform;

                //Check space to Right
                for (int o = 0; o < explodeSize; o++)
                {
                    RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x + o, transform.position.y), Vector2.right, 0.65f, blockLayer);
                   
                   if(hit.collider == null)
                    {
                       childExplodeRight.transform.position = new Vector2(transform.position.x + 1 + o * 0.5f, transform.position.y);
                       childExplodeRight.transform.localScale = new Vector2(transform.localScale.x + o, transform.localScale.y);
                    }

                   else
                    {
                        return;
                        //break;
                    }
                       // GameObject childExplode = 
                       // childExplode.transform.parent = parentExplode.transform;
                }
                    break;
            //Explode to Left
            case 3:
                GameObject childExplodeLeft = Instantiate(explodeSide, new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
                childExplodeLeft.transform.parent = transform;

                //Check space to Left
                for (int o = 0; o < explodeSize; o++)
                {
                    RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x - o, transform.position.y), Vector2.left, 0.65f, blockLayer);

                    if (hit.collider == null)
                    {
                        childExplodeLeft.transform.position = new Vector2(transform.position.x - 1 - o * 0.5f, transform.position.y);
                        childExplodeLeft.transform.localScale = new Vector2(transform.localScale.x + o, transform.localScale.y);
                    }

                    else
                    {
                        return;
                        //break;
                    }
                    // GameObject childExplode = 
                    // childExplode.transform.parent = parentExplode.transform;
                }
                break;

            //Explode to Down
            case 2:
                GameObject childExplodeDown = Instantiate(explodeSide, new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
                childExplodeDown.transform.parent = transform;
                childExplodeDown.transform.eulerAngles = Vector3.forward * 90f;

                //Check space to Down
                for (int o = 0; o < explodeSize; o++)
                {
                    RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x , transform.position.y - o), Vector2.down, 0.65f, blockLayer);

                    if (hit.collider == null)
                    {
                        childExplodeDown.transform.position = new Vector2(transform.position.x , transform.position.y - 1 - o * 0.5f);
                        childExplodeDown.transform.localScale = new Vector2(transform.localScale.x + o, transform.localScale.y);
                    }

                    else
                    {
                        return;
                        //break;
                    }
                    // GameObject childExplode = 
                    // childExplode.transform.parent = parentExplode.transform;
                }
                break;

            //Explode to Up
            case 1:
                GameObject childExplodeUp = Instantiate(explodeSide, new Vector2(transform.position.x, transform.position.y), Quaternion.identity);
                childExplodeUp.transform.parent = transform;
                childExplodeUp.transform.eulerAngles = Vector3.forward * 90f;

                //Check space to Up
                for (int o = 0; o < explodeSize; o++)
                {
                    RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x, transform.position.y + o), Vector2.up, 0.65f, blockLayer);

                    if (hit.collider == null)
                    {
                        childExplodeUp.transform.position = new Vector2(transform.position.x, transform.position.y + 1 + o * 0.5f);
                        childExplodeUp.transform.localScale = new Vector2(transform.localScale.x + o, transform.localScale.y);
                    }

                    else
                    {
                        return;
                        //break;
                    }
                    // GameObject childExplode = 
                    // childExplode.transform.parent = parentExplode.transform;
                }
                break;
        }
    }
}