Search in sources :

Example 6 with Component

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

the class PrefabSerializer method serialize.

/**
 * @param prefab
 * @return The serialized prefab
 */
public EntityData.Prefab serialize(Prefab prefab) {
    EntityData.Prefab.Builder prefabData = EntityData.Prefab.newBuilder();
    prefabData.setName(prefab.getName());
    if (prefab.getParent() != null) {
        prefabData.setParentName(prefab.getParent().getName());
    }
    prefabData.setAlwaysRelevant(prefab.isAlwaysRelevant());
    prefabData.setPersisted(prefab.isPersisted());
    // Delta off the parent
    for (Component component : prefab.iterateComponents()) {
        if (prefab.getParent() != null && prefab.getParent().hasComponent(component.getClass())) {
            EntityData.Component serializedComponent = componentSerializer.serialize(prefab.getParent().getComponent(component.getClass()), component);
            if (serializedComponent != null) {
                prefabData.addComponent(serializedComponent);
            }
        } else {
            prefabData.addComponent(componentSerializer.serialize(component));
        }
    }
    if (prefab.getParent() != null) {
        for (Component parentComp : prefab.getParent().iterateComponents()) {
            if (!prefab.hasComponent(parentComp.getClass())) {
                prefabData.addRemovedComponent(componentLibrary.getMetadata(parentComp).getId().toString());
            }
        }
    }
    return prefabData.build();
}
Also used : EntityData(org.terasology.protobuf.EntityData) Component(org.terasology.gestalt.entitysystem.component.Component) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 7 with Component

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

the class EntitySerializer method createInitialComponents.

/**
 * Creates the components for the entity being deserialized based on its prefab (if any)
 *
 * @param entityData
 * @return The mapping of components
 */
private Map<Class<? extends Component>, Component> createInitialComponents(EntityData.Entity entityData) {
    Set<ComponentMetadata<?>> removedComponents = Sets.newHashSet();
    for (String removedComp : entityData.getRemovedComponentList()) {
        ComponentMetadata<?> removedMetadata = componentLibrary.resolve(removedComp);
        if (removedMetadata != null) {
            removedComponents.add(removedMetadata);
        }
    }
    Map<Class<? extends Component>, Component> componentMap = Maps.newHashMap();
    if (entityData.hasParentPrefab() && !entityData.getParentPrefab().isEmpty() && prefabManager.exists(entityData.getParentPrefab())) {
        Prefab prefab = prefabManager.getPrefab(entityData.getParentPrefab());
        for (Component component : prefab.iterateComponents()) {
            ComponentMetadata<?> metadata = componentLibrary.getMetadata(component);
            if (!removedComponents.contains(metadata)) {
                componentMap.put(component.getClass(), componentLibrary.copy(component));
            }
        }
        componentMap.put(EntityInfoComponent.class, new EntityInfoComponent(prefab, true, prefab.isAlwaysRelevant()));
    }
    return componentMap;
}
Also used : ComponentMetadata(org.terasology.engine.entitySystem.metadata.ComponentMetadata) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent) Component(org.terasology.gestalt.entitysystem.component.Component) Prefab(org.terasology.engine.entitySystem.prefab.Prefab) EntityInfoComponent(org.terasology.engine.entitySystem.entity.internal.EntityInfoComponent)

Example 8 with Component

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

the class EntityAwareWorldProvider method cleanUpTemporaryEntity.

private void cleanUpTemporaryEntity(EntityRef entity) {
    Prefab prefab = entity.getParentPrefab();
    for (Component comp : entity.iterateComponents()) {
        // TODO: should this also check for components listed in `RetainComponentsComponent`?
        if (!COMMON_BLOCK_COMPONENTS.contains(comp.getClass()) && (prefab == null || !prefab.hasComponent(comp.getClass()))) {
            entity.removeComponent(comp.getClass());
        }
    }
    entity.removeComponent(NetworkComponent.class);
    if (prefab != null) {
        for (Component comp : prefab.iterateComponents()) {
            Component currentComp = entity.getComponent(comp.getClass());
            if (currentComp == null) {
                entity.addComponent(entityManager.getComponentLibrary().copy(comp));
            } else {
                ComponentMetadata<?> metadata = entityManager.getComponentLibrary().getMetadata(comp.getClass());
                boolean changed = false;
                for (FieldMetadata field : metadata.getFields()) {
                    Object expected = field.getValue(comp);
                    if (!Objects.equal(expected, field.getValue(currentComp))) {
                        field.setValue(currentComp, expected);
                        changed = true;
                    }
                }
                if (changed) {
                    entity.saveComponent(currentComp);
                }
            }
        }
    }
    entityManager.destroyEntityWithoutEvents(entity);
}
Also used : FieldMetadata(org.terasology.reflection.metadata.FieldMetadata) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 9 with Component

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

the class EntityAwareWorldProvider method updateBlockEntityComponents.

/**
 * Transforms a block entity with the change of block type. This is driven from the delta between the old and new
 * block type prefabs, but takes into account changes made to the block entity.
 * Components contained in `blockEntity` that
 * <ul>
 *     <li>are not "common block components" (e.g. `NetworkComponent`)</li>
 *     <li>don't have `reatinUnalteredOnBlockChange` metadata</li>
 *     <li>are not listed in the block prefab</li>
 *     <li>are not listed in the set of components to be retained</li>
 * </ul>
 * will be removed.
 *
 * @param blockEntity      The entity to update
 * @param oldType          The previous type of the block
 * @param type             The new type of the block
 * @param retainComponents List of components to be retained
 */
private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
    BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);
    Optional<Prefab> oldPrefab = oldType.getPrefab();
    EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab.orElse(null));
    oldEntityBuilder.addComponent(new BlockComponent(oldType, blockComponent.getPosition()));
    BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab.orElse(null), oldEntityBuilder.iterateComponents());
    blockEntity.send(oldEntityEvent);
    for (Component comp : oldEntityEvent.getResultComponents()) {
        oldEntityBuilder.addComponent(comp);
    }
    Optional<Prefab> newPrefab = type.getPrefab();
    EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab.orElse(null));
    newEntityBuilder.addComponent(new BlockComponent(type, blockComponent.getPosition()));
    BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab.orElse(null), newEntityBuilder.iterateComponents());
    blockEntity.send(newEntityEvent);
    for (Component comp : newEntityEvent.getResultComponents()) {
        newEntityBuilder.addComponent(comp);
    }
    for (Component component : blockEntity.iterateComponents()) {
        if (!COMMON_BLOCK_COMPONENTS.contains(component.getClass()) && !entityManager.getComponentLibrary().getMetadata(component.getClass()).isRetainUnalteredOnBlockChange() && !newEntityBuilder.hasComponent(component.getClass()) && !retainComponents.contains(component.getClass())) {
            blockEntity.removeComponent(component.getClass());
        }
    }
    BlockComponent newBlockComponent = new BlockComponent(type, blockComponent.getPosition());
    blockEntity.saveComponent(newBlockComponent);
    for (Component comp : newEntityBuilder.iterateComponents()) {
        copyIntoPrefab(blockEntity, comp, retainComponents);
    }
}
Also used : BlockComponent(org.terasology.engine.world.block.BlockComponent) BeforeEntityCreated(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityBuilder(org.terasology.engine.entitySystem.entity.EntityBuilder) Component(org.terasology.gestalt.entitysystem.component.Component) BeforeDeactivateComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) RetainComponentsComponent(org.terasology.engine.logic.common.RetainComponentsComponent) LocationComponent(org.terasology.engine.logic.location.LocationComponent) NetworkComponent(org.terasology.engine.network.NetworkComponent) BlockRegionComponent(org.terasology.engine.world.block.regions.BlockRegionComponent) BlockComponent(org.terasology.engine.world.block.BlockComponent) OnActivatedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.engine.entitySystem.entity.lifecycleEvents.OnChangedComponent) Prefab(org.terasology.engine.entitySystem.prefab.Prefab)

Example 10 with Component

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

the class BehaviorEditorScreen method onEntitySelected.

private void onEntitySelected(Interpreter value, PropertyProvider provider) {
    if (selectedInterpreter != null) {
        selectedInterpreter.setCallback(null);
    }
    selectedInterpreter = value;
    if (selectedInterpreter != null) {
        EntityRef entity = value.actor().getEntity();
        onTreeSelected(selectedInterpreter.getTree());
        entityProperties.clear();
        for (Component component : entity.iterateComponents()) {
            String name = component.getClass().getSimpleName().replace("Component", "");
            List<Property<?, ?>> componentProperties = provider.createProperties(component);
            entityProperties.addProperties(name, componentProperties);
        }
        selectedInterpreter.setCallback(behaviorEditor);
    }
}
Also used : BehaviorComponent(org.terasology.engine.logic.behavior.BehaviorComponent) Component(org.terasology.gestalt.entitysystem.component.Component) EntityRef(org.terasology.engine.entitySystem.entity.EntityRef) Property(org.terasology.nui.properties.Property)

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