feat: better win checking and win message/sound

This commit is contained in:
Ninjdai 2024-09-28 22:04:17 +02:00
parent 897dd837ad
commit 66fd30d8b3
2 changed files with 44 additions and 9 deletions

View File

@ -7,19 +7,33 @@ import dev.ninjdai.werewolf.uhc.teams.Team;
import dev.ninjdai.werewolf.uhc.teams.Teams; import dev.ninjdai.werewolf.uhc.teams.Teams;
import dev.ninjdai.werewolf.uhc.teams.WinCondition; import dev.ninjdai.werewolf.uhc.teams.WinCondition;
import io.github.togar2.pvp.events.EntityPreDeathEvent; import io.github.togar2.pvp.events.EntityPreDeathEvent;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextColor;
import net.minestom.server.MinecraftServer; import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.GameMode; import net.minestom.server.entity.GameMode;
import net.minestom.server.entity.Player; import net.minestom.server.entity.Player;
import net.minestom.server.event.EventFilter; import net.minestom.server.event.EventFilter;
import net.minestom.server.event.EventNode; import net.minestom.server.event.EventNode;
import net.minestom.server.event.trait.EntityEvent; import net.minestom.server.event.trait.EntityEvent;
import net.minestom.server.sound.SoundEvent;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class UHCPlayerDeathEvent { public class UHCPlayerDeathEvent {
public static void onPlayerDeath(EntityPreDeathEvent event) { public static void onPlayerDeath(EntityPreDeathEvent event) {
UHCPlayer player = (UHCPlayer) event.getEntity(); UHCPlayer player = (UHCPlayer) event.getEntity();
if(event.getDamage().getAttacker() instanceof UHCPlayer killer) { if(WinCondition.checkWin(event.getInstance())) {
Team team = Team.teamMap.get(killer.getUHCTeam().getId()); event.getInstance().playSound(Sound.sound().type(SoundEvent.ENTITY_ENDER_DRAGON_GROWL).source(Sound.Source.PLAYER).build());
WinCondition.checkTeamWin(team, event.getInstance().getPlayers()); event.getInstance().sendMessage(Component.empty()
.append(Component.text("Team "))
.append(Component.text(WinCondition.winningTeam.getName(), TextColor.color(WinCondition.winningTeam.getColor())))
.append(Component.text(" wins !"))
);
} }
event.setCancelDeath(true); event.setCancelDeath(true);

View File

@ -1,17 +1,21 @@
package dev.ninjdai.werewolf.uhc.teams; package dev.ninjdai.werewolf.uhc.teams;
import net.minestom.server.entity.Player; import dev.ninjdai.werewolf.uhc.UHCPlayer;
import net.minestom.server.entity.GameMode;
import net.minestom.server.instance.Instance;
import org.jetbrains.annotations.NotNull;
import java.util.List; import java.util.HashSet;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
public class WinCondition { public class WinCondition {
public static boolean checkTeamWin(Team team, Set<Player> players){ public static Team winningTeam;
public static boolean checkTeamWin(@NotNull Team team, @NotNull Set<UHCPlayer> players){
switch (team.getWinConditionType()){ switch (team.getWinConditionType()){
case WinConditionType.ONLY_TEAM_REMAINING -> { case WinConditionType.ONLY_TEAM_REMAINING -> {
for (Player player: players) { for (UHCPlayer player: players) {
if (! Objects.equals(player.getTag(Team.teamTag), team.getId())) return false; if (!(player.getUHCTeam() == team)) return false;
} }
return true; return true;
} }
@ -19,6 +23,23 @@ public class WinCondition {
return false; return false;
} }
public static boolean checkWin(Instance instance) {
Set<UHCPlayer> inGamePlayers = inGamePlayers(instance);
for (Teams team: Teams.values()) {
if(checkTeamWin(team.team, inGamePlayers)){
winningTeam = team.team;
return true;
}
}
return false;
}
public static Set<UHCPlayer> inGamePlayers(Instance instance) {
Set<UHCPlayer> players = HashSet.newHashSet(instance.getPlayers().size());
instance.getPlayers().forEach((player -> players.add((UHCPlayer) player)));
return players.stream().filter((p) -> p.getGameMode()== GameMode.SURVIVAL).collect(Collectors.toSet());
}
public enum WinConditionType { public enum WinConditionType {
ONLY_TEAM_REMAINING, ONLY_TEAM_REMAINING,
} }