Generators can now produce mechanical energy

This commit is contained in:
Calclavia 2014-01-18 02:07:56 +08:00
parent 9527175f0c
commit 22b4188443
5 changed files with 130 additions and 10 deletions

View file

@ -25,9 +25,9 @@ public class BlockGenerator extends BlockRIRotatable
if (!world.isRemote)
{
((TileGenerator) tileEntity).isInversed = !((TileGenerator) tileEntity).isInversed;
entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "electrical" : "mechanical") + " energy.");
entityPlayer.addChatMessage("Generator now producing " + (((TileGenerator) tileEntity).isInversed ? "mechanical" : "electrical") + " energy.");
}
return true;
}
return false;

View file

@ -1,10 +1,11 @@
package resonantinduction.electrical.generator;
import java.util.EnumSet;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.mechanical.network.IMechanical;
import resonantinduction.mechanical.network.IMechanicalNetwork;
import resonantinduction.mechanical.network.MechanicalNetwork;
import universalelectricity.api.energy.EnergyStorageHandler;
import universalelectricity.api.vector.Vector3;
import calclavia.lib.prefab.tile.TileElectrical;
/**
@ -37,11 +38,44 @@ public class TileGenerator extends TileElectrical implements IMechanical
}
else
{
// TODO:Do something here to set mechanical energy.
Vector3 outputVector = new Vector3(this).modifyPositionFromSide(getOuputDirection());
Object mechanical = outputVector.getTileEntity(worldObj);
if (mechanical instanceof IMechanical)
{
long extract = energy.extractEnergy();
((IMechanical) mechanical).onReceiveEnergy(getOuputDirection().getOpposite(), (long) (extract / 0.5f), 0.5f);
}
}
}
}
@Override
public EnumSet<ForgeDirection> getInputDirections()
{
EnumSet<ForgeDirection> dirs = EnumSet.noneOf(ForgeDirection.class);
dirs.add(getInputDirection());
return dirs;
}
@Override
public EnumSet<ForgeDirection> getOutputDirections()
{
EnumSet<ForgeDirection> dirs = EnumSet.noneOf(ForgeDirection.class);
dirs.add(getOuputDirection());
return dirs;
}
public ForgeDirection getInputDirection()
{
return ForgeDirection.getOrientation(this.getBlockMetadata()).getOpposite();
}
public ForgeDirection getOuputDirection()
{
return ForgeDirection.getOrientation(this.getBlockMetadata());
}
private boolean isFunctioning()
{
return true;

View file

@ -2,6 +2,7 @@ package resonantinduction.mechanical;
import resonantinduction.mechanical.gear.PartGear;
import codechicken.multipart.MultiPartRegistry;
import codechicken.multipart.MultipartGenerator;
import codechicken.multipart.MultiPartRegistry.IPartFactory;
import codechicken.multipart.TMultiPart;
@ -14,6 +15,7 @@ public class MultipartMechanical implements IPartFactory
public MultipartMechanical()
{
MultiPartRegistry.registerParts(this, PART_TYPES);
MultipartGenerator.registerTrait("resonantinduction.mechanical.network.IMechanical", "resonantinduction.mechanical.gear.TraitMechanical");
}
@Override

View file

@ -36,7 +36,7 @@ import cpw.mods.fml.relauncher.SideOnly;
* @author Calclavia
*
*/
public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart, IMechanicalConnector
public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart, IMechanical, IMechanicalConnector
{
public static Cuboid6[][] oBoxes = new Cuboid6[6][2];
@ -104,7 +104,6 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
angle += this.getNetwork().getAngularVelocity() / 20;
else
angle -= this.getNetwork().getAngularVelocity() / 20;
// this.sendRotationUpdate();
}
public void refresh()
@ -208,6 +207,9 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{
getNetwork().applyEnergy(torque, angularVelocity);
markRotationUpdate = true;
if (!world().isRemote)
this.sendRotationUpdate(torque, angularVelocity);
}
@Override
@ -242,13 +244,13 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{
if (packetID == 0)
{
((MechanicalNetwork) this.getNetwork()).angularVelocity = packet.readFloat();
onReceiveEnergy(null, packet.readLong(), packet.readFloat());
}
}
public void sendRotationUpdate()
public void sendRotationUpdate(long torque, float angularVelocity)
{
tile().getWriteStream(this).writeByte(0).writeFloat(this.getNetwork().getAngularVelocity());
tile().getWriteStream(this).writeByte(0).writeLong(torque).writeFloat(angularVelocity);
}
@Override

View file

@ -0,0 +1,82 @@
package resonantinduction.mechanical.gear;
import java.util.HashSet;
import java.util.Set;
import net.minecraftforge.common.ForgeDirection;
import resonantinduction.mechanical.network.IMechanical;
import codechicken.multipart.TMultiPart;
import codechicken.multipart.TileMultipart;
public class TraitMechanical extends TileMultipart implements IMechanical
{
public Set<IMechanical> mechanicalInterfaces = new HashSet<IMechanical>();
@Override
public void copyFrom(TileMultipart that)
{
super.copyFrom(that);
if (that instanceof TraitMechanical)
{
this.mechanicalInterfaces = ((TraitMechanical) that).mechanicalInterfaces;
}
}
@Override
public void bindPart(TMultiPart part)
{
super.bindPart(part);
if (part instanceof IMechanical)
{
this.mechanicalInterfaces.add((IMechanical) part);
}
}
@Override
public void partRemoved(TMultiPart part, int p)
{
super.partRemoved(part, p);
if (part instanceof IMechanical)
{
this.mechanicalInterfaces.remove(part);
}
}
@Override
public void clearParts()
{
super.clearParts();
this.mechanicalInterfaces.clear();
}
@Override
public boolean canConnect(ForgeDirection direction)
{
for (IMechanical connector : this.mechanicalInterfaces)
{
if (connector.canConnect(direction.getOpposite()))
{
return true;
}
}
return false;
}
@Override
public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity)
{
TMultiPart part = this.partMap(from.ordinal());
if (part != null)
{
if (this.mechanicalInterfaces.contains(part))
{
((IMechanical) part).onReceiveEnergy(from, torque, angularVelocity);
}
}
}
}