Generate Path from 2D array

Hi, Im trying to generate a dynamic Path from a 2D array of which I also create my game’s map.
The problem is that that path gets distorted when generated, I understand its because my code loops through the array from index 0 -> index++ so the natural way of this is from left to right. But this path is from right to left, which means index–. How do I solve this the best way so I can all sorts of maps and directions? Thank you very much in advance!

Code and screenshot:

generatePath(map) {
const tileHW = 16, tileScale = 3, translateTile = { 1: 'floor1', 0: 'wall1' }
var graphics = this.add.graphics()

for (var y = 0; y < map.length; y++) {
  for (var x = 0; x < map[y].length; x++) {
    const xPx = x * 16 * tileScale, yPx = y * tileHW * tileScale

    if (translateTile[map[y][x]] === 'floor1') {
      if (!this.path) {
        this.path = this.add.path(xPx + 24, yPx - 10)
        continue
      }

      this.path.lineTo(xPx + 24, yPx + 24)
    }
  }
}

graphics.lineStyle(3, 0xffffff, 1)
this.path.draw(graphics)

}

Screenshot from 2019-12-17 15-46-42

@robertkofoed : Create a codepen demo if you need help. :slight_smile:

1 Like