I am specifically looking for input on naming ModifierCalculator
and Modifiers
. I would also appreciate input on whether ModifierCalculator
should extend EnumMap
instead of having it as a field.
These classes are for modifying the status of units. The StatType
enum contains different types of ..stats, such as "life", "armor", "weapon damage", etc.
StatModifier
objects' purpose is to modify a unit's stat. This kind of modification can be a flat bonus, a percentage increment or a multiplication, using the formula
total = (base + flat)*(1 + percent_increments)*(1 + mult_1)*(1 + mult_2)*... *(1 + mult_n)
ModifierCalculator
puts all three kinds of modifiers in a map of Modifiers
data structures with StatType
serving as keyword.
public enum StatType
{
//code is trivial to the problem
}
public enum StatModifierType
{
FLAT, PERCENTAGE, MULTIPLICATIVE;
}
public class StatModifier
{
private StatType statType;
private double magnitude;
private StatModifierType modType;
public StatModifier(StatType statType, double magnitude, StatModifierType modType)
{
this.statType = statType;
this.magnitude = magnitude;
this.modType = modType;
}
public StatType getStatType()
{
return statType;
}
public double getMagnitude()
{
return magnitude;
}
public StatModifierType getModifierType()
{
return modType;
}
}
public class Modifiers
{
public double flat = 0;
public double percentage = 0;
public final ArrayList<Double> multipliers = new ArrayList<Double>();
}
public class ModifierCalculator
{
private EnumMap<Stat, Modifiers> statToModifiers
= new EnumMap<Stat, Modifiers>(Stat.class);
public ModifierCalculator()
{
for (StatType s : Stat.values())
statToModifiers.put(s, new Modifiers());
}
public void evaluate(ArrayList<StatModifier> modifiers)
{
for (StatModifier modifier : modifiers)
evaluate(modifier);
}
public void evaluate(StatModifier modifier)
{
double magnitude = modifier.getMagnitude();
Modifiers mods = statToModifiers.get(modifier.getStat());
switch (modifier.getModifierType())
{
case FLAT:
mods.flat += magnitude;
break;
case PERCENTAGE:
mods.percentage += magnitude;
break;
case MULTIPLICATIVE:
mods.multipliers.add(1 + magnitude);
break;
}
}
public Modifiers getStatModifiers(StatType statType)
{
return statToModifiers.get(statType);
}
}