Game Development Reference
In-Depth Information
enqueue(new Callable(){
public Object call() throws Exception {
rootNode.attachChild(playerNode);
return null;
}
});
11. Finally, we return the created
ClientPlayerControl
instance to the calling
method.
12. We add a new method called
setThisPlayer
. This method will be called
when the player's
WelcomeMessage
is received. Inside this, we create
Cam-
eraNode
, which will be attached to the player, as follows:
public void setThisPlayer(ClientPlayerControl player){
this.thisPlayer = player;
CameraNode camNode = new CameraNode("CamNode", cam);
camNode.setControlDir(CameraControl.ControlDirection.SpatialToCamera);
((Node)player.getSpatial()).attachChild(camNode);
}
13. We also have to override the
destroy
method to make sure we close the con-
nection to the server when the client is shutdown. This can be implemented as fol-
lows:
public void destroy() {
super.destroy();
client.close();
}
14. Now, we need to create the client representation of
NetworkedPlayerCon-
trol
and extend it in a class called
ClientPlayerControl
.
15. It has a
Vector3f
field called
tempLocation
and a
Quaternion
field
called
tempRotation
. These are used to hold received updates from the server.
It can also have a
float
field called
yaw
for head movement.
16. In the
onMessageReceived
method, we only look for
PlayerUp-
dateMessages
and set
tempLocation
and
tempRotation
with the val-
ues received in the message, as follows: