Game Development Reference
In-Depth Information
How it works...
The server is running in the headless mode, which means it won't do any rendering and
there will be no graphical output, but we still have access to the full jMonkeyEngine ap-
plication. In this recipe, one server instance will only have one game active at a time.
We instantiate all network messages inside a class called GameUtil , since they have to be
the same (and serialized in the same order) on the client and server.
The client will try to connect to the server as soon as it launches. Once connected, it will
receive playerId from the server via WelcomeMessage , as well as Player-
JoinMessages for all other players that are already connected. Likewise, all other play-
ers will receive PlayerJoinMessage with the new player's ID.
The client sends any actions the players perform to the server using PlayerAc-
tionMessage , which applies them to its instance of the game. The server, which runs at
30 fps, will send positions and directions of each player to all the other players, using
PlayerUpdateMessages .
The InputAppState class on the client is very similar to the one in Chapter 2 , Cameras
and Game Controls . The only difference is that instead of directly updating a Control in-
stance, it creates a message and sends it to the server. In the onAction class, we set the
Boolean value of the message, whereas in onAnalog (to look and rotate), floatValue
will be used instead, as shown in the following code snippet:
public void onAction(String name, boolean isPressed, float
tpf) {
InputMapping input = InputMapping.valueOf(name);
PlayerActionMessage action = new PlayerActionMessage();
action.setAction(name);
action.setPressed(isPressed);
action.setPlayerId(client.getThisPlayer().getId());
client.send(action);
}
In the event of a player leaving the game, PlayerJoinMessages will be sent to the
other players, with leaving set to true .
Search WWH ::




Custom Search