Thursday 4 April 2013

Introduce to The GDX Engine Architecture.


The Engine Architecture



Engine architecture
Between the rectangle connected with red or purple. Order of the collections is from left to right and from top to bottom. Red denotes inheritance. Purple indicates an object may be item / or can be added to another object or not.
Example:
GameComponent => (Red) => ObjectCollection: ObjectCollection inherited from GameComponent!
GameComponent => (purple) => ObjectCollection: GameComponent is item of / can be added to ObjectCollection!
ObjectCollection => (Red) => GameScene: GameScene inherited from ObjectCollection!
GameScene => (purple) => Game: GameScene  is item of / can add in Gam eclass!
etc ...
Because of inheritance so GameComponent will have boolean variables like visible and dead from class CollectionItem, similar ObjectCollection game will also support Game-services like the game component ... etc.
The object is to build specifically for game2D and game3D aims to optimize. Class with string '3D 'in the class name will be used solely for Game3D Development, Game2D Development use the remaining objects of the class. Example: DrawableGameComponent for game2D. AnimatedSprite for game2D. DefaultObject3D for game3D, etc ...  

It really very simple. This architecture divide your game into 5 different groups. (Indicates letters of yellow, blue background), there are Collection item, Game component, Object collection, Game scene and Game.

Collection Item

 This is the most basic class of engine. No support game services, and most of other game objects in the engine will inherit from this class. It have few boolean variables that indicate:
     Enabled (default true): If enable is true, the collection item will be automatically updated as you add it to a object collection, or a GameScene because the object collection will automatically call the update () method of collection items
     Visible (default true): If Visible is true, collection item will only signal that it should be rendered when it is added to an object collection. Beside, due to the collection item don’t have Game-services for rendering, so it can not render, so it does not have the method render () ... The expansion of the collection item class in the game component will also own render function (eg: drawable game component will render for game2D, base object 3D rendering for 3D game)
     Dead (default false): A collection of items is dead will automatically set enabled and visible is false, the collection of items that will not be able to auto-update and auto- render again, in addition, if the object has been added to Game-scene, Game-scene will automatically remove dead items from its collection. But Object Collectionwill not automatically remove the dead collection item.

Game Component

Object support Game-services, and have all the properties of the collection item. Game-services are a special object is created in the Game classes. Game-services contain all services provided by the game, such as taking out the sprite batch, took out the camera, scene management, add an object to do custom services ... Can understand the Game-services is a variable that contains references to the objects will be frequently used during game development. Game-services is shared with all game components and extend class from the game component. Game-services can be retrieved by method getGameServices (). Unlike Collection item, Game component have own render() method depend on it is 2D Component or 3D Component.
Object Inherited from game object component are four main types:
·        drawable game component: With the variable boolean visible: If Visible is true, which collection items will be automatically rendered when you add it into a collection object, or a GameScene because the object collection automatically call render () method of the drawable game component class. The class does not contain available texture or texture region, you have to declare it, then the game can use the services to draw by method drawTexture () or call the service of sprite batch for customization of rendering.
     Default sprite: The class extend from the sprite class of Libgdx class framework. Since the sprite contains available constructor permits transmission to a texture or texture region, so you will be very comfortable when using this class. Course because default sprite is among the game component, it completely can be added to any Game-scene or an object collection using the their add... () method. Animated sprite class is extended from the default sprite that supports frame-based animation through an instance of the Animation class.
     Base object 3D: Object base class for all 3D in your game. Support method renderGL1 () and method renderGL2 (). When you add a base object 3D into a 3D object collection or a 3D Game-scene If android device support OpenGL2.0, method renderGL2 () will be used to render 3D objects. If android device only supports OpenGL1.x, method renderGL1 () will be used. The use of any method is completely determined by the engine itself, so you do not need to bother.
     Default 3D objects: 3D extension of the standard base class object. support Model, Texture of Model and variables store reference to the services of the Game-services for the development Game3D as ShaderManager, LightManager ... Use this class is easy, but if you abuse it can lead to wasted RAM. Do the best that you should refrain from creating new objects from this class. For example: class Player can extend from DefaultObject3D. Because the player in the game usually only one instance so the amount of wasting RAM will be negligible.

