From ce5a276b0ba645b0c740e22f5f554bcd65bfe8ff Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Tue, 9 Jul 2024 19:35:17 -0400 Subject: [PATCH] Add a delay to game closing Fixes #5 --- .../volleyball/game/VolleyballConfig.java | 12 ++++++++++- .../game/phase/VolleyballActivePhase.java | 21 ++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/main/java/io/github/haykam821/volleyball/game/VolleyballConfig.java b/src/main/java/io/github/haykam821/volleyball/game/VolleyballConfig.java index 5728490..142c6e0 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/VolleyballConfig.java +++ b/src/main/java/io/github/haykam821/volleyball/game/VolleyballConfig.java @@ -4,7 +4,10 @@ import com.mojang.serialization.codecs.RecordCodecBuilder; import io.github.haykam821.volleyball.entity.BallEntityConfig; +import net.minecraft.SharedConstants; import net.minecraft.util.Identifier; +import net.minecraft.util.math.intprovider.ConstantIntProvider; +import net.minecraft.util.math.intprovider.IntProvider; import xyz.nucleoid.plasmid.game.common.config.PlayerConfig; import xyz.nucleoid.plasmid.game.common.team.GameTeamList; @@ -15,6 +18,7 @@ public class VolleyballConfig { PlayerConfig.CODEC.fieldOf("players").forGetter(VolleyballConfig::getPlayerConfig), GameTeamList.CODEC.fieldOf("teams").forGetter(VolleyballConfig::getTeams), BallEntityConfig.CODEC.optionalFieldOf("ball_entity", BallEntityConfig.DEFAULT).forGetter(VolleyballConfig::getBallEntityConfig), + IntProvider.NON_NEGATIVE_CODEC.optionalFieldOf("ticks_until_close", ConstantIntProvider.create(SharedConstants.TICKS_PER_SECOND * 5)).forGetter(VolleyballConfig::getTicksUntilClose), Codec.INT.optionalFieldOf("required_score", 10).forGetter(VolleyballConfig::getRequiredScore), Codec.INT.optionalFieldOf("reset_ball_ticks", 20 * 3).forGetter(VolleyballConfig::getResetBallTicks), Codec.INT.optionalFieldOf("inactive_ball_ticks", 20 * 15).forGetter(VolleyballConfig::getInactiveBallTicks) @@ -28,12 +32,14 @@ public class VolleyballConfig { private final int requiredScore; private final int resetBallTicks; private final int inactiveBallTicks; + private final IntProvider ticksUntilClose; - public VolleyballConfig(Identifier map, PlayerConfig playerConfig, GameTeamList teams, BallEntityConfig ballEntityConfig, int requiredScore, int resetBallTicks, int inactiveBallTicks) { + public VolleyballConfig(Identifier map, PlayerConfig playerConfig, GameTeamList teams, BallEntityConfig ballEntityConfig, IntProvider ticksUntilClose, int requiredScore, int resetBallTicks, int inactiveBallTicks) { this.map = map; this.playerConfig = playerConfig; this.teams = teams; this.ballEntityConfig = ballEntityConfig; + this.ticksUntilClose = ticksUntilClose; this.requiredScore = requiredScore; this.resetBallTicks = resetBallTicks; this.inactiveBallTicks = inactiveBallTicks; @@ -55,6 +61,10 @@ public BallEntityConfig getBallEntityConfig() { return this.ballEntityConfig; } + public IntProvider getTicksUntilClose() { + return this.ticksUntilClose; + } + public int getRequiredScore() { return this.requiredScore; } diff --git a/src/main/java/io/github/haykam821/volleyball/game/phase/VolleyballActivePhase.java b/src/main/java/io/github/haykam821/volleyball/game/phase/VolleyballActivePhase.java index 0476461..445c184 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/phase/VolleyballActivePhase.java +++ b/src/main/java/io/github/haykam821/volleyball/game/phase/VolleyballActivePhase.java @@ -58,6 +58,7 @@ public class VolleyballActivePhase implements PlayerAttackEntityEvent, GameActiv private final VolleyballScoreboard scoreboard; private BallState ballState; + private int ticksUntilClose = -1; public VolleyballActivePhase(ServerWorld world, GameSpace gameSpace, VolleyballMap map, TeamManager teamManager, GlobalWidgets widgets, VolleyballConfig config, Text shortName) { this.world = world; @@ -163,11 +164,21 @@ public void onTick() { entry.onTick(); } + // Decrease ticks until game end to zero + if (this.isGameEnding()) { + if (this.ticksUntilClose == 0) { + this.gameSpace.close(GameCloseReason.FINISHED); + } + + this.ticksUntilClose -= 1; + return; + } + this.ballState.onTick(); // Attempt to determine a winner if (this.winManager.checkForWinner()) { - gameSpace.close(GameCloseReason.FINISHED); + this.endGame(); } } @@ -264,6 +275,14 @@ public void setBallState(BallState ballState) { this.ballState = ballState; } + private void endGame() { + this.ticksUntilClose = this.config.getTicksUntilClose().get(this.world.getRandom()); + } + + private boolean isGameEnding() { + return this.ticksUntilClose >= 0; + } + public boolean hasBallLandedOffCourt(Entity ball) { return ball.isOnGround() && !this.map.getBallSpawnBox().intersects(ball.getBoundingBox()); }