Skip to content

Commit

Permalink
Add markers to indicate where the ball landed
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Jul 9, 2024
1 parent 6acd1d2 commit 2a53b72
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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();
}

Expand All @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand Down

0 comments on commit 2a53b72

Please sign in to comment.