Class Game
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:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic 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'sSoundEnginecomponent that can be used to play sounds and music.
Sound can be loaded and accessed using theResourcesAPI 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'sRenderEnginecomponent that is used to renderImages, Shapes or Textwith respect to the environment and the render scale and theCamera.static booleanIndicates whether the game has already been started.static voidhideGUI(boolean noGui) This flag indicates whether the game should display theGameWindowor 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 voidInitializes the infrastructure of the LITIENGINE game.static voidstatic voidInitializes the infrastructure of the LITIENGINE game.static booleanisDebug()This flag globally controls the game's debugging state.static booleanIndicates whether the game should display theGameWindowor not.static 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 anyUpdatableinstance 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'sPhysicsEnginecomponent 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 JavaRandomimplementation with helpful additions.static voidremoveGameListener(GameListener listener) Removes the specified game listener.static ScreenManagerscreens()Gets the game'sScreenManagerthat is responsible for organizing allScreensof your game and providing the currently activeScreenthat is used to render the currentEnvironment.
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 voidSets theGame'sbasic information by the specifiedGameInfoinstance.static voidSets theGame'sbasic information by loading theGameInfofrom the specified path to an XML file.static voidstatic voidsetUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets anUncaughtExceptionHandlerused to handle all unexpected exceptions happening in the game.static voidstart()Starts theGameLoopsand 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 theRenderComponentlives.
This class e.g.static GameWorldworld()Gets the game's world which is a global environment manager that contains allEnvironmentsand provides the currently activeEnvironmentandCamera.
-
Field Details
-
EXIT_GAME_CLOSED
public static final int EXIT_GAME_CLOSED- See Also:
-
EXIT_GAME_CRASHED
public static final int EXIT_GAME_CRASHED- See Also:
-
COMMANDLINE_ARG_RELEASE
- See Also:
-
COMMANDLINE_ARG_NOGUI
- See Also:
-
-
Method Details
-
addGameListener
Adds the specified game listener to receive events about the basic game life-cycle.- Parameters:
listener- The listener to add.
-
removeGameListener
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
COMMANDLINE_ARG_RELEASEwhen 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 theGameWindowor not. This can only be set before the game has been initialized with theGame.init(String...)method. Afterwards it doesn't have an effect anymore. If enabled, theScreenManager#setVisible(boolean)method won't be set to true and theRenderLoopwon't be started. Also theCamerawon't be updated.- Parameters:
noGui- If set to true, the GUI will be hidden.- See Also:
-
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:
-
isInNoGUIMode
public static boolean isInNoGUIMode()Indicates whether the game should display theGameWindowor 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:
-
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:
-
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:
-
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=truein the config.settings.- Returns:
- Metrics about the game's runtime.
- See Also:
-
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:
-
window
Gets the game's window in which theRenderComponentlives.
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:
-
audio
Gets the engine'sSoundEnginecomponent that can be used to play sounds and music.
Sound can be loaded and accessed using theResourcesAPI and are managed by the
Resources.sounds()resource container.Upon playing a sound, the engine returns an
SoundPlaybackinstance that can then be used to further control the audio line.- Returns:
- The engine's
SoundEnginecomponent. - See Also:
-
physics
Gets the engine'sPhysicsEnginecomponent 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
Rectanglesthat can further restrict the game world.- Returns:
- The engine's
PhysicsEnginecomponent. - See Also:
-
graphics
Gets the engine'sRenderEnginecomponent that is used to renderImages, Shapes or Textwith respect to the environment and the render scale and theCamera.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
Rendererimplementations.- Returns:
- The engine's
RenderEnginecomponent. - See Also:
-
loop
Gets the game's main loop that is used to execute and manage all game logic apart from input processing.
You can attach anyUpdatableinstance 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 theGraphics2Dobject to allGuiComponentsand 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:
-
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
Gets the game's pseudo-random generator that enhances the default JavaRandomimplementation with helpful additions.- Returns:
- The game's pseudo random generator.
-
screens
Gets the game'sScreenManagerthat is responsible for organizing allScreensof your game and providing the currently activeScreenthat is used to render the currentEnvironment.
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:
-
world
Gets the game's world which is a global environment manager that contains allEnvironmentsand provides the currently activeEnvironmentandCamera.
The
GameWorldreturns the same instance for a particular map/mapName until theGameWorld.reset(String)method is called.Moreover, it provides the possibility to attach game logic via
EnvironmentListenersto different events of theEnvrionment'slife 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:
-
tweens
Gets the game's Tween manager that holds all currently active Tween instances.- Returns:
- The game's Tween manager.
-
init
- Parameters:
preInitialization- a runnable used to prepare the game for initialization. This runnable will be started in the Swing Event Dispatch Thread.postInitialization- a runnable used to setup the game directly after initialization. This runnable will be started in the Swing Event Dispatch Thread.args- The arguments passed to the program's entry point.- Throws:
AWTError- if this method is called on the Swing Event Dispatcher thread
-
init
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
Localeaccording to the configured values. - initialize and attach core components like the
PhysicsEngine - initialize the
ScreenManger - initialize the
Input - initialize the
GameLoopandRenderLoop - set a default
Camera
- Parameters:
args- The arguments passed to the programs entry point.
- load the
-
init
Initializes the infrastructure of the LITIENGINE game.- Parameters:
initInSwingThread- used to determine if the game should be launched in the swing worker thread. This should be true in most circumstances, unless you're already in the swing worker thread, in which case you MUST supply false.args-- See Also:
-
setUncaughtExceptionHandler
public static void setUncaughtExceptionHandler(Thread.UncaughtExceptionHandler uncaughtExceptionHandler) Sets anUncaughtExceptionHandlerused 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
GameLoopsand other components. After this method is called, the engine will start to render contents of the currentScreenof theScreenManager, theSoundEnginewill start to playbackSoundsand the different input devices (e.g.Mouse,Keyboard) will start to process player input.When the
Gamehas started up successfully, it'll callback to the registeredGameListeners.- See Also:
-
exit
public static void exit() -
setInfo
Sets theGame'sbasic information by the specifiedGameInfoinstance.Typically, this should not be called manually because the
Gamealready provides aGameInfoobject which can be adjusted.
If you just want to edit some of it's information, use the provided instance ofinfo().- Parameters:
info- TheGameInfothat contains the basic information for the game.- See Also:
-
setInfo
Sets theGame'sbasic information by loading theGameInfofrom the specified path to an XML file.- Parameters:
gameInfoFile- The path to the XML file that contains the serializedGameInfo.- See Also:
-
setInfo
-