Class GameWorld

java.lang.Object
de.gurkenlabs.litiengine.environment.GameWorld
All Implemented Interfaces:
IUpdateable

public final class GameWorld
extends java.lang.Object
implements IUpdateable
The 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.

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.
See Also:
Environment, Camera, environment(), camera(), reset(String)
  • Constructor Summary

    Constructors 
    ConstructorDescription
    GameWorld()
    You should never call this manually! Instead use the Game.world() instance.
  • Method Summary

    Modifier and TypeMethodDescription
    voidaddListener​(EnvironmentListener listener)
    Adds the specified environment listener to receive events about the basic life-cycle of environments.
    voidaddListener​(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.
    voidattach​(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.
    ICameracamera()
    Gets the game's current Camera.
    voidclear()
    Clears the currently active camera and environment, removes all previously loaded environments and clears all listener lists.
    booleancontainsEnvironment​(java.lang.String mapName)
    Indicates whether this instance already contains an Environment for the specified map name.
    voiddetach​(java.lang.String mapName, IUpdateable updateable)
    Detaches the specified updatable instance from the updating of environments with the specified map name.
    Environmentenvironment()
    Gets the game's current Environment.
    EnvironmentgetEnvironment​(IMap map)
    Gets the environment that's related to the specified map.
    This method implicitly creates a new Environment if necessary.
    EnvironmentgetEnvironment​(java.lang.String mapName)
    Gets the environment that's related to the specified mapName.
    This method implicitly creates a new Environment if necessary.
    java.util.Collection<Environment>getEnvironments()
    Gets all environments that are known to the game world.
    intgravity()
    Gets the game worlds gravity that is applied to any environment.
    voidloadEnvironment​(Environment env)
    Loads the specified Environment and sets it as current environment of the game.
    EnvironmentloadEnvironment​(IMap map)
    Loads the Environment that is identified by the specified map and sets it as current environment of the game.
    EnvironmentloadEnvironment​(java.lang.String mapName)
    Loads the Environment that is identified by the specified map name and sets it as current environment of the game.
    voidonLoaded​(EnvironmentLoadedListener listener)
    Adds the specified environment loaded listener to receive events for when an environment gets loaded.
    voidonLoaded​(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.
    voidonUnloaded​(EnvironmentUnloadedListener listener)
    Adds the specified environment unloaded listener to receive events for when an environment gets unloaded.
    voidonUnloaded​(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.
    voidremoveListener​(EnvironmentListener listener)
    Removes the specified environment listener.
    voidremoveListener​(java.lang.String mapName, EnvironmentListener listener)
    Removes the specified environment listener.
    voidremoveLoadedListener​(EnvironmentLoadedListener listener)
    Removes the specified environment loaded listener.
    voidremoveLoadedListener​(java.lang.String mapName, EnvironmentLoadedListener listener)
    Removes the specified environment loaded listener for the specified map name.
    voidremoveUnloadedListener​(EnvironmentUnloadedListener listener)
    Removes the specified environment unloaded listener.
    voidremoveUnloadedListener​(java.lang.String mapName, EnvironmentUnloadedListener listener)
    Removes the specified environment unloaded listener for the specified map name.
    Environmentreset​(IMap map)
    Resets the previously loaded Environment for the specified map so that it can be re-initiated upon the next access.
    Environmentreset​(java.lang.String mapName)
    Resets the previously loaded Environment for the specified map name so that it can be re-initiated upon the next access.
    voidsetCamera​(ICamera cam)
    Sets the active camera of the game.
    voidsetGravity​(int gravity)
    Specify the general gravity that will be used as default value for all environments that are loaded.
    voidunloadEnvironment()
    Unloads the current Environment and sets it to null.
    voidupdate()
    Don't call this manually!

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 interface IUpdateable
      See Also:
      ClientConfiguration.setMaxFps(int)
    • addListener

      public void addListener​(EnvironmentListener listener)
      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

      public void removeListener​(EnvironmentListener listener)
      Removes the specified environment listener.
      Parameters:
      listener - The listener to remove.
    • onLoaded

      public void onLoaded​(EnvironmentLoadedListener listener)
      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

      public void removeLoadedListener​(EnvironmentLoadedListener listener)
      Removes the specified environment loaded listener.
      Parameters:
      listener - The listener to remove.
    • onUnloaded

      public void onUnloaded​(EnvironmentUnloadedListener listener)
      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

      public void removeUnloadedListener​(EnvironmentUnloadedListener listener)
      Removes the specified environment unloaded listener.
      Parameters:
      listener - The listener to remove.
    • onLoaded

      public 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.
      Parameters:
      mapName - The name of the map for which to add the listener.
      listener - The listener to add.
    • removeLoadedListener

      public void removeLoadedListener​(java.lang.String mapName, EnvironmentLoadedListener listener)
      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

      public 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.
      Parameters:
      mapName - The name of the map for which to add the listener.
      listener - The listener to add.
    • removeUnloadedListener

      public void removeUnloadedListener​(java.lang.String mapName, EnvironmentUnloadedListener listener)
      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

      public 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.
      Parameters:
      mapName - The name of the map for which to add the listener.
      listener - The listener to add.
    • removeListener

      public void removeListener​(java.lang.String mapName, EnvironmentListener listener)
      Removes the specified environment listener.
      Parameters:
      mapName - The name of the map for which to remove the listener.
      listener - The listener to remove.
    • attach

      public 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.
      Parameters:
      mapName - The name of the map for which to attach the updatable instance.
      updateable - The updatable instance to attach.
    • detach

      public void detach​(java.lang.String mapName, IUpdateable updateable)
      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

      public ICamera camera()
      Gets the game's current Camera.
      Returns:
      The currently active camera.
      See Also:
      ICamera
    • environment

      public Environment environment()
      Gets the game's current Environment.
      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

      public java.util.Collection<Environment> getEnvironments()
      Gets all environments that are known to the game world.
      Returns:
      All known environments.
    • getEnvironment

      public Environment getEnvironment​(java.lang.String mapName)
      Gets the environment that's related to the specified mapName.
      This method implicitly creates a new Environment 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

      public Environment getEnvironment​(IMap map)
      Gets the environment that's related to the specified map.
      This method implicitly creates a new Environment 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 an Environment 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

      public void loadEnvironment​(Environment env)
      Loads the specified Environment 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

      public Environment loadEnvironment​(java.lang.String mapName)
      Loads the Environment 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

      public Environment loadEnvironment​(IMap map)
      Loads the Environment 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 current Environment and sets it to null.
    • reset

      public Environment reset​(java.lang.String mapName)
      Resets the previously loaded Environment 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

      public Environment reset​(IMap map)
      Resets the previously loaded Environment 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

      public void setCamera​(ICamera cam)
      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 a IMobileEntity.
      Parameters:
      gravity - The default gravity for all environments.
      See Also:
      IMobileEntity.getVelocity()