diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/ItemFactory.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/ItemFactory.java index 1703c45..1bcefe0 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/ItemFactory.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/ItemFactory.java @@ -44,6 +44,7 @@ public final class ItemFactory { .state(Boolean.TRUE.equals(dto.initialState())) .onText(dto.onText()) .offText(dto.offText()) + .effects(thb.jeanluc.adventure.loader.dto.EffectDto.toModelList(dto.effects())) .build(); default -> throw new WorldLoadException( "Unknown item type '" + dto.type() + "' on item '" + dto.id() + "'"); diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/ItemDto.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/ItemDto.java index 8139f8c..24ec7f1 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/ItemDto.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/loader/dto/ItemDto.java @@ -1,5 +1,7 @@ package thb.jeanluc.adventure.loader.dto; +import java.util.List; + /** * YAML representation of a single item. Fields outside of the type's * scope (e.g. {@code readText} on a switchable) are simply ignored. @@ -12,6 +14,7 @@ package thb.jeanluc.adventure.loader.dto; * @param initialState initial on/off state of a switchable item * @param onText message printed when a switchable transitions to on * @param offText message printed when a switchable transitions to off + * @param effects effects applied when a switchable transitions to on; nullable */ public record ItemDto( String type, @@ -21,6 +24,12 @@ public record ItemDto( String readText, Boolean initialState, String onText, - String offText + String offText, + List effects ) { + /** Backward-compatible constructor without effects. */ + public ItemDto(String type, String id, String name, String description, + String readText, Boolean initialState, String onText, String offText) { + this(type, id, name, description, readText, initialState, onText, offText, null); + } } diff --git a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java index e3b7684..8f8e1a3 100644 --- a/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java +++ b/Semesterprojekt/src/main/java/thb/jeanluc/adventure/model/item/SwitchableItem.java @@ -2,7 +2,11 @@ package thb.jeanluc.adventure.model.item; import lombok.Getter; import lombok.experimental.SuperBuilder; +import thb.jeanluc.adventure.game.Effects; import thb.jeanluc.adventure.game.GameContext; +import thb.jeanluc.adventure.model.Effect; + +import java.util.List; /** * Item with two boolean states (on / off). Each {@code use} toggles the @@ -24,6 +28,9 @@ public class SwitchableItem extends Item { */ private boolean state; + /** Effects applied when this item transitions to on; null = none. */ + private final List effects; + /** * Toggles the state and writes the corresponding transition text. * @@ -33,6 +40,9 @@ public class SwitchableItem extends Item { public void use(GameContext ctx) { state = !state; ctx.getIo().write(state ? onText : offText); + if (state) { + Effects.applyAll(effects, ctx); + } } /** diff --git a/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/item/SwitchEffectsTest.java b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/item/SwitchEffectsTest.java new file mode 100644 index 0000000..b468546 --- /dev/null +++ b/Semesterprojekt/src/test/java/thb/jeanluc/adventure/model/item/SwitchEffectsTest.java @@ -0,0 +1,28 @@ +package thb.jeanluc.adventure.model.item; + +import org.junit.jupiter.api.Test; +import thb.jeanluc.adventure.game.GameContext; +import thb.jeanluc.adventure.io.TestIO; +import thb.jeanluc.adventure.model.Effect; +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 SwitchEffectsTest { + @Test + void switchingOnAppliesEffects() { + SwitchableItem breaker = SwitchableItem.builder() + .id("breaker").name("Breaker").description("d") + .state(false).onText("hums to life").offText("off") + .effects(List.of(new Effect(Effect.Type.SET_FLAG, "power_on"))) + .build(); + Player player = new Player(new Room("k", "K", "d"), 0); + GameContext ctx = new GameContext(null, player, new TestIO()); + + breaker.use(ctx); + assertThat(ctx.getState().isSet("power_on")).isTrue(); + } +}