Friday, 8 February 2013

#5.1 Tutorial (LibGDX & MTX) Game Manager

Now lets talk about game/live wallpaper structure/logic that can be built with LibGDX Scene2D. As you know Scene2D consists of Stage/Group/Actors. By using Mtx, I have created objects which extends these Scene2D elements with very useful features. (AbstractScreen, AbstractWorldScene2D, AbstractActor)

I want my screen is to be start of everything, at the same time I do not give direct responsibility to my screen, thus gameManager is responsible for creating and managing the world. A little bit Cohesion/Coupling of object oriented programming.

Screen (extends AbstractScreen of Mtx) - constructing Scene2D Stage
World and WorldLayers (extends AbstractWorldScene2D) - Scene2D Groups
Rest of objects are Scene2D Actors  (extends AbstractActors)


As you can see, I only construct the gameManager and gameScreenMenu classes in my screen, this is all responsibility of the screen. gameScreenMenu is for creating menu buttons and etc. (For example, play/stop button).


You may notice that I implemented IScreen and IGameScreen interfaces, I love interfaces. Because, everytime I start a project, I do not need to think about structure or common methods that being used such as "setUpMenu()", so by using interfaces, in a single second, I will know what to do


GameManager is responsible of world and world layers and game state (I will talk about in later tutorials), So I create the main world (Scene2D Group) and add world layers to that world. It may sound like worlds in a main world. (Scene2D Groups being added to a single Scene2D Group)

At the end I add to main world to Scene2D Stage.


The sructure goes like this;
  1. Screen creates the stage
  2. Screen constructs the GameManager and pass stage to GameManager
  3. GameManager creates the Main World (Scene2D Group) and adds WorldLayers (Scene2D Groups)
  4. Each world layer has different Scene2D Actors due to their purposes (Clouds, Snows, Emeny objects, etc...)
I hope you gain some understanding of the fundementals of Scene2D Game/LiveWallpaer structure/logic.


Again an interface that helps me alot

    public void setUpWorld();
    public void startLevel(int levelNumber);
    public void checkGameCondition();
    public void update(float delta);
    public void saveGame();
    public void exitGame();

In each project of mine, I mostly use the methods above, so why not store them in an interface, and easily use it. This is a good practice for your programming skills, saves time and increases efficiency. Furthermore, I can improve it in time.

1 comment:

  1. Hi. Nice tutorial, but actually i'm still facing with design issues. My actors like enemies or the elements controlled by the player have hp. When they die (hp == 0) their removes themselves from the Stage. I would need to increment the Player's score when he kills an enemy, but i don't know where i should store the game data (score, money ecc..) and how to update the HUD consequently.