TerraForged/src/main/java/com/terraforged/util/RangeModifier.java

38 lines
917 B
Java
Raw Normal View History

2020-05-14 18:56:57 +00:00
package com.terraforged.util;
2020-05-13 16:22:34 +00:00
public abstract class RangeModifier {
protected final float from;
protected final float to;
2020-05-13 16:22:34 +00:00
protected final float max;
private final float range;
public RangeModifier(float from, float max, boolean exclusive) {
this.from = from;
this.to = max;
this.max = exclusive ? 0 : 1;
this.range = Math.abs(max - from);
2020-05-13 16:22:34 +00:00
}
public float apply(float value) {
if (from < to) {
if (value <= from) {
2020-05-13 16:22:34 +00:00
return 0F;
}
if (value >= to) {
return max;
2020-05-13 16:22:34 +00:00
}
return (value - from) / range;
} else if (from > to) {
if (value <= to) {
return max;
2020-05-13 16:22:34 +00:00
}
if (value >= from) {
2020-05-13 16:22:34 +00:00
return 0F;
}
return 1 - ((value - to) / range);
2020-05-13 16:22:34 +00:00
}
return 0F;
2020-05-13 16:22:34 +00:00
}
}