Game Development Reference
In-Depth Information
The jewel cart is represented in the
JewelCart
class. We define a couple of things.
First, we define how much the jewel cart should be moved back when the player
finds a correct combination. This is stored in the
push
member variable. We also
want to set a minimal
x
position, so that the jewel cart will never be drawn over the
playing field. This we do in the
minxpos
variable, which is accessed by a property
carrying the same name. We add a
Push
method that can be called when the player
finds a correct combination of jewels. For the complete class, see Listing
16.3
.
We add an instance of this class to the game world as follows:
JewelCart jewelcart =
new
JewelCart(1, "jewelcart");
jewelcart.Position =
new
Vector2(410, 230);
jewelcart.MinXPos = 410;
this
.Add(jewelcart);
The jewel cart object also gets an ID, so that we can find it later on when it needs to
be pushed. Furthermore, we set its position and its minimal
x
position to appropriate
values. As you can see in Listing
16.3
, we assign a positive
x
velocity to the cart.
Because
JewelCart
is a subclass of
SpriteGameObject
, which in turn is a subclass of
GameObject
,the
Update
method will update the cart position according to its velocity
for us (assuming that this method is called from somewhere else).
16.4 Finding Combinations of Jewels
16.4.1 Handling the Combinations
Whenever a player has constructed a valid combination in the middle column us-
ing the arrow keys, the player can press the space bar to let the game check if the
combination is valid, and if so, add points to the score and push the jewel cart back.
The question is: in which object should we handle the event that the player presses
the space bar? Since we have many game objects, we could do it in any of their
HandleInput
methods. For example, we could do it in the
RowSelectGameObject
class,
because we already handle row selection in there. However, that would not be very
logical. Checking for valid combinations of jewels is something that fits much better
in the
JewelGrid
class, since that is were the grid of jewels is stored. Therefore, we
are going to add a
HandleInput
method to that class to deal with finding combinations
of symbols. In this method, we will check if the player has pressed space. Therefore,
the skeleton of the method is given as follows:
public override void
HandleInput(InputHelper inputHelper)
{
if
(!inputHelper.KeyPressed(Keys.Space))
return
;
dosomething...
}