Workshop › Bomberman-style Arena › Step 31 of 42
The classic fix for shaky enemies: on each tile, leave out going back and pick among the rest. Only at a dead end, when going back is the only way, does an enemy turn round. Now they stroll along corridors like they have somewhere to be.
```js
// Enemies wander: at each tile they pick a way they can go, and only turn back at a dead end.
function updateEnemy(e) {
const forward = options.filter(([dr, dc]) => dr !== -e.dir[0] || dc !== -e.dir[1])
const pick = forward.length ? forward : options
e.dir = pick[Math.floor(Math.random() * pick.length)]
```forward keeps the directions that are not the reverse of e.dir: a direction differs from the reverse if either
number differs.forward.length ? forward : options: a length of 0 counts as false, so at a dead end we pick among all options.pick.updateEnemy, replace the e.dir = options[...] line with the three new lines.function updateEnemy(e) { write the comment.Skills: Game states, Arrays and objects
← Enemies wander Fight and touch →