Search in sources :

Example 6 with EntityBuilder

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

the class VisualCharacterSystem method onCreateDefaultVisualCharacter.

@ReceiveEvent(priority = EventPriority.PRIORITY_TRIVIAL)
public void onCreateDefaultVisualCharacter(CreateVisualCharacterEvent event, EntityRef characterEntity) {
    Prefab prefab = assetManager.getAsset("engine:defaultVisualCharacter", Prefab.class).get();
    EntityBuilder entityBuilder = event.getVisualCharacterBuilder();
    entityBuilder.addPrefab(prefab);
    event.consume();
}
Also used : EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) Prefab(org.terasology.entitySystem.prefab.Prefab) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 7 with EntityBuilder

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

the class ExplosionAuthoritySystem method doExplosion.

void doExplosion(ExplosionActionComponent explosionComp, Vector3f origin, EntityRef instigatingBlockEntity) {
    EntityBuilder builder = entityManager.newBuilder("core:smokeExplosion");
    builder.getComponent(LocationComponent.class).setWorldPosition(origin);
    EntityRef smokeEntity = builder.build();
    smokeEntity.send(new PlaySoundEvent(getRandomExplosionSound(), 1f));
    Vector3i blockPos = new Vector3i();
    for (int i = 0; i < explosionComp.maxRange; i++) {
        Vector3f direction = random.nextVector3f(1.0f);
        for (int j = 0; j < 4; j++) {
            Vector3f target = new Vector3f(origin);
            target.x += direction.x * j;
            target.y += direction.y * j;
            target.z += direction.z * j;
            blockPos.set((int) target.x, (int) target.y, (int) target.z);
            Block currentBlock = worldProvider.getBlock(blockPos);
            /* PHYSICS */
            if (currentBlock.isDestructible()) {
                EntityRef blockEntity = blockEntityRegistry.getEntityAt(blockPos);
                // allow explosions to chain together,  but do not chain on the instigating block
                if (!blockEntity.equals(instigatingBlockEntity) && blockEntity.hasComponent(ExplosionActionComponent.class)) {
                    doExplosion(blockEntity.getComponent(ExplosionActionComponent.class), blockPos.toVector3f(), blockEntity);
                } else {
                    blockEntity.send(new DoDamageEvent(explosionComp.damageAmount, explosionComp.damageType));
                }
            }
        }
    }
}
Also used : PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) DoDamageEvent(org.terasology.logic.health.DoDamageEvent) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 8 with EntityBuilder

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

the class TunnelAction method onActivate.

@ReceiveEvent
public void onActivate(ActivateEvent event, EntityRef entity, TunnelActionComponent tunnelActionComponent) {
    Vector3f dir = new Vector3f(event.getDirection());
    dir.scale(4.0f);
    Vector3f origin = new Vector3f(event.getOrigin());
    origin.add(dir);
    Vector3i blockPos = new Vector3i();
    int particleEffects = 0;
    int blockCounter = tunnelActionComponent.maxDestroyedBlocks;
    for (int s = 0; s <= tunnelActionComponent.maxTunnelDepth; s++) {
        origin.add(dir);
        if (!worldProvider.isBlockRelevant(origin)) {
            break;
        }
        for (int i = 0; i < tunnelActionComponent.maxRaysCast; i++) {
            Vector3f direction = random.nextVector3f();
            Vector3f impulse = new Vector3f(direction);
            impulse.scale(tunnelActionComponent.explosiveForce);
            for (int j = 0; j < 3; j++) {
                Vector3f target = new Vector3f(origin);
                target.x += direction.x * j;
                target.y += direction.y * j;
                target.z += direction.z * j;
                blockPos.set((int) target.x, (int) target.y, (int) target.z);
                Block currentBlock = worldProvider.getBlock(blockPos);
                if (currentBlock.isDestructible()) {
                    if (particleEffects < tunnelActionComponent.maxParticalEffects) {
                        EntityBuilder builder = entityManager.newBuilder("Core:smokeExplosion");
                        builder.getComponent(LocationComponent.class).setWorldPosition(target);
                        builder.build();
                        particleEffects++;
                    }
                    if (random.nextFloat() < tunnelActionComponent.thoroughness) {
                        EntityRef blockEntity = blockEntityRegistry.getEntityAt(blockPos);
                        blockEntity.send(new DoDamageEvent(tunnelActionComponent.damageAmount, tunnelActionComponent.damageType));
                    }
                    blockCounter--;
                }
                if (blockCounter <= 0) {
                    return;
                }
            }
        }
    }
    // No blocks were destroyed, so cancel the event
    if (blockCounter == tunnelActionComponent.maxDestroyedBlocks) {
        event.consume();
    }
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) DoDamageEvent(org.terasology.logic.health.DoDamageEvent) LocationComponent(org.terasology.logic.location.LocationComponent) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 9 with EntityBuilder

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

the class BlockDamageAuthoritySystem method createBlockParticleEffect.

/**
 * Creates a new entity for the block damage particle effect.
 *
 * If the terrain texture of the damaged block is available, the particles will have the block texture. Otherwise,
 * the default sprite (smoke) is used.
 *
 * @param family the {@link BlockFamily} of the damaged block
 * @param location the location of the damaged block
 */
private void createBlockParticleEffect(BlockFamily family, Vector3f location) {
    EntityBuilder builder = entityManager.newBuilder("core:defaultBlockParticles");
    builder.getComponent(LocationComponent.class).setWorldPosition(location);
    Optional<Texture> terrainTexture = Assets.getTexture("engine:terrain");
    if (terrainTexture.isPresent() && terrainTexture.get().isLoaded()) {
        final BlockAppearance blockAppearance = family.getArchetypeBlock().getPrimaryAppearance();
        final float relativeTileSize = worldAtlas.getRelativeTileSize();
        final float particleScale = 0.25f;
        final float spriteSize = relativeTileSize * particleScale;
        ParticleDataSpriteComponent spriteComponent = builder.getComponent(ParticleDataSpriteComponent.class);
        spriteComponent.texture = terrainTexture.get();
        spriteComponent.textureSize.set(spriteSize, spriteSize);
        final List<Vector2f> offsets = computeOffsets(blockAppearance, particleScale);
        TextureOffsetGeneratorComponent textureOffsetGeneratorComponent = builder.getComponent(TextureOffsetGeneratorComponent.class);
        textureOffsetGeneratorComponent.validOffsets.addAll(offsets);
    }
    builder.build();
}
Also used : ParticleDataSpriteComponent(org.terasology.particles.components.ParticleDataSpriteComponent) TextureOffsetGeneratorComponent(org.terasology.particles.components.generators.TextureOffsetGeneratorComponent) BlockAppearance(org.terasology.world.block.BlockAppearance) Vector2f(org.terasology.math.geom.Vector2f) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) LocationComponent(org.terasology.logic.location.LocationComponent) Texture(org.terasology.rendering.assets.texture.Texture)

Example 10 with EntityBuilder

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

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