Пример #1
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.TryGetComponent(out Ball ball))
     {
         BallDestroyed?.Invoke(ball);
     }
 }
Пример #2
0
 /// <summary>
 /// Destoys ball with score
 /// </summary>
 public void ScoreBall()
 {
     BallDestroyed?.Invoke(_scoreCost);
     Instantiate(EffectPrefab, transform.position, transform.rotation);
     Destroy(gameObject);
 }
Пример #3
0
    private void OnCollisionEnter2D(Collision2D other)
    {
        List <BallPlay> HandleBalls = new List <BallPlay>();

        if (isPlay)
        {
            if (other.collider.CompareTag("Wall"))
            {
                direction = new Vector2(-direction.x, direction.y);
            }

            if (other.collider.CompareTag("GridBall"))
            {
                List <BallPlay> SameColorBalls = new List <BallPlay>();
                minDistance = Vector2.Distance(transform.position, other.transform.position);

                BallPlay Left      = null;
                BallPlay Right     = null;
                BallPlay LeftUp    = null;
                BallPlay LeftDown  = null;
                BallPlay RightUp   = null;
                BallPlay RightDown = null;
                DetectNeighbours(other.gameObject.GetComponent <BallPlay>(), ref Left, ref Right, ref LeftUp,
                                 ref LeftDown, ref RightUp, ref RightDown);

                if (Math.Abs(distance - MaxDragDistance) < 0.05f)
                {
                    transform.position = other.gameObject.transform.position;
                    row    = other.gameObject.GetComponent <BallPlay>().row;
                    column = other.gameObject.GetComponent <BallPlay>().column;
                    GameObject pop = Instantiate(Pop);
                    pop.transform.position = other.gameObject.transform.position;
                    Destroy(other.gameObject);
                    LevelBuild.Instance.BallsArray[row, column] = this;
                }
                else
                {
                    if (Left)
                    {
                        FindingPlace(Left);
                    }
                    if (Right)
                    {
                        FindingPlace(Right);
                    }
                    if (LeftUp)
                    {
                        FindingPlace(LeftUp);
                    }
                    if (RightDown)
                    {
                        FindingPlace(RightDown);
                    }
                    if (LeftDown)
                    {
                        FindingPlace(LeftDown);
                    }
                    if (RightUp)
                    {
                        FindingPlace(RightUp);
                    }
                }
                isPlay                = false;
                readyForShoot         = false;
                rb.isKinematic        = false;
                this.tag              = "GridBall";
                this.gameObject.layer = 0;
                tj.enabled            = true;

                ClusterFinder(this, SameColorBalls);

                if (SameColorBalls.Count >= 3)
                {
                    foreach (var colorBall in SameColorBalls)
                    {
                        GameObject pop = Instantiate(Pop);
                        pop.transform.position = colorBall.transform.position;
                        colorBall.gameObject.SetActive(false);
                        colorBall.gameObject.layer      = 9;
                        colorBall.color                 = EmptyColor;
                        colorBall.spriteRenderer.sprite = LevelBuild.Instance.ballPlay.colors[colorBall.color];
                        BallDestroyed?.Invoke();
                    }
                }
                BallCollided?.Invoke();
            }
            for (int column = 0; column < LevelBuild.Instance.MaxColumns; column++)
            {
                if (LevelBuild.Instance.BallsArray[0, column].color != EmptyColor)
                {
                    CheckFreeBalls(LevelBuild.Instance.BallsArray[0, column], HandleBalls);
                }
            }

            foreach (var ball in LevelBuild.Instance.BallsArray)
            {
                if (!HandleBalls.Contains(ball) && ball.color != EmptyColor)
                {
                    ball.gameObject.tag = "Untagged";
                    ball.rb.isKinematic = false;
                    ball.tj.enabled     = false;
                    ball.coll.isTrigger = true;
                }
            }

            if (other.collider.CompareTag("Roof"))
            {
                Destroy(this.gameObject);
                BallCollided?.Invoke();
            }
        }
    }