Game Development Reference
In-Depth Information
connectionFilters.get(game.getId());
connsForGame.add(conn);
9. It then creates a GameStatusMessage object, letting all players in the game
know the current status (which is WAITING ) and any player information it might
have, as shown in the following code snippet:
GameStatusMessage waitMessage = new
GameStatusMessage();
waitMessage.setGameId(game.getId());
waitMessage.setGameStatus(Game.GAME_WAITING);
waitMessage.setPlayerOneId(game.getPlayerOne() !=
null ? game.getPlayerOne().getId() : 0);
waitMessage.setPlayerTwoId(game.getPlayerTwo() !=
null ? game.getPlayerTwo().getId() : 0);
server.broadcast(Filters.in(connsForGame),
waitMessage);
}
We're going to take a look at message handling on the client side and see how its Mes-
sageListener interface will handle incoming WelcomeMessages and game up-
dates:
1. We create a class called ClientMessageHandler , which implements Mes-
sageListener . First, we will walk through the part handling the start of a
game.
2. The thisPlayer object has already been instanced in the client, so all we need
to do when receiving WelcomeMessage is set the player's ID. Additionally, we
can display something to the player letting it know the connection is set up:
public void messageReceived(Client source, Message m)
{
if(m instanceof WelcomeMessage){
WelcomeMessage welcomeMess = ((WelcomeMessage)m);
Player p = gameClient.getThisPlayer();
p.setId(welcomeMessage.getMyPlayerId());
}
Search WWH ::




Custom Search