2020-05-14 18:56:57 +00:00
|
|
|
package com.terraforged.util;
|
2020-05-13 16:22:34 +00:00
|
|
|
|
|
|
|
public abstract class RangeModifier {
|
|
|
|
|
2020-05-15 21:21:13 +00:00
|
|
|
protected final float from;
|
|
|
|
protected final float to;
|
2020-05-13 16:22:34 +00:00
|
|
|
protected final float max;
|
|
|
|
private final float range;
|
|
|
|
|
2020-05-15 21:21:13 +00:00
|
|
|
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) {
|
2020-05-15 21:21:13 +00:00
|
|
|
if (from < to) {
|
|
|
|
if (value <= from) {
|
2020-05-13 16:22:34 +00:00
|
|
|
return 0F;
|
|
|
|
}
|
2020-05-15 21:21:13 +00:00
|
|
|
if (value >= to) {
|
|
|
|
return max;
|
2020-05-13 16:22:34 +00:00
|
|
|
}
|
2020-05-15 21:21:13 +00:00
|
|
|
return (value - from) / range;
|
|
|
|
} else if (from > to) {
|
|
|
|
if (value <= to) {
|
|
|
|
return max;
|
2020-05-13 16:22:34 +00:00
|
|
|
}
|
2020-05-15 21:21:13 +00:00
|
|
|
if (value >= from) {
|
2020-05-13 16:22:34 +00:00
|
|
|
return 0F;
|
|
|
|
}
|
2020-05-15 21:21:13 +00:00
|
|
|
return 1 - ((value - to) / range);
|
2020-05-13 16:22:34 +00:00
|
|
|
}
|
2020-05-15 21:21:13 +00:00
|
|
|
return 0F;
|
2020-05-13 16:22:34 +00:00
|
|
|
}
|
|
|
|
}
|