user3075478
user3075478

Reputation: 137

Game logic in the screen or in another class?

I'm dealing with a design issue.

Actually, i have a class GameManager, instantiated and updated by a Screen class. The class GameManager, as its name suggests, holds the game logic (transitions between levels, updates other managers, keeps data as score, money ecc...) and the game state (paused, running, game over, transition).

Developing the mechanism of transition between levels, while the transition phase is running, the game should not being updated, but i can't do that from the GameManager because the stages are updated in the Screen class.

I thought about 2 solutions:

1) I move all the game logic in the screen class, so by doing that, in the render method i can easily stop updating stages if i'm in the transition state.

2) Learn how to use finite-state machines in Libgdx

What could i do? Thank you for your help.

Update

I solved (but not avoided bad design) creating this class:

public class GameStateManager {

    public static enum GameState {
        RUNNING, PAUSE, GAME_OVER, LEVEL_TRANSITION;
    }

    private static GameState currentGameState;

    public static GameState getCurrentGameState() {
        return currentGameState;
    }

    public static void setCurrentGameState(GameState gameState) {
        currentGameState = gameState;
    }
}

In this way, in the act method of the Actors i can check if the current state of the game is RUNNING. If not, they don't act.

Upvotes: 0

Views: 237

Answers (2)

Vladislav Rastrusny
Vladislav Rastrusny

Reputation: 29975

I think you have an invalid relation between your classes. Currently your GameManager is owned by Screen. But I tend to think they should be on the same level in your class design table.

I can propose the following:

  • Create a new class Coordinator. It will own both Screen and GameManager.
  • Make simple event handling for your classes, so that Coordinator could subscribe to the events, fired by Screen and GameManager

Now, whenever GameManager thinks game situation changed, it can fire some event. Coordinator will decide what to do - to call Screen or not to. And vice versa.

Also, I think your GameManager now handles too much and should be converted into a module because you are heading straight towards God Object antipattern. And that means the whole class design could be changed.

Upvotes: 1

Kush
Kush

Reputation: 749

As much as i understand your situation, what you are trying to do is enabling and disabling stage update from a GameManager Class whose object is instantiated in a Screen. And stage is in Screen instead of GameManager. If i got you right then the easiest solution is create a public static boolean TRANSITION = false in Screen.

And enable/disable this from your game manager class by Screen.TRANSITION = true/false. So that stage.act() or whatever code that you want to get stopped when levels are loading can be haulted. Add this in screen class for eg.

if(!TRANSITION)
{   //code
    stage.act();
}

Upvotes: 1

Related Questions