Class Game

java.lang.Object
de.gurkenlabs.litiengine.Game

public final class Game
extends java.lang.Object

The Game class is without any doubt one of the classes that you will call a lot when creating a game with the LITIENGINE. It is designed to be the static container that provides access to all important aspects of the engine, e.g. it holds the GameInfo, the RenderEngine, the SoundEngine and many other major components.

We designed the API such that all important parts that make up the game are directly accessible via the Game class in a static manner. To be a little bit more technical, it is essentially a collection of core Singleton instances.

This class will also be your starting point when setting up a new LITIENGINE project. In order to launch your game, you need to at least call init(String...) and start() from your programs main(String[]) method.

Additionally, it provides an interface to hook up event listeners (e.g. GameListener or EnvironmentLoadedListener) for the most basic operations of a Game life cycle. 

See Also:
GameListener
  • Field Summary

    Fields 
    Modifier and TypeFieldDescription
    static java.lang.StringCOMMADLINE_ARG_NOGUI 
    static java.lang.StringCOMMADLINE_ARG_RELEASE 
    static intEXIT_GAME_CLOSED 
    static intEXIT_GAME_CRASHED 
  • Method Summary

    Modifier and TypeMethodDescription
    static voidaddGameListener​(GameListener listener)
    Adds the specified game listener to receive events about the basic game life-cycle.
    static voidallowDebug​(boolean allow)
    This flag indicates if the game currently supports debugging.
    static SoundEngineaudio()
    Gets the engine's SoundEngine component that can be used to play sounds and music.
    Sound can be loaded and accessed using the Resources API and are managed by the
    Resources.sounds() resource container.
    static GameConfigurationconfig()
    Gets the game's runtime configuration.
    It contains default engine settings for the game client, graphics, audio, input and debugging.
    Additionally, it can be used to register and manage custom settings that are specific to your game.
    static voidexit() 
    static RenderEnginegraphics()
    Gets the engine's RenderEngine component that is used to render Images, Shapes or Text with respect to the environment and the render scale and the Camera.
    static booleanhasStarted()
    Indicates whether the game has already been started.
    static voidhideGUI​(boolean noGui)
    This flag indicates whether the game should display the GameWindow or not.
    static GameInfoinfo()
    Gets the static meta information about this game.
    This can be used to define meta information about your game, like it's name, version or web site.

    It's also possible to provide additional custom information using the method group
    Game.getInfo().setValue("CUSTOM_STRING", "my-value").
    static voidinit​(java.lang.String... args)
    Initializes the infrastructure of the LITIENGINE game.
    static booleanisDebug()
    This flag globally controls the game's debugging state.
    static booleanisInNoGUIMode()
    Indicates whether the game should display the GameWindow or not.
    static java.util.logging.Loggerlog()
    Gets the game's default logger instance that can be used to quickly log messages without the need to initialize custom logger instances.
    static IGameLooploop()
    Gets the game's main loop that is used to execute and manage all game logic apart from input processing.
    You can attach any Updatable instance to this loop if you want to execute custom game logic that is executed at the configured max fps.
    static GameMetricsmetrics()
    Gets basic client metrics about the game's runtime.
    static PhysicsEnginephysics()
    Gets the engine's PhysicsEngine component that can be used to detect and resolve collision and move entities with respect to all collision entities on the environment.
    The boundaries of the loaded environment also pose a "non-walkable" area that will be taken into account when moving entities with this engine.
    static GameRandomrandom()
    Gets the game's pseudo-random generator that enhances the default Java Random implementation with helpful additions.
    static voidremoveGameListener​(GameListener listener)
    Removes the specified game listener.
    static ScreenManagerscreens()
    Gets the game's ScreenManager that is responsible for organizing all Screens of your game and providing the currently active Screen that is used to render the current Environment.
    Screens are the containers that allow you to organize the visible contents of your game and are identified and addressed by a unique name.
    static voidsetInfo​(GameInfo info)
    Sets the Game's basic information by the specified GameInfo instance.
    static voidsetInfo​(java.lang.String gameInfoFile)
    Sets the Game's basic information by loading the GameInfo from the specified path to an XML file.
    static voidsetInfo​(java.net.URL gameInfoFile) 
    static voidsetUncaughtExceptionHandler​(java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
    Sets an UncaughtExceptionHandler used to handle all unexpected exceptions happening in the game.
    static voidstart()
    Starts the GameLoops and other components.
    static GameTimetime()
    Gets time information about the running game/environment.
    static TweenEnginetweens()
    Gets the game's Tween manager that holds all currently active Tween instances.
    static GameWindowwindow()
    Gets the game's window in which the RenderComponent lives.
    This class e.g.
    static GameWorldworld()
    Gets the game's world which is a global environment manager that contains all Environments and provides the currently active Environment and Camera.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Method Details

    • addGameListener

      public static void addGameListener​(GameListener listener)
      Adds the specified game listener to receive events about the basic game life-cycle.
      Parameters:
      listener - The listener to add.
    • removeGameListener

      public static void removeGameListener​(GameListener listener)
      Removes the specified game listener.
      Parameters:
      listener - The listener to remove.
    • allowDebug

      public static void allowDebug​(boolean allow)
      This flag indicates if the game currently supports debugging. This should be set to false for release builds. The default value here is true and will allow debugging unless explicitly disabled by calling this method or providing the command line argument COMMADLINE_ARG_RELEASE when running the game.
      Parameters:
      allow - If set to true, the game will be told to allow debugging.
    • hideGUI

      public static void hideGUI​(boolean noGui)
      This flag indicates whether the game should display the GameWindow or not. This can only be set before the game has been initialized with the Game.init(String...) method. Afterwards it doesn't have an effect anymore. If enabled, the ScreenManager#setVisible(boolean) method won't be set to true and the RenderLoop won't be started. Also the Camera won't be updated.
      Parameters:
      noGui - If set to true, the GUI will be hidden.
      See Also:
      GameWindow, init(String...), Camera, isInNoGUIMode()
    • isDebug

      public static boolean isDebug()
      This flag globally controls the game's debugging state. If enabled, debugging functionality (e.g. rendering collision boxes) can potentially be enabled in the configuration.
      Returns:
      True if debugging functionality is enabled; otherwise false.
      See Also:
      allowDebug(boolean), GameConfiguration.debug()
    • isInNoGUIMode

      public static boolean isInNoGUIMode()
      Indicates whether the game should display the GameWindow or not.
      Returns:
      True if the game should display visual components; otherwise false.
    • hasStarted

      public static boolean hasStarted()
      Indicates whether the game has already been started.
      Returns:
      True if the game has been started; otherwise false.
      See Also:
      start()
    • info

      public static GameInfo info()
      Gets the static meta information about this game.
      This can be used to define meta information about your game, like it's name, version or web site.

      It's also possible to provide additional custom information using the method group
      Game.getInfo().setValue("CUSTOM_STRING", "my-value").
      Returns:
      The game's basic meta information.
      See Also:
      GameInfo, ICustomPropertyProvider, GameInfo.setName(String), CustomPropertyProvider.setValue(String, String)
    • config

      public static GameConfiguration config()
      Gets the game's runtime configuration.
      It contains default engine settings for the game client, graphics, audio, input and debugging.
      Additionally, it can be used to register and manage custom settings that are specific to your game.

      Elements of this configuration are also presented in a config.properties file in the game's root directory.
      This way its possible to adjust elements without having to recompile the game.

      Returns:
      The game's runtime configuration.
      See Also:
      SoundConfiguration, GraphicConfiguration, ClientConfiguration, DebugConfiguration, InputConfiguration
    • metrics

      public static GameMetrics metrics()
      Gets basic client metrics about the game's runtime. This includes information about network, the frames-per-second or the updates-per-second and the used memory.

      This information can be rendered by setting
      Game.config().client().setShowGameMetrics(boolean) to true or
      cl_showGameMetrics=true in the config.settings.

      Returns:
      Metrics about the game's runtime.
      See Also:
      GameMetrics.getFramesPerSecond(), ClientConfiguration.setShowGameMetrics(boolean)
    • time

      public static GameTime time()
      Gets time information about the running game/environment.

      This allow to measure the time between actions, track how long something took, evaluate cooldowns or just get information about the played game time.

      Returns:
      The game's temporal information.
      See Also:
      GameTime.now()
    • window

      public static GameWindow window()
      Gets the game's window in which the RenderComponent lives.
      This class e.g. provides the possibility to set a title, provide an icon, get information about the resolution or set a cursor.
      Returns:
      The window that hosts the game's RenderComponent.
      See Also:
      RenderComponent, GameWindow.getResolution(), GameWindow.setTitle(String), GameWindow.setIcon(java.awt.Image), GameWindow.cursor()
    • audio

      public static SoundEngine audio()
      Gets the engine's SoundEngine component that can be used to play sounds and music.
      Sound can be loaded and accessed using the Resources API and are managed by the
      Resources.sounds() resource container.

      Upon playing a sound, the engine returns an SoundPlayback instance that can then be used to further control the audio line.

      Returns:
      The engine's SoundEngine component.
      See Also:
      Sound, Resources.sounds(), SoundPlayback, SoundEngine.playSound(de.gurkenlabs.litiengine.sound.Sound), SoundEngine.playMusic(de.gurkenlabs.litiengine.sound.Sound)
    • physics

      public static PhysicsEngine physics()
      Gets the engine's PhysicsEngine component that can be used to detect and resolve collision and move entities with respect to all collision entities on the environment.
      The boundaries of the loaded environment also pose a "non-walkable" area that will be taken into account when moving entities with this engine.

      It is also possible to manually register static collision Rectangles that can further restrict the game world.

      Returns:
      The engine's PhysicsEngine component.
      See Also:
      PhysicsEngine, PhysicsEngine.move(IMobileEntity, float), ICollisionEntity
    • graphics

      public static RenderEngine graphics()
      Gets the engine's RenderEngine component that is used to render Images, Shapes or Text with respect to the environment and the render scale and the Camera.

      In case you want to render something in a static manner that is unrelated to the environment, you can use the engine's different static Renderer implementations.

      Returns:
      The engine's RenderEngine component.
      See Also:
      RenderEngine.getBaseRenderScale(), TextRenderer, ShapeRenderer, ImageRenderer
    • loop

      public static IGameLoop loop()
      Gets the game's main loop that is used to execute and manage all game logic apart from input processing.
      You can attach any Updatable instance to this loop if you want to execute custom game logic that is executed at the configured max fps. The game's loop also executes the rendering process on the GameFrame's RenderComponent.
      This internally renders the currently active screen which passes the Graphics2D object to all GuiComponents and the Environment for rendering.

      The LITIENGINE has two separate loops for game logic/rendering and input processing.
      This prevents them from interfering with each other and to be able to process player input independent of the game's framerate.

      Returns:
      The game's main loop.
      See Also:
      ClientConfiguration.getMaxFps(), IUpdateable, ILoop.attach(IUpdateable), ILoop.detach(IUpdateable)
    • log

      public static java.util.logging.Logger log()
      Gets the game's default logger instance that can be used to quickly log messages without the need to initialize custom logger instances.
      Returns:
      The game's default logger instance.
    • random

      public static GameRandom random()
      Gets the game's pseudo-random generator that enhances the default Java Random implementation with helpful additions.
      Returns:
      The game's pseudo random generator.
    • screens

      public static ScreenManager screens()
      Gets the game's ScreenManager that is responsible for organizing all Screens of your game and providing the currently active Screen that is used to render the current Environment.
      Screens are the containers that allow you to organize the visible contents of your game and are identified and addressed by a unique name.

      Examples: Menu Screen, Credits Screen, Game Screen, Inventory Screen

      Returns:
      The game's screen manager.
      See Also:
      Screen, GameWorld.environment(), world()
    • world

      public static GameWorld world()
      Gets the game's world which is a global environment manager that contains all Environments and provides the currently active Environment and Camera.

      The GameWorld returns the same instance for a particular map/mapName until the GameWorld.reset(String) method is called.

      Moreover, it provides the possibility to attach game logic via EnvironmentListeners to different events of the Envrionment's life cycle (e.g. loaded, initialized, ...).
      This is typically used to provide some per-level logic or to trigger general loading behavior.
      Returns:
      The game's environment manager.
      See Also:
      GameWorld, Environment, Camera, GameWorld.environment(), GameWorld.camera(), GameWorld.reset(String)
    • tweens

      public static TweenEngine tweens()
      Gets the game's Tween manager that holds all currently active Tween instances.
      Returns:
      The game's Tween manager.
    • init

      public static void init​(java.lang.String... args)
      Initializes the infrastructure of the LITIENGINE game. The following tasks are carried out by this method:
      • load the GameConfiguration
      • handle the specified program parameters
      • configure the logging
      • set the programs Locale according to the configured values.
      • initialize and attach core components like the PhysicsEngine
      • initialize the ScreenManger
      • initialize the Input
      • initialize the GameLoop and RenderLoop
      • set a default Camera
      Parameters:
      args - The arguments passed to the programs entry point.
    • setUncaughtExceptionHandler

      public static void setUncaughtExceptionHandler​(java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
      Sets an UncaughtExceptionHandler used to handle all unexpected exceptions happening in the game.
      Parameters:
      uncaughtExceptionHandler - The handler to be used for uncaught exceptions.
    • start

      public static void start()

      Starts the GameLoops and other components. After this method is called, the engine will start to render contents of the current Screen of the ScreenManager, the SoundEngine will start to playback Sounds and the different input devices (e.g. Mouse, Keyboard) will start to process player input.

      When the Game has started up successfully, it'll callback to the registered GameListeners.

      See Also:
      ScreenManager.current(), SoundEngine, Input, GameListener.started(), hasStarted()
    • exit

      public static void exit()
    • setInfo

      public static void setInfo​(GameInfo info)
      Sets the Game's basic information by the specified GameInfo instance.

      Typically, this should not be called manually because the Game already provides a GameInfo object which can be adjusted.
      If you just want to edit some of it's information, use the provided instance of info().

      Parameters:
      info - The GameInfo that contains the basic information for the game.
      See Also:
      info(), GameInfo
    • setInfo

      public static void setInfo​(java.lang.String gameInfoFile)
      Sets the Game's basic information by loading the GameInfo from the specified path to an XML file.
      Parameters:
      gameInfoFile - The path to the XML file that contains the serialized GameInfo.
      See Also:
      setInfo(GameInfo), info(), GameInfo
    • setInfo

      public static void setInfo​(java.net.URL gameInfoFile)