Skip to content

Commit

Permalink
Add a delay to game closing
Browse files Browse the repository at this point in the history
Fixes #5
  • Loading branch information
haykam821 committed Jul 9, 2024
1 parent 2a53b72 commit ce5a276
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)
Expand All @@ -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;
Expand All @@ -55,6 +61,10 @@ public BallEntityConfig getBallEntityConfig() {
return this.ballEntityConfig;
}

public IntProvider getTicksUntilClose() {
return this.ticksUntilClose;
}

public int getRequiredScore() {
return this.requiredScore;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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());
}
Expand Down

0 comments on commit ce5a276

Please sign in to comment.