IndustrialWires/src/main/java/malte0811/industrialWires/mech_mb/MechEnergy.java

116 lines
2.9 KiB
Java
Raw Normal View History

2017-10-04 17:05:04 +02:00
/*
* This file is part of Industrial Wires.
2018-02-28 21:06:33 +01:00
* Copyright (C) 2016-2018 malte0811
2017-10-04 17:05:04 +02:00
* Industrial Wires is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Industrial Wires is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Industrial Wires. If not, see <http://www.gnu.org/licenses/>.
*/
package malte0811.industrialWires.mech_mb;
2017-10-04 17:05:04 +02:00
import net.minecraft.util.math.MathHelper;
2017-10-04 17:05:04 +02:00
public final class MechEnergy {
private double speed = 0;
public boolean invalid = false;
2017-10-04 17:05:04 +02:00
public final double weight;
2017-10-04 17:05:04 +02:00
public MechEnergy(double weight, double speed) {
this.weight = weight;
this.speed = speed;
}
2017-10-04 17:05:04 +02:00
public double getEnergy() {
return .5 * weight * speed * speed;
2017-10-04 17:05:04 +02:00
}
public double getSpeed() {
return speed;
}
public void addEnergy(double energy) {
if (energy <= 0) {
return;
}
double targetEnergy = getEnergy() + energy;
speed = Math.sqrt(2 * targetEnergy / weight);
2017-10-04 17:05:04 +02:00
}
public void extractEnergy(double energy) {
if (energy <= 0) {
return;
}
2017-10-04 17:05:04 +02:00
double oldEnergy = getEnergy();
energy = Math.min(energy, oldEnergy);
speed = Math.sqrt(2 * (oldEnergy - energy) / weight);
2017-10-04 17:05:04 +02:00
}
public void decaySpeed(double decay) {
speed *= decay;
2018-05-13 18:54:26 +02:00
if (speed < .01)
speed = 0;
2017-10-04 17:05:04 +02:00
}
private static final int TICKS_FOR_ADJUSTMENT = 30;
private double targetSpeed;
private double oldSpeed = -1;
2018-05-13 18:54:26 +02:00
private int ticksTillReached = -1;
2017-10-04 17:05:04 +02:00
//ONLY USE FOR SYNCING
public void setTargetSpeed(double speed) {
targetSpeed = speed;
oldSpeed = getSpeed();
ticksTillReached = TICKS_FOR_ADJUSTMENT;
}
public boolean clientUpdate() {
if (ticksTillReached >= 0) {
speed = ((TICKS_FOR_ADJUSTMENT - ticksTillReached) * targetSpeed +
ticksTillReached * oldSpeed) / TICKS_FOR_ADJUSTMENT;
ticksTillReached--;
return true;
} else {
oldSpeed = -1;
return false;
}
}
//Sound helper methods
public float getVolumeSlow() {
return (1-getSoundLambda())*getTotalVolume();
}
public float getVolumeFast() {
return getSoundLambda()*getTotalVolume();
}
public float getPitch() {
return (float) Math.min(.3*Math.sqrt(getSpeedForSound()), 2);
}
private float getTotalVolume() {
if (invalid)
return 0;
float ret = (float) (weight / 20e3 * Math.tanh(getSpeedForSound()/30));
ret = Math.min(ret, 1.5F);
return ret;
}
private float getSoundLambda() {
return (float) MathHelper.clamp(getSpeedForSound() / 20 - .5F, 0, 1);
}
private double getSpeedForSound() {
final double MIN_SPEED = 5;
return getSpeed()-MIN_SPEED;//Volume should be zero by the time the sound stops
2017-10-04 17:05:04 +02:00
}
}