Search in sources :

Example 1 with OnAddedComponent

use of org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent in project Terasology by MovingBlocks.

the class EntityBuilder method build.

/**
 * Produces an entity with the components contained in this entity builder
 *
 * @return The built entity.
 */
public EntityRef build() {
    if (id.isPresent() && !entityManager.registerId(id.get())) {
        return EntityRef.NULL;
    }
    long finalId = id.orElse(entityManager.createEntity());
    components.values().forEach(c -> entityManager.getComponentStore().put(finalId, c));
    entityManager.assignToPool(finalId, pool);
    EntityRef entity = entityManager.getEntity(finalId);
    if (sendLifecycleEvents && entityManager.getEventSystem() != null) {
        // TODO: don't send OnAddedComponent when the entity is being re-loaded from storage
        entity.send(OnAddedComponent.newInstance());
        entity.send(OnActivatedComponent.newInstance());
    }
    // Retrieve the components again in case they were modified by the previous events
    for (Component component : entityManager.iterateComponents(entity.getId())) {
        entityManager.notifyComponentAdded(entity, component.getClass());
    }
    entity.setScope(scope.orElse(getEntityInfo().scope));
    return entity;
}
Also used : EntityInfoComponent(org.terasology.entitySystem.entity.internal.EntityInfoComponent) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnAddedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent) Component(org.terasology.entitySystem.Component)

Aggregations

Component (org.terasology.entitySystem.Component)1 EntityInfoComponent (org.terasology.entitySystem.entity.internal.EntityInfoComponent)1 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1 OnAddedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent)1