Class GameWorld
- All Implemented Interfaces:
IUpdateable
public final class GameWorld
extends java.lang.Object
implements IUpdateable
GameWorld
class 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.
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.
- See Also:
Environment
,Camera
,environment()
,camera()
,reset(String)
Constructor Summary
Constructors Constructor Description GameWorld()
You should never call this manually! Instead use theGame.world()
instance.Method Summary
Modifier and Type Method Description void
addListener(EnvironmentListener listener)
Adds the specified environment listener to receive events about the basic life-cycle of environments.void
addListener(java.lang.String mapName, EnvironmentListener listener)
Adds the specified environment listener to receive events about the basic life-cycle of environments with the specified map name.void
attach(java.lang.String mapName, IUpdateable updateable)
Attaches the specified updatable instance that only gets updated when an environment with the specified map name is currently loaded.ICamera
camera()
Gets the game's currentCamera
.void
clear()
Clears the currently active camera and environment, removes all previously loaded environments and clears all listener lists.boolean
containsEnvironment(java.lang.String mapName)
Indicates whether this instance already contains anEnvironment
for the specified map name.void
detach(java.lang.String mapName, IUpdateable updateable)
Detaches the specified updatable instance from the updating of environments with the specified map name.Environment
environment()
Gets the game's currentEnvironment
.Environment
getEnvironment(IMap map)
Gets the environment that's related to the specified map.
This method implicitly creates a newEnvironment
if necessary.Environment
getEnvironment(java.lang.String mapName)
Gets the environment that's related to the specified mapName.
This method implicitly creates a newEnvironment
if necessary.java.util.Collection<Environment>
getEnvironments()
Gets all environments that are known to the game world.int
gravity()
Gets the game worlds gravity that is applied to any environment.void
loadEnvironment(Environment env)
Loads the specifiedEnvironment
and sets it as current environment of the game.Environment
loadEnvironment(IMap map)
Loads theEnvironment
that is identified by the specified map and sets it as current environment of the game.Environment
loadEnvironment(java.lang.String mapName)
Loads theEnvironment
that is identified by the specified map name and sets it as current environment of the game.void
onLoaded(EnvironmentLoadedListener listener)
Adds the specified environment loaded listener to receive events for when an environment gets loaded.void
onLoaded(java.lang.String mapName, EnvironmentLoadedListener listener)
Adds the specified environment loaded listener to receive events for when an environment with the specified map name gets loaded.void
onUnloaded(EnvironmentUnloadedListener listener)
Adds the specified environment unloaded listener to receive events for when an environment gets unloaded.void
onUnloaded(java.lang.String mapName, EnvironmentUnloadedListener listener)
Adds the specified environment unloaded listener to receive events for when an environment with the specified map name gets unloaded.void
removeListener(EnvironmentListener listener)
Removes the specified environment listener.void
removeListener(java.lang.String mapName, EnvironmentListener listener)
Removes the specified environment listener.void
removeLoadedListener(EnvironmentLoadedListener listener)
Removes the specified environment loaded listener.void
removeLoadedListener(java.lang.String mapName, EnvironmentLoadedListener listener)
Removes the specified environment loaded listener for the specified map name.void
removeUnloadedListener(EnvironmentUnloadedListener listener)
Removes the specified environment unloaded listener.void
removeUnloadedListener(java.lang.String mapName, EnvironmentUnloadedListener listener)
Removes the specified environment unloaded listener for the specified map name.Environment
reset(IMap map)
Resets the previously loadedEnvironment
for the specified map so that it can be re-initiated upon the next access.Environment
reset(java.lang.String mapName)
Resets the previously loadedEnvironment
for the specified map name so that it can be re-initiated upon the next access.void
setCamera(ICamera cam)
Sets the active camera of the game.void
setGravity(int gravity)
Specify the general gravity that will be used as default value for all environments that are loaded.void
unloadEnvironment()
Unloads the currentEnvironment
and sets it to null.void
update()
Don't call this manually!
Constructor Details
GameWorld
public GameWorld()You should never call this manually! Instead use the
Game.world()
instance.- See Also:
Game.world()
Method Details
update
public void update()Don't call this manually!- Specified by:
update
in interfaceIUpdateable
- See Also:
ClientConfiguration.setMaxFps(int)
addListener
Adds the specified environment listener to receive events about the basic life-cycle of environments. This is a global event that gets called for any map.- Parameters:
listener
- The listener to add.
removeListener
Removes the specified environment listener.- Parameters:
listener
- The listener to remove.
onLoaded
Adds the specified environment loaded listener to receive events for when an environment gets loaded. This is a global event that gets called for any map.- Parameters:
listener
- The listener to add.
removeLoadedListener
Removes the specified environment loaded listener.- Parameters:
listener
- The listener to remove.
onUnloaded
Adds the specified environment unloaded listener to receive events for when an environment gets unloaded. This is a global event that gets called for any map.- Parameters:
listener
- The listener to add.
removeUnloadedListener
Removes the specified environment unloaded listener.- Parameters:
listener
- The listener to remove.
onLoaded
Adds the specified environment loaded listener to receive events for when an environment with the specified map name gets loaded.- Parameters:
mapName
- The name of the map for which to add the listener.listener
- The listener to add.
removeLoadedListener
Removes the specified environment loaded listener for the specified map name.- Parameters:
mapName
- The name of the map for which to remove the listener.listener
- The listener to remove.
onUnloaded
Adds the specified environment unloaded listener to receive events for when an environment with the specified map name gets unloaded.- Parameters:
mapName
- The name of the map for which to add the listener.listener
- The listener to add.
removeUnloadedListener
Removes the specified environment unloaded listener for the specified map name.- Parameters:
mapName
- The name of the map for which to remove the listener.listener
- The listener to remove.
addListener
Adds the specified environment listener to receive events about the basic life-cycle of environments with the specified map name.- Parameters:
mapName
- The name of the map for which to add the listener.listener
- The listener to add.
removeListener
Removes the specified environment listener.- Parameters:
mapName
- The name of the map for which to remove the listener.listener
- The listener to remove.
attach
Attaches the specified updatable instance that only gets updated when an environment with the specified map name is currently loaded.- Parameters:
mapName
- The name of the map for which to attach the updatable instance.updateable
- The updatable instance to attach.
detach
Detaches the specified updatable instance from the updating of environments with the specified map name.- Parameters:
mapName
- The name of the map for which to detach the updatable instance.updateable
- The updatable instance to detach.
camera
Gets the game's currentCamera
.- Returns:
- The currently active camera.
- See Also:
ICamera
environment
Gets the game's currentEnvironment
.- Returns:
- The currently active environment.
- See Also:
Environment
gravity
public int gravity()Gets the game worlds gravity that is applied to any environment. This can e.g. be useful for platformers.- Returns:
- The gravity of the game world that gets applied to any environment.
clear
public void clear()Clears the currently active camera and environment, removes all previously loaded environments and clears all listener lists.getEnvironments
Gets all environments that are known to the game world.- Returns:
- All known environments.
getEnvironment
Gets the environment that's related to the specified mapName.
This method implicitly creates a newEnvironment
if necessary.- Parameters:
mapName
- The map name by which the environment is identified.- Returns:
- The environment for the map name or null if no such map can be found.
getEnvironment
Gets the environment that's related to the specified map.
This method implicitly creates a newEnvironment
if necessary.- Parameters:
map
- The map by which the environment is identified.- Returns:
- The environment for the map or null if no such map can be found.
containsEnvironment
public boolean containsEnvironment(java.lang.String mapName)Indicates whether this instance already contains anEnvironment
for the specified map name.- Parameters:
mapName
- The map name by which the environment is identified.- Returns:
- True if the game world already has an environment for the specified map name; otherwise false.
loadEnvironment
Loads the specifiedEnvironment
and sets it as current environment of the game. This implicitly unloads the previously loaded environment (if present).The loaded environment can then be accessed via
GameWorld#environment()
.- Parameters:
env
- The environment to be loaded.- See Also:
environment()
loadEnvironment
Loads theEnvironment
that is identified by the specified map name and sets it as current environment of the game. This implicitly unloads the previously loaded environment (if present).The loaded environment can then be accessed via
GameWorld#environment()
.- Parameters:
mapName
- The map name by which the environment is identified.- Returns:
- The loaded environment.
- See Also:
environment()
,loadEnvironment(Environment)
loadEnvironment
Loads theEnvironment
that is identified by the specified map and sets it as current environment of the game. This implicitly unloads the previously loaded environment (if present).The loaded environment can then be accessed via
GameWorld#environment()
.- Parameters:
map
- The map by which the environment is identified.- Returns:
- The loaded environment.
- See Also:
environment()
,loadEnvironment(Environment)
unloadEnvironment
public void unloadEnvironment()Unloads the currentEnvironment
and sets it to null.reset
Resets the previously loadedEnvironment
for the specified map name so that it can be re-initiated upon the next access.This can be used if one wants to completely reset the state of a level to its initial state. It'll just throw away the current environment instance and reload a new one upon the next access.
- Parameters:
mapName
- The map name by which the environment is identified.- Returns:
- The environment instance that was reset or null if none was previously loaded.
- See Also:
getEnvironment(String)
,reset(IMap)
reset
Resets the previously loadedEnvironment
for the specified map so that it can be re-initiated upon the next access.This can be used if one wants to completely reset the state of a level to its initial state. It'll just throw away the current environment instance and reload a new one upon the next access.
- Parameters:
map
- The map by which the environment is identified.- Returns:
- The environment instance that was reset or null if none was previously loaded.
- See Also:
getEnvironment(String)
,reset(IMap)
setCamera
Sets the active camera of the game.- Parameters:
cam
- The new camera to be set.
setGravity
public void setGravity(int gravity)Specify the general gravity that will be used as default value for all environments that are loaded. The value's unit of measure is pixel/second (similar to the velocity of aIMobileEntity
.- Parameters:
gravity
- The default gravity for all environments.- See Also:
IMobileEntity.getVelocity()