Skip to content

Commit

Permalink
Update to new waiting lobby ui
Browse files Browse the repository at this point in the history
Signed-off-by: Patbox <[email protected]>
  • Loading branch information
Patbox committed Nov 15, 2024
1 parent e37b27f commit f5d35ac
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/main/java/eu/pb4/destroythemonument/game/SpawnLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.util.PlayerMap;
import xyz.nucleoid.plasmid.api.util.PlayerRef;

import java.util.Set;

public record SpawnLogic(GameSpace gameSpace, GameMap map,
Object2ObjectMap<PlayerRef, PlayerData> participants,
PlayerMap<PlayerData> participants,
Teams teams) {

public void resetPlayer(ServerPlayerEntity player, GameMode gameMode) {
Expand Down
26 changes: 23 additions & 3 deletions src/main/java/eu/pb4/destroythemonument/game/WaitingLobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
import eu.pb4.destroythemonument.game.map.GameMap;
import eu.pb4.destroythemonument.other.DtmUtil;
import eu.pb4.destroythemonument.ui.ClassSelectorUI;
import eu.pb4.sgui.api.ClickType;
import eu.pb4.sgui.api.elements.GuiElement;
import eu.pb4.sgui.api.elements.GuiElementInterface;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.SlotActionType;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
Expand All @@ -29,8 +33,12 @@
import xyz.nucleoid.plasmid.api.game.common.GameWaitingLobby;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey;
import xyz.nucleoid.plasmid.api.game.common.team.TeamSelectionLobby;
import xyz.nucleoid.plasmid.api.game.common.ui.WaitingLobbyUiElement;
import xyz.nucleoid.plasmid.api.game.common.ui.WaitingLobbyUiLayout;
import xyz.nucleoid.plasmid.api.game.event.GameActivityEvents;
import xyz.nucleoid.plasmid.api.game.event.GamePlayerEvents;
import xyz.nucleoid.plasmid.api.game.event.GameWaitingLobbyEvents;
import xyz.nucleoid.plasmid.api.util.PlayerMap;
import xyz.nucleoid.plasmid.api.util.PlayerRef;
import xyz.nucleoid.stimuli.event.EventResult;
import xyz.nucleoid.stimuli.event.item.ItemUseEvent;
Expand All @@ -43,7 +51,7 @@ public class WaitingLobby {
private final GameConfig config;
private final SpawnLogic spawnLogic;
private final Teams teams;
private final Object2ObjectMap<PlayerRef, PlayerData> participants = new Object2ObjectOpenHashMap<>();
private final PlayerMap<PlayerData> participants = PlayerMap.of(new Object2ObjectOpenHashMap<>());

private final TeamSelectionLobby teamSelection;
private final PlayerClass defaultKit;
Expand Down Expand Up @@ -88,7 +96,6 @@ public static GameOpenProcedure open(GameOpenContext<GameConfig> context) {
return context.openWithWorld(worldConfig, (game, world) -> {
map.world = world;
GameWaitingLobby.addTo(game, config.players());

TeamSelectionLobby teamSelection = TeamSelectionLobby.addTo(game, config.teams());

WaitingLobby waiting = new WaitingLobby(game.getGameSpace(), map, context.config(), teamSelection);
Expand All @@ -100,6 +107,8 @@ public static GameOpenProcedure open(GameOpenContext<GameConfig> context) {
game.listen(PlayerDeathEvent.EVENT, waiting::onPlayerDeath);
game.listen(PlayerDamageEvent.EVENT, waiting::onPlayerDamage);
game.listen(ItemUseEvent.EVENT, waiting::onUseItem);

game.listen(GameWaitingLobbyEvents.BUILD_UI_LAYOUT, waiting::buildUiLayout);
});
}

Expand All @@ -121,6 +130,17 @@ private ActionResult onUseItem(ServerPlayerEntity player, Hand hand) {
return ActionResult.PASS;
}


private void buildUiLayout(WaitingLobbyUiLayout layout, ServerPlayerEntity player) {
if (this.gameSpace.getPlayers().participants().contains(player)) {
layout.addTrailing(() -> new GuiElement(DtmItems.CLASS_SELECTOR.getDefaultStack(), (index, type, action) -> {
if (type.isRight) {
ClassSelectorUI.openSelector(player, this.participants.get(player), this.config.kits());
}
}));
}
}

private GameResult requestStart() {
Multimap<GameTeamKey, ServerPlayerEntity> playerTeams = HashMultimap.create();
this.teamSelection.allocate(this.gameSpace.getPlayers().participants(), playerTeams::put);
Expand Down Expand Up @@ -163,6 +183,6 @@ private EventResult onPlayerDeath(ServerPlayerEntity player, DamageSource source
private void spawnPlayer(ServerPlayerEntity player) {
this.spawnLogic.resetPlayer(player, GameMode.ADVENTURE, false);
this.spawnLogic.spawnPlayer(player);
player.getInventory().setStack(8, new ItemStack(DtmItems.CLASS_SELECTOR));
//player.getInventory().setStack(8, new ItemStack(DtmItems.CLASS_SELECTOR));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import xyz.nucleoid.plasmid.api.game.rule.GameRuleType;
import xyz.nucleoid.plasmid.api.game.stats.GameStatisticBundle;
import xyz.nucleoid.plasmid.api.game.stats.StatisticKeys;
import xyz.nucleoid.plasmid.api.util.PlayerMap;
import xyz.nucleoid.plasmid.api.util.PlayerRef;
import xyz.nucleoid.stimuli.event.EventResult;
import xyz.nucleoid.stimuli.event.block.BlockBreakEvent;
Expand All @@ -91,7 +92,7 @@ public abstract class BaseGameLogic {

public final GameSpace gameSpace;
public final GameMap gameMap;
public final Object2ObjectMap<PlayerRef, PlayerData> participants;
public final PlayerMap<PlayerData> participants;
public final Object2IntMap<PlayerRef> deadPlayers = new Object2IntArrayMap<>();
public final Teams teams;
public final ArrayList<PlayerClass> kits = new ArrayList<>();
Expand All @@ -106,7 +107,7 @@ public abstract class BaseGameLogic {
protected long closeTime = -1;
protected boolean setSpectator = false;

public BaseGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap<PlayerRef, PlayerData> participants, Teams teams) {
public BaseGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap<PlayerData> participants, Teams teams) {
this.gameSpace = gameSpace;
this.config = config;
this.gameMap = map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey;
import xyz.nucleoid.plasmid.api.util.ColoredBlocks;
import xyz.nucleoid.plasmid.api.util.PlayerMap;
import xyz.nucleoid.plasmid.api.util.PlayerRef;
import xyz.nucleoid.stimuli.event.EventResult;

Expand All @@ -43,7 +44,7 @@ public class CaptureGameLogic extends BaseGameLogic {
protected int sidebarTeamPos = 0;
protected List<TeamData> sidebarTeams;

public CaptureGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap<PlayerRef, PlayerData> participants, Teams teams) {
public CaptureGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap<PlayerData> participants, Teams teams) {
super(gameSpace, map, config, participants, teams);
List<Text> texts = new ArrayList<>();

Expand All @@ -57,7 +58,7 @@ public CaptureGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Obj
}
}

public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap<GameTeamKey, ServerPlayerEntity> playerTeams, Object2ObjectMap<PlayerRef, PlayerData> participants, Teams teams) {
public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap<GameTeamKey, ServerPlayerEntity> playerTeams, PlayerMap<PlayerData> participants, Teams teams) {
gameSpace.setActivity(game -> {
BaseGameLogic active = new CaptureGameLogic(gameSpace, map, config, participants, teams);
active.setupGame(game, map, config, playerTeams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeam;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey;
import xyz.nucleoid.plasmid.api.util.PlayerMap;
import xyz.nucleoid.plasmid.api.util.PlayerRef;

public class DebugGameLogic extends StandardGameLogic {

public DebugGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap<PlayerRef, PlayerData> participants, Teams teams) {
public DebugGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap<PlayerData> participants, Teams teams) {
super(gameSpace, map, config, participants, teams);

Text text = Text.literal("+-----------------DEBUG----------------+").formatted(Formatting.AQUA);
this.gameSpace.getPlayers().sendMessage(text);
}

public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap<GameTeamKey, ServerPlayerEntity> playerTeams, Object2ObjectMap<PlayerRef, PlayerData> participants, Teams teams) {
public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap<GameTeamKey, ServerPlayerEntity> playerTeams, PlayerMap<PlayerData> participants, Teams teams) {
gameSpace.setActivity(game -> {
BaseGameLogic active = new DebugGameLogic(gameSpace, map, config, participants, teams);
active.setupGame(game, map, config, playerTeams);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import xyz.nucleoid.plasmid.api.game.GameActivity;
import xyz.nucleoid.plasmid.api.game.GameSpace;
import xyz.nucleoid.plasmid.api.game.common.team.GameTeamKey;
import xyz.nucleoid.plasmid.api.util.PlayerMap;
import xyz.nucleoid.plasmid.api.util.PlayerRef;
import xyz.nucleoid.stimuli.event.EventResult;

Expand All @@ -40,7 +41,7 @@ public class StandardGameLogic extends BaseGameLogic {
protected int sidebarTeamPos = 0;
protected List<TeamData> sidebarTeams;

public StandardGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Object2ObjectMap<PlayerRef, PlayerData> participants, Teams teams) {
public StandardGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, PlayerMap<PlayerData> participants, Teams teams) {
super(gameSpace, map, config, participants, teams);
List<Text> texts = new ArrayList<>();

Expand All @@ -54,7 +55,7 @@ public StandardGameLogic(GameSpace gameSpace, GameMap map, GameConfig config, Ob
}
}

public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap<GameTeamKey, ServerPlayerEntity> playerTeams, Object2ObjectMap<PlayerRef, PlayerData> participants, Teams teams) {
public static void open(GameSpace gameSpace, GameMap map, GameConfig config, Multimap<GameTeamKey, ServerPlayerEntity> playerTeams, PlayerMap<PlayerData> participants, Teams teams) {
gameSpace.setActivity(game -> {
BaseGameLogic active = new StandardGameLogic(gameSpace, map, config, participants, teams);
active.setupGame(game, map, config, playerTeams);
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/eu/pb4/destroythemonument/ui/ClassSelectorUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import eu.pb4.destroythemonument.game.playerclass.ClassRegistry;
import eu.pb4.destroythemonument.other.DtmUtil;
import eu.pb4.destroythemonument.other.FormattingUtil;
import eu.pb4.sgui.api.GuiHelpers;
import eu.pb4.sgui.api.elements.GuiElementBuilder;
import eu.pb4.sgui.api.gui.GuiInterface;
import eu.pb4.sgui.api.gui.SimpleGui;
import net.minecraft.screen.ScreenHandlerType;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -17,6 +19,7 @@
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.map_templates.BlockBounds;
import xyz.nucleoid.plasmid.api.util.PlayerRef;

Expand All @@ -27,12 +30,15 @@ public class ClassSelectorUI extends SimpleGui {
private final PlayerData playerData;
private final BaseGameLogic game;
private final List<PlayerClass> kits;
@Nullable
private final GuiInterface previousUi;

public ClassSelectorUI(ServerPlayerEntity player, PlayerData data, BaseGameLogic game, List<PlayerClass> kits) {
super(getType(kits.size()), player, kits.size() > 53);
this.playerData = data;
this.game = game;
this.kits = kits;
this.previousUi = GuiHelpers.getCurrentGui(player);
this.setTitle(DtmUtil.getText("ui", "select_class"));
this.updateIcons();
}
Expand All @@ -53,6 +59,14 @@ private static ScreenHandlerType<?> getType(int size) {
}
}

@Override
public void onClose() {
super.onClose();
if (this.previousUi != null) {
this.previousUi.open();
}
}

public static void openSelector(ServerPlayerEntity player, BaseGameLogic logic) {
new ClassSelectorUI(player, logic.participants.get(PlayerRef.of(player)), logic, logic.kits).open();
}
Expand Down

0 comments on commit f5d35ac

Please sign in to comment.