mirror of
https://github.com/guezoloic/racing-game.git
synced 2026-03-28 18:03:50 +00:00
feat(Map.java): ajout followPath fonction
This commit is contained in:
57
src/Map.java
57
src/Map.java
@@ -39,6 +39,63 @@ public class Map
|
||||
return new Point[] {start, end};
|
||||
}
|
||||
|
||||
private boolean followPath(Point start, Point end)
|
||||
{
|
||||
// remettre à 0 la liste
|
||||
pathMap.clear();
|
||||
|
||||
// positions
|
||||
Point current = start;
|
||||
Point previous = null;
|
||||
|
||||
int[][] coord = {
|
||||
{1, 0}, // haut
|
||||
{-1, 0}, // bas
|
||||
{0, -1}, // gauche
|
||||
{0, 1} // droite
|
||||
};
|
||||
|
||||
// sécurité pour éviter les boucles infinie
|
||||
int step = 0;
|
||||
int max = map.length * map[0].length;
|
||||
for (; step < max; step++)
|
||||
{
|
||||
pathMap.add(current);
|
||||
|
||||
if (current.equals(end))
|
||||
return true;
|
||||
|
||||
boolean moved = false;
|
||||
|
||||
for (int[] pos : coord)
|
||||
{
|
||||
int x = current.x + pos[0];
|
||||
int y = current.y + pos[1];
|
||||
|
||||
if ((x >= 0 || map[0].length > x) || (y >= 0 || map.length > y))
|
||||
{
|
||||
Point next = new Point(x, y);
|
||||
|
||||
if (next.equals(previous))
|
||||
continue;
|
||||
|
||||
CircuitCell type = map[y][x].getType();
|
||||
if (type == CircuitCell.FINISH || type == CircuitCell.ROAD
|
||||
|| type == CircuitCell.YROAD)
|
||||
{
|
||||
previous = current;
|
||||
current = next;
|
||||
moved = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Si il est bloqué
|
||||
if (!moved) break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public Circuit getElement(int x, int y)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user