mdbd/src/main/java/dev/loki/hatch/HatchCondition.java

76 lines
2.4 KiB
Java

package dev.loki.hatch;
import dev.loki.player.BasePlayer;
import dev.loki.player.PlayerState;
import java.util.Collection;
public class HatchCondition {
private final int minSurvivorsForOpen;
public HatchCondition(int minSurvivorsForOpen) {
this.minSurvivorsForOpen = minSurvivorsForOpen;
}
public boolean shouldSpawn(Collection<BasePlayer> players, int repairedGenerators) {
int aliveSurvivors = countAliveSurvivors(players);
return aliveSurvivors <= repairedGenerators;
}
public boolean shouldOpen(Hatch hatch, Collection<BasePlayer> players, int repairedGenerators) {
if (hatch.isOpen()) return false;
if (!hatch.isClosed() && !hatch.isHidden()) return false;
return shouldSpawn(players, repairedGenerators);
}
public boolean canOpen(Hatch hatch, Collection<BasePlayer> players, int repairedGenerators) {
if (!hatch.isHidden() && !hatch.isClosed()) return false;
int aliveSurvivors = countAliveSurvivors(players);
int requiredGens = Math.max(minSurvivorsForOpen, aliveSurvivors);
return repairedGenerators >= requiredGens;
}
public int countAliveSurvivors(Collection<BasePlayer> players) {
return (int) players.stream()
.filter(p -> !p.isKiller())
.filter(this::isAlive)
.count();
}
private boolean isAlive(BasePlayer player) {
PlayerState state = player.getState();
return state != PlayerState.DEAD
&& state != PlayerState.ESCAPED
&& state != PlayerState.HOOKED;
}
public int getRemainingAlive(Collection<BasePlayer> players) {
return countAliveSurvivors(players);
}
public boolean isLastSurvivor(Collection<BasePlayer> players) {
return countAliveSurvivors(players) == 1;
}
public boolean checkAndReveal(Hatch hatch, Collection<BasePlayer> players, int repairedGenerators) {
if (hatch.isHidden() && shouldSpawn(players, repairedGenerators)) {
hatch.reveal();
return true;
}
return false;
}
public boolean checkAndOpen(Hatch hatch, Collection<BasePlayer> players, int repairedGenerators) {
if (canOpen(hatch, players, repairedGenerators)) {
if (hatch.isHidden()) {
hatch.reveal();
}
hatch.open();
return true;
}
return false;
}
}