Game Development Reference
In-Depth Information
With the messages defined, let's see what the server code looks like:
1. We define a new class called FPSServer , which extends SimpleApplica-
tion .
2. It needs to keep track of the following fields. Apart from the Server field, it
also keeps track of the next ID to give to a connecting player, a Game, and a Map
of all the currently connected players, with their connection as the key:
private Server server;
private int nextPlayerId = 1;
private Game game;
private HashMap<HostedConnection,
ServerPlayerControl> playerMap = new
HashMap<HostedConnection, ServerPlayerControl>();
3. Like in the previous recipe, we use a class called GameUtil to register all our
message classes. We also set frameRate to 30 fps . This might be different
depending on the game type. Finally, we start the application in the headless
mode, to save resources as follows:
public static void main(String[] args ) throws
Exception{
GameUtil.initialize();
FPSServer gameServer = new FPSServer();
AppSettings settings = new AppSettings(true);
settings.setFrameRate(30);
gameServer.setSettings(settings);
gameServer.start(JmeContext.Type.Headless);
}
4. We initialize the server as in the Making a networked game - Battleships recipe
and create a ConnectionListener instance to look for connecting and dis-
connecting players. This will call addPlayer and removePlayer respect-
ively, when players connect or disconnect.
5. In the addPlayer method, we create a new ServerPlayerControl in-
stance, which is the server-side implementation of NetworkedPlayerCon-
trol , and assign an ID to it for easier reference, as follows:
private void addPlayer(Game game, HostedConnection
conn){
Search WWH ::




Custom Search