Workshop › Zelda-style Dungeon › Step 11 of 22

They chase you

What we are doing

An enemy closer than 4 tiles chases the hero, a little slower than you walk. It moves along whichever axis the hero is farther away on, like a Zelda enemy that only walks straight.

```js
} else {
  const ex = e.x + SIZE / 2 - cx
  const ey = e.y + SIZE / 2 - cy
  if (Math.hypot(ex, ey) < 4 * T) {
    // Close: chase the player along the longer axis.
    e.dir = Math.abs(ex) > Math.abs(ey) ? [-Math.sign(ex), 0] : [0, -Math.sign(ey)]
    move(e, e.dir[0] * 1.1, e.dir[1] * 1.1)
  }
}
```

What it means

Your turn

In the enemy loop, after the knock-back block, add an else that chases when close.

Skills: Velocity and gravity

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