From c9c019cea1ee16a8fc100a4a23c40b848c228966 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Sun, 31 May 2026 21:52:57 +0200 Subject: [PATCH] feat: state-dependent room descriptions (LookCommand resolution) Co-Authored-By: Claude Opus 4.8 (1M context) --- .../adventure/command/impl/LookCommand.java | 11 +++++- .../command/impl/DescriptionStateTest.java | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 Semesterprojekt/src/test/java/thb/jeanluc/adventure/command/impl/DescriptionStateTest.java diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/LookCommand.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/LookCommand.java index 2a1fb5b..d5a08c8 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/LookCommand.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/command/impl/LookCommand.java @@ -1,8 +1,10 @@ package thb.jeanluc.adventure.command.impl; import thb.jeanluc.adventure.command.Command; +import thb.jeanluc.adventure.game.Conditions; import thb.jeanluc.adventure.game.GameContext; import thb.jeanluc.adventure.io.text.RoomView; +import thb.jeanluc.adventure.model.DescriptionState; import thb.jeanluc.adventure.model.Direction; import thb.jeanluc.adventure.model.Npc; import thb.jeanluc.adventure.model.Room; @@ -22,7 +24,14 @@ public class LookCommand implements Command { List items = room.getItems().values().stream().map(Item::getName).toList(); List npcs = room.getNpcs().values().stream().map(Npc::getName).toList(); List exits = room.getExits().keySet().stream().map(Direction::getLabel).toList(); - ctx.getIo().showRoom(new RoomView(room.getName(), room.getDescription(), items, npcs, exits)); + String description = room.getDescription(); + for (DescriptionState ds : room.getDescriptionStates()) { + if (Conditions.all(ds.when(), ctx)) { + description = ds.text(); + break; + } + } + ctx.getIo().showRoom(new RoomView(room.getName(), description, items, npcs, exits)); } @Override diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/command/impl/DescriptionStateTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/command/impl/DescriptionStateTest.java new file mode 100644 index 0000000..d1c0a17 --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/command/impl/DescriptionStateTest.java @@ -0,0 +1,34 @@ +package thb.jeanluc.adventure.command.impl; + +import org.junit.jupiter.api.Test; +import thb.jeanluc.adventure.game.GameContext; +import thb.jeanluc.adventure.io.TestIO; +import thb.jeanluc.adventure.model.Condition; +import thb.jeanluc.adventure.model.DescriptionState; +import thb.jeanluc.adventure.model.Player; +import thb.jeanluc.adventure.model.Room; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class DescriptionStateTest { + @Test + void descriptionSwitchesWithFlag() { + Room cellar = new Room("cellar", "Cellar", "Pitch black down here."); + cellar.addDescriptionState(new DescriptionState( + List.of(new Condition(Condition.Type.FLAG, "power_on")), + "Lit now, a workbench stands against the wall.")); + Player player = new Player(cellar, 0); + TestIO io = new TestIO(); + GameContext ctx = new GameContext(null, player, io); + + new LookCommand().execute(ctx, List.of()); + assertThat(io.allOutput()).contains("Pitch black"); + + io.outputs().clear(); + ctx.getState().set("power_on"); + new LookCommand().execute(ctx, List.of()); + assertThat(io.allOutput()).contains("workbench").doesNotContain("Pitch black"); + } +}