Class Entity

java.lang.Object
tripleplay.entity.Entity
All Implemented Interfaces:
AutoCloseable, react.Closeable

public final class Entity extends Object implements react.Closeable
Tracks the state of a single entity. This includes its enabled state, as well as the components which are attached to this entity.
  • Nested Class Summary

    Nested classes/interfaces inherited from interface react.Closeable

    react.Closeable.Set, react.Closeable.Util
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    final int
    This entity's unique id.
    final World
    The world to which this entity belongs.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Entity(World world, int id)
    Creates an entity in the specified world, initializes it with the supplied set of components and queues it to be added to the world on the next update.
  • Method Summary

    Modifier and Type
    Method
    Description
    add(Component comp)
    Adds the specified component to this entity.
    add(Component[] comps)
    Adds the supplied components to this entity.
    add(Component c1, Component c2, Component... rest)
    Adds the specified components to this entity.
    void
    An alias for dispose().
    void
    Indicates that this entity has changed, and causes it to be reconsidered for inclusion or exclusion from systems on the next update.
    void
    Disposes this entity, causing it to be removed from the world on the next update.
    boolean
    has(Component comp)
    Returns true if this entity has the component comp, false otherwise.
    boolean
    Returns whether this entity has been disposed.
    boolean
    Returns whether this entity is currently enabled.
    Removes the specified component from this entity.
    void
    setEnabled(boolean enabled)
    Enables or disables this entity.
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • world

      public final World world
      The world to which this entity belongs.
    • id

      public final int id
      This entity's unique id. This id will be valid for as long as the entity remains alive. Once close() is called, this id may be reused by a new entity.
  • Constructor Details

    • Entity

      public Entity(World world, int id)
      Creates an entity in the specified world, initializes it with the supplied set of components and queues it to be added to the world on the next update.
  • Method Details

    • isDisposed

      public boolean isDisposed()
      Returns whether this entity has been disposed.
    • isEnabled

      public boolean isEnabled()
      Returns whether this entity is currently enabled.
    • setEnabled

      public void setEnabled(boolean enabled)
      Enables or disables this entity. When an entity is disabled, it is removed from all systems in which it is currently an active participant (prior to the next update). When it is re-enabled, it is added back to all systems that are interested in it (prior to the next update).
    • has

      public boolean has(Component comp)
      Returns true if this entity has the component comp, false otherwise.
    • add

      public Entity add(Component comp)
      Adds the specified component to this entity. This will queue the component up to be added or removed to appropriate systems on the next update.
      Returns:
      this entity for call chaining.
    • add

      public Entity add(Component c1, Component c2, Component... rest)
      Adds the specified components to this entity. This will queue the component up to be added or removed to appropriate systems on the next update. Note: this method uses varags and thus creates an array every time it is called. If you are striving to eliminate all unnecessary garbage generation, use repeated calls to add(Component), or add(Component[]) with a pre-allocated array.
      Returns:
      this entity for call chaining.
    • add

      public Entity add(Component[] comps)
      Adds the supplied components to this entity. This will queue the component up to be added or removed to appropriate systems on the next update. This avoids the garbage generation of the varags add method, and is slightly more efficient than a sequence of calls to add(Component). The expectation is that you would keep an array around with the components for a particular kind of entity, like so:
      
       Entity createFoo (...) {
         Entity foo = create(true).add(FOO_COMPS);
         // init foo...
         return foo;
       }
       private static final Component[] FOO_COMPS = { pos, vel, etc. };
       
      Returns:
      this entity for call chaining.
    • remove

      public Entity remove(Component comp)
      Removes the specified component from this entity. This will queue the component up to be added or removed to appropriate systems on the next update.
    • dispose

      public void dispose()
      Disposes this entity, causing it to be removed from the world on the next update.
    • close

      public void close()
      An alias for dispose(). Needed to implement Closeable.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface react.Closeable
    • didChange

      public void didChange()
      Indicates that this entity has changed, and causes it to be reconsidered for inclusion or exclusion from systems on the next update. This need not be called when adding or removing components, and should only be called if some other external circumstance changes that requires recalculation of which systems are interested in this entity.
    • toString

      public String toString()
      Overrides:
      toString in class Object