Exemple #1
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //if (!Initializer.balls.Contains(collision.gameObject))
        //    return;

        if (Time.time - lastCollisionTime < 1f)
        {
            return;
        }

        if (DyingFreefall)
        {
            return;
        }

        lastCollisionTime = Time.time;

        if (collision.gameObject.layer == 9)
        {
            Health -= .3f;
            Initializer.AddScore(300);
        }
        else if (collision.gameObject.layer == 10)
        {
            Health -= .15f;
            Initializer.AddScore(150);
        }

        if (health < 0f)
        {
            Debug.Log($"-- {name} ------------------------------------------------------");

            if (!NextBound)
            {
                next.GetComponent <BrickController>().Previous = Previous;
                Debug.Log($"-- {next.name}->Previous = {Next.GetComponent<BrickController>().previous.name}");
            }
            else
            {
                var controller = previous.GetComponent <BrickController>();
                if (controller != null)
                {
                    controller.NextBound = true;
                }
                else
                {
                    DyingFreefall = true;
                    Destroy(gameObject, 10);
                    foreach (var ball in Initializer.balls)
                    {
                        var ballController = ball.GetComponent <BallController>();
                        ballController.constantSpeed = 0;
                        ballController.jiggle        = false;
                    }
                }
            }

            if (!PreviousBound)
            {
                previous.GetComponent <BrickController>().Next = Next;
                Debug.Log($"-- {previous.name}->Next = { previous.GetComponent<BrickController>().Next.name}");
            }
            else
            {
                if (!DyingFreefall) // scene finished
                {
                    next.GetComponent <BrickController>().PreviousBound = true;
                }
            }

            Debug.Log($"-- {name} ------------------------------------------------------");

            previous.GetComponent <SpringJoint2D>().connectedBody = next.GetComponent <Rigidbody2D>();
            next.GetComponent <SpringJoint2D>().connectedBody     = previous.GetComponent <Rigidbody2D>();

            if (prefabAnchorBottom != null)
            {
                Destroy(prefabAnchorBottom);
            }

            if (prefabAnchorTop != null)
            {
                Destroy(prefabAnchorTop);
            }

            var spring = GetComponent <SpringJoint2D>();
            Destroy(spring);
            DyingFreefall = true;

            Destroy(gameObject, 10);
        }
    }