Class 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 Type Field Description static java.lang.String
COMMADLINE_ARG_NOGUI
static java.lang.String
COMMADLINE_ARG_RELEASE
static int
EXIT_GAME_CLOSED
static int
EXIT_GAME_CRASHED
Method Summary
Modifier and Type Method Description static void
addGameListener(GameListener listener)
Adds the specified game listener to receive events about the basic game life-cycle.static void
allowDebug(boolean allow)
This flag indicates if the game currently supports debugging.static SoundEngine
audio()
Gets the engine'sSoundEngine
component that can be used to play sounds and music.
Sound can be loaded and accessed using theResources
API and are managed by the
Resources.sounds()
resource container.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.static void
exit()
static RenderEngine
graphics()
Gets the engine'sRenderEngine
component that is used to renderImages, Shapes or Text
with respect to the environment and the render scale and theCamera
.static boolean
hasStarted()
Indicates whether the game has already been started.static void
hideGUI(boolean noGui)
This flag indicates whether the game should display theGameWindow
or not.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")
.static void
init(java.lang.String... args)
Initializes the infrastructure of the LITIENGINE game.static boolean
isDebug()
This flag globally controls the game's debugging state.static boolean
isInNoGUIMode()
Indicates whether the game should display theGameWindow
or not.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.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 anyUpdatable
instance to this loop if you want to execute custom game logic that is executed at the configured max fps.static GameMetrics
metrics()
Gets basic client metrics about the game's runtime.static PhysicsEngine
physics()
Gets the engine'sPhysicsEngine
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 GameRandom
random()
Gets the game's pseudo-random generator that enhances the default JavaRandom
implementation with helpful additions.static void
removeGameListener(GameListener listener)
Removes the specified game listener.static ScreenManager
screens()
Gets the game'sScreenManager
that is responsible for organizing allScreens
of your game and providing the currently activeScreen
that 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 void
setInfo(GameInfo info)
Sets theGame's
basic information by the specifiedGameInfo
instance.static void
setInfo(java.lang.String gameInfoFile)
Sets theGame's
basic information by loading theGameInfo
from the specified path to an XML file.static void
setInfo(java.net.URL gameInfoFile)
static void
setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler)
Sets anUncaughtExceptionHandler
used to handle all unexpected exceptions happening in the game.static void
start()
Starts theGameLoops
and other components.static GameTime
time()
Gets time information about the running game/environment.static TweenEngine
tweens()
Gets the game's Tween manager that holds all currently active Tween instances.static GameWindow
window()
Gets the game's window in which theRenderComponent
lives.
This class e.g.static GameWorld
world()
Gets the game's world which is a global environment manager that contains allEnvironments
and provides the currently activeEnvironment
andCamera
.
Field Details
EXIT_GAME_CLOSED
public static final int EXIT_GAME_CLOSED- See Also:
- Constant Field Values
EXIT_GAME_CRASHED
public static final int EXIT_GAME_CRASHED- See Also:
- Constant Field Values
COMMADLINE_ARG_RELEASE
public static final java.lang.String COMMADLINE_ARG_RELEASE- See Also:
- Constant Field Values
COMMADLINE_ARG_NOGUI
public static final java.lang.String COMMADLINE_ARG_NOGUI- See Also:
- Constant Field Values
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 argumentCOMMADLINE_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 theGameWindow
or 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 theRenderLoop
won't be started. Also theCamera
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 theGameWindow
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
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
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
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
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
Gets the game's window in which theRenderComponent
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
Gets the engine'sSoundEngine
component that can be used to play sounds and music.
Sound can be loaded and accessed using theResources
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
Gets the engine'sPhysicsEngine
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
Gets the engine'sRenderEngine
component that is used to renderImages, Shapes or Text
with 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
Renderer
implementations.- Returns:
- The engine's
RenderEngine
component. - See Also:
RenderEngine.getBaseRenderScale()
,TextRenderer
,ShapeRenderer
,ImageRenderer
loop
Gets the game's main loop that is used to execute and manage all game logic apart from input processing.
You can attach anyUpdatable
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'sRenderComponent
.
This internally renders the currently active screen which passes theGraphics2D
object to allGuiComponents
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
Gets the game's pseudo-random generator that enhances the default JavaRandom
implementation with helpful additions.- Returns:
- The game's pseudo random generator.
screens
Gets the game'sScreenManager
that is responsible for organizing allScreens
of your game and providing the currently activeScreen
that 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:
Screen
,GameWorld.environment()
,world()
world
Gets the game's world which is a global environment manager that contains allEnvironments
and provides the currently activeEnvironment
andCamera
.The
Moreover, it provides the possibility to attach game logic viaGameWorld
returns the same instance for a particular map/mapName until theGameWorld.reset(String)
method is called.EnvironmentListeners
to different events of theEnvrionment'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
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
andRenderLoop
- set a default
Camera
- Parameters:
args
- The arguments passed to the programs entry point.
- load the
setUncaughtExceptionHandler
public static void setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler uncaughtExceptionHandler)Sets anUncaughtExceptionHandler
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 currentScreen
of theScreenManager
, theSoundEngine
will start to playbackSounds
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 registeredGameListeners
.exit
public static void exit()setInfo
Sets theGame's
basic information by the specifiedGameInfo
instance.Typically, this should not be called manually because the
Game
already provides aGameInfo
object which can be adjusted.
If you just want to edit some of it's information, use the provided instance ofinfo()
.setInfo
public static void setInfo(java.lang.String gameInfoFile)Sets theGame's
basic information by loading theGameInfo
from the specified path to an XML file.- Parameters:
gameInfoFile
- The path to the XML file that contains the serializedGameInfo
.- See Also:
setInfo(GameInfo)
,info()
,GameInfo
setInfo
public static void setInfo(java.net.URL gameInfoFile)