feat: restore hooks for flags, quest progress, switch state
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package thb.jeanluc.adventure.game;
|
package thb.jeanluc.adventure.game;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -24,4 +25,10 @@ public class GameState {
|
|||||||
public Set<String> all() {
|
public Set<String> all() {
|
||||||
return Collections.unmodifiableSet(flags);
|
return Collections.unmodifiableSet(flags);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Replaces all flags with the given collection (used by load). */
|
||||||
|
public void restore(Collection<String> newFlags) {
|
||||||
|
flags.clear();
|
||||||
|
flags.addAll(newFlags);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package thb.jeanluc.adventure.game;
|
package thb.jeanluc.adventure.game;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
@@ -46,4 +47,12 @@ public class QuestLog {
|
|||||||
public Set<String> completed() {
|
public Set<String> completed() {
|
||||||
return Collections.unmodifiableSet(completed);
|
return Collections.unmodifiableSet(completed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Replaces all progress with the given stage map and completed set (load). */
|
||||||
|
public void restore(Map<String, Integer> stages, Collection<String> completedIds) {
|
||||||
|
stageIndex.clear();
|
||||||
|
stageIndex.putAll(stages);
|
||||||
|
completed.clear();
|
||||||
|
completed.addAll(completedIds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,4 +53,14 @@ public class SwitchableItem extends Item {
|
|||||||
public boolean isOn() {
|
public boolean isOn() {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directly sets the on/off state without running effects or printing.
|
||||||
|
* Used only when restoring a saved game.
|
||||||
|
*
|
||||||
|
* @param state the state to restore
|
||||||
|
*/
|
||||||
|
public void setState(boolean state) {
|
||||||
|
this.state = state;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
package thb.jeanluc.adventure.game;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import thb.jeanluc.adventure.model.item.SwitchableItem;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
class RestoreHooksTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void gameStateRestoreReplacesFlags() {
|
||||||
|
GameState s = new GameState();
|
||||||
|
s.set("old");
|
||||||
|
s.restore(List.of("a", "b"));
|
||||||
|
assertThat(s.isSet("old")).isFalse();
|
||||||
|
assertThat(s.all()).containsExactlyInAnyOrder("a", "b");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void questLogRestoreReplacesProgress() {
|
||||||
|
QuestLog q = new QuestLog();
|
||||||
|
q.start("stale");
|
||||||
|
q.restore(Map.of("active", 2), Set.of("done"));
|
||||||
|
assertThat(q.isActive("stale")).isFalse();
|
||||||
|
assertThat(q.stageIndex("active")).isEqualTo(2);
|
||||||
|
assertThat(q.isCompleted("done")).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void switchableSetStateDoesNotRunEffects() {
|
||||||
|
SwitchableItem lamp = SwitchableItem.builder()
|
||||||
|
.id("lamp").name("Lamp").description("d").light(true)
|
||||||
|
.onText("on").offText("off").state(false).effects(List.of())
|
||||||
|
.build();
|
||||||
|
lamp.setState(true);
|
||||||
|
assertThat(lamp.isOn()).isTrue();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user