Search in sources :

Example 21 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class PreviewWorldScreen method resetEnvironment.

private void resetEnvironment() {
    CoreRegistry.setContext(context);
    if (environment != null) {
        EnvironmentSwitchHandler environmentSwitchHandler = context.get(EnvironmentSwitchHandler.class);
        environmentSwitchHandler.handleSwitchBackFromPreviewEnvironment(subContext);
        environment.close();
        environment = null;
    }
    previewGen.close();
    WorldConfigurator worldConfig = worldGenerator.getConfigurator();
    Map<String, Component> params = worldConfig.getProperties();
    if (params != null) {
        config.setModuleConfigs(worldGenerator.getUri(), params);
    }
}
Also used : WorldConfigurator(org.terasology.engine.world.generator.WorldConfigurator) Component(org.terasology.gestalt.entitysystem.component.Component) EnvironmentSwitchHandler(org.terasology.engine.core.bootstrap.EnvironmentSwitchHandler)

Example 22 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class PojoEntityManager method saveComponent.

/**
 * Saves a component to an entity
 *
 * @param entityId
 * @param component
 */
@Override
public void saveComponent(long entityId, Component component) {
    Optional<Component> oldComponent = getPool(entityId).map(pool -> pool.getComponentStore().put(entityId, component));
    if (!oldComponent.isPresent()) {
        logger.error("Saving a component ({}) that doesn't belong to this entity {}", component.getClass(), entityId);
    }
    if (eventSystem != null) {
        EntityRef entityRef = getEntity(entityId);
        if (!oldComponent.isPresent()) {
            eventSystem.send(entityRef, OnAddedComponent.newInstance(), component);
            eventSystem.send(entityRef, OnActivatedComponent.newInstance(), component);
        } else {
            eventSystem.send(entityRef, OnChangedComponent.newInstance(), component);
        }
    }
    if (!oldComponent.isPresent()) {
        notifyComponentAdded(getEntity(entityId), component.getClass());
    } else {
        notifyComponentChanged(getEntity(entityId), component.getClass());
    }
}
Also used : Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) SectorSimulationComponent(org.terasology.engine.entitySystem.sectors.SectorSimulationComponent) BeforeRemoveComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent) OnAddedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef)

Example 23 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class PojoEntityManager method deactivateForStorage.

@Override
public void deactivateForStorage(EntityRef entity) {
    if (!entity.exists()) {
        return;
    }
    long entityId = entity.getId();
    if (eventSystem != null) {
        eventSystem.send(entity, BeforeDeactivateComponent.newInstance());
    }
    List<Component> components = Collections.unmodifiableList(getPool(entityId).map(pool -> pool.getComponentStore().getComponentsInNewList(entityId)).orElse(Collections.emptyList()));
    notifyBeforeDeactivation(entity, components);
    for (Component component : components) {
        getPool(entityId).ifPresent(pool -> pool.getComponentStore().remove(entityId, component.getClass()));
    }
    loadedIds.remove(entityId);
}
Also used : Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) SectorSimulationComponent(org.terasology.engine.entitySystem.sectors.SectorSimulationComponent) BeforeRemoveComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent) OnAddedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent)

Example 24 with Component

use of org.terasology.gestalt.entitysystem.component.Component 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)

Example 25 with Component

use of org.terasology.gestalt.entitysystem.component.Component in project Terasology by MovingBlocks.

the class ItemPickupAuthoritySystem method onDropItemEvent.

@ReceiveEvent
public void onDropItemEvent(DropItemEvent event, EntityRef itemEntity, ItemComponent itemComponent) {
    for (Component component : itemComponent.pickupPrefab.iterateComponents()) {
        Component componentCopy = library.getComponentLibrary().copy(component);
        if (componentCopy instanceof LocationComponent) {
            ((LocationComponent) componentCopy).setWorldPosition(event.getPosition());
        }
        itemEntity.addOrSaveComponent(componentCopy);
    }
    if (!itemEntity.hasComponent(LocationComponent.class)) {
        itemEntity.addComponent(new LocationComponent(event.getPosition()));
    }
}
Also used : Component(org.terasology.gestalt.entitysystem.component.Component) RigidBodyComponent(org.terasology.engine.physics.components.RigidBodyComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) OnAddedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent) BlockItemComponent(org.terasology.engine.world.block.items.BlockItemComponent) BoxShapeComponent(org.terasology.engine.physics.components.shapes.BoxShapeComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Aggregations

Component (org.terasology.gestalt.entitysystem.component.Component)57 EntityRef (org.terasology.engine.entitySystem.entity.EntityRef)20 Prefab (org.terasology.engine.entitySystem.prefab.Prefab)12 LocationComponent (org.terasology.engine.logic.location.LocationComponent)12 OnActivatedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent)11 NetworkComponent (org.terasology.engine.network.NetworkComponent)10 OnChangedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent)9 EntityData (org.terasology.protobuf.EntityData)8 BeforeDeactivateComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)7 OnAddedComponent (org.terasology.engine.entitySystem.entity.lifecycleEvents.OnAddedComponent)7 BlockComponent (org.terasology.engine.world.block.BlockComponent)7 EntityInfoComponent (org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)6 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)6 EntityManager (org.terasology.engine.entitySystem.entity.EntityManager)5 ClientComponent (org.terasology.engine.network.ClientComponent)5 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 FieldMetadata (org.terasology.reflection.metadata.FieldMetadata)4 ByteString (com.google.protobuf.ByteString)3