Skip to content

Commit

Permalink
Collect elimination results after a swap
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Nov 25, 2022
1 parent be24230 commit 9ca55ef
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class DeathSwapConfig {
DeathSwapMapConfig.CODEC.fieldOf("map").forGetter(DeathSwapConfig::getMapConfig),
Codec.INT.optionalFieldOf("initial_swap_ticks", SharedConstants.TICKS_PER_MINUTE * 5).forGetter(DeathSwapConfig::getInitialSwapTicks),
Codec.INT.optionalFieldOf("swap_ticks", SharedConstants.TICKS_PER_MINUTE * 2).forGetter(DeathSwapConfig::getSwapTicks),
Codec.INT.optionalFieldOf("swap_warning_ticks", SharedConstants.TICKS_PER_SECOND * 30).forGetter(DeathSwapConfig::getSwapWarningTicks)
Codec.INT.optionalFieldOf("swap_warning_ticks", SharedConstants.TICKS_PER_SECOND * 30).forGetter(DeathSwapConfig::getSwapWarningTicks),
Codec.INT.optionalFieldOf("swap_elimination_collection_ticks", SharedConstants.TICKS_PER_SECOND * 5).forGetter(DeathSwapConfig::getSwapEliminationCollectionTicks)
).apply(instance, DeathSwapConfig::new);
});

Expand All @@ -23,13 +24,15 @@ public class DeathSwapConfig {
private final int initialSwapTicks;
private final int swapTicks;
private final int swapWarningTicks;
private final int swapEliminationCollectionTicks;

public DeathSwapConfig(PlayerConfig playerConfig, DeathSwapMapConfig mapConfig, int initialSwapTicks, int swapTicks, int swapWarningTicks) {
public DeathSwapConfig(PlayerConfig playerConfig, DeathSwapMapConfig mapConfig, int initialSwapTicks, int swapTicks, int swapWarningTicks, int swapEliminationCollectionTicks) {
this.playerConfig = playerConfig;
this.mapConfig = mapConfig;
this.initialSwapTicks = initialSwapTicks;
this.swapTicks = swapTicks;
this.swapWarningTicks = swapWarningTicks;
this.swapEliminationCollectionTicks = swapEliminationCollectionTicks;
}

public PlayerConfig getPlayerConfig() {
Expand All @@ -51,4 +54,8 @@ public int getSwapTicks() {
public int getSwapWarningTicks() {
return this.swapWarningTicks;
}

public int getSwapEliminationCollectionTicks() {
return this.swapEliminationCollectionTicks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ private void swap() {

this.swapTicks = this.phase.getConfig().getSwapTicks();
this.warningTicks = this.phase.getConfig().getSwapWarningTicks();

this.phase.getEliminationCollector().start();
}

private void updateNoSwapBar() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package io.github.haykam821.deathswap.game;

import java.util.HashSet;
import java.util.Set;

import io.github.haykam821.deathswap.game.phase.DeathSwapActivePhase;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import xyz.nucleoid.plasmid.game.player.PlayerIterable;
import xyz.nucleoid.plasmid.util.PlayerRef;

public class EliminationCollector {
private static final int FADE_IN_TICKS = 20;
private static final int STAY_TICKS = 10;
private static final int FADE_OUT_TICKS = 70;

private static final Formatting CONTINUE_COLOR = Formatting.GREEN;
private static final Formatting ELIMINATION_COLOR = Formatting.RED;

private static final Text CONTINUE_TITLE = createText("title", CONTINUE_COLOR, true);
private static final Text CONTINUE_SUBTITLE = createText("continue.subtitle", CONTINUE_COLOR, false);

private static final Text ELIMINATION_TITLE = createText("title", ELIMINATION_COLOR, true);
private static final Text ELIMINATION_SUBTITLE_SINGULAR = createText("elimination.subtitle.singular", ELIMINATION_COLOR, false);

private static final SoundEvent CONTINUE_SOUND = SoundEvents.ENTITY_PLAYER_LEVELUP;
private static final SoundEvent ELIMINATION_SOUND = SoundEvents.ENTITY_WITHER_SPAWN;

private final DeathSwapActivePhase phase;

private final Set<PlayerRef> eliminated = new HashSet<>();
private int ticksRemaining;

public EliminationCollector(DeathSwapActivePhase phase) {
this.phase = phase;
}

public void start() {
this.eliminated.clear();
this.ticksRemaining = this.phase.getConfig().getSwapEliminationCollectionTicks();
}

/**
* @return whether the player's elimination display was integrated into an active elimination collector
*/
public boolean add(ServerPlayerEntity player) {
if (this.ticksRemaining > 0) {
this.eliminated.add(PlayerRef.of(player));
return true;
}

return false;
}

public void tick() {
if (this.ticksRemaining > 0) {
this.ticksRemaining -= 1;

if (this.ticksRemaining == 0) {
this.showResults();
}
}
}

private void showResults() {
PlayerIterable players = this.phase.getGameSpace().getPlayers();

players.showTitle(this.getTitle(), this.getSubtitle(), STAY_TICKS, FADE_OUT_TICKS, FADE_IN_TICKS);
players.playSound(this.getSound());
}

private Text getTitle() {
return this.isContinue() ? CONTINUE_TITLE : ELIMINATION_TITLE;
}

private Text getSubtitle() {
if (this.isContinue()) {
return CONTINUE_SUBTITLE;
}

int size = this.eliminated.size();
return size == 1 ? ELIMINATION_SUBTITLE_SINGULAR : createText("elimination.subtitle", ELIMINATION_COLOR, false, size);
}

private SoundEvent getSound() {
return this.isContinue() ? CONTINUE_SOUND : ELIMINATION_SOUND;
}

private boolean isContinue() {
return this.eliminated.isEmpty();
}

@Override
public String toString() {
return "EliminationCollector{phase=" + this.phase + ", eliminated=" + this.eliminated + ", ticksRemaining=" + this.ticksRemaining + "}";
}

private static Text createText(String keySuffix, Formatting formatting, boolean title, Object... args) {
return new TranslatableText("text.deathswap.elimination_collector." + keySuffix, args).styled(style -> {
return style.withFormatting(formatting).withBold(title);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.github.haykam821.deathswap.game.DeathSwapConfig;
import io.github.haykam821.deathswap.game.DeathSwapTimer;
import io.github.haykam821.deathswap.game.EliminationCollector;
import io.github.haykam821.deathswap.game.map.DeathSwapMap;
import io.github.haykam821.deathswap.game.map.DeathSwapMapConfig;
import net.minecraft.entity.damage.DamageSource;
Expand Down Expand Up @@ -39,6 +40,7 @@ public class DeathSwapActivePhase implements GameActivityEvents.Enable, GameActi
private final DeathSwapConfig config;
private final Set<ServerPlayerEntity> players;
private final DeathSwapTimer timer;
private final EliminationCollector eliminationCollector = new EliminationCollector(this);
private boolean singleplayer;

public DeathSwapActivePhase(GameSpace gameSpace, ServerWorld world, GlobalWidgets widgets, DeathSwapMap map, DeathSwapConfig config, Set<ServerPlayerEntity> players) {
Expand Down Expand Up @@ -87,6 +89,7 @@ public void onEnable() {
@Override
public void onTick() {
this.timer.tick();
this.eliminationCollector.tick();

// Eliminate players that are out of bounds
Iterator<ServerPlayerEntity> iterator = this.players.iterator();
Expand Down Expand Up @@ -145,6 +148,10 @@ public Set<ServerPlayerEntity> getPlayers() {
return this.players;
}

public EliminationCollector getEliminationCollector() {
return this.eliminationCollector;
}

// Utilities
private MutableText getEndingMessage() {
if (this.players.size() == 1) {
Expand All @@ -167,7 +174,10 @@ private boolean eliminate(ServerPlayerEntity player, String suffix, boolean remo

if (removed) {
this.setSpectator(player);
this.sendEliminateMessage(player, suffix);

if (!this.eliminationCollector.add(player)) {
this.sendEliminateMessage(player, suffix);
}
}

return removed;
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/data/deathswap/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
"gameType.deathswap.death_swap": "Death Swap",
"text.deathswap.eliminated": "%s has been eliminated!",
"text.deathswap.eliminated.out_of_bounds": "%s has been eliminated by going out of bounds!",
"text.deathswap.elimination_collector.continue.subtitle": "No players were eliminated",
"text.deathswap.elimination_collector.elimination.subtitle": "%s players were eliminated",
"text.deathswap.elimination_collector.elimination.subtitle.singular": "1 player was eliminated",
"text.deathswap.elimination_collector.title": "Swapped!",
"text.deathswap.timer.no_swap": "No swap",
"text.deathswap.timer.swap": "You swapped with %s!",
"text.deathswap.timer.warning.minute": "Swapping in 1 minute",
Expand Down

0 comments on commit 9ca55ef

Please sign in to comment.