Skip to content

Commit

Permalink
Allow teams to score points from the ball landing off of the court
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Jul 9, 2024
1 parent 0e0b78a commit 7b51b78
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import net.minecraft.world.gen.chunk.ChunkGenerator;
Expand Down Expand Up @@ -49,6 +50,10 @@ public PlayerOfferResult acceptOffer(PlayerOffer offer, ServerWorld world, GameM
});
}

public Box getBallSpawnBox() {
return this.ballSpawn.getBounds().asBox();
}

public void spawnAtBall(ServerWorld world, Entity entity) {
this.spawn(world, entity, this.ballSpawn.getBounds().center());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ public class VolleyballActivePhase implements PlayerAttackEntityEvent, GameActiv
* The number of ticks since the ball was last hit.
*/
private int inactiveBallTicks = -1;
/**
* The team that last hit the ball.
*/
private TeamEntry possessionTeam;

public VolleyballActivePhase(ServerWorld world, GameSpace gameSpace, VolleyballMap map, TeamManager teamManager, GlobalWidgets widgets, VolleyballConfig config, Text shortName) {
this.world = world;
Expand Down Expand Up @@ -140,7 +144,14 @@ public static void open(GameSpace gameSpace, ServerWorld world, VolleyballMap ma
@Override
public ActionResult onAttackEntity(ServerPlayerEntity attacker, Hand hand, Entity attacked, EntityHitResult hitResult) {
if (attacked == this.ball) {
PlayerEntry entry = this.getPlayerEntry(attacker);

if (entry != null) {
this.possessionTeam = entry.getTeam();
}

this.inactiveBallTicks = 0;

return ActionResult.PASS;
}
return ActionResult.FAIL;
Expand Down Expand Up @@ -176,10 +187,13 @@ public void onTick() {
for (TeamEntry team : this.getTeams()) {
if (team.isBallOnCourt(this.ball)) {
team.getOtherTeam().incrementScore();
this.scoreboard.update();
break;
}
}

if (this.possessionTeam != null && this.hasBallLandedOffCourt()) {
this.possessionTeam.getOtherTeam().incrementScore();
}
}

// Attempt to determine a winner
Expand Down Expand Up @@ -239,6 +253,10 @@ public Set<TeamEntry> getTeams() {
return this.teams;
}

public VolleyballScoreboard getScoreboard() {
return this.scoreboard;
}

// Utilities
public TeamEntry getChatTeam(ServerPlayerEntity sender) {
if (!(sender instanceof HasChatChannel)) return null;
Expand All @@ -253,6 +271,7 @@ public TeamEntry getChatTeam(ServerPlayerEntity sender) {
public Entity spawnBall() {
this.ball = this.config.getBallEntityConfig().createEntity(this.world, this.world.getRandom());
this.inactiveBallTicks = -1;
this.possessionTeam = null;

this.map.spawnAtBall(this.world, this.ball);
this.world.spawnEntity(this.ball);
Expand All @@ -269,6 +288,10 @@ public void resetBall() {
this.ballTicks = this.config.getResetBallTicks();
}

private boolean hasBallLandedOffCourt() {
return this.ball != null && this.ball.isOnGround() && !this.map.getBallSpawnBox().intersects(ball.getBoundingBox());
}

public Text getInactiveBallResetText() {
return Text.translatable("text.volleyball.inactive_ball_reset").formatted(Formatting.RED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,14 @@ public boolean isBallOnCourt(Entity ball) {
return ball.isOnGround() || ball.getY() < this.courtBox.minY;
}

public int incrementScore() {
public void incrementScore() {
this.phase.getGameSpace().getPlayers().sendMessage(this.getScoreText());
this.phase.pling();

this.phase.resetBall();

return this.score += 1;
this.score += 1;
this.phase.getScoreboard().update();
}

public boolean hasRequiredScore() {
Expand Down

0 comments on commit 7b51b78

Please sign in to comment.