diff --git a/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala b/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala index 810cdb4..aa301a0 100644 --- a/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala +++ b/src/main/scala/fr/ynerant/leveleditor/api/editor/RawMap.scala @@ -28,6 +28,10 @@ case class RawMap(var cases: List[RawCase], var width: Int, var height: Int) { list ::= getCase(c.getPosX + 1, c.getPosY) list ::= getCase(c.getPosX, c.getPosY + 1) list ::= getCase(c.getPosX - 1, c.getPosY) + list ::= getCase(c.getPosX - 1, c.getPosY + 1) + list ::= getCase(c.getPosX - 1, c.getPosY - 1) + list ::= getCase(c.getPosX + 1, c.getPosY + 1) + list ::= getCase(c.getPosX + 1, c.getPosY - 1) list.filter((_c: RawCase) => _c != null) } diff --git a/src/main/scala/fr/ynerant/leveleditor/game/PathFinder.scala b/src/main/scala/fr/ynerant/leveleditor/game/PathFinder.scala index 64d9e60..f7f7091 100644 --- a/src/main/scala/fr/ynerant/leveleditor/game/PathFinder.scala +++ b/src/main/scala/fr/ynerant/leveleditor/game/PathFinder.scala @@ -11,28 +11,55 @@ case class PathFinder(game: GameFrame) { def calculatePath(): Unit = { pred = Map() - val queue = new util.ArrayDeque[RawCase] + val queueactu = new util.ArrayDeque[RawCase] + val queuesuivdroit = new util.ArrayDeque[(RawCase, RawCase)] + val queuesuivdiag = new util.ArrayDeque[(RawCase, RawCase)] + var edges = List(): List[(RawCase, RawCase)] for (i <- 0 until game.getMap.getHeight / 16) { val start = game.getMap.getCase(0, i) if (!start.getCollision.equals(Collision.FULL)) { pred += (coords(start) -> null) - queue.add(start) + queueactu.add(start) } } - while (!queue.isEmpty) { - val visiting = queue.poll + while (!queueactu.isEmpty) { + val visiting = queueactu.poll game.getMap.getNeighbours(visiting).foreach(neighbour => { - if (neighbour != null && !neighbour.collision.equals(Collision.FULL) && !pred.contains(coords(neighbour))) { - pred += (coords(neighbour) -> visiting) - queue.add(neighbour) + if ((neighbour != null && !neighbour.collision.equals(Collision.FULL)) && !edges.contains(visiting, neighbour) + && (!game.getMap.getCase(neighbour.getPosX, visiting.getPosY).collision.equals(Collision.FULL) + && !game.getMap.getCase(visiting.getPosX, neighbour.getPosY).collision.equals(Collision.FULL))) { + edges ::= (visiting, neighbour) + if (visiting.getPosY == neighbour.getPosY || visiting.getPosX == neighbour.getPosX) { + queuesuivdroit.add((neighbour, visiting)) + } + else { + queuesuivdiag.add((neighbour, visiting)) + } } }) + + if (queueactu.isEmpty) { + while (!queuesuivdroit.isEmpty) { + val (actu, prev) = queuesuivdroit.poll + if (!pred.contains(coords(actu))) { + pred += (coords(actu) -> prev) + queueactu.add(actu) + } + } + while (!queuesuivdiag.isEmpty) { + val (actu, prev) = queuesuivdiag.poll + if (!pred.contains(coords(actu))) { + pred += (coords(actu) -> prev) + queueactu.add(actu) + } + } + } } } - def coords(rawCase: RawCase): Int = rawCase.getPosY * game.getMap.getWidth / 16 + rawCase.getPosX + def coords(RawCase: RawCase): Int = RawCase.getPosY * game.getMap.getWidth / 16 + RawCase.getPosX def nextPos(x: Int, y: Int): RawCase = { pred.getOrElse(y * game.getMap.getWidth / 16 + x, null)