Game Development Reference
In-Depth Information
joinMessage.setPlayerId(player.getId());
server.broadcast(joinMessage);
}
10. The
removePlayer
method works similarly, but it only has to send a message
to each player currently connected about the disconnected player. It also uses
PlayerJoinMessage
but it sets the
leaving
Boolean to
true
to indicate
the player is leaving, not joining the game.
11. Then, the server will continuously send location and rotation (direction) updates
to all players. Since we set
fps
to
30
, it will try to do this every 33 ms as fol-
lows:
public void simpleUpdate(float tpf) {
super.simpleUpdate(tpf);
Collection<NetworkedPlayerControl> players =
game.getPlayers().values();
for(NetworkedPlayerControl p: players){
p.update(tpf);
PlayerUpdateMessage updateMessage = new
PlayerUpdateMessage();
updateMessage.setPlayerId(p.getId());
updateMessage.setLookDirection(p.getSpatial().getLocalRotation());
updateMessage.setPosition(p.getSpatial().getLocalTranslation());
updateMessage.setYaw(p.getYaw());
server.broadcast(updateMessage);
}
}
12. We also create a
ServerMessageHandler
class that implements
Mes-
sageListener
. It's a short class in this case, which will only listen to mes-
sages extending
PlayerMessage
and pass it on to the correct
Net-
workedPlayerControl
class to update it. In this recipe, this will mean the
input coming from the player, as follows:
public void messageReceived(HostedConnection source,
Message m) {
if(m instanceof PlayerMessage){
PlayerMessage message = (PlayerMessage)m;
NetworkedPlayerControl p =