feat(model): track visited rooms on Player for the map
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,9 @@ import lombok.Setter;
|
|||||||
import thb.jeanluc.adventure.model.item.Item;
|
import thb.jeanluc.adventure.model.item.Item;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The player avatar. Tracks the current room, an ordered inventory, and a
|
* The player avatar. Tracks the current room, an ordered inventory, and a
|
||||||
@@ -15,9 +17,11 @@ import java.util.Optional;
|
|||||||
public class Player {
|
public class Player {
|
||||||
|
|
||||||
/** Room the player is currently standing in. */
|
/** Room the player is currently standing in. */
|
||||||
@Setter
|
|
||||||
private Room currentRoom;
|
private Room currentRoom;
|
||||||
|
|
||||||
|
/** Ids of rooms the player has entered, in first-visit order (for the map). */
|
||||||
|
private final Set<String> visitedRoomIds = new LinkedHashSet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inventory keyed by item id. {@link LinkedHashMap} keeps insertion
|
* Inventory keyed by item id. {@link LinkedHashMap} keeps insertion
|
||||||
* order for stable {@code inventory} listings while still providing
|
* order for stable {@code inventory} listings while still providing
|
||||||
@@ -44,6 +48,17 @@ public class Player {
|
|||||||
}
|
}
|
||||||
this.currentRoom = startRoom;
|
this.currentRoom = startRoom;
|
||||||
this.gold = startGold;
|
this.gold = startGold;
|
||||||
|
this.visitedRoomIds.add(startRoom.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Moves the player into a room and records it as visited.
|
||||||
|
*
|
||||||
|
* @param room the room now occupied; must not be null
|
||||||
|
*/
|
||||||
|
public void setCurrentRoom(Room room) {
|
||||||
|
this.currentRoom = room;
|
||||||
|
this.visitedRoomIds.add(room.getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -54,4 +54,20 @@ class PlayerTest {
|
|||||||
|
|
||||||
assertThat(p.getCurrentRoom()).isEqualTo(b);
|
assertThat(p.getCurrentRoom()).isEqualTo(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void startRoomIsVisitedFromTheStart() {
|
||||||
|
Room start = new Room("kitchen", "Kitchen", "desc");
|
||||||
|
Player p = new Player(start, 0);
|
||||||
|
assertThat(p.getVisitedRoomIds()).containsExactly("kitchen");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void movingMarksTheNewRoomVisited() {
|
||||||
|
Room start = new Room("kitchen", "Kitchen", "desc");
|
||||||
|
Room hall = new Room("hallway", "Hallway", "desc");
|
||||||
|
Player p = new Player(start, 0);
|
||||||
|
p.setCurrentRoom(hall);
|
||||||
|
assertThat(p.getVisitedRoomIds()).containsExactly("kitchen", "hallway");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user