Added load calculation mechanic

This commit is contained in:
Calclavia 2014-01-18 13:22:37 +08:00
parent a5c124eeac
commit 2cfd22a8d3
5 changed files with 59 additions and 18 deletions

View file

@ -19,7 +19,7 @@ public class TileGenerator extends TileElectrical implements IMechanical
/** Generator turns KE -> EE. Inverted one will turn EE -> KE. */
public boolean isInversed = false;
private float torqueRatio = 500;
private float torqueRatio = 8000;
public TileGenerator()
{
@ -29,7 +29,7 @@ public class TileGenerator extends TileElectrical implements IMechanical
public float toggleRatio()
{
return torqueRatio = (torqueRatio + 100) % energy.getMaxExtract();
return torqueRatio = (torqueRatio + 1000) % energy.getMaxExtract();
}
@Override
@ -54,7 +54,7 @@ public class TileGenerator extends TileElectrical implements IMechanical
{
float angularVelocity = extract / torqueRatio;
long torque = (long) (extract / angularVelocity);
((IMechanical) mechanical).onReceiveEnergy(getOuputDirection().getOpposite(), torque, angularVelocity);
((IMechanical) mechanical).onReceiveEnergy(getOuputDirection().getOpposite(), torque, angularVelocity, true);
}
}
}
@ -93,9 +93,9 @@ public class TileGenerator extends TileElectrical implements IMechanical
}
@Override
public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity)
public long onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity, boolean doReceive)
{
energy.receiveEnergy((long) (torque * angularVelocity), true);
return energy.receiveEnergy((long) (torque * angularVelocity), doReceive);
}
@Override

View file

@ -90,7 +90,7 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
{
if (manualCrankTime > 0)
{
onReceiveEnergy(null, 20, 0.3f);
onReceiveEnergy(null, 20, 0.3f, true);
manualCrankTime--;
}
}
@ -99,10 +99,13 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
/**
* Update angle rotation.
*/
if (isClockwise)
angle += this.getNetwork().getAngularVelocity() / 20f;
else
angle -= this.getNetwork().getAngularVelocity() / 20f;
if (getNetwork().getPower() > 0)
{
if (isClockwise)
angle += getNetwork().getAngularVelocity() / 20f;
else
angle -= getNetwork().getAngularVelocity() / 20f;
}
}
}
@ -295,13 +298,15 @@ public class PartGear extends JCuboidPart implements JNormalOcclusion, TFacePart
return false;
}
public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity)
public long onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity, boolean doReceive)
{
if (!world().isRemote)
if (!world().isRemote && doReceive)
{
getNetwork().applyEnergy(torque, angularVelocity);
markRotationUpdate = true;
}
return (long) (torque * angularVelocity);
}
@Override

View file

@ -67,7 +67,7 @@ public class TraitMechanical extends TileMultipart implements IMechanical
}
@Override
public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity)
public long onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity, boolean doReceive)
{
TMultiPart part = this.partMap(from.ordinal());
@ -75,8 +75,10 @@ public class TraitMechanical extends TileMultipart implements IMechanical
{
if (this.mechanicalInterfaces.contains(part))
{
((IMechanical) part).onReceiveEnergy(from, torque, angularVelocity);
return ((IMechanical) part).onReceiveEnergy(from, torque, angularVelocity, doReceive);
}
}
return 0;
}
}

View file

@ -14,5 +14,5 @@ public interface IMechanical extends IConnectable
* @param doReceive If false, the charge will only be simulated.
* @return Amount of energy that was accepted by the block.
*/
public void onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity);
public long onReceiveEnergy(ForgeDirection from, long torque, float angularVelocity, boolean doReceive);
}

View file

@ -44,8 +44,36 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
@Override
public void update()
{
/**
* Calculate load
*/
if (getPower() > 0)
{
float division = 0;
for (IMechanical node : this.getNodes())
{
for (ForgeDirection dir : handlerDirectionMap.get(node))
{
division += node.onReceiveEnergy(dir, torque, angularVelocity, false) / torque;
}
}
if (division > 0)
{
torque /= division / 2;
angularVelocity /= division / 2;
}
}
/**
* Update all connectors
*/
if (getPrevTorque() != getTorque() || getPrevAngularVelocity() != getAngularVelocity())
{
/**
* Send network update packet for connectors.
*/
boolean isFirst = true;
for (IMechanicalConnector connector : this.getConnectors())
@ -60,11 +88,17 @@ public class MechanicalNetwork extends Network<IMechanicalNetwork, IMechanicalCo
}
}
for (IMechanical node : this.getNodes())
/**
* Distribute energy to handlers
*/
if (getPower() > 0)
{
for (ForgeDirection dir : handlerDirectionMap.get(node))
for (IMechanical node : this.getNodes())
{
node.onReceiveEnergy(dir, torque, angularVelocity);
for (ForgeDirection dir : handlerDirectionMap.get(node))
{
node.onReceiveEnergy(dir, torque, angularVelocity, true);
}
}
}