ObjectCollection

 When you use Gamecomponent, engine will turn the call to the update function and render the game component with the each gameloop. If you would like to optimize the program, the object collection is your choice. Imagine you have 100 enemies. Or you create 100 instances of the Enemy class, then add 100 instances in the Game-scene, or you just need to create one instance of EnemyCollection and add 100 enemies in that collection, then add EnemyCollection Game-scene. Add This process can be done very easily by using the Add method ... () of ObjectCollection class. EnemyCollection will be responsible for updating and rendering 100 enemies using the method update () and render () method of the object collection inherit from game component. Engine has some object collection as follows:
     ObjectCollection class: Using the Collection item as object item. Can customize update and render process of all the items in the built-in collection.
     SpriteCollection: Use Default Sprite object item. Default simply calls the update method and render of every Default Sprite object.
     Object3DCollection: Using the Collection item as object item. Can customize update and render process of all the items in the built-in collection.
     DefaultObject3DCollection: Using to Default 3D object object item. Default simply calls the update method and default rendering of every 3D objects.

Game-scene

 When you have created the component and Object games collection, how to use them? Game-scene is the perfect solution for you. Game-scene similar to Object collection because Game-scene inherited from Object collection. Here are a few features for Game-scene:
     Dead items were automatically removed from the collection of Game-scene but not object in the object collection.
     Game-scene represent a scene in the game. For example: Start Scene, Help Scene, gameplay Scene ... while the object collection represents a group of objects in a scene., For example, EnemyCollection only represent all objects "Enemy" in gameplay Scene.
     Game-scene directly receive game-services from Game class and communication the service to other components of the Scene.
     Game-scene is responsible for calling the appropriate method of the Game Object in the Scene, depending on the state of game applications. For example,: when starting activate a Scene, it will call the method initialize () of all components in the scene's collection to initialze all Object item. If the application pause or resume (in android), the current active scene will call the method pause () and resume () to all of the object's item.
      Game-scene has a boolean variable is continueInit. If continueInit is true, at any time you activate a scene, initialize () method of Game-scene will be called. If continueInit is false, the initialize() method is only called the first time the scene is activated.
You can activate a scene (Switch to a different scene) easily using getGameServices().ChangeScene(SceneClass) method.
Example: You have a StartScene, class StartScene extend BaseGameScene class. You are in any scene, you can return StartScene only need to call: getGameServices().ChangeScene(StartScene.class);
In case you have more than one instance for a class Scene, then you need to use SceneIndex to specify the right scene to activate. When you add scenes in the game, the first scene has index is 0, the next scene is similar to 1 and gradually increase.
Example:
/ / Declaration, in your game
StartScene startScene;
GamePlayScene playScene1;
GamePlayScene playScene2;
GamePlayScene playScene3;
Scenes to Game, in your game class:
addScene(startScene);/ / index of the scene is 0
addScene(playScene1);/ / index of the scene is 1
addScene(playScene2);/ / index of the scene is 2
addScene (playScene3);/ / index of the scene is 3
/ / Change the scene, in any game component!
getGameServices (). changeScene (0) / / Activate startscene
getGameServices (). changeScene (2) / / Activate playScene2
Or can create the final index variable and assign it:

class YourGameClass extend Game {
public static final int StartSceneIndex = 0;
/ / some game objects ...
}

Then change the scene easily
getGameServices (). changeScene (YourGameClass.StartSceneIndex);

Game




 When you have more than one scene, you just need to create a new class Extended Game inherited from class Game (for game2D) or class Game3D (for game3D). In the new extended game class, You need to initialize the Game-services and then add your scene to Game and set a scene as active scene. So you have finished games, Engine will automatically update and render the active scene for you! Game-services are quite complex, so I will cover in another article. Game in Gdx Engine should use the y-down coordinate system. The origin (0,0) is located at the top on the left side of the screen. Ox axis runs horizontally, from left to right in the positive. Oy axis running from top to bottom in the positive. Default, Gdx Engine will make game using the y-down coordinate system.


This picture will present the y-down system: axis-y down

If you want the default coordinate system as of Libgdx framework, simply use method setYdown (false):
@ Override
       public void create () {setYdown(false); // ...}

setYdown () method only makes sense in 2D. In 3D game you don’t need to care about this method. setYdown () must be called before calling the initializeGameServices() method of Game

No comments:

Post a Comment