Game Development Reference
In-Depth Information
How to do it...
1. We begin by creating a new class called InteractionTrigger , which extends
Trigger and also implements ActionListener .
2. The InteractionTrigger class needs a Vector3f field called position, a
BoundingVolume field called volume , a Spatial field called player , and
a boolean field called inside .
3. Furthermore, the InteractionTrigger class needs access to the application's
guiNode , which we store in a Node field with the same name and a Bit-
mapText field called interactionPrompt . The text will be displayed when
interaction is possible.
4. We also have to define a static string called INTERACTION_KEY = "Inter-
act" either in this class or the input manager class
5. The update method will check whether the player is inside BoundingVolume .
If it is and inside is false , it will show interactionPrompt . On the other
hand, if inside is true and the player is not inside BoundingVolume , it will
remove it, as shown in the following code:
Boolean contains =
volume.contains(player.getWorldTranslation());
if(!inside && contains){
guiNode.attachChild(interactionPrompt);
} else if (inside &&
!contains){guiNode.detachChild(interactionPrompt);
}
inside = contains;
6. In the implemented onAction method, we check for when the key corresponding
to INTERACTION_KEY is released. Then, we see whether the trigger is enabled
and whether inside is true or not. If both are true , we call trigger() .
7. Some logic outside the class is required to get the trigger to work. Apart from sup-
plying guiNode and BitmapText to the trigger, the INTERACTION_KEY
needs to be bound to inputManager . This can be done with the following line:
inputManager.addMapping(INTERACTION_KEY, new
KeyTrigger(KeyInput.KEY_SPACE));
Search WWH ::




Custom Search