Search in sources :

Example 11 with EntityBuilder

use of org.terasology.entitySystem.entity.EntityBuilder 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 12 with EntityBuilder

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

the class EntityAwareWorldProvider method createBlockEntity.

private EntityRef createBlockEntity(Vector3i blockPosition, Block block) {
    EntityBuilder builder = entityManager.newBuilder(block.getPrefab().orElse(null));
    builder.addComponent(new LocationComponent(blockPosition.toVector3f()));
    builder.addComponent(new BlockComponent(block, blockPosition));
    boolean isTemporary = isTemporaryBlock(builder, block);
    if (!isTemporary && !builder.hasComponent(NetworkComponent.class)) {
        builder.addComponent(new NetworkComponent());
    }
    EntityRef blockEntity;
    if (isTemporary) {
        blockEntity = builder.buildWithoutLifecycleEvents();
        temporaryBlockEntities.add(blockEntity);
    } else {
        blockEntity = builder.build();
    }
    blockEntityLookup.put(new Vector3i(blockPosition), blockEntity);
    return blockEntity;
}
Also used : BlockComponent(org.terasology.world.block.BlockComponent) NetworkComponent(org.terasology.network.NetworkComponent) Vector3i(org.terasology.math.geom.Vector3i) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 13 with EntityBuilder

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

the class BlockEntitySystem method commonDestroyed.

private void commonDestroyed(DoDestroyEvent event, EntityRef entity, Block block) {
    entity.send(new CreateBlockDropsEvent(event.getInstigator(), event.getDirectCause(), event.getDamageType()));
    BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
    // TODO: Configurable via block definition
    if (blockDamageModifierComponent == null || !blockDamageModifierComponent.skipPerBlockEffects) {
        // dust particle effect
        if (entity.hasComponent(LocationComponent.class) && block.isDebrisOnDestroy()) {
            EntityBuilder dustBuilder = entityManager.newBuilder("core:dustEffect");
            // TODO: particle system stuff should be split out better - this is effectively a stealth dependency on Core from the engine
            if (dustBuilder.hasComponent(LocationComponent.class)) {
                dustBuilder.getComponent(LocationComponent.class).setWorldPosition(entity.getComponent(LocationComponent.class).getWorldPosition());
                dustBuilder.build();
            }
        }
        // sound to play for destroyed block
        BlockSounds sounds = block.getSounds();
        if (!sounds.getDestroySounds().isEmpty()) {
            StaticSound sound = random.nextItem(sounds.getDestroySounds());
            entity.send(new PlaySoundEvent(sound, 0.6f));
        }
    }
}
Also used : BlockSounds(org.terasology.world.block.sounds.BlockSounds) StaticSound(org.terasology.audio.StaticSound) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent)

Example 14 with EntityBuilder

use of org.terasology.entitySystem.entity.EntityBuilder 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)

Example 15 with EntityBuilder

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

the class PojoEntityPool method newBuilder.

@Override
public EntityBuilder newBuilder(Prefab prefab) {
    EntityBuilder builder = newBuilder();
    builder.addPrefab(prefab);
    return builder;
}
Also used : EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder)

Aggregations

EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)34 EntityRef (org.terasology.entitySystem.entity.EntityRef)16 LocationComponent (org.terasology.logic.location.LocationComponent)10 NetworkComponent (org.terasology.network.NetworkComponent)7 Test (org.junit.Test)5 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)5 Prefab (org.terasology.entitySystem.prefab.Prefab)5 Vector3f (org.terasology.math.geom.Vector3f)5 Vector3i (org.terasology.math.geom.Vector3i)5 Component (org.terasology.entitySystem.Component)4 DoDamageEvent (org.terasology.logic.health.DoDamageEvent)3 ItemComponent (org.terasology.logic.inventory.ItemComponent)3 Quat4f (org.terasology.math.geom.Quat4f)3 Block (org.terasology.world.block.Block)3 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)2 DisplayNameComponent (org.terasology.logic.common.DisplayNameComponent)2 LightComponent (org.terasology.rendering.logic.LightComponent)2 BlockComponent (org.terasology.world.block.BlockComponent)2 Before (org.junit.Before)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1