Workshop › Zelda-style Dungeon › Step 16 of 22

A world that remembers

What we are doing

Leave the room and come back: the heart is there again, because enter loads the room fresh. The world must remember what was taken. Every taken tile gets a name like '0,0,11,7' (room and tile), kept in a Set; enter turns those tiles into floor.

```js
let taken // what has been picked up or opened in each room, so it stays that way

  taken = new Set()

// Load a room: its tiles (minus what was already taken), and fresh enemies on its e tiles.
function enter(rx, ry) {
  room = { rx, ry }
  tiles = ROOMS[ry][rx].map((line, row) =>
    [...line].map((ch, col) => (taken.has(rx + ',' + ry + ',' + col + ',' + row) ? '.' : ch === 'P' || ch === 'e' ? '.' : ch)),
  )

  const id = room.rx + ',' + room.ry + ',' + col + ',' + row

    taken.add(id)
```

What it means

Your turn

  1. Under tiles, write taken; at the top of reset, make it a new Set.
  2. Rewrite the tiles line of enter to skip taken tiles, and write the comment above enter.
  3. In update, name the tile under the hero, and remember it when something is picked up.

Skills: Game states, Arrays and objects

All steps

  1. A room
  2. The hero
  3. The game loop
  4. Walk
  5. Walls
  6. A touch pad
  7. Next room
  8. The sword
  9. Enemies
  10. Strike!
  11. They chase you
  12. They wander
  13. Ouch
  14. Blinking and hearts
  15. Keys and hearts
  16. A world that remembers
  17. The locked door
  18. A timer
  19. Game over
  20. The stairs out
  21. Best time
  22. Build it yourself: cleared rooms