From 2a53b72d1596fd36faf1974786bd44a1cb3b0fc9 Mon Sep 17 00:00:00 2001 From: haykam821 <24855774+haykam821@users.noreply.github.com> Date: Tue, 9 Jul 2024 03:27:21 -0400 Subject: [PATCH] Add markers to indicate where the ball landed --- .../volleyball/game/ball/ActiveBallState.java | 4 +- .../volleyball/game/ball/BallState.java | 7 ++++ .../volleyball/game/ball/EntityBallState.java | 41 ++++++++++++++++++- .../game/ball/InactiveBallState.java | 14 ++++++- .../game/phase/VolleyballActivePhase.java | 6 ++- .../game/player/team/TeamEntry.java | 14 +++++-- 6 files changed, 76 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/github/haykam821/volleyball/game/ball/ActiveBallState.java b/src/main/java/io/github/haykam821/volleyball/game/ball/ActiveBallState.java index f9b4533..aada441 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/ball/ActiveBallState.java +++ b/src/main/java/io/github/haykam821/volleyball/game/ball/ActiveBallState.java @@ -31,13 +31,13 @@ public void onTick() { if (this.onEntityTick()) return; if (this.ticksSinceHit >= this.phase.getConfig().getInactiveBallTicks()) { - this.phase.resetBall(); + this.phase.resetBall(null); this.phase.getGameSpace().getPlayers().sendMessage(INACTIVE_BALL_RESET_MESSAGE); } else { this.ticksSinceHit += 1; if (this.possessionTeam != null && this.phase.hasBallLandedOffCourt(this.ball)) { - this.possessionTeam.getOtherTeam().incrementScore(); + this.possessionTeam.incrementOtherTeamScore(); } } } diff --git a/src/main/java/io/github/haykam821/volleyball/game/ball/BallState.java b/src/main/java/io/github/haykam821/volleyball/game/ball/BallState.java index e21e3b7..edcc437 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/ball/BallState.java +++ b/src/main/java/io/github/haykam821/volleyball/game/ball/BallState.java @@ -2,9 +2,12 @@ import java.util.Objects; +import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; import io.github.haykam821.volleyball.game.phase.VolleyballActivePhase; import io.github.haykam821.volleyball.game.player.PlayerEntry; +import io.github.haykam821.volleyball.game.player.team.TeamEntry; import net.minecraft.entity.Entity; +import net.minecraft.server.world.ServerWorld; public abstract class BallState { protected final VolleyballActivePhase phase; @@ -22,4 +25,8 @@ public boolean onAttackEntity(PlayerEntry entry, Entity entity) { public void destroy(BallState newState) { return; } + + public HolderAttachment createMarker(TeamEntry team, ServerWorld world) { + return null; + } } diff --git a/src/main/java/io/github/haykam821/volleyball/game/ball/EntityBallState.java b/src/main/java/io/github/haykam821/volleyball/game/ball/EntityBallState.java index 3788c07..6a31aba 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/ball/EntityBallState.java +++ b/src/main/java/io/github/haykam821/volleyball/game/ball/EntityBallState.java @@ -2,12 +2,30 @@ import java.util.Objects; +import org.joml.Matrix4f; + +import eu.pb4.polymer.virtualentity.api.ElementHolder; +import eu.pb4.polymer.virtualentity.api.attachment.ChunkAttachment; +import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; +import eu.pb4.polymer.virtualentity.api.elements.TextDisplayElement; import io.github.haykam821.volleyball.game.phase.VolleyballActivePhase; import io.github.haykam821.volleyball.game.player.PlayerEntry; import io.github.haykam821.volleyball.game.player.team.TeamEntry; import net.minecraft.entity.Entity; +import net.minecraft.entity.decoration.DisplayEntity.BillboardMode; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; +import net.minecraft.util.math.MathHelper; public abstract class EntityBallState extends BallState { + private static final Text MARKER_TEXT = Text.literal("❌").formatted(Formatting.BOLD); + + private static final Matrix4f MARKER_TRANSFORMATION = new Matrix4f() + .translate(-1 / 32f, 0, 21 / 64f) + .rotateX(MathHelper.PI / -2) + .scale(2); + protected final Entity ball; public EntityBallState(VolleyballActivePhase phase, Entity ball) { @@ -21,13 +39,13 @@ public EntityBallState(VolleyballActivePhase phase, Entity ball) { */ protected final boolean onEntityTick() { if (!this.ball.isAlive()) { - this.phase.resetBall(); + this.phase.resetBall(null); return true; } for (TeamEntry team : this.phase.getTeams()) { if (team.isBallOnCourt(this.ball)) { - team.getOtherTeam().incrementScore(); + team.incrementOtherTeamScore(); return true; } } @@ -53,4 +71,23 @@ public final void destroy(BallState newState) { this.ball.discard(); } } + + @Override + public HolderAttachment createMarker(TeamEntry team, ServerWorld world) { + Text text = MARKER_TEXT.copy().formatted(team.getFormatting()); + TextDisplayElement element = new TextDisplayElement(text); + + element.setShadow(true); + element.setBackground(0); + + element.setInvisible(true); + + element.setTransformation(MARKER_TRANSFORMATION); + element.setBillboardMode(BillboardMode.VERTICAL); + + ElementHolder holder = new ElementHolder(); + holder.addElement(element); + + return ChunkAttachment.of(holder, world, this.ball.getPos()); + } } diff --git a/src/main/java/io/github/haykam821/volleyball/game/ball/InactiveBallState.java b/src/main/java/io/github/haykam821/volleyball/game/ball/InactiveBallState.java index db5f14b..e14196c 100644 --- a/src/main/java/io/github/haykam821/volleyball/game/ball/InactiveBallState.java +++ b/src/main/java/io/github/haykam821/volleyball/game/ball/InactiveBallState.java @@ -1,13 +1,17 @@ package io.github.haykam821.volleyball.game.ball; +import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; import io.github.haykam821.volleyball.game.phase.VolleyballActivePhase; public class InactiveBallState extends BallState { + private final HolderAttachment markerAttachment; + private int ticksUntilSpawn; - public InactiveBallState(VolleyballActivePhase phase) { + public InactiveBallState(VolleyballActivePhase phase, HolderAttachment markerAttachment) { super(phase); + this.markerAttachment = markerAttachment; this.ticksUntilSpawn = phase.getConfig().getResetBallTicks(); } @@ -19,4 +23,12 @@ public void onTick() { this.phase.spawnBall(); } } + + @Override + public void destroy(BallState newState) { + if (this.markerAttachment != null) { + this.markerAttachment.holder().destroy(); + this.markerAttachment.destroy(); + } + } } 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 7bc9d44..0476461 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 @@ -5,6 +5,7 @@ import java.util.Map; import java.util.Set; +import eu.pb4.polymer.virtualentity.api.attachment.HolderAttachment; import io.github.haykam821.volleyball.game.VolleyballConfig; import io.github.haykam821.volleyball.game.ball.BallState; import io.github.haykam821.volleyball.game.ball.InactiveBallState; @@ -250,8 +251,9 @@ public void spawnBall() { /** * Resets the ball, transitioning it into the ready state. */ - public void resetBall() { - this.setBallState(new InactiveBallState(this)); + public void resetBall(TeamEntry markerTeam) { + HolderAttachment markerAttachment = markerTeam == null ? null : this.ballState.createMarker(markerTeam, this.world); + this.setBallState(new InactiveBallState(this, markerAttachment)); } public void setBallState(BallState ballState) { 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 c0aa8f8..9b73d30 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,11 +62,15 @@ public boolean isBallOnCourt(Entity ball) { return ball.isOnGround() || ball.getY() < this.courtBox.minY; } - public void incrementScore() { + public void incrementOtherTeamScore() { + this.getOtherTeam().incrementScore(this); + } + + private void incrementScore(TeamEntry causer) { this.phase.getGameSpace().getPlayers().sendMessage(this.getScoreText()); this.phase.pling(); - this.phase.resetBall(); + this.phase.resetBall(causer); this.score += 1; this.phase.getScoreboard().update(); @@ -88,6 +92,10 @@ public Text getScoreboardEntryText() { return Text.translatable("text.volleyball.scoreboard.entry", this.getName(), this.score); } + public Formatting getFormatting() { + return this.config.chatFormatting(); + } + public Text getName() { return this.config.name(); } @@ -101,7 +109,7 @@ private TemplateRegion getRegion(MapTemplate template, String key) { return template.getMetadata().getFirstRegion(this.key.id() + "_" + key); } - public TeamEntry getOtherTeam() { + private TeamEntry getOtherTeam() { for (TeamEntry team : this.phase.getTeams()) { if (this != team) return team; }