Package org.terasology.entitySystem.entity

Examples of org.terasology.entitySystem.entity.EntityBuilder


        entityCache.clear();
    }

    @Override
    public EntityBuilder newBuilder() {
        return new EntityBuilder(this);
    }
View Full Code Here


    public EntityBuilder newBuilder(String prefabName) {
        if (prefabName != null && !prefabName.isEmpty()) {
            Prefab prefab = prefabManager.getPrefab(prefabName);
            if (prefab == null) {
                logger.warn("Unable to instantiate unknown prefab: \"{}\"", prefabName);
                return new EntityBuilder(this);
            }
            return newBuilder(prefab);
        }
        return newBuilder();
    }
View Full Code Here

        return newBuilder();
    }

    @Override
    public EntityBuilder newBuilder(Prefab prefab) {
        EntityBuilder builder = new EntityBuilder(this);
        if (prefab != null) {
            for (Component component : prefab.iterateComponents()) {
                builder.addComponent(componentLibrary.copy(component));
            }
            builder.addComponent(new EntityInfoComponent(prefab.getName(), prefab.isPersisted(), prefab.isAlwaysRelevant()));
        }
        return builder;
    }
View Full Code Here

                        }
                        dropResult = dropResult.substring(pipeIndex + 1);
                    }
                    if (dropping) {
                        DropParser dropParser = new DropParser(random, dropResult).invoke();
                        EntityBuilder dropEntity = entityManager.newBuilder(dropParser.getDrop());
                        if (dropParser.getCount() > 1) {
                            ItemComponent itemComponent = dropEntity.getComponent(ItemComponent.class);
                            itemComponent.stackCount = (byte) dropParser.getCount();
                        }
                        EntityRef dropItem = dropEntity.build();
                        if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                            createDrop(dropItem, locationComp.getWorldPosition(), false);
                        }
                    }
                }
View Full Code Here

            }
        }
    }

    public EntityRef deserialize(EntityData.PackedEntity entityData) {
        EntityBuilder target;
        if (entityData.hasParentPrefabUri()) {
            target = entityManager.newBuilder(entityData.getParentPrefabUri());
        } else {
            target = entityManager.newBuilder();
        }
        deserializeOnto(target, entityData);
        if (entityData.hasId()) {
            return entityManager.createEntityWithId(entityData.getId(), target.iterateComponents());
        } else {
            return target.build();
        }
    }
View Full Code Here

     */
    private void updateBlockEntityComponents(EntityRef blockEntity, Block oldType, Block type, Set<Class<? extends Component>> retainComponents) {
        BlockComponent blockComponent = blockEntity.getComponent(BlockComponent.class);

        Prefab oldPrefab = Assets.getPrefab(oldType.getPrefab());
        EntityBuilder oldEntityBuilder = entityManager.newBuilder(oldPrefab);
        oldEntityBuilder.addComponent(new BlockComponent(oldType, new Vector3i(blockComponent.getPosition())));
        BeforeEntityCreated oldEntityEvent = new BeforeEntityCreated(oldPrefab, oldEntityBuilder.iterateComponents());
        blockEntity.send(oldEntityEvent);
        for (Component comp : oldEntityEvent.getResultComponents()) {
            oldEntityBuilder.addComponent(comp);
        }

        Prefab newPrefab = Assets.getPrefab(type.getPrefab());
        EntityBuilder newEntityBuilder = entityManager.newBuilder(newPrefab);
        newEntityBuilder.addComponent(new BlockComponent(type, new Vector3i(blockComponent.getPosition())));
        BeforeEntityCreated newEntityEvent = new BeforeEntityCreated(newPrefab, 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);

        HealthComponent health = blockEntity.getComponent(HealthComponent.class);
        if (health == null && type.isDestructible()) {
            blockEntity.addComponent(new HealthComponent(type.getHardness(), type.getHardness() / BLOCK_REGEN_SECONDS, 1.0f));
        } else if (health != null && !type.isDestructible()) {
            blockEntity.removeComponent(HealthComponent.class);
        } else if (health != null && type.isDestructible()) {
            health.maxHealth = type.getHardness();
            health.currentHealth = Math.min(health.currentHealth, health.maxHealth);
            blockEntity.saveComponent(health);
        }

        for (Component comp : newEntityBuilder.iterateComponents()) {
            copyIntoPrefab(blockEntity, comp, retainComponents);
        }


    }
View Full Code Here

            }
        }
    }

    private EntityRef createBlockEntity(Vector3i blockPosition, Block block) {
        EntityBuilder builder = entityManager.newBuilder(block.getPrefab());
        builder.addComponent(new LocationComponent(blockPosition.toVector3f()));
        builder.addComponent(new BlockComponent(block, blockPosition));
        if (block.isDestructible() && !builder.hasComponent(HealthComponent.class)) {
            // Block regen should always take the same amount of time,  regardless of its hardness
            builder.addComponent(new HealthComponent(block.getHardness(), block.getHardness() / BLOCK_REGEN_SECONDS, 1.0f));
        }
        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;
    }
View Full Code Here

            generateBlockTypeEntity(block);
        }
    }

    private void generateBlockTypeEntity(Block block) {
        EntityBuilder builder = entityManager.newBuilder(blockTypePrefab);
        builder.getComponent(BlockTypeComponent.class).block = block;
        // TODO: Copy across settings as necessary
        Prefab prefab = (!block.getPrefab().isEmpty()) ? prefabManager.getPrefab(block.getPrefab()) : null;
        if (prefab != null) {
            for (Component comp : prefab.iterateComponents()) {
                if (!(comp instanceof NetworkComponent)) {
                    builder.addComponent(entityManager.getComponentLibrary().copy(comp));
                }
            }
        }
        block.setEntity(builder.build());
    }
View Full Code Here

            onPlayBlockDamageCommon(blockFamily, location);
        }
    }

    private void onPlayBlockDamageCommon(BlockFamily family, Vector3f location) {
        EntityBuilder builder = entityManager.newBuilder("engine:defaultBlockParticles");
        builder.getComponent(LocationComponent.class).setWorldPosition(location);
        builder.getComponent(BlockParticleEffectComponent.class).blockType = family;
        builder.build();

        if (family.getArchetypeBlock().isDebrisOnDestroy()) {
            EntityBuilder dustBuilder = entityManager.newBuilder("engine:dustEffect");
            dustBuilder.getComponent(LocationComponent.class).setWorldPosition(location);
            dustBuilder.build();
        }

        BlockSounds sounds = family.getArchetypeBlock().getSounds();
        if (!sounds.getDigSounds().isEmpty()) {
            StaticSound sound = random.nextItem(sounds.getDigSounds());
View Full Code Here

    public EntityRef newInstance(BlockFamily blockFamily, int quantity) {
        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
        Prefab prefab = Assets.getPrefab(blockFamily.getArchetypeBlock().getPrefab());
        if (prefab != null) {
            for (Component component : prefab.iterateComponents()) {
                if (component.getClass().getAnnotation(AddToBlockBasedItem.class) != null) {
                    builder.addComponent(entityManager.getComponentLibrary().copy(component));
                }
            }
        }

        DisplayNameComponent displayNameComponent = builder.getComponent(DisplayNameComponent.class);
        displayNameComponent.name = blockFamily.getDisplayName();

        ItemComponent item = builder.getComponent(ItemComponent.class);
        if (blockFamily.getArchetypeBlock().isStackable()) {
            item.stackId = "block:" + blockFamily.getURI().toString();
            item.stackCount = (byte) quantity;
        }

        BlockItemComponent blockItem = builder.getComponent(BlockItemComponent.class);
        blockItem.blockFamily = blockFamily;

        return builder.build();
    }
View Full Code Here

TOP

Related Classes of org.terasology.entitySystem.entity.EntityBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.