All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.lessvoid.nifty.slick2d.NiftyBasicGameState Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package de.lessvoid.nifty.slick2d;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.slick2d.input.PlainSlickInputSystem;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.state.StateBasedGame;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * This is the basic game state implementation that supports the Nifty-GUI. Its used for game states that display only
 * the Nifty-GUI and nothing other then that.
 *
 * @author Martin Karing >[email protected]<
 */
public abstract class NiftyBasicGameState extends NiftyOverlayBasicGameState {
  /**
   * The screen that is called upon entering the game state.
   */
  @Nullable
  private final String startScreen;

  /**
   * Create this game state.
   */
  protected NiftyBasicGameState() {
    this("start");
  }

  /**
   * Create this game state and set the screen that is called upon entering this state.
   *
   * @param niftyStartScreen the name of the screen Nifty is supposed to goto once the game state is entered
   */
  protected NiftyBasicGameState(@Nullable final String niftyStartScreen) {
    startScreen = niftyStartScreen;
  }

  /**
   * Enter this game state.
   */
  @Override
  public void enterState(@Nonnull final GameContainer container, @Nonnull final StateBasedGame game) {
    if (startScreen != null) {
      Nifty nifty = getNifty();
      if (nifty == null) {
        throw new IllegalStateException("Nifty is not initialized, but it should be.");
      }
      nifty.gotoScreen(startScreen);
    }
  }

  /**
   * When initializing the game its only needed to prepare the GUI for display.
   */
  @Override
  protected final void initGameAndGUI(@Nonnull final GameContainer container, @Nonnull final StateBasedGame game) {
    initNifty(container, game, new PlainSlickInputSystem());
    if (startScreen != null) {
      Nifty nifty = getNifty();
      if (nifty == null) {
        throw new IllegalStateException("Nifty is not initialized, but it should be.");
      }
      nifty.gotoScreen(startScreen);
    }
  }

  /**
   * Leave this game state.
   */
  @Override
  public void leaveState(@Nonnull final GameContainer container, @Nonnull final StateBasedGame game) {
    // nothing
  }

  /**
   * Rendering the GUI only requires that the display is cleared before rendering the screen.
   */
  @Override
  protected void renderGame(
      @Nonnull final GameContainer container, @Nonnull final StateBasedGame game, @Nonnull final Graphics g) {
    g.clear();
  }

  /**
   * Updating the game is not needed in this implementation as only the GUI is displayed.
   */
  @Override
  protected void updateGame(
      @Nonnull final GameContainer container,
      @Nonnull final StateBasedGame game,
      final int delta) {
    // nothing to do
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy