-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split ball state into multiple classes
- Loading branch information
Showing
6 changed files
with
209 additions
and
61 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
src/main/java/io/github/haykam821/volleyball/game/ball/ActiveBallState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.haykam821.volleyball.game.ball; | ||
|
||
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.text.Text; | ||
import net.minecraft.util.Formatting; | ||
|
||
public class ActiveBallState extends EntityBallState { | ||
private static final Text INACTIVE_BALL_RESET_MESSAGE = Text.translatable("text.volleyball.inactive_ball_reset").formatted(Formatting.RED); | ||
|
||
/** | ||
* The number of ticks since the ball was last hit. | ||
*/ | ||
private int ticksSinceHit = 0; | ||
|
||
/** | ||
* The team that last hit the ball. | ||
*/ | ||
private TeamEntry possessionTeam; | ||
|
||
public ActiveBallState(VolleyballActivePhase phase, Entity ball, TeamEntry possessionTeam) { | ||
super(phase, ball); | ||
|
||
this.possessionTeam = possessionTeam; | ||
} | ||
|
||
@Override | ||
public void onTick() { | ||
if (this.onEntityTick()) return; | ||
|
||
if (this.ticksSinceHit >= this.phase.getConfig().getInactiveBallTicks()) { | ||
this.phase.resetBall(); | ||
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(); | ||
} | ||
} | ||
} | ||
|
||
public void onHitBall(PlayerEntry entry) { | ||
this.ticksSinceHit = 0; | ||
|
||
if (entry != null) { | ||
this.possessionTeam = entry.getTeam(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/io/github/haykam821/volleyball/game/ball/BallState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.github.haykam821.volleyball.game.ball; | ||
|
||
import java.util.Objects; | ||
|
||
import io.github.haykam821.volleyball.game.phase.VolleyballActivePhase; | ||
import io.github.haykam821.volleyball.game.player.PlayerEntry; | ||
import net.minecraft.entity.Entity; | ||
|
||
public abstract class BallState { | ||
protected final VolleyballActivePhase phase; | ||
|
||
public BallState(VolleyballActivePhase phase) { | ||
this.phase = Objects.requireNonNull(phase); | ||
} | ||
|
||
public abstract void onTick(); | ||
|
||
public boolean onAttackEntity(PlayerEntry entry, Entity entity) { | ||
return false; | ||
} | ||
|
||
public void destroy(BallState newState) { | ||
return; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/io/github/haykam821/volleyball/game/ball/EntityBallState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.github.haykam821.volleyball.game.ball; | ||
|
||
import java.util.Objects; | ||
|
||
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; | ||
|
||
public abstract class EntityBallState extends BallState { | ||
protected final Entity ball; | ||
|
||
public EntityBallState(VolleyballActivePhase phase, Entity ball) { | ||
super(phase); | ||
|
||
this.ball = Objects.requireNonNull(ball); | ||
} | ||
|
||
/** | ||
* @return whether the ball tick was handled by shared logic | ||
*/ | ||
protected final boolean onEntityTick() { | ||
if (!this.ball.isAlive()) { | ||
this.phase.resetBall(); | ||
return true; | ||
} | ||
|
||
for (TeamEntry team : this.phase.getTeams()) { | ||
if (team.isBallOnCourt(this.ball)) { | ||
team.getOtherTeam().incrementScore(); | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public final boolean onAttackEntity(PlayerEntry entry, Entity entity) { | ||
if (entity == this.ball) { | ||
this.onHitBall(entry); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected abstract void onHitBall(PlayerEntry entry); | ||
|
||
@Override | ||
public final void destroy(BallState newState) { | ||
if (!(newState instanceof EntityBallState)) { | ||
this.ball.discard(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/github/haykam821/volleyball/game/ball/InactiveBallState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.haykam821.volleyball.game.ball; | ||
|
||
import io.github.haykam821.volleyball.game.phase.VolleyballActivePhase; | ||
|
||
public class InactiveBallState extends BallState { | ||
private int ticksUntilSpawn; | ||
|
||
public InactiveBallState(VolleyballActivePhase phase) { | ||
super(phase); | ||
|
||
this.ticksUntilSpawn = phase.getConfig().getResetBallTicks(); | ||
} | ||
|
||
@Override | ||
public void onTick() { | ||
this.ticksUntilSpawn -= 1; | ||
|
||
if (this.ticksUntilSpawn <= 0) { | ||
this.phase.spawnBall(); | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/io/github/haykam821/volleyball/game/ball/ReadyBallState.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.github.haykam821.volleyball.game.ball; | ||
|
||
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; | ||
|
||
public class ReadyBallState extends EntityBallState { | ||
public ReadyBallState(VolleyballActivePhase phase, Entity ball) { | ||
super(phase, ball); | ||
} | ||
|
||
@Override | ||
public void onTick() { | ||
this.onEntityTick(); | ||
} | ||
|
||
@Override | ||
public void onHitBall(PlayerEntry entry) { | ||
TeamEntry possessionTeam = entry == null ? null : entry.getTeam(); | ||
this.phase.setBallState(new ActiveBallState(this.phase, this.ball, possessionTeam)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters