### Navigation

- index
- modules |
- next |
- previous |
- Grease v0.2 documentation »

# `grease.world` – Entity Environment

Worlds are environments described by a configuration of components, systems and renderers. These parts describe the data, behavioral and presentation aspects of the world respectively.

The world environment is the context within which entities exist. A typical application consists of one or more worlds containing entities that evolve over time and react to internal and external interaction.

See an example of world configuration in the tutorial.

class `grease.world.``World`(step_rate=60, master_clock=pyglet.clock)

  A coordinated collection of components, systems and entities

  A world is also a mode that may be pushed onto a `grease.mode.Manager`

  Parameters: | step_rate – The rate of `step()` calls per second. master_clock – The `pyglet.clock.Clock` interface used as the master clock that ticks the world’s clock. This defaults to the main pyglet clock.

  `__getitem__`(entity_class)

    Return an `EntityExtent` for the given entity class. This extent can be used to access the set of entities of that class in the world or to query these entities via their components.

    Examples:

        world[MyEntity]
        world[...]

    Parameter: | entity_class – The entity class for the extent. May also be a tuple of entity classes, in which case the extent returned contains union of all entities of the classes in the world. May also be the special value ellipsis (`...`), which returns an extent containing all entities in the world. This allows you to conveniently query all entities using `world[...]`.

  `activate`(manager)

    Activate the world/mode for the given manager, if the world is already active, do nothing. This method is typically not used directly, it is called automatically by the mode manager when the world becomes active.

    The systems of the world are pushed onto manager.event_dispatcher so they can receive system events.

    Parameter: | manager – `mode.BaseManager` instance

  `configure`()

    Hook to configure the world after construction. This method is called immediately after the world is initialized. Override in a subclass to configure the world’s components, systems, and renderers.

    The default implementation does nothing.

  `deactivate`(manager)

    Deactivate the world/mode, if the world is not active, do nothing. This method is typically not used directly, it is called automatically by the mode manager when the world becomes active.

    Removes the system handlers from the manager.event_dispatcher

    Parameter: | manager – `mode.BaseManager` instance

  `on_draw`(gl=<module 'pyglet.gl' from '/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/pyglet/gl/__init__.pyc'>)
    Clear the current OpenGL context, reset the model/view matrix and invoke the draw() methods of the renderers in order
  `step`(dt)

    Execute a time step for the world. Updates the world time and invokes the world’s systems.

    Note that the specified time delta will be pinned to 10x the configured step rate. For example if the step rate is 60, then dt will be pinned at a maximum of 0.1666. This avoids pathological behavior when the time between steps goes much longer than expected.

    Parameter: | dt (float) – The time delta since the last time step

  `tick`(dt)

    Tick the mode’s clock, but only if the world is currently running

    Parameter: | dt (float) – The time delta since the last tick

  `clock`
    `pyglet.clock` interface for use by constituents of the world for scheduling
  `components`
    `ComponentParts` object containing all world components. `grease.component.Component` objects define and contain all entity data
  `entities`
    Set of all entities that exist in the world
  `renderers`
    `Parts` object containing all world renderers. `grease.Renderer` objects define world presentation
  `running`
    Flag to indicate that the world clock is running, advancing time and stepping the world. Set running to False to pause the world.
  `systems`
    `Parts` object containing all world systems. `grease.System` objects define world and entity behavior
  `time`
    Current clock time of the world, starts at 0 when the world is instantiated

class `grease.world.``Parts`(world)

  Maps world parts to attributes. The parts are kept in the order they are set. Parts may also be inserted out of order.

  Used for:

  - `World.systems`
  - `World.renderers`
  `insert`(name, part, before=None, index=None)

    Add a part with a particular name at a particular index. If a part by that name already exists, it is replaced.

    Parameters: | name (str) – The name of the part. part – The component, system, or renderer part to insert before – A part object or name. If specified, the part is inserted before the specified part in order. index (int) – If specified, the part is inserted in the position specified. You cannot specify both before and index.

class `grease.world.``ComponentParts`(world)

  Maps world components to attributes. The components are kept in the order they are set. Components may also be inserted out of order.

  Used for: `World.components`

  `insert`(name, part, before=None, index=None)

    Add a part with a particular name at a particular index. If a part by that name already exists, it is replaced.

    Parameters: | name (str) – The name of the part. part – The component, system, or renderer part to insert before – A part object or name. If specified, the part is inserted before the specified part in order. index (int) – If specified, the part is inserted in the position specified. You cannot specify both before and index.

  `join`(*component_names)

    Join and iterate entity data from multiple components together.

    For each entity in all of the components named, yield a tuple containing the entity data from each component specified.

    This is useful in systems that pull data from multiple components.

    Typical Usage:

        for position, movement in world.components.join("position", "movement"):
                # Do something with each entity's position and movement data

class `grease.world.``EntityExtent`(world, entities)

  Encapsulates a set of entities queriable by component. Extents are accessed by using an entity class as a key on the `World`:

      extent = world[MyEntity]

  `__getattr__`(name)

    Return a queriable `ComponentEntitySet` for the named component

    Example:

        world[MyEntity].movement.velocity > (0, 0)

    Returns a set of entities where the value of the `velocity` field of the `movement` component is greater than `(0, 0)`.

  `entities`
    The full set of entities in the extent

### Navigation

- index
- modules |
- next |
- previous |
- Grease v0.2 documentation »