package model.world; import model.entities.IActive; import model.world.graphs.Edge; import model.world.graphs.MappedGraph; import model.world.graphs.Vertex; import org.jgrapht.GraphPath; import org.jgrapht.alg.interfaces.AStarAdmissibleHeuristic; import org.jgrapht.alg.shortestpath.AStarShortestPath; import java.util.Iterator; /* @TODO : fabrique un labyrinthe à partir d'une MappedGraph * gère les interactions de déplacement d'une case à une autre (cases liees entre elles sans mur), zones accessibles, limites de la "grille", ... * */ public class Labyrinth extends MappedGraph { public Labyrinth(MappedGraph graph) { super(graph.getSizeX(), graph.getSizeY()); initializeVertex(); for (Edge edge: graph.getEdgeSet()) { Position source = edge.getSource().getPos(); Position target = edge.getTarget().getPos(); // On garantit if (source.x > target.x || source.y > target.y) addEdge(source, target); else addEdge(target, source); } } public boolean isMoveValid(IActive active, Direction direction) { Position current = active.getPos(); Position wanted = getNeighbor(current, direction); return isInMap(wanted) && isLinked(current, wanted); } /* ======================================================================== * I.A utility * ======================================================================== */ public Iterator getShorterPath(Position pStart, Position pEnd){ AStarShortestPath AstarInstance= new AStarShortestPath<>(graph, heuristic); GraphPath path = AstarInstance.getPath(getVertex(pStart), getVertex(pEnd)); Iterator iterator=path.getVertexList().iterator(); iterator.next();//squizzing the begining return iterator; } //TODO le foutre ailleurs AStarAdmissibleHeuristic heuristic=new AStarAdmissibleHeuristic() { @Override public double getCostEstimate(Object o, Object v1) { Vertex vertex1=(Vertex) o; Vertex vertex2=(Vertex) v1; return Position.distance(vertex1.getPos(), (vertex2.getPos())); } }; }