Search in sources :

Example 6 with Side

use of org.terasology.engine.math.Side 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 blockFamily = blockItem.blockFamily;
    Side surfaceSide = Side.inDirection(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 = new Vector3i(blockComponent.getPosition());
    Vector3i placementPos = new Vector3i(targetBlock);
    placementPos.add(surfaceSide.direction());
    Vector2f relativeAttachmentPosition = getRelativeAttachmentPosition(event);
    Block block = blockFamily.getBlockForPlacement(new BlockPlacementData(placementPos, surfaceSide, event.getDirection(), relativeAttachmentPosition));
    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), event.getInstigator()));
            } else {
                event.consume();
            }
        }
        recordBlockPlaced(event, blockFamily);
        event.getInstigator().send(new PlaySoundEvent(Assets.getSound("engine:PlaceBlock").get(), 0.5f));
    } else {
        event.consume();
    }
}
Also used : Side(org.terasology.engine.math.Side) BlockComponent(org.terasology.engine.world.block.BlockComponent) BlockPlacementData(org.terasology.engine.world.block.family.BlockPlacementData) Vector2f(org.joml.Vector2f) PlaySoundEvent(org.terasology.engine.audio.events.PlaySoundEvent) Vector3i(org.joml.Vector3i) Block(org.terasology.engine.world.block.Block) BlockFamily(org.terasology.engine.world.block.family.BlockFamily) PlaceBlocks(org.terasology.engine.world.block.entity.placement.PlaceBlocks) ReceiveEvent(org.terasology.engine.entitySystem.event.ReceiveEvent)

Example 7 with Side

use of org.terasology.engine.math.Side in project Terasology by MovingBlocks.

the class BlockBuilder method setBlockFullSides.

private void setBlockFullSides(Block block, BlockShape shape, Rotation rot) {
    for (Side side : Side.values()) {
        BlockPart targetPart = BlockPart.fromSide(rot.rotate(side));
        block.setFullSide(targetPart.getSide(), shape.isBlockingSide(side));
    }
}
Also used : Side(org.terasology.engine.math.Side) BlockPart(org.terasology.engine.world.block.BlockPart)

Example 8 with Side

use of org.terasology.engine.math.Side in project Terasology by MovingBlocks.

the class SideTest method testRelativeSides.

@Test
public void testRelativeSides() {
    Side side = Side.FRONT;
    assertEquals(Side.LEFT, side.getRelativeSide(Direction.LEFT));
    assertEquals(Side.RIGHT, side.getRelativeSide(Direction.RIGHT));
    assertEquals(Side.TOP, side.getRelativeSide(Direction.UP));
    assertEquals(Side.BOTTOM, side.getRelativeSide(Direction.DOWN));
    assertEquals(Side.FRONT, side.getRelativeSide(Direction.FORWARD));
    assertEquals(Side.BACK, side.getRelativeSide(Direction.BACKWARD));
}
Also used : Side(org.terasology.engine.math.Side) Test(org.junit.jupiter.api.Test)

Example 9 with Side

use of org.terasology.engine.math.Side in project Terasology by MovingBlocks.

the class BlockMeshGeneratorSingleShape method generateChunkMesh.

