Post navigation

Make realtime multiplayer AndEngine games

AndEngine is a free Android 2D OpenGL Game Engine. Using AppWarp, you can add realtime multiplayer game play and virtual rooms logic to your AndEngine games. In this post, we will walk through the integration steps for a simple demo game called Fruity Monster. Players connect to AppWarp cloud, can create rooms or join existing rooms. Once inside a room, players can see each other move in realtime and place fruits to the four corners of the room.

Let’s browse through the source code and walk through some of the key concepts in building this simple game.

Once WarpClient is initialized, we go ahead and connect with AppWarp Cloud Service. This happens when the user selects a monster, enters a name and clicks on the play button. In order to receive callback result from the connect operation, we need to add a connection request listener. We make the MainActivity class implement the ConnectionRequestListener and implement the required methods.

1

theClient.connectWithUserName(userName);

If we get a successful callback for this operation, we go ahead and enter the RoomlistActivity.

This screen shows a list of active game rooms to the user. The list contains rooms with at least one user. We use our matchmaking API to build this list.

1

theClient.getRoomWithNUser(1);// trying to get room with at least one user

The user has the option to either join a room from this list or to start a new room.

Join An Existing Room

To receive the callback for the above request, we need to add a zone request listener as this is a zone level request. We make the RoomlistActivity implement the zone request listener interface and add itself as the listener. In the callback, we set up the list adapter with the room ids of all the matched rooms.

To join a new room, we first need to create a room with the defined properties before joining it. In this game we are defining four locations in room which will be used later in the game play. This will simply create a new room with the name given (random in this case) by the user and specify maxUsers as 4 and the local user as the owner. The result of the request will be invoked on our zone request listener interface.

Once the room is created successfully, we extract the room id from the event and join that room. Similarly, if the user clicks on the join button of one of the rooms from the list, we go ahead and send a join room request.

1

theClient.joinRoom(roomId);

The result of the request will be invoked on our room request listener interface. If successful, we move to the next activity.

This screen represents SimpleBaseGameActivity(andengine). In this we have defined hashmap to maintain online user those are playing game in this room. We initilize andengine in onCreateEngineOptions() and load all resources used in game in onCreateResources()

To handle all event in game we have used EventHandler.java which implements RoomRequestListener and NotifyListener to register and listens to all the relevant game events. Whenever the user taps on the screen, we need to notify other players in the room of its movement. We do this by simply constructing a json object representing the coordinates and send that using the WarpClient chat API.

Now players can move fruits to the four corners of the rooms and others can see all this happen in realtime. To do this the user selects a fruit and the press on any corner where he has to put the fruit. Then we send an updateRoomProperty request in which we update the property whose key represent destination e.g.(topLeft) and value is index of fruit.

Finally all users get a notification in EventHandler.java when a property of the room changes. It the property is updated by a remote user then we update UI with this new value according to the property values in the event.