Time for action – GamePiece class methods – part 4 – GetSourceRect
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
variable does not contain a W
(signifying that the piece is filled with water), the x
coordinate will simply remain 1
.
If the pieceSuffix
does contain the letter W
(indicating the pipe...