0

For example: I have a grid where each square is identified by a value denoting row and column, eg: 32 is in the 3rd column and 2nd row. Nearby locations would be squares 31, 33, 41, 42, 43, 21, 22, 23.

if (main.location === player.location - 1 || 
main.location === player.location + 1 || 
main.location === player.location - 9 || 
main.location === player.location + 9 || 
main.location === player.location - 10 || 
main.location === player.location + 10 || 
main.location === player.location - 11 || 
main.location === player.location + 11) {
    console.log("player is nearby");
    }
ivanle0
  • 1
  • 1
  • `[player.location - 1, player.location + 1, /* ... */].includes(main.location)`. Even better if you extract it to a variable to describe it `nearbyPositions = [player.location - 1, player.location + 1, /* ... */]; if (nearbyPositions.includes(main.location) {}` – VLAZ Jun 13 '22 at 09:20
  • I doubt you can use this coordinate system at all. You would need a ton of arrays for comparing. Rather use a `location` object, and compare the difference of rows and cols. If the absolute difference is smaller or equal to 1, the player is "nearby", if the sum of the differences is zero, that's a full hit. – Teemu Jun 13 '22 at 09:46

0 Answers0