Эх сурвалжийг харах

add directions to graph edge

mesinger 6 жил өмнө
parent
commit
0dd5ef9ca8

+ 4 - 12
src/me/hammerle/supersnuvi/Main.java

@@ -1,7 +1,7 @@
 package me.hammerle.supersnuvi;
 
 import me.hammerle.supersnuvi.gamelogic.Level;
-import me.hammerle.supersnuvi.gamelogic.pathfinding.LevelPathParser;
+import me.hammerle.supersnuvi.gamelogic.pathfinding.LevelGraphBuilder;
 
 import java.util.List;
 import java.util.Map;
@@ -14,17 +14,9 @@ public class Main
 
         Level[] levels = game.getLevels();
 
-        Map<Integer, List<Integer>> coordinates = LevelPathParser.getCoordinatesOfCollisionTiles(levels[0]);
+        LevelGraphBuilder levelGraphBuilder = new LevelGraphBuilder();
+        levelGraphBuilder.getLevelAsGraph(levels[0]);
 
-        LevelPathParser.getGraphCoordinates(coordinates);
-
-//        for(Level lvl : levels) {
-//
-//            Set<Pair<Integer, Integer>> coordinates = LevelPathParser.getCoordinatesOfCollisionTiles(lvl);
-//
-//
-//        }
-
-//         game.run();
+        game.run();
     }
 }

+ 14 - 0
src/me/hammerle/supersnuvi/gamelogic/pathfinding/IDirectedEdge.java

@@ -0,0 +1,14 @@
+package me.hammerle.supersnuvi.gamelogic.pathfinding;
+
+/**
+ * edge with or without direction
+ */
+public interface IDirectedEdge {
+
+    // possible directions
+    int DIRECTION_NODE1_TO_NODE2 = 0x01;
+    int DIRECTION_NODE2_TO_NODE1 = 0x02;
+    int DIRECTION_DUPLEX = 0x03;
+
+    int getDirection();
+}

+ 2 - 2
src/me/hammerle/supersnuvi/gamelogic/pathfinding/IEdge.java

@@ -5,7 +5,7 @@ package me.hammerle.supersnuvi.gamelogic.pathfinding;
  */
 public interface IEdge {
 
-    INode getStart();
-    INode getEnd();
+    INode getNode1();
+    INode getNode2();
     double getWeight();
 }

+ 52 - 19
src/me/hammerle/supersnuvi/gamelogic/pathfinding/LevelPathParser.java → src/me/hammerle/supersnuvi/gamelogic/pathfinding/LevelGraphBuilder.java

@@ -10,25 +10,41 @@ import me.hammerle.supersnuvi.util.Pair;
 
 import java.util.*;
 
