Workshop › Zelda-style Dungeon › Step 9 of 22

Enemies

What we are doing

Each e in a room becomes an enemy when you enter it: a purple square the size of the hero. An enemy remembers a lot that the next steps will use: which way it walks, when it turns, its health, and a knock-back.

```js
let enemies

  enemies = []
  ROOMS[ry][rx].forEach((line, row) => {
    ;[...line].forEach((ch, col) => {
      if (ch !== 'e') return
      const x = col * T + (T - SIZE) / 2
      enemies.push({ x, y: row * T + (T - SIZE) / 2, dir: [1, 0], turnIn: 0, hp: 1, knock: 0, kx: 0, ky: 0 })
    })
  })

  for (const e of enemies) {
    ctx.fillStyle = e.knock > 0 ? '#fca5a5' : '#7c3aed'
    ctx.fillRect(e.x, e.y, SIZE, SIZE)
  }
```

What it means

Your turn

  1. Under player, write enemies.
  2. In enter, after tiles, build the enemies.
  3. In draw, draw them before the hero.

Skills: Arrays and objects, Drawing on a canvas

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