A星之地形成本的实现

昨天提交了一点点的改动,是关于地形成本的。

https://git.oschina.net/dubenju/encv

 

AStarConstants.java追加对于地形的定义。

 

--- a/src/java/astar/AStarConstants.java

+++ b/src/java/astar/AStarConstants.java

@@ -8,10 +8,15 @@ package astar;

  * @author DBJ([email protected])

  */

 public class AStarConstants {

+            public static int COST_NONE = 0;

     /** 正交移动一格的路径分值 */

     public static int COST_ORTHOGONAL = 10;

     /** 对角线移动一格的路径分值 */

     public static int COST_DIAGONAL = 14;

+    public static int COST_GRASS = 12; // 草地

+    public static int COST_HILL = 20; // 丘陵

+    public static int COST_SWAMP = 30; // 沼泽

+    public static int COST_RIVER = 40; // 河流

 

在地形类Terrain中,追加对地形的考虑。

 

--- a/src/java/astar/Terrain.java

+++ b/src/java/astar/Terrain.java

@@ -10,6 +10,7 @@ public class Terrain {

 

     private int val;

     private int walkable;

+    private int cost;

 

     /**

      * 构造函数

@@ -17,19 +18,54 @@ public class Terrain {

     public Terrain(int val) {

         this.val = val;

         if (this.val == 0) {

+                    // 0:unwalkable

             this.walkable = AStarConstants.NOTE_UNWALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 1) {

+                    // 1:walkbale,ground

             this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 2) {

+                    // 2:

             this.walkable = AStarConstants.NOTE_UNWALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 3) {

+                    // 3:侧壁

             this.walkable = AStarConstants.NOTE_UNWALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

         if (this.val == 4) {

+                    // 4:target:目标

             this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_NONE;

+        }

+        if (this.val == 5) {

+                    // 5:grass:草地

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_GRASS;

+        }

+        if (this.val == 6) {

+                    // 6:hill:丘陵

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_HILL;

+        }

+        if (this.val == 7) {

+                    // 7:swamp:沼泽

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_SWAMP;

+        }

+        if (this.val == 8) {

+                    //  8:river:河流

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_RIVER;

+        }

+        if (this.val == 9) {

+                    // 9:bridge:桥

+            this.walkable = AStarConstants.NOTE_WALKABLE;

+            this.cost = AStarConstants.COST_NONE;

         }

     }

 

@@ -60,4 +96,18 @@ public class Terrain {

     public void setWalkable(int walkable) {

         this.walkable = walkable;

     }

+

+            /**

+             * @return cost

+             */

+            public int getCost() {

+                          return cost;

+            }

+

+            /**

+             * @param cost

+             */

+            public void setCost(int cost) {

+                          this.cost = cost;

+            }

 

AStar中计算成本时考虑地形的成本。

--- a/src/java/astar/AStar.java

+++ b/src/java/astar/AStar.java

            node.setG(parent.getG() + step);

                      // 考虑地形的成本

            node.setG(parent.getG() + step + node.getTerrain().getCost());

 

测试程序结果图:

A星之地形成本的实现

 

关于A*请参照这里。



你可能感兴趣的:(实现,A*,astar,A星,地形成本)