Okay, it’s been a while since the last blog post but better late than never, right? So I’ve put my tactical game development on hold for a month to make straightforward top-down shooter. Inspired by finnish classic game Tapan Kaikki (my tactical game’s alpha versions were compared to it because of the top down view). Its only gimmick currently is real-time multiplayer game on android. It is actually already available on Google Play (https://play.google.com/store/apps/details?id=com.mygdx.rop.android). It’s still somewhat deep in beta phase but there shouldn’t be any ultimate game stopping bugs. In this and next few blogs I explain what steps I had to take to make a real-time multiplayer Android game with Libgdx and Google Play Game Service and I will reveal some of my messy code.
First you need to setup the game in Google Play Developer Console and add the needed projects to your game project as libraries. These steps are explained in the developers documentation (https://developers.google.com/games/services/android/quickstart). Take a look especially at steps 2 and 3. Best getting started guide for Libgdx however is blog post by Nathan Williams (http://helios.hud.ac.uk/u1070589/blog/?p=202). It doesn’t include implementation for multiplayer game, but you will have to go through the same steps to make a multiplayer game with libgdx. I use the similar googleInterface class as mentioned in the blog which is implemented by the AndroidLauncher class, but after the implementing of the real-time multiplaying, my class has swollen into a pretty complicated interface.
public interface GoogleInterface { public void Login(); public void LogOut(); //Play with your friends public void goToWaitingRoom(); public boolean getSignedIn(); //submit a score to a leaderboard public void submitScore(int score); //Save score after a game public void saveAddedScore(int score); //gets the scores and displays them threw googles default widget public int getScores(); //gets the score and gives access to the raw score data public int getScoresData(); //This is for testing purposes public void testScreenChange(); //Checks if game is ready to start public boolean isGameReadyToStart(); // Has player received an invitation public boolean isInvitationReceived(); //Checks if game is cancelled public boolean isGameCancelled(); // Sets game ready to start public void setGameReadyToStart(boolean ready); //This sends players game characters coordinates public void sendMovingData(float x,float y,float rotation); // This sends messages like shot fired, character has been hit, character has died etc. public void sendMessageData(EventMessage em); // Get players in game public Array<PlayerPerson> getPlayerPersons(); // Get players participant ID public String getMyId(); // Get movedata array public Array<Character> getMovedata(); // Get message array public Array<EventMessage> getMessageData(); // Leave room public void leaveRoom(); //Start auto-matching public void quickGame(); // Yes I’ve added few ads hopefully they don’t disturb gaming experience public void showAds(boolean show); public void showAchievements(); public void unlockAchievement(int i); public void displayInterstitial(); public void displayLeaderboard(); // Sets current game cancelled public void setGameCancelled(boolean b); public void setInvitationReceived(boolean b); // Clears invitation from gamehelper public void clearInvitation(); // Get ids of disconnected players public Array<String> getDisconnected(); // Get ids og players who have left public Array<String> getLeft(); }
Google interface is an interface between Libgdx game classes and Google Play Game Services. It’s important to know that for example when an activity for result is started (e.g. an activity to select friends for multiplayer session) the onActivityResult method runs on differrent thread than the OpenGL thread that is the thread for changing screens in LibGDX. This caused lot headache for me. This implementation was pretty complicated for me and had lot of trial and error stuff so I will divide my experiences into a multiple blog posts. I’ll explain more about the minefields in multiplayer game implementation, the methods implemented in the androidLauncher class and the multiplayer game concepts in the following blogs and I’ll also try to describe the implementation in more tutorial manner.