Search in sources :

Example 1 with BlockDamageModifierComponent

use of org.terasology.world.block.entity.damage.BlockDamageModifierComponent in project Terasology by MovingBlocks.

the class BlockEntitySystem method commonDefaultDropsHandling.

public void commonDefaultDropsHandling(CreateBlockDropsEvent event, EntityRef entity, Vector3i location, Block block) {
    BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
    float chanceOfBlockDrop = 1;
    if (blockDamageModifierComponent != null) {
        chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
    }
    if (random.nextFloat() < chanceOfBlockDrop) {
        EntityRef item = blockItemFactory.newInstance(block.getBlockFamily(), entity);
        entity.send(new OnBlockToItem(item));
        if (shouldDropToWorld(event, block, blockDamageModifierComponent, item)) {
            float impulsePower = 0;
            if (blockDamageModifierComponent != null) {
                impulsePower = blockDamageModifierComponent.impulsePower;
            }
            processDropping(item, location, impulsePower);
        }
    }
}
Also used : BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent) OnBlockToItem(org.terasology.world.block.items.OnBlockToItem) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 2 with BlockDamageModifierComponent

use of org.terasology.world.block.entity.damage.BlockDamageModifierComponent 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 3 with BlockDamageModifierComponent

use of org.terasology.world.block.entity.damage.BlockDamageModifierComponent in project Terasology by MovingBlocks.

the class BlockDropGrammarSystem method onDestroyed.

@ReceiveEvent
public void onDestroyed(DoDestroyEvent event, EntityRef entity, BlockDropGrammarComponent blockDrop, LocationComponent locationComp) {
    BlockDamageModifierComponent blockDamageModifierComponent = event.getDamageType().getComponent(BlockDamageModifierComponent.class);
    float chanceOfBlockDrop = 1;
    if (blockDamageModifierComponent != null) {
        chanceOfBlockDrop = 1 - blockDamageModifierComponent.blockAnnihilationChance;
    }
    if (random.nextFloat() < chanceOfBlockDrop) {
        List<String> blockDrops = blockDrop.blockDrops;
        List<String> itemDrops = blockDrop.itemDrops;
        if (blockDamageModifierComponent != null && blockDrop.droppedWithTool != null) {
            for (String toolType : blockDamageModifierComponent.materialDamageMultiplier.keySet()) {
                if (blockDrop.droppedWithTool.containsKey(toolType)) {
                    BlockDropGrammarComponent.DropDefinition dropDefinition = blockDrop.droppedWithTool.get(toolType);
                    blockDrops = dropDefinition.blockDrops;
                    itemDrops = dropDefinition.itemDrops;
                    break;
                }
            }
        }
        if (blockDrops != null) {
            for (String drop : blockDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    dropResult = dropResult.substring(pipeIndex + 1);
                }
                if (dropping) {
                    DropParser dropParser = new DropParser(random, dropResult).invoke();
                    EntityRef dropItem = blockItemFactory.newInstance(blockManager.getBlockFamily(dropParser.getDrop()), dropParser.getCount());
                    if (shouldDropToWorld(event, blockDamageModifierComponent, dropItem)) {
                        createDrop(dropItem, locationComp.getWorldPosition(), true);
                    }
                }
            }
        }
        if (itemDrops != null) {
            for (String drop : itemDrops) {
                String dropResult = drop;
                boolean dropping = true;
                int pipeIndex = dropResult.indexOf('|');
                if (pipeIndex > -1) {
                    float chance = Float.parseFloat(dropResult.substring(0, pipeIndex));
                    if (random.nextFloat() >= chance) {
                        dropping = false;
                    }
                    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);
                    }
                }
            }
        }
    }
}
Also used : ItemComponent(org.terasology.logic.inventory.ItemComponent) BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent) EntityBuilder(org.terasology.entitySystem.entity.EntityBuilder) EntityRef(org.terasology.entitySystem.entity.EntityRef) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 4 with BlockDamageModifierComponent

use of org.terasology.world.block.entity.damage.BlockDamageModifierComponent in project Terasology by MovingBlocks.

the class BlockDamageAuthoritySystem method onDamagedCommon.

private void onDamagedCommon(OnDamagedEvent event, BlockFamily blockFamily, Vector3f location, EntityRef entityRef) {
    BlockDamageModifierComponent blockDamageSettings = event.getType().getComponent(BlockDamageModifierComponent.class);
    boolean skipDamageEffects = false;
    if (blockDamageSettings != null) {
        skipDamageEffects = blockDamageSettings.skipPerBlockEffects;
    }
    if (!skipDamageEffects) {
        onPlayBlockDamageCommon(blockFamily, location, entityRef);
    }
}
Also used : BlockDamageModifierComponent(org.terasology.world.block.entity.damage.BlockDamageModifierComponent)

Aggregations

BlockDamageModifierComponent (org.terasology.world.block.entity.damage.BlockDamageModifierComponent)4 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)2 EntityRef (org.terasology.entitySystem.entity.EntityRef)2 StaticSound (org.terasology.audio.StaticSound)1 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)1 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)1 ItemComponent (org.terasology.logic.inventory.ItemComponent)1 LocationComponent (org.terasology.logic.location.LocationComponent)1 OnBlockToItem (org.terasology.world.block.items.OnBlockToItem)1 BlockSounds (org.terasology.world.block.sounds.BlockSounds)1