Game Development Reference
In-Depth Information
3. When a GameStatusMessage is received, we need to accomplish three things.
First, set the ID of the game. Knowing the ID of the game is not necessary for the
client in this implementation, but can be useful for communication with the serv-
er:
else if(m instanceof GameStatusMessage){
int status = ((GameStatusMessage)m).getGameStatus();
switch(status){
case Game.GAME_WAITING:
if(game.getId() == 0 &&
((GameStatusMessage)m).getGameId() > 0){
game.setId(((GameStatusMessage)m).getGameId());
}
4. Then, we set the playerOne and playerTwo fields by simply checking
whether they have been set before or not. We also need to identify the player by
comparing the IDs of the players in the message with the ID associated with this
client. Once found, we let him or her start placing ships, as follows:
if(game.getPlayerOne() == null &&
((GameStatusMessage)m).getPlayerOneId() > 0){
int playerOneId =
((GameStatusMessage)m).getPlayerOneId();
if(gameClient.getThisPlayer().getId() ==
playerOneId){
game.setPlayerOne(gameClient.getThisPlayer());
gameClient.placeShips();
} else {
Player otherPlayer = new Player();
otherPlayer.setId(playerOneId);
game.setPlayerOne(otherPlayer);
}
}
game.setStatus(status);
5. When TurnMessage is received, we should extract activePlayer from it
and set it on the game. If activePlayer is the same as thisPlayer of
gameClient , set myTurn to true on gameClient .
Search WWH ::




Custom Search