Game Development Reference
In-Depth Information
11. The
onTrigger
method should apply the position and rotation to the target and
attach it to the
sceneNode
. Depending on the implementation, we might want
to subtract the
worldTranslation
and
worldRotation
values from the
values we apply:
target.setLocalTranslation(position);
target.setLocalRotation(rotation);
sceneNode.attachChild(target);
Let's have a look at another common game object that can be picked up. In many games,
characters can pick up various power-up weapons or other items simply by walking over
them. This section will have the following eight steps:
1. We create a new class called
Pickup
extending
Trigger
.
2. Like
EnterableTrigger
, the
Pickup
class needs a position and a
List<Spatial>
called
actors
. We also need to add a
Spatial
field called
triggeringActor
and a float called
triggeringDistance
.
3. For this class, we also need something to pick up, represented here by an interface
called
Pickupable
. In addition, we need to keep track of whether it's been
picked up by a Boolean called
pickedUp
.
4. The difference between the previous ScriptObjects we've worked with and the
current ScriptObjects is that the one in this recipe should be visible in the world,
represented by a
Spatial
called
model
.
5. In the
update
method, we should check whether the
Pickup
object is enabled
and not
pickedUp
.
6. To make it stand out a bit in the game world, we rotate the model a little bit by
applying the
model.rotate(0, 0.05f, 0)
value.
7. Still inside the
if
clause, we check that
actors
is not null and parse through
the list. If any of the actors is inside the radius of the
triggerDistance
, we
set it to be
triggeringActor
and call the
trigger
method:
for(int i = 0; i<actors.size(); i++ ){
Spatial actor = actors.get(i);
if((actor.getWorldTranslation().distance(position)
<triggerDistance)){
triggeringActor = actor;
trigger();