Game Development Reference
In-Depth Information
Getting ready
It is highly recommended to familiarize yourself with the content of the previous recipes in
the chapter, if you haven't already.
The amount of messages will increase greatly compared to the previous recipes. Since both
the server and client need to keep a track of the same messages and they need to be re-
gistered in the same order, we can create a GameUtil class. It has a static method called
initialize() . For every new message type we create, we add a line like this:
Serializer.registerClass(WelcomeMessage.class);
The game revolves around a couple of objects that we'll define before getting into the net-
working aspect.
We need a Ship class. For this implementation, it only needs the name and segments
fields. We add methods so that once a tile containing Ship is hit, we can decrease the seg-
ments. When segments reach zero, it's sunk. Likewise, Player can be a simple class, with
only an ID necessary for identification with the server, and the number of ships still alive.
If the number of ships reaches zero, the player loses.
Many of the message types extend a class called GameMessage . This class in turn ex-
tends AbstractMessage and needs to contain the ID of the game, and state that the
message should be reliable, thus using the TCP protocol.
Search WWH ::




Custom Search