Workshop › Zelda-style Dungeon › Step 7 of 22

Next room

What we are doing

When the middle of the hero leaves the room through a gap, the room next to it is loaded, and the hero appears on the opposite side, as if walking through the doorway.

```js
// Walking out through a gap in the wall: into the next room, on the opposite side.
const cx = player.x + SIZE / 2
const cy = player.y + SIZE / 2
if (cx < 0 || cx > COLS * T || cy < 0 || cy > ROWS * T) {
  const rx = room.rx + (cx < 0 ? -1 : cx > COLS * T ? 1 : 0)
  const ry = room.ry + (cy < 0 ? -1 : cy > ROWS * T ? 1 : 0)
  if (cx < 0) player.x += COLS * T - 4
  if (cx > COLS * T) player.x -= COLS * T - 4
  if (cy < 0) player.y += ROWS * T - 4
  if (cy > ROWS * T) player.y -= ROWS * T - 4
  enter(rx, ry)
  return
}
```

What it means

Your turn

At the end of update, walk into the next room.

Skills: Game states

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