44import java .io .File ;
55import java .io .IOException ;
66import java .util .ArrayList ;
7+ import java .util .Collections ;
8+ import java .util .Comparator ;
79import java .util .List ;
810import java .util .logging .Level ;
911
1517import com .redomar .game .entities .PlayerMP ;
1618import com .redomar .game .gfx .Screen ;
1719import com .redomar .game .level .tiles .Tile ;
20+ import com .redomar .game .lib .utils .Vector2i ;
1821import com .redomar .game .net .packets .Packet01Disconnect ;
1922
2023public class LevelHandler {
@@ -26,6 +29,16 @@ public class LevelHandler {
2629 private String imagePath ;
2730 private BufferedImage image ;
2831
32+ private Comparator <Node > nodeSorter = new Comparator <Node >() {
33+
34+ public int compare (Node n0 , Node n1 ) {
35+ if (n1 .fCost < n0 .fCost ) return +1 ;
36+ if (n1 .fCost > n0 .fCost ) return -1 ;
37+ return 0 ;
38+ }
39+
40+ };
41+
2942 public LevelHandler (String imagePath ) {
3043
3144 if (imagePath != null ) {
@@ -194,6 +207,60 @@ public void movePlayer(String username, int x, int y, int numSteps,
194207 player .setMovingDir (movingDir );
195208 }
196209
210+ public List <Node > findPath (Vector2i start , Vector2i goal ){
211+ List <Node > openList = new ArrayList <Node >();
212+ List <Node > closedList = new ArrayList <Node >();
213+ Node current = new Node (start , null , 0 , getDistance (start , goal ));
214+ openList .add (current );
215+ while (openList .size () > 0 ){
216+ Collections .sort (openList , nodeSorter );
217+ current = openList .get (0 );
218+ if (current .tile .equals (goal )){
219+ List <Node > path = new ArrayList <Node >();
220+ while (current .parent != null ) {
221+ path .add (current );
222+ current = current .parent ;
223+ }
224+ openList .clear ();
225+ closedList .clear ();
226+ return path ;
227+ }
228+ openList .remove (current );
229+ closedList .add (current );
230+ for (int i = 0 ; i < 9 ; i ++){
231+ if (i == 4 ) continue ;
232+ int x = current .tile .getX ();
233+ int y = current .tile .getY ();
234+ int xi = (i % 3 ) - 1 ;
235+ int yi = (i / 3 ) - 1 ;
236+ Tile at = getTile (x + xi ,y + yi );
237+ if (at == null ) continue ;
238+ if (at .isSolid ()) continue ;
239+ Vector2i a = new Vector2i (x + xi , y + yi );
240+ double gCost = current .gCost + getDistance (current .tile , a );
241+ double hCost = getDistance (a , goal );
242+ Node node = new Node (a , current , gCost , hCost );
243+ if (isVectorInList (closedList , a ) && gCost >= node .gCost ) continue ;
244+ if (!isVectorInList (openList , a ) || gCost < node .gCost ) openList .add (node );
245+ }
246+ }
247+ closedList .clear ();
248+ return null ;
249+ }
250+
251+ private boolean isVectorInList (List <Node > list , Vector2i vector ){
252+ for (Node n : list ){
253+ if (n .tile .equals (vector )) return true ;
254+ }
255+ return false ;
256+ }
257+
258+ private double getDistance (Vector2i tile , Vector2i goal ){
259+ double dx = tile .getX () - goal .getX ();
260+ double dy = tile .getY () - goal .getY ();
261+ return Math .sqrt (dx * dx + dy * dy );
262+ }
263+
197264 public List <Entity > getEntities (Entity e , int radius ){
198265 List <Entity > result = new ArrayList <Entity >();
199266 int ex = (int ) e .getX ();
0 commit comments