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();
}
}
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));
}
}
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));
}
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);
}
}
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);
}
}
}
Aggregations