Game Development Reference
In-Depth Information
a faster motion effect, we multiply this value by 8. Then, we add this velocity to the
position vector (taking into account the elapsed game time), so that the game object
moves toward the target position. For the complete Jewel class, see the JewelJam3
project.
14.4.6 Selecting Rows in the Grid
The final game object that we want to discuss in this chapter is the game object that
is used for selecting different rows in the grid. This game object is represented by
a rectangular sprite which is drawn around the currently selected row. Because of
that, we need to keep track of the currently selected row inside the class that defines
the row selector game object. Finally, the row selection object needs to access the
grid, so that it can call the row shifting operation on it. Since the row selection
game object basically is a sprite that is drawn at a certain position, we are going
to design the class RowSelectGameObject as a subclass of SpriteGameObject .Inthe
constructor, we set the selected row to be the first one, we store a reference to the grid
that the selection object works on, and we call the base constructor. The complete
RowSelectGameObject constructor is given as follows:
public RowSelectGameObject(JewelGrid grid, Texture2D selector_frame, int layer = 0)
: base (selector_frame, layer)
{
selectedRow = 0;
this .grid = grid;
}
The row-selection game object will be part of the playing field, just like the grid.
Therefore, we can express the local position of the row-selection game object rel-
ative to the local position of the grid. The only thing we have to do in this class is
override the HandleInput method so that we can react to what the player is doing and
perform an action accordingly.
First, we check if the player has pressed the up or down arrow key, and increment
or decrement the selectedRow variable accordingly. Once that is done, we have to
make sure that the selected row is within the range of the grid. We can use the Clamp
method from the MathHelper class for that, as follows:
if (inputHelper.KeyPressed(Keys.Up))
selectedRow
−− ;
else if (inputHelper.KeyPressed(Keys.Down))
selectedRow++;
selectedRow = ( int )MathHelper.Clamp(selectedRow, 0, grid.Rows
1);
This way, we are sure that the selected row always is a valid row in the grid. Now
that we know what the currently selected row is, we can calculate the position of the
row selection game object relative to the grid:
Search WWH ::




Custom Search