Game Development Reference
In-Depth Information
How to do it...
We begin by defining a few classes that will be used commonly across both server and cli-
ent:
1. First off, we define a class called NetworkedPlayerControl extending Ab-
stractControl . We will use this both as an identifier for a player object and as
a control for the spatial representation of the player.
2. The class will be extended in further recipes, but for now it should keep track of an
integer called ID .
3. It also needs an abstract method called onMessageReceived , taking Player-
Message as input. This is the method that our message handlers will call to apply
changes. In ServerPlayerControl , the message will contain the actual input
from the player, whereas ClientPlayerControl simply replicates what has
happened on the server.
4. Now, we define a class called Game , which will be shared by both the client and
server.
5. We add a HashMap object called players , where playerId is the key and
NetworkedPlayerControl is the value. It keeps track of the players.
We will need a couple of new messages for this example. All messages are assumed to be
in a bean pattern with getters and setters. We define the messages with the following steps:
1. We create a base message to be used for player-related information and call it
PlayerMessage , extending AbstractMessage . This only needs an integer
called playerId .
2. We create the first message that extends PlayerMessage . It is called Player-
ActionMessage and handles player input. This should be set to be reliable as
we don't want to ever miss a player's input.
3. Since player input can either be a key press or mouse click, it needs to have both a
Boolean value called pressed and a float value called floatValue .
4. In addition, we also have to add a String value called action .
5. We extend PlayerMessage in another class called PlayerUpdateMessage .
This will be used to distribute player location information from the server to the
clients. This should not be reliable to avoid unnecessary delays.
6. It has a Vector3f field called position and a Quaternion field called
lookDirection .
Search WWH ::




Custom Search