Game Development Reference
In-Depth Information
Fig. 23.2
When the player
clicks on a penguin, four
arrows are shown to choose
the direction in which the
penguin should move
ter). In the
HandleInput
method, we first check if the selector is visible. If not, it does
not need to handle input:
if
(!visible)
return
;
We then check if one of the arrows was pressed, and if so, we calculate the desired
penguin velocity:
Vector2 animalVelocity = Vector2.Zero;
if
(arrowdown.Pressed)
animalVelocity.Y = 1;
else if
(arrowup.Pressed)
animalVelocity.Y =
1;
else if
(arrowleft.Pressed)
animalVelocity.X =
−
−
1;
else if
(arrowright.Pressed)
animalVelocity.X = 1;
animalVelocity
∗
= 300;
If the player clicked the left mouse button (does not matter where), we set the state
of the animal selector to invisible again:
if
(inputHelper.MouseLeftButtonPressed())
this
.Visible =
false
;
Finally, if the velocity we calculated is not zero, and there is a target penguin, we
update its velocity:
if
(selectedAnimal !=
null
&& animalVelocity != Vector2.Zero)
selectedAnimal.Velocity = animalVelocity;
Inside the
HandleInput
method in the
Animal
class, we have to handle clicking on
an animal. However, there are some situations where we do not have to handle this: