Gears now work omni-directional

This commit is contained in:
Calclavia 2014-01-16 21:32:30 +08:00
parent 4c4423e282
commit 102db7356d
5 changed files with 201 additions and 170 deletions

View file

@ -19,7 +19,7 @@ public class TileGenerator extends TileElectrical implements IMechanical
public TileGenerator() public TileGenerator()
{ {
energy = new EnergyStorageHandler(10000); energy = new EnergyStorageHandler(10000, 100);
this.ioMap = 728; this.ioMap = 728;
} }
@ -30,7 +30,7 @@ public class TileGenerator extends TileElectrical implements IMechanical
{ {
if (!isInversed) if (!isInversed)
{ {
this.energy.receiveEnergy(power, true); this.power -= this.energy.receiveEnergy(power, true);
this.produce(); this.produce();
} }
else else
@ -51,6 +51,12 @@ public class TileGenerator extends TileElectrical implements IMechanical
this.power = (long) Math.abs(torque * speed); this.power = (long) Math.abs(torque * speed);
} }
@Override
public long getPower()
{
return this.power;
}
@Override @Override
public void onTorqueChange(ForgeDirection side, int speed) public void onTorqueChange(ForgeDirection side, int speed)
{ {

View file

@ -22,175 +22,175 @@ import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
public class RenderPipe extends TileEntitySpecialRenderer public class RenderPipe extends TileEntitySpecialRenderer
{ {
public static ModelPipe MODEL_PIPE = new ModelPipe(); public static ModelPipe MODEL_PIPE = new ModelPipe();
public static ModelOpenTrough MODEL_TROUGH_PIPE = new ModelOpenTrough(); public static ModelOpenTrough MODEL_TROUGH_PIPE = new ModelOpenTrough();
private static HashMap<Pair<FluidContainerMaterial, Integer>, ResourceLocation> TEXTURES = new HashMap<Pair<FluidContainerMaterial, Integer>, ResourceLocation>(); private static HashMap<Pair<FluidContainerMaterial, Integer>, ResourceLocation> TEXTURES = new HashMap<Pair<FluidContainerMaterial, Integer>, ResourceLocation>();
public static ResourceLocation TEXTURE = new ResourceLocation(Reference.DOMAIN, Reference.MODEL_DIRECTORY + "pipe/iron.png"); public static ResourceLocation TEXTURE = new ResourceLocation(Reference.DOMAIN, Reference.MODEL_PATH + "pipe/iron.png");
@Override @Override
public void renderTileEntityAt(TileEntity te, double d, double d1, double d2, float f) public void renderTileEntityAt(TileEntity te, double d, double d1, double d2, float f)
{ {
FluidContainerMaterial mat = FluidContainerMaterial.IRON; FluidContainerMaterial mat = FluidContainerMaterial.IRON;
if (te.getBlockMetadata() < FluidContainerMaterial.values().length) if (te.getBlockMetadata() < FluidContainerMaterial.values().length)
{ {
mat = FluidContainerMaterial.values()[te.getBlockMetadata()]; mat = FluidContainerMaterial.values()[te.getBlockMetadata()];
} }
if (te instanceof TilePipe) if (te instanceof TilePipe)
{ {
TilePipe tile = (TilePipe) te; TilePipe tile = (TilePipe) te;
if (mat == FluidContainerMaterial.WOOD || mat == FluidContainerMaterial.STONE) if (mat == FluidContainerMaterial.WOOD || mat == FluidContainerMaterial.STONE)
{ {
FluidStack liquid = tile.getInternalTank().getFluid(); FluidStack liquid = tile.getInternalTank().getFluid();
int cap = tile.getInternalTank().getCapacity(); int cap = tile.getInternalTank().getCapacity();
// FluidStack liquid = new FluidStack(FluidRegistry.WATER, cap); // FluidStack liquid = new FluidStack(FluidRegistry.WATER, cap);
if (liquid != null && liquid.amount > 100) if (liquid != null && liquid.amount > 100)
{ {
float per = Math.max(1, (float) liquid.amount / (float) (cap)); float per = Math.max(1, (float) liquid.amount / (float) (cap));
int[] displayList = RenderFluidHelper.getFluidDisplayLists(liquid, te.worldObj, false); int[] displayList = RenderFluidHelper.getFluidDisplayLists(liquid, te.worldObj, false);
bindTexture(RenderFluidHelper.getFluidSheet(liquid)); bindTexture(RenderFluidHelper.getFluidSheet(liquid));
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ENABLE_BIT); GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_CULL_FACE); GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glDisable(GL11.GL_LIGHTING); GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND); GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glTranslatef((float) d + 0.3F, (float) d1 + 0.1F, (float) d2 + 0.3F); GL11.glTranslatef((float) d + 0.3F, (float) d1 + 0.1F, (float) d2 + 0.3F);
GL11.glScalef(0.4F, 0.4F, 0.4F); GL11.glScalef(0.4F, 0.4F, 0.4F);
GL11.glCallList(displayList[(int) (per * (RenderFluidHelper.DISPLAY_STAGES - 1))]); GL11.glCallList(displayList[(int) (per * (RenderFluidHelper.DISPLAY_STAGES - 1))]);
GL11.glPopAttrib(); GL11.glPopAttrib();
GL11.glPopMatrix(); GL11.glPopMatrix();
for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS) for (ForgeDirection direction : ForgeDirection.VALID_DIRECTIONS)
{ {
if (tile.canRenderSide(direction) && direction != ForgeDirection.UP && direction != ForgeDirection.DOWN) if (tile.canRenderSide(direction) && direction != ForgeDirection.UP && direction != ForgeDirection.DOWN)
{ {
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glPushAttrib(GL11.GL_ENABLE_BIT); GL11.glPushAttrib(GL11.GL_ENABLE_BIT);
GL11.glEnable(GL11.GL_CULL_FACE); GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glDisable(GL11.GL_LIGHTING); GL11.glDisable(GL11.GL_LIGHTING);
GL11.glEnable(GL11.GL_BLEND); GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
switch (direction.ordinal()) switch (direction.ordinal())
{ {
case 4: case 4:
GL11.glTranslatef((float) d + 0F, (float) d1 + 0.1F, (float) d2 + 0.3F); GL11.glTranslatef((float) d + 0F, (float) d1 + 0.1F, (float) d2 + 0.3F);
break; break;
case 5: case 5:
GL11.glTranslatef((float) d + 0.7F, (float) d1 + 0.1F, (float) d2 + 0.3F); GL11.glTranslatef((float) d + 0.7F, (float) d1 + 0.1F, (float) d2 + 0.3F);
break; break;
case 2: case 2:
GL11.glTranslatef((float) d + 0.3F, (float) d1 + 0.1F, (float) d2 + 0F); GL11.glTranslatef((float) d + 0.3F, (float) d1 + 0.1F, (float) d2 + 0F);
break; break;
case 3: case 3:
GL11.glTranslatef((float) d + 0.3F, (float) d1 + 0.1F, (float) d2 + 0.7F); GL11.glTranslatef((float) d + 0.3F, (float) d1 + 0.1F, (float) d2 + 0.7F);
break; break;
} }
GL11.glScalef(0.3F, 0.4F, 0.4F); GL11.glScalef(0.3F, 0.4F, 0.4F);
GL11.glCallList(displayList[(int) (per * (RenderFluidHelper.DISPLAY_STAGES - 1))]); GL11.glCallList(displayList[(int) (per * (RenderFluidHelper.DISPLAY_STAGES - 1))]);
GL11.glPopAttrib(); GL11.glPopAttrib();
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
} }
} }
} }
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glTranslatef((float) d + 0.5F, (float) d1 + 1.5F, (float) d2 + 0.5F); GL11.glTranslatef((float) d + 0.5F, (float) d1 + 1.5F, (float) d2 + 0.5F);
GL11.glScalef(1.0F, -1F, -1F); GL11.glScalef(1.0F, -1F, -1F);
bindTexture(RenderPipe.getTexture(mat, 0)); bindTexture(RenderPipe.getTexture(mat, 0));
render(mat, tile.getSubID(), tile.renderSides); render(mat, tile.getSubID(), tile.renderSides);
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
else else
{ {
GL11.glPushMatrix(); GL11.glPushMatrix();
GL11.glTranslatef((float) d + 0.5F, (float) d1 + 1.5F, (float) d2 + 0.5F); GL11.glTranslatef((float) d + 0.5F, (float) d1 + 1.5F, (float) d2 + 0.5F);
GL11.glScalef(1.0F, -1F, -1F); GL11.glScalef(1.0F, -1F, -1F);
render(mat, 0, (byte) 0b0); render(mat, 0, (byte) 0b0);
GL11.glPopMatrix(); GL11.glPopMatrix();
} }
} }
public static ResourceLocation getTexture(FluidContainerMaterial mat, int pipeID) public static ResourceLocation getTexture(FluidContainerMaterial mat, int pipeID)
{ {
if (mat != null) if (mat != null)
{ {
Pair<FluidContainerMaterial, Integer> index = new Pair<FluidContainerMaterial, Integer>(mat, pipeID); Pair<FluidContainerMaterial, Integer> index = new Pair<FluidContainerMaterial, Integer>(mat, pipeID);
if (!TEXTURES.containsKey(index)) if (!TEXTURES.containsKey(index))
{ {
String pipeName = ""; String pipeName = "";
if (EnumPipeType.get(pipeID) != null) if (EnumPipeType.get(pipeID) != null)
{ {
pipeName = EnumPipeType.get(pipeID).getName(pipeID); pipeName = EnumPipeType.get(pipeID).getName(pipeID);
} }
TEXTURES.put(index, new ResourceLocation(Reference.DOMAIN, Reference.MODEL_PATH + "pipe/" + mat.matName + ".png")); TEXTURES.put(index, new ResourceLocation(Reference.DOMAIN, Reference.MODEL_PATH + "pipe/" + mat.matName + ".png"));
} }
return TEXTURES.get(index); return TEXTURES.get(index);
} }
return TEXTURE; return TEXTURE;
} }
public static ResourceLocation getTexture(int meta) public static ResourceLocation getTexture(int meta)
{ {
return getTexture(FluidContainerMaterial.getFromItemMeta(meta), FluidContainerMaterial.getType(meta)); return getTexture(FluidContainerMaterial.getFromItemMeta(meta), FluidContainerMaterial.getType(meta));
} }
public static void render(FluidContainerMaterial mat, int pipeID, byte side) public static void render(FluidContainerMaterial mat, int pipeID, byte side)
{ {
if (mat == FluidContainerMaterial.WOOD) if (mat == FluidContainerMaterial.WOOD)
{ {
MODEL_TROUGH_PIPE.render(side, false); MODEL_TROUGH_PIPE.render(side, false);
} }
else if (mat == FluidContainerMaterial.STONE) else if (mat == FluidContainerMaterial.STONE)
{ {
MODEL_TROUGH_PIPE.render(side, true); MODEL_TROUGH_PIPE.render(side, true);
} }
else else
{ {
if (TileFluidNetwork.canRenderSide(side, ForgeDirection.DOWN)) if (TileFluidNetwork.canRenderSide(side, ForgeDirection.DOWN))
{ {
MODEL_PIPE.renderBottom(); MODEL_PIPE.renderBottom();
} }
if (TileFluidNetwork.canRenderSide(side, ForgeDirection.UP)) if (TileFluidNetwork.canRenderSide(side, ForgeDirection.UP))
{ {
MODEL_PIPE.renderTop(); MODEL_PIPE.renderTop();
} }
if (TileFluidNetwork.canRenderSide(side, ForgeDirection.NORTH)) if (TileFluidNetwork.canRenderSide(side, ForgeDirection.NORTH))
{ {
MODEL_PIPE.renderBack(); MODEL_PIPE.renderBack();
} }
if (TileFluidNetwork.canRenderSide(side, ForgeDirection.SOUTH)) if (TileFluidNetwork.canRenderSide(side, ForgeDirection.SOUTH))
{ {
MODEL_PIPE.renderFront(); MODEL_PIPE.renderFront();
} }
if (TileFluidNetwork.canRenderSide(side, ForgeDirection.WEST)) if (TileFluidNetwork.canRenderSide(side, ForgeDirection.WEST))
{ {
MODEL_PIPE.renderLeft(); MODEL_PIPE.renderLeft();
} }
if (TileFluidNetwork.canRenderSide(side, ForgeDirection.EAST)) if (TileFluidNetwork.canRenderSide(side, ForgeDirection.EAST))
{ {
MODEL_PIPE.renderRight(); MODEL_PIPE.renderRight();
} }
MODEL_PIPE.renderMiddle(); MODEL_PIPE.renderMiddle();
} }
} }
public static void render(int meta, byte sides) public static void render(int meta, byte sides)
{ {
render(FluidContainerMaterial.getFromItemMeta(meta), FluidContainerMaterial.getType(meta), sides); render(FluidContainerMaterial.getFromItemMeta(meta), FluidContainerMaterial.getType(meta), sides);
} }
} }

View file

@ -77,18 +77,17 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
public long getTorque() public long getTorque()
{ {
return (long) (force * radius); return (long) torque;// (force * radius);
} }
@Override @Override
public void update() public void update()
{ {
/**
* Update angle rotation. if (angularVelocity < 0 || torque == 0)
*/
if (angularVelocity > 0 && torque > 0)
{ {
angle += angularVelocity / 20; angularVelocity = 0;
torque = 0;
} }
// TODO: Should we average the torque? // TODO: Should we average the torque?
@ -103,12 +102,12 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
if (part instanceof PartGear) if (part instanceof PartGear)
{ {
torque = (torque + ((PartGear) part).torque) / 2; equatePower((PartGear) part, false);
((PartGear) part).torque = torque;
} }
} }
else if (tile instanceof IMechanical) else if (tile instanceof IMechanical)
{ {
torque = (long) (((IMechanical) tile).getPower() / angularVelocity);
((IMechanical) tile).setPower(torque, angularVelocity); ((IMechanical) tile).setPower(torque, angularVelocity);
} }
@ -124,10 +123,9 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{ {
TMultiPart neighbor = ((TileMultipart) checkTile).partMap(this.placementSide.ordinal()); TMultiPart neighbor = ((TileMultipart) checkTile).partMap(this.placementSide.ordinal());
if (neighbor instanceof PartGear) if (neighbor != this && neighbor instanceof PartGear)
{ {
torque = (torque - ((PartGear) neighbor).torque) / 2; equatePower((PartGear) neighbor, false);
((PartGear) neighbor).torque = -torque;
} }
} }
} }
@ -136,28 +134,53 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
for (int i = 0; i < 6; i++) for (int i = 0; i < 6; i++)
{ {
// TODO: Make it work with UP-DOWN // TODO: Make it work with UP-DOWN
if (i < 2) if (i < 4)
{ {
TMultiPart neighbor = tile().partMap(this.placementSide.getRotation(ForgeDirection.getOrientation(i)).ordinal()); TMultiPart neighbor = tile().partMap(this.placementSide.getRotation(ForgeDirection.getOrientation(i)).ordinal());
if (neighbor instanceof PartGear) if (neighbor != this && neighbor instanceof PartGear)
{ {
torque = (torque - ((PartGear) neighbor).torque) / 2; equatePower((PartGear) neighbor, false);
((PartGear) neighbor).torque = -torque;
} }
} }
} }
/**
* Update angle rotation.
*/
if (angularVelocity > 0 && torque != 0)
{
angle += angularVelocity / 20;
}
}
public void equatePower(PartGear neighbor, boolean isPositive)
{
if (isPositive)
{
torque = (torque + ((PartGear) neighbor).torque) / 2;
((PartGear) neighbor).torque = torque;
}
else
{
torque = (torque - ((PartGear) neighbor).torque) / 2;
((PartGear) neighbor).torque = -torque;
}
angularVelocity = (angularVelocity + ((PartGear) neighbor).angularVelocity) / 2;
((PartGear) neighbor).angularVelocity = angularVelocity;
} }
@Override @Override
public boolean activate(EntityPlayer player, MovingObjectPosition hit, ItemStack item) public boolean activate(EntityPlayer player, MovingObjectPosition hit, ItemStack item)
{ {
System.out.println("Torque" + this.torque + " Angular Velocity" + this.angularVelocity); System.out.println("Torque" + torque + " Angular Velocity" + angularVelocity);
if (player.isSneaking()) if (player.isSneaking())
{ {
this.torque += 10; this.torque += 10;
this.angularVelocity += 0.1f; this.angularVelocity += 0.2f;
} }
return false; return false;

View file

@ -63,7 +63,7 @@ public class RenderGear
break; break;
} }
GL11.glRotatef((float) Math.toDegrees(part.angle), 0, 1, 0); GL11.glRotatef((float) Math.toDegrees(part.angle) * (part.getTorque() > 0 ? 1 : -1), 0, 1, 0);
FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE); FMLClientHandler.instance().getClient().renderEngine.bindTexture(TEXTURE);
MODEL.renderAll(); MODEL.renderAll();

View file

@ -12,6 +12,8 @@ public interface IMechanical extends IConnectable
{ {
public void setPower(long torque, float speed); public void setPower(long torque, float speed);
public long getPower();
/** Called by the network when its torque value changes. */ /** Called by the network when its torque value changes. */
public void onTorqueChange(ForgeDirection side, int speed); public void onTorqueChange(ForgeDirection side, int speed);