Time for action – GamePiece class methods – part 4 – GetSourceRect

  1. Add the GetSourceRect() method to the GamePiece class:
      public Rectangle GetSourceRect()
      {
          int x = textureOffsetX;
          int y = textureOffsetY;
            
          if (pieceSuffix.Contains("W"))
              x += PieceWidth + texturePaddingX;
    
          y += (Array.IndexOf(PieceTypes, pieceType) * 
               (PieceHeight + texturePaddingY));
    
    
          return new Rectangle(x, y, PieceWidth, PieceHeight);
      }

What just happened?

Initially, the x and y variables are set to the textureOffsets that are listed in the GamePiece class declaration. This means they will both start with a value of one.

Because the sprite sheet is organized with a single type of pipe on each row, the x coordinate of the Rectangle is the easiest to determine. If the pieceSuffix

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.