From 93f9dceb902c07f3706f9f9ad179d96380650362 Mon Sep 17 00:00:00 2001 From: CovertJaguar Date: Sun, 20 Oct 2013 00:36:37 -0700 Subject: [PATCH] Improve Facade Rendering + Interaction --- .../transport/BlockGenericPipe.java | 57 ++++++++++------ common/buildcraft/transport/ItemFacade.java | 50 +++++++------- .../buildcraft/transport/PipeRenderState.java | 2 + .../transport/TransportConstants.java | 4 +- .../transport/render/PipeRendererWorld.java | 66 +++++++++++++++---- 5 files changed, 119 insertions(+), 60 deletions(-) diff --git a/common/buildcraft/transport/BlockGenericPipe.java b/common/buildcraft/transport/BlockGenericPipe.java index d33b9e4e..414796f3 100644 --- a/common/buildcraft/transport/BlockGenericPipe.java +++ b/common/buildcraft/transport/BlockGenericPipe.java @@ -81,11 +81,12 @@ public class BlockGenericPipe extends BlockContainer { private static final ForgeDirection[] DIR_VALUES = ForgeDirection.values(); private static Random rand = new Random(); private boolean skippedFirstIconRegister; - private char renderAxis = 'a'; + private boolean[] renderSide = new boolean[6]; /* Defined subprograms ************************************************* */ public BlockGenericPipe(int i) { super(i, Material.glass); + setRenderAllSides(); } @Override @@ -114,19 +115,33 @@ public class BlockGenericPipe extends BlockContainer { } public void setRenderAxis(char axis) { - this.renderAxis = axis; + Arrays.fill(renderSide, false); + if (axis == 'x') { + renderSide[4] = true; + renderSide[5] = true; + } + if (axis == 'y') { + renderSide[0] = true; + renderSide[1] = true; + } + if (axis == 'z') { + renderSide[2] = true; + renderSide[3] = true; + } + } + + public final void setRenderAllSides() { + Arrays.fill(renderSide, true); + } + + public void setRenderSide(ForgeDirection side, boolean render) { + renderSide[side.ordinal()] = render; } @Override @SideOnly(Side.CLIENT) public boolean shouldSideBeRendered(IBlockAccess blockAccess, int x, int y, int z, int side) { - if (renderAxis == 'x') - return side == 4 || side == 5; - if (renderAxis == 'y') - return side == 0 || side == 1; - if (renderAxis == 'z') - return side == 2 || side == 3; - return true; + return renderSide[side]; } @Override @@ -583,11 +598,11 @@ public class BlockGenericPipe extends BlockContainer { if (rayTraceResult != null && rayTraceResult.boundingBox != null) { switch (rayTraceResult.hitPart) { - case Gate: - Pipe pipe = getPipe(world, x, y, z); - return pipe.gate.getGateItem(); - case Plug: - return new ItemStack(BuildCraftTransport.plugItem); + case Gate: + Pipe pipe = getPipe(world, x, y, z); + return pipe.gate.getGateItem(); + case Plug: + return new ItemStack(BuildCraftTransport.plugItem); } } return super.getPickBlock(target, world, x, y, z); @@ -772,15 +787,17 @@ public class BlockGenericPipe extends BlockContainer { } private boolean addOrStripFacade(World world, int x, int y, int z, EntityPlayer player, ForgeDirection side, Pipe pipe) { + RaytraceResult rayTraceResult = doRayTrace(world, x, y, z, player); if (player.isSneaking()) { - RaytraceResult rayTraceResult = doRayTrace(world, x, y, z, player); if (rayTraceResult != null && rayTraceResult.hitPart == Part.Facade) { if (stripFacade(pipe, rayTraceResult.sideHit)) return true; } } - if (addFacade(player, pipe, side)) - return true; + if (rayTraceResult != null && rayTraceResult.hitPart == Part.Pipe) { + if (addFacade(player, pipe, rayTraceResult.sideHit != null && rayTraceResult.sideHit != ForgeDirection.UNKNOWN ? rayTraceResult.sideHit : side)) + return true; + } return false; } @@ -874,11 +891,13 @@ public class BlockGenericPipe extends BlockContainer { @SuppressWarnings({"all"}) @SideOnly(Side.CLIENT) @Override - public Icon getBlockTexture(IBlockAccess iblockaccess, int i, int j, int k, int l) { + public Icon getBlockTexture(IBlockAccess iblockaccess, int x, int y, int z, int side) { - TileEntity tile = iblockaccess.getBlockTileEntity(i, j, k); + TileEntity tile = iblockaccess.getBlockTileEntity(x, y, z); if (!(tile instanceof IPipeRenderState)) return null; + if (((IPipeRenderState) tile).getRenderState().textureArray != null) + return ((IPipeRenderState) tile).getRenderState().textureArray[side]; return ((IPipeRenderState) tile).getRenderState().currentTexture; // Pipe pipe = getPipe(iblockaccess, i, j, k); diff --git a/common/buildcraft/transport/ItemFacade.java b/common/buildcraft/transport/ItemFacade.java index f76c96e3..eec7a89f 100644 --- a/common/buildcraft/transport/ItemFacade.java +++ b/common/buildcraft/transport/ItemFacade.java @@ -1,6 +1,7 @@ package buildcraft.transport; import buildcraft.BuildCraftTransport; +import buildcraft.api.core.Position; import buildcraft.api.recipes.AssemblyRecipe; import buildcraft.core.CreativeTabBuildCraft; import buildcraft.core.ItemBuildCraft; @@ -17,12 +18,15 @@ import java.util.Set; import net.minecraft.block.Block; import net.minecraft.client.renderer.texture.IconRegister; import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; +import net.minecraftforge.common.ForgeDirection; public class ItemFacade extends ItemBuildCraft { @@ -68,30 +72,26 @@ public class ItemFacade extends ItemBuildCraft { } } -// @Override -// public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World worldObj, int x, int y, int z, int side, float hitX, float hitY, float hitZ) { -// if (worldObj.isRemote) -// return false; -// TileEntity tile = worldObj.getBlockTileEntity(x, y, z); -// if (!(tile instanceof TileGenericPipe)) -// return false; -// TileGenericPipe pipeTile = (TileGenericPipe) tile; -// -// if (player.isSneaking()) { // Strip facade -// if (!pipeTile.hasFacade(ForgeDirection.VALID_DIRECTIONS[side])) -// return false; -// pipeTile.dropFacade(ForgeDirection.VALID_DIRECTIONS[side]); -// return true; -// } else { -// if (((TileGenericPipe) tile).addFacade(ForgeDirection.values()[side], ItemFacade.getBlockId(stack), ItemFacade.getMetaData(stack))) { -// if (!player.capabilities.isCreativeMode) { -// stack.stackSize--; -// } -// return true; -// } -// return false; -// } -// } + @Override + public boolean onItemUse(ItemStack stack, EntityPlayer player, World worldObj, int x, int y, int z, int side, float hitX, float hitY, float hitZ) { + if (worldObj.isRemote) + return false; + Position pos = new Position(x, y, z, ForgeDirection.getOrientation(side)); + pos.moveForwards(1.0); + + TileEntity tile = worldObj.getBlockTileEntity((int) pos.x, (int) pos.y, (int) pos.z); + if (!(tile instanceof TileGenericPipe)) + return false; + TileGenericPipe pipeTile = (TileGenericPipe) tile; + + if (pipeTile.addFacade(ForgeDirection.getOrientation(side).getOpposite(), ItemFacade.getBlockId(stack), ItemFacade.getMetaData(stack))) { + if (!player.capabilities.isCreativeMode) { + stack.stackSize--; + } + return true; + } + return false; + } public static void initialize() { for (Field f : Block.class.getDeclaredFields()) { @@ -253,7 +253,7 @@ public class ItemFacade extends ItemBuildCraft { public static ItemStack getStack(Block block, int metadata) { return getStack(block.blockID, metadata); } - + public static ItemStack getStack(int blockID, int metadata) { ItemStack stack = new ItemStack(BuildCraftTransport.facadeItem, 1, 0); NBTTagCompound nbt = new NBTTagCompound("tag"); diff --git a/common/buildcraft/transport/PipeRenderState.java b/common/buildcraft/transport/PipeRenderState.java index 8f25c942..200ea884 100644 --- a/common/buildcraft/transport/PipeRenderState.java +++ b/common/buildcraft/transport/PipeRenderState.java @@ -28,6 +28,8 @@ public class PipeRenderState implements IClientState { */ @SideOnly(Side.CLIENT) public Icon currentTexture; + @SideOnly(Side.CLIENT) + public Icon[] textureArray; public void setHasGate(boolean value) { if (hasGate != value) { diff --git a/common/buildcraft/transport/TransportConstants.java b/common/buildcraft/transport/TransportConstants.java index 10a2eaf8..e76ef048 100644 --- a/common/buildcraft/transport/TransportConstants.java +++ b/common/buildcraft/transport/TransportConstants.java @@ -8,14 +8,12 @@ */ package buildcraft.transport; -import net.minecraft.item.ItemStack; - /** * * @author CovertJaguar */ public class TransportConstants { - public static final float FACADE_THICKNESS = 1F / 16F; + public static final float FACADE_THICKNESS = 2F / 16F; public static final float PIPE_NORMAL_SPEED = 0.01F; } diff --git a/common/buildcraft/transport/render/PipeRendererWorld.java b/common/buildcraft/transport/render/PipeRendererWorld.java index 77f8f4a8..2a9db5f2 100644 --- a/common/buildcraft/transport/render/PipeRendererWorld.java +++ b/common/buildcraft/transport/render/PipeRendererWorld.java @@ -22,9 +22,6 @@ import net.minecraftforge.common.ForgeDirection; public class PipeRendererWorld implements ISimpleBlockRenderingHandler { - - - private void renderAllFaceExeptAxe(RenderBlocks renderblocks, BlockGenericPipe block, Icon icon, int x, int y, int z, char axe) { float minX = (float) renderblocks.renderMinX; float minY = (float) renderblocks.renderMinY; @@ -148,27 +145,60 @@ public class PipeRendererWorld implements ISimpleBlockRenderingHandler { } - private void pipeFacadeRenderer(RenderBlocks renderblocks, Block block, PipeRenderState state, int x, int y, int z) { + private void pipeFacadeRenderer(RenderBlocks renderblocks, BlockGenericPipe block, PipeRenderState state, int x, int y, int z) { float zFightOffset = 1F / 4096F; float[][] zeroState = new float[3][2]; // X START - END - zeroState[0][0] = 0.0F - zFightOffset / 2; - zeroState[0][1] = 1.0F + zFightOffset / 2; + zeroState[0][0] = 0.0F; + zeroState[0][1] = 1.0F; // Y START - END - zeroState[1][0] = 0.0F - zFightOffset; + zeroState[1][0] = 0.0F; zeroState[1][1] = TransportConstants.FACADE_THICKNESS; // Z START - END zeroState[2][0] = 0.0F; zeroState[2][1] = 1.0F; + float[] xOffsets = new float[6]; + xOffsets[0] = zFightOffset; + xOffsets[1] = zFightOffset; + xOffsets[2] = 0; + xOffsets[3] = 0; + xOffsets[4] = 0; + xOffsets[5] = 0; + + float[] yOffsets = new float[6]; + yOffsets[0] = 0; + yOffsets[1] = 0; + yOffsets[2] = zFightOffset; + yOffsets[3] = zFightOffset; + yOffsets[4] = 0; + yOffsets[5] = 0; + + float[] zOffsets = new float[6]; + zOffsets[0] = zFightOffset; + zOffsets[1] = zFightOffset; + zOffsets[2] = 0; + zOffsets[3] = 0; + zOffsets[4] = 0; + zOffsets[5] = 0; + + state.textureArray = new Icon[6]; + for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) { int facadeId = state.facadeMatrix.getFacadeBlockId(direction); if (facadeId != 0) { Block renderBlock = Block.blocksList[facadeId]; int renderMeta = state.facadeMatrix.getFacadeMetaId(direction); - state.currentTexture = renderBlock.getIcon(direction.ordinal(), renderMeta); + + for (ForgeDirection side : ForgeDirection.VALID_DIRECTIONS) { + state.textureArray[side.ordinal()] = renderBlock.getIcon(side.ordinal(), renderMeta); + if (side == direction || side == direction.getOpposite()) + block.setRenderSide(side, true); + else + block.setRenderSide(side, state.facadeMatrix.getFacadeBlockId(side) == 0); + } try { BlockGenericPipe.facadeRenderColor = Item.itemsList[state.facadeMatrix.getFacadeBlockId(direction)].getColorFromItemStack(new ItemStack(facadeId, 1, renderMeta), 0); @@ -190,16 +220,18 @@ public class PipeRendererWorld implements ISimpleBlockRenderingHandler { // Hollow facade if (state.pipeConnectionMatrix.isConnected(direction)) { float[][] rotated = MatrixTranformations.deepClone(zeroState); + rotated[0][0] = CoreConstants.PIPE_MIN_POS - zFightOffset; + rotated[0][1] = CoreConstants.PIPE_MAX_POS + zFightOffset; rotated[2][0] = 0.0F; rotated[2][1] = CoreConstants.PIPE_MIN_POS - zFightOffset; - rotated[1][0] -= zFightOffset / 2; MatrixTranformations.transform(rotated, direction); renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]); renderblocks.renderStandardBlock(block, x, y, z); rotated = MatrixTranformations.deepClone(zeroState); + rotated[0][0] = CoreConstants.PIPE_MIN_POS - zFightOffset; + rotated[0][1] = CoreConstants.PIPE_MAX_POS + zFightOffset; rotated[2][0] = CoreConstants.PIPE_MAX_POS + zFightOffset; - rotated[1][0] -= zFightOffset / 2; MatrixTranformations.transform(rotated, direction); renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]); renderblocks.renderStandardBlock(block, x, y, z); @@ -207,7 +239,6 @@ public class PipeRendererWorld implements ISimpleBlockRenderingHandler { rotated = MatrixTranformations.deepClone(zeroState); rotated[0][0] = 0.0F; rotated[0][1] = CoreConstants.PIPE_MIN_POS - zFightOffset; - rotated[1][1] -= zFightOffset; MatrixTranformations.transform(rotated, direction); renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]); renderblocks.renderStandardBlock(block, x, y, z); @@ -215,14 +246,20 @@ public class PipeRendererWorld implements ISimpleBlockRenderingHandler { rotated = MatrixTranformations.deepClone(zeroState); rotated[0][0] = CoreConstants.PIPE_MAX_POS + zFightOffset; rotated[0][1] = 1F; - rotated[1][1] -= zFightOffset; MatrixTranformations.transform(rotated, direction); renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]); renderblocks.renderStandardBlock(block, x, y, z); } else { // Solid facade float[][] rotated = MatrixTranformations.deepClone(zeroState); MatrixTranformations.transform(rotated, direction); - renderblocks.setRenderBounds(rotated[0][0], rotated[1][0], rotated[2][0], rotated[0][1], rotated[1][1], rotated[2][1]); + + renderblocks.setRenderBounds( + rotated[0][0] + xOffsets[direction.ordinal()], + rotated[1][0] + yOffsets[direction.ordinal()], + rotated[2][0] + zOffsets[direction.ordinal()], + rotated[0][1] + xOffsets[direction.ordinal()], + rotated[1][1] + yOffsets[direction.ordinal()], + rotated[2][1] + zOffsets[direction.ordinal()]); renderblocks.renderStandardBlock(block, x, y, z); } @@ -239,6 +276,9 @@ public class PipeRendererWorld implements ISimpleBlockRenderingHandler { BlockGenericPipe.facadeRenderColor = -1; } + state.textureArray = null; + block.setRenderAllSides(); + // X START - END zeroState[0][0] = CoreConstants.PIPE_MIN_POS; zeroState[0][1] = CoreConstants.PIPE_MAX_POS;