Class Entity

All Implemented Interfaces:
EventDispatcherContext<EntityEvent>, Lifecycle, Printable, ApplicationContext, ApplicationProvider, ComponentsContext, EntityContext, SceneContext, SceneProvider

Represents a unique entity in the world with a list of components.
  • Field Details

    • id

      public final int id
    • tag

      public String tag
    • isActive

      protected boolean isActive
    • transform

      public final Transform transform
    • world

      protected final World world
    • application

      protected final Application application
    • logger

      protected final Logger logger
    • scene

      protected final Scene scene
    • components

      public final List<Component> components
      Components of this entity
  • Constructor Details

    • Entity

      public Entity(World world)
      Creates an entity at the position (0, 0)
    • Entity

      public Entity(World world, String name)
      Creates an entity at the position (0, 0)
    • Entity

      public Entity(World world, float x, float y)
      Creates an entity at the position (x, y)
    • Entity

      public Entity(World world, String name, float x, float y)
      Creates an entity at the position (x, y)
  • Method Details

    • destroyChildren

      public void destroyChildren()
    • destroy

      public void destroy()
      Destroys this entity at the end of the game loop.
      Specified by:
      destroy in interface Lifecycle
      Overrides:
      destroy in class EventDispatcher<EntityEvent>
    • addChild

      public Entity addChild(Prefab prefab) throws IllegalStateException, IllegalArgumentException
      Instantiates a prefab and adds the instance as a child of this entity
      Parameters:
      prefab - Prefab for the child entity
      Returns:
      Child entity
      Throws:
      IllegalStateException
      IllegalArgumentException
    • addChild

      Adds a child to this entity. Also adds the child to the world if this entity is inside the world.
      Parameters:
      child - Entity to add as a child
      Returns:
      Child entity
      Throws:
      IllegalStateException
      IllegalArgumentException
    • addChildren

      public void addChildren(Entity... children) throws IllegalStateException, IllegalArgumentException
      Adds children to this entity.
      Parameters:
      children - Child objects
      Throws:
      IllegalStateException
      IllegalArgumentException
    • removeChild

      public void removeChild(Entity child) throws IllegalStateException, IllegalArgumentException
      Detaches a child from this entity without removing it from the world.
      Parameters:
      child - Child object
      Throws:
      IllegalStateException
      IllegalArgumentException
    • removeChildren

      public void removeChildren(Entity... children) throws IllegalStateException, IllegalArgumentException
      Detaches children from this entity without removing them from the world.
      Parameters:
      children - Child entities
      Throws:
      IllegalStateException
      IllegalArgumentException
    • removeAllChildren

      public void removeAllChildren()
    • getFirstChild

      public Entity getFirstChild()
      Specified by:
      getFirstChild in interface EntityContext
    • getLastChild

      public Entity getLastChild()
      Specified by:
      getLastChild in interface EntityContext
    • isDescendantOf

      public boolean isDescendantOf(Transform parent)
      Specified by:
      isDescendantOf in interface EntityContext
    • getChildWithTag

      public Entity getChildWithTag(String tag)
      Description copied from interface: EntityContext
      Gets a child entity with a given tag.
      Specified by:
      getChildWithTag in interface EntityContext
      Parameters:
      tag - Tag of the child entity.
      Returns:
      The child entity with the given tag, or null if there isn't one.
    • getParentWithTag

      public Entity getParentWithTag(String tag)
      Description copied from interface: EntityContext
      Gets a parent entity with a given tag.
      Specified by:
      getParentWithTag in interface EntityContext
      Parameters:
      tag - Tag of the parent entity.
      Returns:
      The parent entity with the given tag, or null if there isn't one.
    • setParent

      public void setParent(Entity parent)
      Setter for the parent entity.
      Parameters:
      parent - Parent entity
    • setActive

      public void setActive(boolean active)
      Toggles the active state of this entity.
    • isActive

      public boolean isActive()
    • addComponent

      public <C extends Component> C addComponent(C component)
      Adds a component to this entity.
      Parameters:
      component - Component
    • removeComponent

      public void removeComponent(Component component)
      Removes a component from this entity.
      Parameters:
      component - Component
    • getComponentInParent

      public <ComponentType extends Component> ComponentType getComponentInParent(Class<ComponentType> componentClass)
      Specified by:
      getComponentInParent in interface ComponentsContext
    • getComponentInParent

      public <ComponentType extends Component> ComponentType getComponentInParent(Class<ComponentType> componentClass, boolean includeAncestors)
      Specified by:
      getComponentInParent in interface ComponentsContext
    • getComponentsInChildren

      public <ComponentType extends Component> List<ComponentType> getComponentsInChildren(Class<ComponentType> componentClass)
      Specified by:
      getComponentsInChildren in interface ComponentsContext
    • hasComponent

      public <ComponentType extends Component> boolean hasComponent(Class<ComponentType> componentClass)
      Checks if this entity has a component of a given class.
      Parameters:
      componentClass - Class of the component.
      Returns:
      True if this entity has a component of type componentClass.
    • getComponent

      public <ComponentType extends Component> ComponentType getComponent(Class<ComponentType> componentClass)
      Returns a component of a given class or null of there isn't one.
      Specified by:
      getComponent in interface ComponentsContext
      Parameters:
      componentClass - Class of the component.
      Returns:
      An instance of componentClass that is attached to this entity.
    • getComponents

      public <ComponentType extends Component> List<ComponentType> getComponents(Class<ComponentType> componentClass)
      Returns all components of a given class.
      Specified by:
      getComponents in interface ComponentsContext
      Parameters:
      componentClass - Class of the components.
      Returns:
      An ArrayList of instance of componentClass that are attached to this entity.
    • getName

      public String getName()
    • getName

      public String getName(String defaultName)
    • hasTag

      public boolean hasTag(String tag)
      Checks whether this entity has a given tag.
    • getApplication

      public Application getApplication()
      Specified by:
      getApplication in interface ApplicationProvider
    • getScene

      public Scene getScene()
      Specified by:
      getScene in interface SceneProvider
    • isRegistered

      public boolean isRegistered()
      Checks whether this entity is registered in the entity manager.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Entity entity)
      Checks whether two entities are equal.
      Parameters:
      entity - Other entity
      Returns:
      True if the entities are equal
    • printHierarchy

      public void printHierarchy()
      Formats and prints the names of this entity's parents.
    • toString

      public String toString()
      Description copied from interface: Printable
      Returns a string representation of this object.
      Specified by:
      toString in interface Printable
      Overrides:
      toString in class Object
      Returns:
      String representation of this object.