Time for action – GamePiece class methods – part 4 – GetSourceRect
Add the
GetSourceRect()
method to theGamePiece
class:Public Function GetSourceRectangle() As Rectangle Dim x As Integer = textureOffsetX Dim y As Integer = textureOffsetY If _pieceSuffix.Contains("W") Then x += PieceWidth + texturePaddingX End If y += (Array.IndexOf(PieceTypes, _pieceType) *(PieceHeight + texturePaddingY)) Return New Rectangle(x, y, PieceWidth, PieceHeight) End Function
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 1
.
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 _pieceSuffix
does contain the letter W
(indicating the pipe...