Workshop › Zelda-style Dungeon › Step 12 of 22

They wander

What we are doing

A far enemy wanders: one pixel a frame in its direction, and every now and then (or when it bumps into a wall) it picks a new random direction. move tells us when it bumped.

```js
} else {
  // Far: wander, turning at random now and then or when a wall is in the way.
  e.turnIn -= 1
  const stuck = move(e, e.dir[0], e.dir[1])
  if (stuck || e.turnIn <= 0) {
    e.dir = Object.values(DIRS)[Math.floor(Math.random() * 4)]
    e.turnIn = 40 + Math.floor(Math.random() * 60)
  }
}
```

What it means

Your turn

In the chase block, add an else that wanders when far.

Skills: Velocity and gravity, 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