Thursday 4 April 2013

Introduction of Game Service


when you develop game with GDX Engine, you will be familiar with a concept very popular in GDX engine, it's the game-services. Game-services are a special object is created in the Game classes. The service contain all services provided by Game, as retrieved sprite batch, take out the camera, scene management, add an object to make custom services ... You can understand the Game-services is a variable that contains a lot of references to objects will be used frequently when development in game. Game shared services to all Game Component and extend the class of Game Component. Game-services will be retrieved by method getGameServices() of Game Component.
Game-services are separated as GameService class for Game 2D (Class Game) and Game3DService for Game 3D (Class Game3D). Game3DService inherited from GameService and added some services such as CameraManager, LightManager and ShaderManager that manage all cameras, lights and shaders in any 3D game using GDX Engine. To construct the Game-service, you must call the initializeGameServices() method of Game Class.
Game-services allow you to render a texture directly on the screen by using method drawTexture(),or render a texture region using the method drawTextureRegion() upon the screen. It also allows you to draw any text on the screen using a bitmap font by using the DrawText () method .
Method changeScene () of the Game-services is very convenient when you want to move the screen, refer to the introduction engine architecture, [ Game-scene ].
You must call initializeGameServices() method in your Game class, but it is not required to create  every instance for every object services to pass into the method as parameters, you ‘re allowed to pass ‘null’ value. If you pass the ‘null’ value into any parameter in the method, the engine will automatically create an instance instead of null value using proper existing default class.
The service contained two basic types of service, These are built-in services, and custom services.

Built-in services

This is the services available, including sprite batch, game asset, game setting, scene changing, camera for Game2D and some 3D service managers in game3D. asset and setting are the objects require to create an instance and then use that instance to initialize the Game-services. 3D service managers, of course, is only necessary when you create a 3D game. With 2D game there is no need to.
When you initialize Game-services, you need to pass in an instance of GameAsset and GameSetting. You can extend from the base class BaseGameAsset andBaseGameSetting in the engine to generate asset or a special setting for your game, or you can use the default class that engine has built.
To initialize the Game-services, You need to call method initializeGameService ( ) of the Game class, there are two parameters using the default built-in class of Gdx engine.
Example:
//Call in Extended Game class
initializeGameService (new DefaultGameAsset (), new DefaultGameSetting ());
In game3D, with 3D services manager, you can create an instance of the 3D Managers, and initialize the Game-services. In game3D, there are five parameters
For example: / / Create managers
CameraManager cameraManager = new CameraManager ();
LightManager lightManager = new LightManager ();
ShaderManager shaderManager = new ShaderManager ();          
/ / Initialize settings and assets
BaseGameSetting setting = new DefaultGameSetting ( );
DefaultGameAsset asset = new Asset ();
/ / Initialize Game-service for all scene can use later
initialzeGameService (assetsettingcameraManagershaderManagerlightManager);
When you call method initializeGameService, engine automatically call the load...() method of GameAsset and GameSetting in Game-services to load the asset and setting when the game is starting.

Custom services

is discretionary services that contain references to other objects in the game. For example, you make the asteroid game, in which the player is a ship floating in space. Ship will be destroyed if a collision with Meteor or hit the Enemy's bullet. So you can think the player is a Game-services. the the Meteor and EnemyBullet class can use the player game-service to check for collision with the player. This actually is the ship which gamer controls in the game. For Player can become a services, it needs implement IService interface. Rest assured this is just empty interface to notify Gdx engine that instances of this class can be used as a service in the game.

/ / Player class declaration
class Player extend GameComponent implements the IService {/ / your code}
/ / Create a real player that gamer controls:
Player player = new Player ();
// Make player into a service.
getGameServices (). addService (player);
You just using method addService () of Game-service so this can turn the player into a services!
To use the Player service , you simply call to getService() method of Game-service object:
/ / In the Meteor class, in initialize () method. Get the Player service.
Player player = getGameServices().GetService(Player.class);
/ / Check collision, in the update method of the meteor class:
If (player.collides(this) { / / do some funny stuffs... }
getService () method is a generic method, which means you do not need to cast the return object to Player.
The method wisely return the Player type, but it will return the data type Player. Similarly if you call getGameServices (). getService(ABCXYZ.class); getService ( ) method will return type ABCXYZ  for you :-D
You need to get a service, then add service before! If you do not correct the order, the service which will be null and Engine will throw an GdxRuntimeException “Service is not existing...”. To be safe, it is best to add services in the constructor of the Scene, and get services in method initialze () of the Game component object.
Default permission for number of using custom services up to ten. If you want to change,  before calling method  initialzeGameService(), you need to specify the number of services that you would like in static variable MAX_SERVICES:
Example:
GameService.
MAX_SERVICES = 20;
              / / Initialize Game-service for all scene can use later
              initializeGameService (asset, baseGameSetting );
code above will make your game supports up to 20 Custom services.

No comments:

Post a Comment