Example #1
0
 public void Draw(Graphics graphics)
 {
     if (Balls.IsDemolitionBallState())
     {
         graphics.DrawImage(demolitionImage, location);
     }
     else
     {
         graphics.DrawImage(image, location);
     }
 }
Example #2
0
        public override void CollisionedBy(CollisionBase collision)
        {
            // ball out of screen
            if (collision.GetType() == Type.GetType("gArkanoid.Entities.OutputLine"))
            {
                Balls.RemoveBall(this);
            }

            // ball hit playerpad: invert vertical direction
            if (collision.GetType() == Type.GetType("gArkanoid.Entities.PlayerPad"))
            {
                // check into what part of cPlayerPad is the ball collisioning (left border, pad center, right border)
                if (this.X + BALL_WIDTH < collision.Location.X + 20)   // left border

                {
                    mov_x -= 1;                      // fixed direction change
                    mov_x -= GenerateRandomFactor(); // plus little random direction change

                    if (mov_x < MIN_SIDE_ONE)
                    {
                        mov_x = MIN_SIDE_ONE;                       // -1.41
                    }
                    mov_y = -1 * CalculateSideTwo(mov_x);
                }
                else if (this.X > collision.Location.X + collision.GetWidth() - 20)     // right border

                {
                    mov_x += 1;                      // fixed direction change
                    mov_x += GenerateRandomFactor(); // plus little random direction change

                    if (mov_x > MAX_SIDE_ONE)
                    {
                        mov_x = MAX_SIDE_ONE;                       // 1.41
                    }
                    mov_y = -1 * CalculateSideTwo(mov_x);
                }
                else     // pad center
                {
                    mov_y *= -1;
                }
            }

            // ball hit a brick
            // detect brick face collisioned, invert ball direction acording to that
            // check 4 posibles directions of the ball (UpLeft, UpRight, DownLeft, DownRight)
            if (collision.GetType() == Type.GetType("gArkanoid.Entities.Brick"))
            {
                // no change ball direction if the ball is in demolition state
                if (Balls.IsDemolitionBallState())
                {
                    return;
                }

                int delta_x, delta_y;

                if (mov_x < 0 && mov_y < 0)   // ball UpLeft

                // posibles brick faces colisioned: right, buttom, right&buttom
                {
                    delta_x = (collision.Location.X + collision.GetWidth()) - this.Location.X;
                    delta_y = (collision.Location.Y + collision.GetHeight()) - this.Location.Y;

                    if (delta_x > delta_y)   // buttom
                    {
                        mov_y *= -1;
                    }
                    else if (delta_x < delta_y)     // right
                    {
                        mov_x *= -1;
                    }
                    else     // both
                    {
                        mov_y *= -1; mov_x *= -1;
                    }
                }

                else if (mov_x >= 0 && mov_y < 0) // ball UpRight
                {
                    // posibles brick faces colisioned: left, buttom, left&buttom
                    delta_x = (this.Location.X + this.GetWidth()) - collision.Location.X;
                    delta_y = (collision.Location.Y + collision.GetHeight()) - this.Location.Y;

                    if (delta_x > delta_y)   // buttom
                    {
                        mov_y *= -1;
                    }
                    else if (delta_x < delta_y)     // left
                    {
                        mov_x *= -1;
                    }
                    else     // both
                    {
                        mov_y *= -1; mov_x *= -1;
                    }
                }

                else if (mov_x < 0 && mov_y >= 0) // ball DownLeft
                {
                    // posibles brick faces colisioned: right, top, right&top
                    delta_x = (collision.Location.X + collision.GetWidth()) - this.Location.X;
                    delta_y = (this.Location.Y + this.GetHeight()) - this.Location.Y;

                    if (delta_x > delta_y)   // top
                    {
                        mov_y *= -1;
                    }
                    else if (delta_x < delta_y)     // right
                    {
                        mov_x *= -1;
                    }
                    else     // both
                    {
                        mov_y *= -1; mov_x *= -1;
                    }
                }

                else if (mov_x >= 0 && mov_y >= 0) // ball DownRight
                {
                    // posibles brick faces colisioned: left, top, left&top
                    delta_x = (this.Location.X + this.GetWidth()) - collision.Location.X;
                    delta_y = (this.Location.Y + this.GetHeight()) - this.Location.Y;

                    if (delta_x > delta_y)   // top
                    {
                        mov_y *= -1;
                    }
                    else if (delta_x < delta_y)     // right
                    {
                        mov_x *= -1;
                    }
                    else     // both
                    {
                        mov_y *= -1; mov_x *= -1;
                    }
                }
            }
        }