Skip to content

Commit

Permalink
feat: allow multiple gates per flag
Browse files Browse the repository at this point in the history
  • Loading branch information
Restioson committed Jun 11, 2024
1 parent b7b9737 commit f5e7a45
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void update(long time) {
Text line;
if (capturing || percent > 0) {
line = Text.literal("(" + percent + "%) ").append(flagName);
} else if (flag.gate != null && time - flag.gate.timeOfLastBash < 5 * 20) {
} else if (flag.gateUnderAttack(time)) {
line = Text.literal("(!) ").append(flagName);
} else {
line = flagName;
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/io/github/restioson/siege/game/map/SiegeFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public final class SiegeFlag {
@Nullable
public SiegeSpawn defenderRespawn;

@Nullable
public SiegeGate gate;
public List<SiegeGate> gates;

public GameTeam team;

Expand Down Expand Up @@ -172,6 +171,18 @@ public void closeCaptureBar() {
public boolean isFrontLine(long time) {
CapturingState state = this.capturingState;
return (state != null && state.hasAlert() && state != CapturingState.SECURING)
|| (this.gate != null && time - this.gate.timeOfLastBash < 5 * 20);
|| (this.gateUnderAttack(time));
}

public boolean gateUnderAttack(long time) {
long lastBash = this.gates
.stream()
.map(gate -> gate.timeOfLastBash)
.max(Long::compareTo)
.stream()
.findAny()
.orElse(0L);

return time - lastBash < 5 * 20;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,27 @@ private void addFlagsToMap(SiegeMap map, MapTemplateMetadata metadata) {
.map(region -> {
NbtCompound data = region.getData();

String id = data.getString("id");
SiegeFlag flag = flags.get(id);
String gateId = data.getString("id");
String flagIdRaw = data.getString("flag");
final String flagId = flagIdRaw.isEmpty() ? gateId : flagIdRaw;

SiegeFlag flag = flags.get(flagId);
if (flag == null) {
throw new GameOpenException(Text.literal("Gate missing flag with id '" + id + "'!"));
var text = Text.literal(String.format("Gate (id '%s') missing flag with id '%s'!", gateId, flagId));

if (flagIdRaw.isEmpty()) {
text = text.append(Text.literal("\nNote: flag id was implicitly defined as the gate id, as `flag` was missing in data."));
}

throw new GameOpenException(text);
}

TemplateRegion portcullisRegion = metadata.getRegions("portcullis")
.filter(r -> id.equalsIgnoreCase(r.getData().getString("id")))
.filter(r -> gateId.equalsIgnoreCase(r.getData().getString("id")))
.findFirst()
.orElseThrow(() -> {
Siege.LOGGER.error("Gate \"{}\" missing portcullis!", id);
return new GameOpenException(Text.literal("Gate missing portcullis!"));
Siege.LOGGER.error("Gate \"{}\" missing portcullis!", gateId);
return new GameOpenException(Text.literal(String.format("Gate (id '%s') missing portcullis!", gateId)));
});

NbtCompound portcullisData = portcullisRegion.getData();
Expand All @@ -241,13 +250,13 @@ private void addFlagsToMap(SiegeMap map, MapTemplateMetadata metadata) {
}

BlockBounds brace = metadata.getRegions("gate_brace")
.filter(r -> id.equalsIgnoreCase(r.getData().getString("id")))
.filter(r -> flagId.equalsIgnoreCase(r.getData().getString("id")))
.map(TemplateRegion::getBounds)
.findFirst()
.orElse(null);

SiegeGate gate = new SiegeGate(flag, region.getBounds(), portcullisRegion.getBounds(), brace, retractHeight, repairHealthThreshold, maxHealth);
flag.gate = gate;
flag.gates.add(gate);
return gate;
})
.collect(Collectors.toList());
Expand Down

0 comments on commit f5e7a45

Please sign in to comment.