Просмотр исходного кода

parsing correct coordinates for the graph

mesinger 6 лет назад
Родитель
Сommit
7f0f586dec

+ 5 - 7
src/me/hammerle/supersnuvi/Main.java

@@ -5,6 +5,8 @@ import me.hammerle.supersnuvi.gamelogic.LevelPathParser;
 import me.hammerle.supersnuvi.util.Pair;
 
 import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 public class Main
@@ -15,13 +17,9 @@ public class Main
 
         Level[] levels = game.getLevels();
 
-        Set<Pair<Integer, Integer>> coordinates = LevelPathParser.getCoordinatesOfCollisionTiles(levels[0]);
+        Map<Integer, List<Integer>> coordinates = LevelPathParser.getCoordinatesOfCollisionTiles(levels[0]);
 
-        Iterator<Pair<Integer, Integer>> it = coordinates.iterator();
-
-        while(it.hasNext()){
-            System.out.println(it.next());
-        }
+        LevelPathParser.getGraphCoordinates(coordinates);
 
 //        for(Level lvl : levels) {
 //
@@ -30,6 +28,6 @@ public class Main
 //
 //        }
 
-         game.run();
+//         game.run();
     }
 }

+ 55 - 8
src/me/hammerle/supersnuvi/gamelogic/LevelPathParser.java

@@ -5,16 +5,15 @@ import me.hammerle.supersnuvi.tiles.Tile;
 import me.hammerle.supersnuvi.util.CollisionObject;
 import me.hammerle.supersnuvi.util.Pair;
 
-import java.util.HashSet;
-import java.util.Set;
+import java.util.*;
 
 public class LevelPathParser {
 
-    public static Set<Pair<Integer, Integer>> getCoordinatesOfCollisionTiles(Level lvl){
+    public static Map<Integer, List<Integer>> getCoordinatesOfCollisionTiles(Level lvl){
 
-        Set<Pair<Integer, Integer>> res = new HashSet<>();
+        Map<Integer, List<Integer>> coordinates = new HashMap<>();
 
-        if(lvl == null) return res;
+        if(lvl == null) return coordinates;
 
         LevelData lvldata = lvl.getData();
 
@@ -30,12 +29,60 @@ public class LevelPathParser {
 
                 if(tile.getMovementBox(x, y, lvl) != CollisionObject.NULL_BOX){
 
-                    Pair<Integer, Integer> point = new Pair<>(x, y);
-                    res.add(point);
+                    if(coordinates.get(x) == null)
+                        coordinates.put(x, new ArrayList<>());
+
+                    coordinates.get(x).add(y);
                 }
             }
         }
 
-        return res;
+        return coordinates;
+    }
+
+    public static List<Pair<Integer, Integer>> getGraphCoordinates(Map<Integer, List<Integer>> collisionTileCoordinates){
+
+        List<Pair<Integer, Integer>> graphCoordinates = new ArrayList<>();
+
+        if(collisionTileCoordinates == null || collisionTileCoordinates.size() <= 0) return graphCoordinates;
+
+        for(int key : collisionTileCoordinates.keySet()){
+            Collections.sort(collisionTileCoordinates.get(key));
+            Collections.reverse(collisionTileCoordinates.get(key));
+
+            Stack<Integer> result = findGraphCoordinatesInRow(collisionTileCoordinates.get(key), new Stack<Integer>());
+            System.out.println(result);
+        }
+
+
+        return graphCoordinates;
+    }
+
+    /**
+     * returns the y coordinates of a rows graph knots as a stack
+     * @param row (has to be ordered in ascending order)
+     * @param result (resulting stack of the y coordinates
+     * @return
+     */
+    private static Stack<Integer> findGraphCoordinatesInRow(List<Integer> row, Stack<Integer> result){
+
+        if(row.isEmpty()) return result;
+
+        if(result.empty()){
+
+            result.push(row.get(0));
+            row.remove(0);
+        }
+
+        int curr = result.pop();
+        if(row.get(0) < curr - 1){
+
+            result.push(curr);
+        }
+
+        result.push(row.get(0));
+        row.remove(0);
+
+        return LevelPathParser.findGraphCoordinatesInRow(row, result);
     }
 }