Added mechanical resistance

This commit is contained in:
Calclavia 2014-01-18 13:29:19 +08:00
parent 2cfd22a8d3
commit 8cab946411
3 changed files with 26 additions and 21 deletions

View file

@ -111,8 +111,9 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
} }
@Override @Override
public void onNetworkChanged() public float getResistance()
{ {
return 0.1f;
} }
/** /**
@ -336,9 +337,12 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
@Override @Override
public void sendNetworkPacket(long torque, float angularVelocity) public void sendNetworkPacket(long torque, float angularVelocity)
{
if (tile() != null)
{ {
tile().getWriteStream(this).writeByte(0).writeLong(torque).writeFloat(angularVelocity).writeBoolean(isClockwise); tile().getWriteStream(this).writeByte(0).writeLong(torque).writeFloat(angularVelocity).writeBoolean(isClockwise);
} }
}
public void read(MCDataInput packet, int packetID) public void read(MCDataInput packet, int packetID)
{ {

View file

@ -10,13 +10,15 @@ import universalelectricity.api.net.IConnector;
*/ */
public interface IMechanicalConnector extends IMechanical, IConnector<IMechanicalNetwork> public interface IMechanicalConnector extends IMechanical, IConnector<IMechanicalNetwork>
{ {
/**
* An update called by the network.
*/
public void onNetworkChanged();
/** /**
* Uses this connector to send a packet to the client. * Uses this connector to send a packet to the client.
*/ */
public void sendNetworkPacket(long torque, float angularVelocity); public void sendNetworkPacket(long torque, float angularVelocity);
/**
* The percentage of resistance caused by this connector.
*
* @return A small value, most likely less than one.
*/
public float getResistance();
} }

View file

@ -35,6 +35,9 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
private long torque = 0; private long torque = 0;
private float angularVelocity = 0; private float angularVelocity = 0;
/** The cached resistance caused by all connectors */
private float connectorResistance = 0;
/** The direction in which a conductor is placed relative to a specific conductor. */ /** The direction in which a conductor is placed relative to a specific conductor. */
protected final HashMap<Object, EnumSet<ForgeDirection>> handlerDirectionMap = new LinkedHashMap<Object, EnumSet<ForgeDirection>>(); protected final HashMap<Object, EnumSet<ForgeDirection>> handlerDirectionMap = new LinkedHashMap<Object, EnumSet<ForgeDirection>>();
@ -49,7 +52,7 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
*/ */
if (getPower() > 0) if (getPower() > 0)
{ {
float division = 0; float division = connectorResistance;
for (IMechanical node : this.getNodes()) for (IMechanical node : this.getNodes())
{ {
@ -74,17 +77,10 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
/** /**
* Send network update packet for connectors. * Send network update packet for connectors.
*/ */
boolean isFirst = true;
for (IMechanicalConnector connector : this.getConnectors()) for (IMechanicalConnector connector : this.getConnectors())
{
if (isFirst)
{ {
connector.sendNetworkPacket(torque, angularVelocity); connector.sendNetworkPacket(torque, angularVelocity);
isFirst = false; break;
}
connector.onNetworkChanged();
} }
} }
@ -174,6 +170,11 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
@Override @Override
public void reconstruct() public void reconstruct()
{ {
// Reset
prevTorque = torque = 0;
prevAngularVelocity = angularVelocity = 0;
connectorResistance = 0;
if (this.getConnectors().size() > 0) if (this.getConnectors().size() > 0)
{ {
// Reset all values related to wires // Reset all values related to wires
@ -196,10 +197,6 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
} }
} }
} }
// Reset energy.
prevTorque = torque = 0;
prevAngularVelocity = angularVelocity = 0;
} }
/** Segmented out call so overriding can be done when conductors are reconstructed. */ /** Segmented out call so overriding can be done when conductors are reconstructed. */
@ -211,6 +208,8 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
{ {
reconstructHandler(connector.getConnections()[i], ForgeDirection.getOrientation(i).getOpposite()); reconstructHandler(connector.getConnections()[i], ForgeDirection.getOrientation(i).getOpposite());
} }
connectorResistance += connector.getResistance();
} }
/** Segmented out call so overriding can be done when machines are reconstructed. */ /** Segmented out call so overriding can be done when machines are reconstructed. */