From 7b51b78eee0e121685606c150948d1c9d124a6d3 Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Tue, 9 Jul 2024 01:33:37 -0400 Subject: [PATCH] Allow teams to score points from the ball landing off of the court --- .../volleyball/game/map/VolleyballMap.java | 5 ++++ .../game/phase/VolleyballActivePhase.java | 25 ++++++++++++++++++- .../game/player/team/TeamEntry.java | 6 +++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/haykam821/volleyball/game/map/VolleyballMap.java b/src/main/java/io/github/haykam821/volleyball/game/map/VolleyballMap.java index 43e3ce3..47ff6f7 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/map/VolleyballMap.java +++ b/src/main/java/io/github/haykam821/volleyball/game/map/VolleyballMap.java @@ -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; @@ -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()); } 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 1421c1d..78787d9 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 @@ -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; @@ -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; @@ -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 @@ -239,6 +253,10 @@ public Set getTeams() { return this.teams; } + public VolleyballScoreboard getScoreboard() { + return this.scoreboard; + } + // Utilities public TeamEntry getChatTeam(ServerPlayerEntity sender) { if (!(sender instanceof HasChatChannel)) return null; @@ -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); @@ -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); } diff --git a/src/main/java/io/github/haykam821/volleyball/game/player/team/TeamEntry.java b/src/main/java/io/github/haykam821/volleyball/game/player/team/TeamEntry.java index d78206a..c0aa8f8 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/player/team/TeamEntry.java +++ b/src/main/java/io/github/haykam821/volleyball/game/player/team/TeamEntry.java @@ -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() {