@Override
public void generateChunkMesh(ChunkView view, ChunkMesh chunkMesh, int x, int y, int z) {
    final BlockAppearance blockAppearance = block.getPrimaryAppearance();
    if (!blockAppearance.hasAppearance()) {
        // perf: Skip mesh generation for blocks without appearance, e.g., air blocks.
        return;
    }
    Color colorCache = new Color();
    // Gather adjacent blocks
    Block[] adjacentBlocks = new Block[Side.allSides().size()];
    for (Side side : Side.allSides()) {
        Vector3ic offset = side.direction();
        Block blockToCheck = view.getBlock(x + offset.x(), y + offset.y(), z + offset.z());
        adjacentBlocks[side.ordinal()] = blockToCheck;
    }
    final ChunkMesh.RenderType renderType = getRenderType(block);
    final ChunkVertexFlag vertexFlag = getChunkVertexFlag(view, x, y, z, block);
    boolean isRendered = false;
    for (final Side side : Side.allSides()) {
        if (isSideVisibleForBlockTypes(adjacentBlocks[side.ordinal()], block, side)) {
            isRendered = true;
            BlockMeshPart blockMeshPart = blockAppearance.getPart(BlockPart.fromSide(side));
            // If the selfBlock isn't lowered, some more faces may have to be drawn
            if (block.isLiquid()) {
                final Block topBlock = adjacentBlocks[Side.TOP.ordinal()];
                // Draw horizontal sides if visible from below
                if (topBlock.isLiquid() && Side.horizontalSides().contains(side)) {
                    final Vector3ic offset = side.direction();
                    final Block adjacentAbove = view.getBlock(x + offset.x(), y + 1, z + offset.z());
                    final Block adjacent = adjacentBlocks[side.ordinal()];
                    if (adjacent.isLiquid() && !adjacentAbove.isLiquid()) {
                        blockMeshPart = block.getTopLiquidMesh(side);
                    }
                } else {
                    if (blockMeshPart != null) {
                        blockMeshPart = block.getLowLiquidMesh(side);
                    }
                }
            }
            if (blockMeshPart != null) {
                // TODO: Needs review since the new per-vertex flags introduce a lot of special scenarios - probably a per-side setting?
                ChunkVertexFlag sideVertexFlag = vertexFlag;
                if (block.isGrass() && side != Side.TOP && side != Side.BOTTOM) {
                    sideVertexFlag = ChunkVertexFlag.COLOR_MASK;
                }
                Colorc colorOffset = block.getColorOffset(BlockPart.fromSide(side));
                Colorc colorSource = block.getColorSource(BlockPart.fromSide(side)).calcColor(view, x, y, z);
                colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
                blockMeshPart.appendTo(chunkMesh, view, x, y, z, renderType, colorCache, sideVertexFlag);
            }
        }
    }
    if (isRendered && blockAppearance.getPart(BlockPart.CENTER) != null) {
        Colorc colorOffset = block.getColorOffset(BlockPart.CENTER);
        Colorc colorSource = block.getColorSource(BlockPart.CENTER).calcColor(view, x, y, z);
        colorCache.setRed(colorSource.rf() * colorOffset.rf()).setGreen(colorSource.gf() * colorOffset.gf()).setBlue(colorSource.bf() * colorOffset.bf()).setAlpha(colorSource.af() * colorOffset.af());
        blockAppearance.getPart(BlockPart.CENTER).appendTo(chunkMesh, view, x, y, z, renderType, colorCache, vertexFlag);
    }
}
Also used : Side(org.terasology.engine.math.Side) Colorc(org.terasology.nui.Colorc) BlockAppearance(org.terasology.engine.world.block.BlockAppearance) Vector3ic(org.joml.Vector3ic) Color(org.terasology.nui.Color) Block(org.terasology.engine.world.block.Block) BlockMeshPart(org.terasology.engine.world.block.shapes.BlockMeshPart)

Example 10 with Side

use of org.terasology.engine.math.Side in project Terasology by MovingBlocks.

the class BlockBuilder method applyLiquidShapes.

private void applyLiquidShapes(Block block, Map<BlockPart, BlockTile> tiles) {
    for (Side side : Side.values()) {
        BlockPart part = BlockPart.fromSide(side);
        BlockTile blockTile = tiles.get(part);
        if (blockTile != null) {
            BlockMeshPart lowMeshPart = lowShape.getMeshPart(part).mapTexCoords(worldAtlas.getTexCoords(blockTile, true), worldAtlas.getRelativeTileSize(), blockTile.getLength());
            block.setLowLiquidMesh(part.getSide(), lowMeshPart);
            BlockMeshPart topMeshPart = topShape.getMeshPart(part).mapTexCoords(worldAtlas.getTexCoords(blockTile, true), worldAtlas.getRelativeTileSize(), blockTile.getLength());
            block.setTopLiquidMesh(part.getSide(), topMeshPart);
        }
    }
}
Also used : Side(org.terasology.engine.math.Side) BlockPart(org.terasology.engine.world.block.BlockPart) BlockTile(org.terasology.engine.world.block.tiles.BlockTile) BlockMeshPart(org.terasology.engine.world.block.shapes.BlockMeshPart)

Aggregations

Side (org.terasology.engine.math.Side)15 Block (org.terasology.engine.world.block.Block)7 Vector3i (org.joml.Vector3i)6 Vector3ic (org.joml.Vector3ic)3 Rotation (org.terasology.engine.math.Rotation)3 BlockPart (org.terasology.engine.world.block.BlockPart)3 BlockUri (org.terasology.engine.world.block.BlockUri)3 Name (org.terasology.gestalt.naming.Name)3 BlockFamily (org.terasology.engine.world.block.family.BlockFamily)2 BlockMeshPart (org.terasology.engine.world.block.shapes.BlockMeshPart)2 Preconditions (com.google.common.base.Preconditions)1 Lists (com.google.common.collect.Lists)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Objects (java.util.Objects)1 Vector2f (org.joml.Vector2f)1 Test (org.junit.jupiter.api.Test)1 PlaySoundEvent (org.terasology.engine.audio.events.PlaySoundEvent)1 ReceiveEvent (org.terasology.engine.entitySystem.event.ReceiveEvent)1