Search in sources :

Example 11 with Block

use of org.terasology.world.block.Block 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 12 with Block

use of org.terasology.world.block.Block 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 13 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class EntityAwareWorldProvider method setBlockRetainComponent.

@Override
@SafeVarargs
public final Block setBlockRetainComponent(Vector3i pos, Block type, Class<? extends Component>... components) {
    if (GameThread.isCurrentThread()) {
        EntityRef blockEntity = getBlockEntityAt(pos);
        Block oldType = super.setBlock(pos, type);
        if (oldType != null) {
            updateBlockEntity(blockEntity, pos, oldType, type, false, Sets.newHashSet(components));
        }
        return oldType;
    }
    return null;
}
Also used : Block(org.terasology.world.block.Block) OnChangedBlock(org.terasology.world.OnChangedBlock) EntityRef(org.terasology.entitySystem.entity.EntityRef)

Example 14 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class WorldProviderCore method setBlocks.

/**
 * Places all given blocks of specific types at their corresponding positions
 * </p>
 * Chunks are
 *
 * @param blocks A mapping from world position to change to the type of block to set
 * @return A mapping from world position to previous block type.
 * The value of a map entry is Null if the change failed (because the necessary chunk was not loaded)
 */
default Map<Vector3i, Block> setBlocks(Map<Vector3i, Block> blocks) {
    Map<Vector3i, Block> resultMap = Maps.newHashMap();
    for (Map.Entry<Vector3i, Block> entry : blocks.entrySet()) {
        Block oldBlock = setBlock(entry.getKey(), entry.getValue());
        resultMap.put(entry.getKey(), oldBlock);
    }
    return resultMap;
}
Also used : Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) Map(java.util.Map)

Example 15 with Block

use of org.terasology.world.block.Block in project Terasology by MovingBlocks.

the class WorldProviderCoreImpl method setBlock.

@Override
public Block setBlock(Vector3i worldPos, Block type) {
    /*
         * Hint: This method has a benchmark available in the BenchmarkScreen, The screen can be opened ingame via the
         * command "showSCreen BenchmarkScreen".
         */
    Vector3i chunkPos = ChunkMath.calcChunkPos(worldPos);
    CoreChunk chunk = chunkProvider.getChunk(chunkPos);
    if (chunk != null) {
        Vector3i blockPos = ChunkMath.calcBlockPos(worldPos);
        Block oldBlockType = chunk.setBlock(blockPos, type);
        if (oldBlockType != type) {
            BlockChange oldChange = blockChanges.get(worldPos);
            if (oldChange == null) {
                blockChanges.put(worldPos, new BlockChange(worldPos, oldBlockType, type));
            } else {
                oldChange.setTo(type);
            }
            for (Vector3i pos : ChunkMath.getChunkRegionAroundWorldPos(worldPos, 1)) {
                RenderableChunk dirtiedChunk = chunkProvider.getChunk(pos);
                if (dirtiedChunk != null) {
                    dirtiedChunk.setDirty(true);
                }
            }
            notifyBlockChanged(worldPos, type, oldBlockType);
        }
        return oldBlockType;
    }
    return null;
}
Also used : CoreChunk(org.terasology.world.chunks.CoreChunk) BlockChange(org.terasology.world.propagation.BlockChange) Vector3i(org.terasology.math.geom.Vector3i) RenderableChunk(org.terasology.world.chunks.RenderableChunk) Block(org.terasology.world.block.Block)

Aggregations

Block (org.terasology.world.block.Block)67 Vector3i (org.terasology.math.geom.Vector3i)32 EntityRef (org.terasology.entitySystem.entity.EntityRef)16 ReceiveEvent (org.terasology.entitySystem.event.ReceiveEvent)11 Side (org.terasology.math.Side)11 Vector3f (org.terasology.math.geom.Vector3f)9 LocationComponent (org.terasology.logic.location.LocationComponent)7 BlockUri (org.terasology.world.block.BlockUri)7 BlockFamily (org.terasology.world.block.family.BlockFamily)7 PlaySoundEvent (org.terasology.audio.events.PlaySoundEvent)6 OnChangedBlock (org.terasology.world.OnChangedBlock)6 BlockComponent (org.terasology.world.block.BlockComponent)5 BlockManager (org.terasology.world.block.BlockManager)5 BlockRegionComponent (org.terasology.world.block.regions.BlockRegionComponent)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Before (org.junit.Before)3 ResourceUrn (org.terasology.assets.ResourceUrn)3 EntityBuilder (org.terasology.entitySystem.entity.EntityBuilder)3 DoDamageEvent (org.terasology.logic.health.DoDamageEvent)3