Game Development Reference
In-Depth Information
10. Finally, we check whether we also have high cover and set the
hasLowCover
and
hasHighCover
fields accordingly.
11. To restrict movement, the
onAction
method needs some modifications. The
first criterion we check is whether the toggle cover button is pressed. If we're
already in cover, we'll release the character from the cover. If we're not in cover,
we check whether it's possible to go into cover:
if(binding.equals("ToggleCover") && value){
if(inCover){
inCover = false;
} else {
checkCover(spatial.getWorldTranslation());
if(hasLowCover || hasHighCover){
inCover = true;
}
}
12. In the following bracket, we limit movement to left and right if we're inside cover.
If neither of the preceding statements applies, movement should be handled as
usual. If we didn't want the player to be able to move inside cover, we'd be done
by now.
13. Since we want to mimic popular cover-based games though, we have some more
work ahead of us.
14. At the top of the update method, we have code to set the direction of the character
based on the camera's rotation. We need to change this a bit, since once the char-
acter is inside cover, it should move based on the direction of the cover rather
than the camera. To achieve this, we add a
!inCover
criterion to the original
if
statement, since outside cover, this should work like it worked previously.
15. Then, if we are in cover, we base
modelForwardDir
and
modelLeftDir
on
the rotation of the spatial, as follows:
modelForwardDir =
spatial.getWorldRotation().mult(Vector3f.UNIT_Z);
modelLeftDir =
spatial.getWorldRotation().mult(Vector3f.UNIT_X);
16. Once the movement has been applied to the
walkDirection
vector but before
it is applied it to the character, we check whether the character will still be inside
cover after moving: