Class StageManager

java.lang.Object
tech.underoaks.coldcase.stages.StageManager

public class StageManager extends Object
Manages the different screens (stages) of the game.

This class is responsible for handling transitions between various stages, such as the main menu, gameplay, settings, etc. It employs a singleton pattern to ensure only one instance exists throughout the game's lifecycle. The StageManager must be initialized via create(Game) before it can be accessed using getInstance().

  • Constructor Details

    • StageManager

      public StageManager()
  • Method Details

    • getInstance

      public static StageManager getInstance()
      Returns the singleton instance of the StageManager.

      This method throws an IllegalStateException if the StageManager has not been created yet.

      Returns:
      the singleton StageManager instance
      Throws:
      IllegalStateException - if the StageManager is not initialized
    • create

      public static StageManager create(com.badlogic.gdx.Game game)
      Creates a new StageManager instance with the specified Game instance.

      This method must be called before any other calls to StageManager.getInstance(). If a StageManager instance already exists, an IllegalStateException is thrown.

      Parameters:
      game - the Game instance to associate with this StageManager
      Returns:
      the newly created StageManager instance
      Throws:
      IllegalStateException - if a StageManager is already initialized
    • showScreen

      public void showScreen(Stages stage, Object... params)
      Displays the screen corresponding to the given stage.

      This method retrieves the new stage from the Stages enum using the provided parameters, builds the stage with an InputMultiplexer, sets it as the current screen in the Game, and disposes the previous stage if it exists.

      Parameters:
      stage - the Stages enum value representing the stage to be shown
      params - optional parameters required to build the stage
    • setNextStage

      public void setNextStage(Stages nextStage, Object... params)
      Sets the next stage to be displayed along with any parameters required to build it.

      The specified stage and parameters will be applied on the next call to update().

      Parameters:
      nextStage - the Stages enum value representing the next stage to display
      params - optional parameters required by the stage's screen builder
    • update

      public void update()
      Checks if a next stage has been set and, if so, transitions to that stage.

      If a next stage is defined, this method calls showScreen with the provided stage and parameters, and then resets the next stage variables.
      This is done because the context in which showScreen can be called doesn't always have the required libGDX context to draw in the application window. This means that update should only be called from within the scope of the Game class.

    • getCurrentStage

      public AbstractStage getCurrentStage()
      Returns the current stage (screen) being displayed by the game.
      Returns:
      the current AbstractStage or null if no stage is currently set