Search in sources :

Example 21 with Block

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

the class HorizontalBlockFamilyFactory method createBlockFamily.

@Override
public BlockFamily createBlockFamily(BlockFamilyDefinition definition, BlockBuilderHelper blockBuilder) {
    if (definition.isFreeform()) {
        throw new IllegalStateException("A shape must be provided when creating a family for a freeform block family definition");
    }
    Map<Side, Block> blockMap = Maps.newHashMap();
    for (Rotation rot : Rotation.horizontalRotations()) {
        Side side = rot.rotate(Side.FRONT);
        blockMap.put(side, blockBuilder.constructTransformedBlock(definition, side.toString().toLowerCase(Locale.ENGLISH), rot));
    }
    return new HorizontalBlockFamily(new BlockUri(definition.getUrn()), getArchetypeSide(), blockMap, definition.getCategories());
}
Also used : Side(org.terasology.math.Side) BlockUri(org.terasology.world.block.BlockUri) Block(org.terasology.world.block.Block) Rotation(org.terasology.math.Rotation)

Example 22 with Block

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

the class BlockBuilder method constructSimpleBlock.

@Override
public Block constructSimpleBlock(BlockFamilyDefinition definition) {
    BlockShape shape = definition.getData().getBaseSection().getShape();
    if (shape == null) {
        shape = cubeShape;
    }
    Block block = constructCustomBlock(definition.getUrn().getResourceName().toString(), shape, Rotation.none(), definition.getData().getBaseSection());
    // Lowered mesh for liquids
    if (block.isLiquid()) {
        applyLoweredShape(block, loweredShape, definition.getData().getBaseSection().getBlockTiles());
    }
    return block;
}
Also used : BlockShape(org.terasology.world.block.shapes.BlockShape) Block(org.terasology.world.block.Block)

Example 23 with Block

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

the class BlockItemSystem method canPlaceBlock.

private boolean canPlaceBlock(Block block, Vector3i targetBlock, Vector3i blockPos) {
    if (block == null) {
        return false;
    }
    Block centerBlock = worldProvider.getBlock(targetBlock.x, targetBlock.y, targetBlock.z);
    if (!centerBlock.isAttachmentAllowed()) {
        return false;
    }
    Block adjBlock = worldProvider.getBlock(blockPos.x, blockPos.y, blockPos.z);
    if (!adjBlock.isReplacementAllowed() || adjBlock.isTargetable()) {
        return false;
    }
    if (block.getBlockFamily().equals(adjBlock.getBlockFamily())) {
        return false;
    }
    // Prevent players from placing blocks inside their bounding boxes
    if (!block.isPenetrable()) {
        Physics physics = CoreRegistry.get(Physics.class);
        AABB blockBounds = block.getBounds(blockPos);
        Vector3f min = new Vector3f(blockBounds.getMin());
        Vector3f max = new Vector3f(blockBounds.getMax());
        /**
         * Characters can enter other solid objects/blocks for certain amount. This is does to detect collsion
         * start and end without noise. So if the user walked as close to a block as possible it is only natural
         * to let it place a block exactly above it even if that technically would mean a collision start.
         */
        min.x += KinematicCharacterMover.HORIZONTAL_PENETRATION;
        max.x -= KinematicCharacterMover.HORIZONTAL_PENETRATION;
        min.y += KinematicCharacterMover.VERTICAL_PENETRATION;
        max.y -= KinematicCharacterMover.VERTICAL_PENETRATION;
        min.z += KinematicCharacterMover.HORIZONTAL_PENETRATION;
        max.z -= KinematicCharacterMover.HORIZONTAL_PENETRATION;
        /*
             * Calculations aren't exact and in the corner cases it is better to let the user place the block.
             */
        // ignore small rounding mistakes
        float additionalAllowedPenetration = 0.04f;
        min.add(ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION);
        max.sub(ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION, ADDITIONAL_ALLOWED_PENETRATION);
        AABB newBounds = AABB.createMinMax(min, max);
        return physics.scanArea(newBounds, StandardCollisionGroup.DEFAULT, StandardCollisionGroup.CHARACTER).isEmpty();
    }
    return true;
}
Also used : Vector3f(org.terasology.math.geom.Vector3f) Block(org.terasology.world.block.Block) Physics(org.terasology.physics.Physics) AABB(org.terasology.math.AABB)

Example 24 with Block

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

the class BlockItemSystem method onPlaceBlock.

@ReceiveEvent(components = { BlockItemComponent.class, ItemComponent.class })
public void onPlaceBlock(ActivateEvent event, EntityRef item) {
    if (!event.getTarget().exists()) {
        event.consume();
        return;
    }
    BlockItemComponent blockItem = item.getComponent(BlockItemComponent.class);
    BlockFamily type = blockItem.blockFamily;
    Side surfaceSide = Side.inDirection(event.getHitNormal());
    Side secondaryDirection = ChunkMath.getSecondaryPlacementDirection(event.getDirection(), event.getHitNormal());
    BlockComponent blockComponent = event.getTarget().getComponent(BlockComponent.class);
    if (blockComponent == null) {
        // If there is no block there (i.e. it's a BlockGroup, we don't allow placing block, try somewhere else)
        event.consume();
        return;
    }
    Vector3i targetBlock = blockComponent.getPosition();
    Vector3i placementPos = new Vector3i(targetBlock);
    placementPos.add(surfaceSide.getVector3i());
    Block block = type.getBlockForPlacement(worldProvider, blockEntityRegistry, placementPos, surfaceSide, secondaryDirection);
    if (canPlaceBlock(block, targetBlock, placementPos)) {
        // TODO: Fix this for changes.
        if (networkSystem.getMode().isAuthority()) {
            PlaceBlocks placeBlocks = new PlaceBlocks(placementPos, block, event.getInstigator());
            worldProvider.getWorldEntity().send(placeBlocks);
            if (!placeBlocks.isConsumed()) {
                item.send(new OnBlockItemPlaced(placementPos, blockEntityRegistry.getBlockEntityAt(placementPos)));
            } else {
                event.consume();
            }
        }
        recordBlockPlaced(event, type);
        event.getInstigator().send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
    } else {
        event.consume();
    }
}
Also used : Side(org.terasology.math.Side) BlockComponent(org.terasology.world.block.BlockComponent) PlaySoundEvent(org.terasology.audio.events.PlaySoundEvent) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) BlockFamily(org.terasology.world.block.family.BlockFamily) PlaceBlocks(org.terasology.world.block.entity.placement.PlaceBlocks) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

Example 25 with Block

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

the class SideBlockSupportRequired method checkForSupport.

@ReceiveEvent
public void checkForSupport(DelayedActionTriggeredEvent event, EntityRef entity, BlockComponent block, SideBlockSupportRequiredComponent supportRequired) {
    if (event.getActionId().equals(SUPPORT_CHECK_ACTION_ID)) {
        if (!isSufficientlySupported(block.getPosition(), null, Collections.<Vector3i, Block>emptyMap(), supportRequired)) {
            PrefabManager prefabManager = CoreRegistry.get(PrefabManager.class);
            entity.send(new DestroyEvent(entity, EntityRef.NULL, prefabManager.getPrefab("engine:supportRemovedDamage")));
        }
    }
}
Also used : DestroyEvent(org.terasology.logic.health.DestroyEvent) PrefabManager(org.terasology.entitySystem.prefab.PrefabManager) Vector3i(org.terasology.math.geom.Vector3i) Block(org.terasology.world.block.Block) ReceiveEvent(org.terasology.entitySystem.event.ReceiveEvent)

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