Pages

Friday, November 7, 2014

Java Game Development - Part 2: Developing multi user game



Extending the game as multi-user application


When extending this game as multi-user game, another player baby fish is added to the game. In addition to escaping from enemy fish and meeting friendly fish, the baby fish players should compete with each other to go home sooner than the other while getting maximum number of points.

Design

Client - server architecture

Client server architecture is used instead of peer-to-peer approach to due to its simplicity and ease of development. 

In a client-server architecture all the game players (baby fishes), or "clients", are connected to a central machine, the Fish game server. 

The server is responsible for important decisions such as creating game friend/ enemy fish collection, managing state and broadcasting this information (x, y coordinates of players and non-players) to the individual clients. 

As a result, the server becomes a key bottleneck for both bandwidth and computations.
However, this approach will consume more network bandwidth. 



Concurrent game playing using multi-threading


Multi- threading approach is used to enable multiple users to play the game concurrently. A separate thread will represent each client.

Network Access using socket communication


TCP/ IP Socket communication (low level network communication) is used for two-way communication between server and clients. Remote Method Invocation (RMI) approach is not used here, as it will incur additional processing overhead.
Using encapsulation to support multiple network protocols

FishServer class and FishClient interface do not include any TCP/ IP networking specific programming. This generic design will support different network protocols with out changing the core game logic. 

Class Diagram


 Sequence Diagram


Implementation

Threading in Java

Synchronized keyword

Since game server is accepting different threads that request or send messages, which access same resources (E.g., objects, variables), for preventing thread interference and memory consistency errors.

Runnable interface

Runnable interface is used to implement what each player client thread is supposed to do once executed.

Networking in Java

Serializable Interface

We need to send the game information such as game scores and player/ non-player x, y coordinates across network.

To achieve the above, state of the objects is transmitted across network by converting objects to byte arrays.

Classes are serialized before sending through network and de-serialized once received using Serializable interface.

Socket and ServerSocket


ServerSocket object is created to listen for incoming game player client connections.
The accept method is used to wait for incoming connections. 

The accept method returns an instance of a Socket class, which represents the connection to the new client.

ObjectOutputStream/ ObjectInputStream methods (writeObject, readObject) are used to get object streams to reading from and writing to the new client. 

UI









 



No comments:

Post a Comment