Game Development Reference
In-Depth Information
ServerPlayerControl player = new
ServerPlayerControl();
player.setId(nextPlayerId++);
playerMap.put(conn, player);
game.addPlayer(player);
6. Then, we create a spatial for it so that it has a reference in the scene graph (and
thus, it will be automatically updated). This is not only for visual representation,
but we are dependent on it to update our method, as follows:
Node s = new Node("");
s.addControl(player);
rootNode.attachChild(s);
7. For any future communication with the server, the client will supply its player-
Id in all messages, so the server sends the assigned ID back to the client in Wel-
comeMessage . It broadcasts the message using the client's connection as a fil-
ter, as follows:
WelcomeMessage welcomeMessage = new
WelcomeMessage();
welcomeMessage.setMyPlayerId(player.getId());
server.broadcast(Filters.in(conn), welcomeMessage);
8. Then, we send information about all the other players to the player that joins, as
follows:
Collection<NetworkedPlayerControl> players =
game.getPlayers().values();
for(NetworkedPlayerControl p: players){
PlayerJoinMessage joinMessage = new
PlayerJoinMessage();
joinMessage.setPlayerId(p.getId());
server.broadcast(Filters.in(conn), joinMessage);
}
9. Lastly, the server sends a message to all the other players about the new player, as
follows:
PlayerJoinMessage joinMessage = new
PlayerJoinMessage();
Search WWH ::




Custom Search