Game Development Reference
In-Depth Information
How to do it...
Perform the following steps to optimize a bandwidth:
1. First of all, we'll add a visible field to our PlayerUpdateMessage . This is so
that a client knows when a player has disappeared from the view.
2. On the server side, we need to change two classes. First, our ServerPlayer-
Control needs to maintain a list of player IDs it currently sees.
3. Before we do our checks, we need to make sure all the players are updated:
Collection<NetworkedPlayerControl> players =
game.getPlayers().values();
for(NetworkedPlayerControl p: players){
p.update(tpf);
}
4. Next, we iterate through our playerMap object. Here, we add a simple range
check to see whether a player is visible or not, and lastly broadcast the information
to the relevant players, as follows:
Iterator<HostedConnection> it =
playerMap.keySet().iterator();
while(it.hasNext()){
HostedConnection conn = it.next();
ServerPlayerControl player = playerMap.get(conn);
for(NetworkedPlayerControl otherPlayer: players){
float distance =
player.getSpatial().getWorldTranslation().distance(otherPlayer.getSpatial().getWorldTranslation());
PlayerUpdateMessage updateMessage = null;
if(distance < 50){
updateMessage = createUpdateMessage(otherPlayer);
player.addVisiblePlayer(otherPlayer.getId());
} else if
(player.removeVisiblePlayer(otherPlayer.getId())){
updateMessage = createUpdateMessage(otherPlayer);
updateMessage.setVisible(false);
}
if(updateMessage != null){
server.broadcast(Filters.in(conn), updateMessage);
Search WWH ::




Custom Search