Les mobs peuvent se déplacer en diagonale
This commit is contained in:
parent
7eb64985c7
commit
def19e5750
|
@ -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 + 1, c.getPosY)
|
||||||
list ::= getCase(c.getPosX, c.getPosY + 1)
|
list ::= getCase(c.getPosX, c.getPosY + 1)
|
||||||
list ::= getCase(c.getPosX - 1, c.getPosY)
|
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)
|
list.filter((_c: RawCase) => _c != null)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,28 +11,55 @@ case class PathFinder(game: GameFrame) {
|
||||||
|
|
||||||
def calculatePath(): Unit = {
|
def calculatePath(): Unit = {
|
||||||
pred = Map()
|
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) {
|
for (i <- 0 until game.getMap.getHeight / 16) {
|
||||||
val start = game.getMap.getCase(0, i)
|
val start = game.getMap.getCase(0, i)
|
||||||
if (!start.getCollision.equals(Collision.FULL)) {
|
if (!start.getCollision.equals(Collision.FULL)) {
|
||||||
pred += (coords(start) -> null)
|
pred += (coords(start) -> null)
|
||||||
queue.add(start)
|
queueactu.add(start)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!queue.isEmpty) {
|
while (!queueactu.isEmpty) {
|
||||||
val visiting = queue.poll
|
val visiting = queueactu.poll
|
||||||
game.getMap.getNeighbours(visiting).foreach(neighbour => {
|
game.getMap.getNeighbours(visiting).foreach(neighbour => {
|
||||||
if (neighbour != null && !neighbour.collision.equals(Collision.FULL) && !pred.contains(coords(neighbour))) {
|
if ((neighbour != null && !neighbour.collision.equals(Collision.FULL)) && !edges.contains(visiting, neighbour)
|
||||||
pred += (coords(neighbour) -> visiting)
|
&& (!game.getMap.getCase(neighbour.getPosX, visiting.getPosY).collision.equals(Collision.FULL)
|
||||||
queue.add(neighbour)
|
&& !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 = {
|
def nextPos(x: Int, y: Int): RawCase = {
|
||||||
pred.getOrElse(y * game.getMap.getWidth / 16 + x, null)
|
pred.getOrElse(y * game.getMap.getWidth / 16 + x, null)
|
||||||
|
|
Loading…
Reference in New Issue