Game Development Reference
In-Depth Information
public void onMessageReceived(PlayerMessage message) {
if(message instanceof PlayerUpdateMessage){
PlayerUpdateMessage updateMessage =
(PlayerUpdateMessage) message;
tempRotation.set(updateMessage.getLookDirection());
tempLocation.set(updateMessage.getPosition());
tempYaw = updateMessage.getYaw();
}
}
17. We will then apply the
temp
variable values in the
controlUpdate
method:
spatial.setLocalTranslation(tempLocation);
spatial.setLocalRotation(tempRotation);
yaw = tempYaw;
Just like on the server side, we need a message handler listening for incoming messages.
To do this, perform the following steps:
1. We create a new class called
ClientMessageHandler
, which implements
MessageListener<Client>
.
2. The
ClientMessageHandler
class should have a reference to
FPSClient
in a field called
gameClient
and
Game
itself in another field called
game
.
3. In the
messageReceived
method, we need to handle a number of messages.
The
WelcomeMessage
is most likely to arrive first. When this happens, we cre-
ate a player object and spatial and assign it to be this client's player, as follows:
public void messageReceived(Client source, Message m)
{
if(m instanceof WelcomeMessage){
ClientPlayerControl p =
gameClient.createPlayer(((WelcomeMessage)m).getMyPlayerId());
gameClient.setThisPlayer(p);
game.addPlayer(gameClient.getThisPlayer());
4. The
PlayerJoinMessage
is received both when player joins and leaves a
game. What sets it apart is the
leaving
Boolean. We call both the
game
and
gameClient
methods based on whether the player is joining or leaving, as
shown in the following code snippet: