Search in sources :

Example 11 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class EntityRestorer method restore.

public Map<String, EntityRef> restore(EntityData.EntityStore store) {
    EntitySerializer serializer = new EntitySerializer(entityManager);
    Map<Class<? extends Component>, Integer> idMap = Maps.newHashMap();
    for (int i = 0; i < store.getComponentClassCount(); ++i) {
        ComponentMetadata<?> metadata = entityManager.getComponentLibrary().resolve(store.getComponentClass(i));
        if (metadata != null) {
            idMap.put(metadata.getType(), i);
        }
    }
    serializer.setComponentIdMapping(idMap);
    store.getEntityList().forEach(serializer::deserialize);
    Map<String, EntityRef> namedEntities = Maps.newHashMap();
    for (int i = 0; i < store.getEntityNameCount() && i < store.getEntityNamedCount(); ++i) {
        namedEntities.put(store.getEntityName(i), entityManager.getEntity(store.getEntityNamed(i)));
    }
    return namedEntities;
}
Also used : Component(org.terasology.entitySystem.Component) EntityRef(org.terasology.entitySystem.entity.EntityRef) EntitySerializer(org.terasology.persistence.serializers.EntitySerializer)

Example 12 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class BlockTypeEntityGenerator method generateBlockTypeEntity.

private void generateBlockTypeEntity(Block block) {
    EntityBuilder builder = entityManager.newBuilder(blockTypePrefab);
    builder.getComponent(BlockTypeComponent.class).block = block;
    // TODO: Copy across settings as necessary
    Optional<Prefab> prefab = block.getPrefab();
    if (prefab.isPresent()) {
        for (Component comp : prefab.get().iterateComponents()) {
            if (!(comp instanceof NetworkComponent)) {
                builder.addComponent(entityManager.getComponentLibrary().copy(comp));
            }
        }
    }
    block.setEntity(builder.build());
}
Also used : NetworkComponent(org.terasology.network.NetworkComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) NetworkComponent(org.terasology.network.NetworkComponent) Component(org.terasology.entitySystem.Component) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 13 with Component

use of org.terasology.entitySystem.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()) {
        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) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 14 with Component

use of org.terasology.entitySystem.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.
 *
 * @param blockEntity The entity to update
 * @param oldType     The previous type of the block
 * @param type        The new type of the block
 */
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, new Vector3i(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, new Vector3i(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.setBlock(type);
    blockEntity.saveComponent(blockComponent);
    for (Component comp : newEntityBuilder.iterateComponents()) {
        copyIntoPrefab(blockEntity, comp, retainComponents);
    }
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) Vector3i(org.terasology.math.geom.Vector3i) BeforeEntityCreated(org.terasology.entitySystem.entity.lifecycleEvents.BeforeEntityCreated) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) OnActivatedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent) OnChangedComponent(org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent) NetworkComponent(org.terasology.network.NetworkComponent) BlockComponent(org.terasology.world.block.BlockComponent) BeforeDeactivateComponent(org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent) LocationComponent(org.terasology.logic.location.LocationComponent) Component(org.terasology.entitySystem.Component) BlockRegionComponent(org.terasology.world.block.regions.BlockRegionComponent) Prefab(org.terasology.entitySystem.prefab.Prefab)

Example 15 with Component

use of org.terasology.entitySystem.Component in project Terasology by MovingBlocks.

the class BlockItemFactory method newInstance.

public EntityRef newInstance(BlockFamily blockFamily, EntityRef blockEntity) {
    if (blockFamily == null) {
        return EntityRef.NULL;
    }
    EntityBuilder builder = entityManager.newBuilder("engine:blockItemBase");
    if (blockFamily.getArchetypeBlock().getLuminance() > 0) {
        builder.addComponent(new LightComponent());
    }
    // Copy the components from block prefab into the block item
    for (Component component : blockEntity.iterateComponents()) {
        if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
            builder.addComponent(entityManager.getComponentLibrary().copy(component));
        }
    }
    DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
    if (displayNameComponent != null) {
        displayNameComponent.name = blockFamily.getDisplayName();
    }
    ItemComponent item = builder.getComponent(ItemComponent.class);
    if (blockFamily.getArchetypeBlock().isStackable()) {
        item.stackId = "block:" + blockFamily.getURI().toString();
        item.stackCount = (byte) 1;
    }
    BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
    blockItem.blockFamily = blockFamily;
    return builder.build();
}
Also used : DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) LightComponent(org.terasology.rendering.logic.LightComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LightComponent(org.terasology.rendering.logic.LightComponent) ItemComponent(org.terasology.logic.inventory.ItemComponent) Component(org.terasology.entitySystem.Component) DisplayNameComponent(org.terasology.logic.common.DisplayNameComponent)

Aggregations

Component (org.terasology.entitySystem.Component)49 EntityRef (org.terasology.entitySystem.entity.EntityRef)15 Prefab (org.terasology.entitySystem.prefab.Prefab)9 LocationComponent (org.terasology.logic.location.LocationComponent)9 EntityData (org.terasology.protobuf.EntityData)8 OnActivatedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnActivatedComponent)7 NetworkComponent (org.terasology.network.NetworkComponent)7 BlockComponent (org.terasology.world.block.BlockComponent)7 OnAddedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnAddedComponent)6 OnChangedComponent (org.terasology.entitySystem.entity.lifecycleEvents.OnChangedComponent)6 EntityInfoComponent (org.terasology.entitySystem.entity.internal.EntityInfoComponent)5 BeforeDeactivateComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeDeactivateComponent)5 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)4 ByteString (com.google.protobuf.ByteString)3 Test (org.junit.Test)3 BeforeRemoveComponent (org.terasology.entitySystem.entity.lifecycleEvents.BeforeRemoveComponent)3 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)3 SectorSimulationComponent (org.terasology.entitySystem.sectors.SectorSimulationComponent)3 ParticleEmitterComponent (org.terasology.particles.components.ParticleEmitterComponent)3 WorldConfigurator (org.terasology.world.generator.WorldConfigurator)3