Boosters with the same multiplier now stack

This commit is contained in:
Indyuce 2020-07-02 19:51:29 +02:00
parent c0141e2972
commit fa0d13efa8
2 changed files with 26 additions and 2 deletions

View File

@ -4,11 +4,15 @@ import java.util.UUID;
public class Booster { public class Booster {
private final UUID uuid = UUID.randomUUID(); private final UUID uuid = UUID.randomUUID();
private final long date = System.currentTimeMillis(), length; private final long date = System.currentTimeMillis();
private final Profession profession; private final Profession profession;
private final double extra; private final double extra;
private final String author; private final String author;
// length not final because boosters can stack, this allows to reduce the
// amount of boosters
private long length;
public Booster(double extra, long length) { public Booster(double extra, long length) {
this(null, null, extra, length); this(null, null, extra, length);
} }
@ -40,6 +44,10 @@ public class Booster {
return length; return length;
} }
public void addLength(long length) {
this.length += length;
}
public boolean hasProfession() { public boolean hasProfession() {
return profession != null; return profession != null;
} }
@ -67,4 +75,8 @@ public class Booster {
public String getAuthor() { public String getAuthor() {
return author; return author;
} }
public boolean canStackWith(Booster booster) {
return extra == booster.extra && (profession != null ? profession.equals(booster.getProfession()) : booster.getProfession() == null);
}
} }

View File

@ -11,7 +11,16 @@ public class BoosterManager {
private List<Booster> map = new ArrayList<>(); private List<Booster> map = new ArrayList<>();
public void register(Booster booster) { public void register(Booster booster) {
// always flush booster list to reduce future calculations
flush(); flush();
for (Booster active : map)
if (active.canStackWith(booster)) {
active.addLength(booster.getLength());
return;
}
map.add(booster); map.add(booster);
} }
@ -25,14 +34,17 @@ public class BoosterManager {
public int calculateExp(Profession profession, double exp) { public int calculateExp(Profession profession, double exp) {
flush(); flush();
for (Booster booster : map) for (Booster booster : map)
if (booster.getProfession() == profession) if (booster.getProfession() == profession)
exp = booster.calculateExp(exp); exp = booster.calculateExp(exp);
return (int) exp; return (int) exp;
} }
public List<Booster> getBoosters() { public List<Booster> getBoosters() {
flush(); flush();
return map; return map;
} }