-public class LevelPathParser {
+public class LevelGraphBuilder {
 
-    private final ILevel level;
-    private final LevelData levelData;
-
-    public LevelPathParser(Level level) {
-        this.level = level;
-        levelData = level.getData();
+    public LevelGraphBuilder() {
     }
 
+    public LevelGraph getLevelAsGraph(Level lvl){
 
+        if(lvl == null) return null;
 
-    public static Map<Integer, List<Integer>> getCoordinatesOfCollisionTiles(Level lvl){
+        LevelData lvlData = lvl.getData();
 
-        Map<Integer, List<Integer>> coordinates = new HashMap<>();
+        if(lvlData == null) return null;
+
+        Map<Integer, List<Integer>> collisionTileCoordinates = getCoordinatesOfCollisionTiles(lvl, lvlData);
+
+        if(collisionTileCoordinates.size() == 0) return null;
+
+        List<Pair<Integer, Integer>> graphCoordinates = getListOfGraphCoordinates(collisionTileCoordinates);
 
-        if(lvl == null) return coordinates;
+        if(graphCoordinates.size() == 0) return null;
 
-        LevelData lvldata = lvl.getData();
+        return null;
+    }
+
+    /**
+     * returns coordinates of all tiles in the given level
+     * which can collide with entites (e.g ground tile)
+     *
+     * @param lvl
+     * @param lvldata
+     * @return coordinates of tiles as map (key = x coord, y = list of y coordinates)
+     */
+    private Map<Integer, List<Integer>> getCoordinatesOfCollisionTiles(Level lvl, LevelData lvldata){
+
+        Map<Integer, List<Integer>> coordinates = new HashMap<>();
 
         int backgroundIndex = lvldata.getBackgroundIndex();
         int width = lvldata.getWidth();
@@ -53,18 +69,32 @@ public class LevelPathParser {
         return coordinates;
     }
 
-    public static List<Pair<Integer, Integer>> getGraphCoordinates(Map<Integer, List<Integer>> collisionTileCoordinates){
+    /**
+     * extracts from all collisiontiles a list of coordinates,
+     * which are used for the graph
+     *
+     * lists in the passed map will be emptied
+     *
+     * @param collisionTileCoordinates
+     * @return list of integer pairs
+     */
+    private List<Pair<Integer, Integer>> getListOfGraphCoordinates(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);
+            Stack<Integer> ycoordinates = findLowestIntegersInRow(collisionTileCoordinates.get(key), new Stack<Integer>());
+
+            while(!ycoordinates.isEmpty()){
+
+                graphCoordinates.add(new Pair<>(key, ycoordinates.pop()));
+            }
         }
 
 
@@ -72,12 +102,15 @@ public class LevelPathParser {
     }
 
     /**
-     * returns the y coordinates of a rows graph knots as a stack
+     * recursively, searches for the lowest integers i in the given list
+     * if there is no (i+1) integer in the list
+     * e.g [10, 9, 5] will return [9, 5]
+     *
      * @param row (has to be ordered in ascending order)
-     * @param result (resulting stack of the y coordinates
-     * @return
+     * @param result (resulting stack of the y coordinates)
+     * @return stack of y coordinates
      */
-    private static Stack<Integer> findGraphCoordinatesInRow(List<Integer> row, Stack<Integer> result){
+    private Stack<Integer> findLowestIntegersInRow(List<Integer> row, Stack<Integer> result){
 
         if(row.isEmpty()) return result;
 
@@ -96,6 +129,6 @@ public class LevelPathParser {
         result.push(row.get(0));
         row.remove(0);
 
-        return LevelPathParser.findGraphCoordinatesInRow(row, result);
+        return findLowestIntegersInRow(row, result);
     }
 }

+ 7 - 19
src/me/hammerle/supersnuvi/gamelogic/pathfinding/PathEdge.java

@@ -4,31 +4,19 @@ package me.hammerle.supersnuvi.gamelogic.pathfinding;
  * edge of the graph, which represents the level layout
  * used for pathfinding
  */
-public class PathEdge implements IEdge {
+public class PathEdge extends SimpleEdge implements IDirectedEdge {
 
-    private PathNode start;
-    private PathNode end;
-    private double weight;
-
-    public PathEdge(PathNode start, PathNode end, double weight) {
-        this.start = start;
-        this.end = end;
-        this.weight = weight;
-    }
+    protected int direction;
 
+    public PathEdge(PathNode node1, PathNode node2, double weight, int direction) {
+        super(node1, node2, weight);
 
-    @Override
-    public INode getStart() {
-        return start;
+        this.direction = direction;
     }
 
-    @Override
-    public INode getEnd() {
-        return end;
-    }
 
     @Override
-    public double getWeight() {
-        return weight;
+    public int getDirection() {
+        return direction;
     }
 }

+ 34 - 0
src/me/hammerle/supersnuvi/gamelogic/pathfinding/SimpleEdge.java

@@ -0,0 +1,34 @@
+package me.hammerle.supersnuvi.gamelogic.pathfinding;
+
+/**
+* edge of a graph, without direction
+* edge of the graph, which represents the level layout
+* used for pathfinding
+*/
+public class SimpleEdge implements IEdge {
+
+    protected PathNode node1;
+    protected PathNode node2;
+    protected double weight;
+
+    public SimpleEdge(PathNode node1, PathNode node2, double weight) {
+        this.node1 = node1;
+        this.node2 = node2;
+        this.weight = weight;
+    }
+
+    @Override
+    public INode getNode1() {
+        return node1;
+    }
+
+    @Override
+    public INode getNode2() {
+        return node2;
+    }
+
+    @Override
+    public double getWeight() {
+        return weight;
+    }
+}