Path finding
This commit is contained in:
parent
c1c438a053
commit
8c7522e1d3
|
@ -4,8 +4,10 @@ import fr.ynerant.leveleditor.editor.Map;
|
|||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class RawMap {
|
||||
private List<RawCase> cases;
|
||||
|
@ -48,6 +50,15 @@ public class RawMap {
|
|||
return cases_map.get(y * getWidth() + x);
|
||||
}
|
||||
|
||||
public Collection<RawCase> getNeighbours(RawCase c) {
|
||||
List<RawCase> list = new ArrayList<>();
|
||||
list.add(getCase(c.getPosX() - 1, c.getPosY()));
|
||||
list.add(getCase(c.getPosX(), c.getPosY() - 1));
|
||||
list.add(getCase(c.getPosX() + 1, c.getPosY()));
|
||||
list.add(getCase(c.getPosX(), c.getPosY() + 1));
|
||||
return list.stream().filter(_c -> _c != null && _c.getCollision() == Collision.ANY).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ import fr.ynerant.leveleditor.api.editor.RawCase;
|
|||
import fr.ynerant.leveleditor.api.editor.sprites.Sprite;
|
||||
import fr.ynerant.leveleditor.api.editor.sprites.SpriteRegister;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.*;
|
||||
|
||||
public abstract class Mob {
|
||||
private static final Random RANDOM = new Random();
|
||||
|
@ -73,13 +73,44 @@ public abstract class Mob {
|
|||
--tickRemains;
|
||||
else {
|
||||
tickRemains = getSlowness();
|
||||
RawCase c = game.getMap().getCase(getX(), getY());
|
||||
RawCase other = game.getMap().getCase(getX() - 1, getY());
|
||||
if (other == null || other.getCollision() == Collision.ANY) {
|
||||
c.setCollision(Collision.ANY);
|
||||
if (other != null)
|
||||
other.setCollision(Collision.PARTIAL);
|
||||
move(getX() - 1, getY());
|
||||
RawCase current = game.getMap().getCase(getX(), getY());
|
||||
current.setCollision(Collision.ANY);
|
||||
|
||||
if (current.getPosX() == 0) {
|
||||
move(-1, getY());
|
||||
return;
|
||||
}
|
||||
|
||||
List<RawCase> visited = new ArrayList<>();
|
||||
Queue<RawCase> queue = new ArrayDeque<>();
|
||||
Map<RawCase, RawCase> pred = new HashMap<>();
|
||||
RawCase last = null;
|
||||
queue.add(current);
|
||||
while (!queue.isEmpty()) {
|
||||
RawCase visiting = queue.poll();
|
||||
visited.add(visiting);
|
||||
for (RawCase neighbour : game.getMap().getNeighbours(visiting)) {
|
||||
if (visited.contains(neighbour))
|
||||
continue;
|
||||
|
||||
pred.put(neighbour, visiting);
|
||||
queue.add(neighbour);
|
||||
|
||||
if (neighbour.getPosX() == 0) {
|
||||
last = neighbour;
|
||||
queue.clear();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (last == null) {
|
||||
current.setCollision(Collision.PARTIAL);
|
||||
}
|
||||
else {
|
||||
while (pred.get(last) != current)
|
||||
last = pred.get(last);
|
||||
move(last.getPosX(), last.getPosY());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue