Search in sources :

Example 1 with OnAddedComponent

use of org.terasology.engine.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.engine.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.gestalt.entitysystem.component.Component) OnAddedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)

Aggregations

EntityInfoComponent (org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)1 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)1 OnAddedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent)1 Component (org.terasology.gestalt.entitysystem.component.Component)1