示例#1
0
        private void BounceOffBrick(BrickCollision collision)
        {
            if (collision == BrickCollision.None)
            {
                return;
            }

            if (collision == BrickCollision.Horizontal)
            {
                dX *= -1;
            }
            else if (collision == BrickCollision.Vertical)
            {
                dY *= -1;
            }

            Bounces++;
        }
示例#2
0
        public void Update(Field field, Player player, Robot robot)
        {
            if (Status == BallStatus.Launch)
            {
                float angle = random.Next(30, 150);

                dX = (int)(Speed * Math.Cos(angle * Math.PI / 180.0f));
                dY = (int)(Speed * Math.Sin(angle * Math.PI / 180.0f));

                Status = BallStatus.Launched;
            }

            // Check for collision
            if (WallCollision())
            {
                BounceOffWall();
            }
            else if (PaddleCollision(robot))
            {
                BounceOffPaddle(robot);
            }
            else if (PaddleCollision(player))
            {
                BounceOffPaddle(player);
            }
            else
            {
                BrickCollision collision = field.Collision(this);
                BounceOffBrick(collision);
            }

            // Increase ball speed based on bounces
            if (Bounces == 5)
            {
                Speed++;
                Bounces = 0;
            }

            // Update ball position
            X += dX;
            Y += dY;
        }
示例#3
0
        private void OnCollisionEnter2D(Collision2D collision)
        {
            PaddleController paddle = collision.gameObject.GetComponent <PaddleController>();
            WallCollision    wall   = collision.gameObject.GetComponent <WallCollision>();
            BrickCollision   brick  = collision.gameObject.GetComponent <BrickCollision>();

            if (brick != null)
            {
                velocity = Vector3.Reflect(velocity, collision.contacts[0].normal);
                brick.HandleCollision();
                AudioManager.Instance.PlayOneShot(brickSFX, true);
                ParticleManager.Instance.PlayParticlesAtPosition(collision.contacts[0].point);
            }

            if (paddle != null)
            {
                velocity.y *= -1f;

                float distanceToCenter = transform.position.x - paddle.transform.position.x;

                if (distanceToCenter < 0)
                {
                    float t = Mathf.InverseLerp(paddle.transform.position.x,
                                                paddle.transform.position.x - paddle.boxCollider.bounds.extents.x,
                                                transform.position.x);

                    Quaternion angle = Quaternion.Lerp(Quaternion.Euler(0f, 0f, 0f), Quaternion.Euler(0f, 0f, 50f), t);
                    velocity = angle * velocity;
                    velocity.Normalize();
                }
                else
                {
                    float t = Mathf.InverseLerp(paddle.transform.position.x,
                                                paddle.transform.position.x + paddle.boxCollider.bounds.extents.x,
                                                transform.position.x);

                    Quaternion angle = Quaternion.Lerp(Quaternion.Euler(0f, 0f, 0f), Quaternion.Euler(0f, 0f, -50f), t);
                    velocity = angle * velocity;
                    velocity.Normalize();
                }

                AudioManager.Instance.PlayOneShot(wallSFX, true);
            }

            if (wall != null)
            {
                if (wall.wallType == WallType.HORIZONTAL)
                {
                    velocity.x *= -1f;
                }
                else if (wall.wallType == WallType.VERTICAL)
                {
                    velocity.y *= -1f;
                }

                StartCoroutine(camShake.Shake(0.125f, 0.2f));
                AudioManager.Instance.PlayOneShot(wallSFX, true);
            }

            Vector2 contactNormal = collision.contacts[0].normal;

            transform.rotation = Quaternion.Euler(0f, 0f, Mathf.Atan2(contactNormal.y, contactNormal.x) * Mathf.Rad2Deg);
            StartCoroutine(SquashAnim());
            StartCoroutine(HitFlash());
        }