Package dev.prozilla.pine.core.entity
Class Entity
- All Implemented Interfaces:
EventDispatcherContext<EntityEvent>
,Lifecycle
,Printable
,ApplicationContext
,ApplicationProvider
,ComponentsContext
,EntityContext
,SceneContext
,SceneProvider
public class Entity
extends EventDispatcher<EntityEvent>
implements Lifecycle, Printable, EntityContext, ComponentsContext, ApplicationProvider, SceneProvider
Represents a unique entity in the world with a list of components.
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionCreates an entity at the position (0, 0)Creates an entity at the position (x, y)Creates an entity at the position (0, 0)Creates an entity at the position (x, y) -
Method Summary
Modifier and TypeMethodDescriptionAdds a child to this entity.Instantiates a prefab and adds the instance as a child of this entityvoid
addChildren
(Entity... children) Adds children to this entity.<C extends Component>
CaddComponent
(C component) Adds a component to this entity.void
destroy()
Destroys this entity at the end of the game loop.void
boolean
Checks whether two entities are equal.getChildWithTag
(String tag) Gets a child entity with a given tag.<ComponentType extends Component>
ComponentTypegetComponent
(Class<ComponentType> componentClass) Returns a component of a given class or null of there isn't one.<ComponentType extends Component>
ComponentTypegetComponentInParent
(Class<ComponentType> componentClass) <ComponentType extends Component>
ComponentTypegetComponentInParent
(Class<ComponentType> componentClass, boolean includeAncestors) getComponents
(Class<ComponentType> componentClass) Returns all components of a given class.getComponentsInChildren
(Class<ComponentType> componentClass) getName()
getParentWithTag
(String tag) Gets a parent entity with a given tag.getScene()
<ComponentType extends Component>
booleanhasComponent
(Class<ComponentType> componentClass) Checks if this entity has a component of a given class.int
hashCode()
boolean
Checks whether this entity has a given tag.boolean
isActive()
boolean
isDescendantOf
(Transform parent) boolean
Checks whether this entity is registered in the entity manager.void
Formats and prints the names of this entity's parents.void
void
removeChild
(Entity child) Detaches a child from this entity without removing it from the world.void
removeChildren
(Entity... children) Detaches children from this entity without removing them from the world.void
removeComponent
(Component component) Removes a component from this entity.void
setActive
(boolean active) Toggles the active state of this entity.void
Setter for the parent entity.toString()
Returns a string representation of this object.Methods inherited from class dev.prozilla.pine.common.event.EventDispatcher
addListener, invoke, removeListener, setLogger
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface dev.prozilla.pine.core.ApplicationProvider
getConfig, getInput, getLogger, getModManager, getRenderer, getTimer, getTracker, getWindow
Methods inherited from interface dev.prozilla.pine.core.entity.EntityContext
isDescendantOf
Methods inherited from interface dev.prozilla.pine.common.Lifecycle
init, init, input, input, render, render, start, update, update
Methods inherited from interface dev.prozilla.pine.core.scene.SceneProvider
getCameraData, getWorld
-
Field Details
-
id
public final int id -
tag
-
isActive
protected boolean isActive -
transform
-
world
-
application
-
logger
-
scene
-
components
Components of this entity
-
-
Constructor Details
-
Entity
Creates an entity at the position (0, 0) -
Entity
Creates an entity at the position (0, 0) -
Entity
Creates an entity at the position (x, y) -
Entity
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 interfaceLifecycle
- Overrides:
destroy
in classEventDispatcher<EntityEvent>
-
addChild
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
Adds children to this entity.- Parameters:
children
- Child objects- Throws:
IllegalStateException
IllegalArgumentException
-
removeChild
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
- Specified by:
getFirstChild
in interfaceEntityContext
-
getLastChild
- Specified by:
getLastChild
in interfaceEntityContext
-
isDescendantOf
- Specified by:
isDescendantOf
in interfaceEntityContext
-
getChildWithTag
Description copied from interface:EntityContext
Gets a child entity with a given tag.- Specified by:
getChildWithTag
in interfaceEntityContext
- Parameters:
tag
- Tag of the child entity.- Returns:
- The child entity with the given tag, or null if there isn't one.
-
getParentWithTag
Description copied from interface:EntityContext
Gets a parent entity with a given tag.- Specified by:
getParentWithTag
in interfaceEntityContext
- Parameters:
tag
- Tag of the parent entity.- Returns:
- The parent entity with the given tag, or null if there isn't one.
-
setParent
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
Adds a component to this entity.- Parameters:
component
- Component
-
removeComponent
Removes a component from this entity.- Parameters:
component
- Component
-
getComponentInParent
public <ComponentType extends Component> ComponentType getComponentInParent(Class<ComponentType> componentClass) - Specified by:
getComponentInParent
in interfaceComponentsContext
-
getComponentInParent
public <ComponentType extends Component> ComponentType getComponentInParent(Class<ComponentType> componentClass, boolean includeAncestors) - Specified by:
getComponentInParent
in interfaceComponentsContext
-
getComponentsInChildren
public <ComponentType extends Component> List<ComponentType> getComponentsInChildren(Class<ComponentType> componentClass) - Specified by:
getComponentsInChildren
in interfaceComponentsContext
-
hasComponent
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 interfaceComponentsContext
- 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 interfaceComponentsContext
- Parameters:
componentClass
- Class of the components.- Returns:
- An ArrayList of instance of
componentClass
that are attached to this entity.
-
getName
-
getName
-
hasTag
Checks whether this entity has a given tag. -
getApplication
- Specified by:
getApplication
in interfaceApplicationProvider
-
getScene
- Specified by:
getScene
in interfaceSceneProvider
-
isRegistered
public boolean isRegistered()Checks whether this entity is registered in the entity manager. -
hashCode
public int hashCode() -
equals
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
Description copied from interface:Printable
Returns a string representation of this object.
-