码迷,mamicode.com
首页 > 其他好文 > 详细

A星之地形成本的实现

时间:2015-07-01 06:27:56      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

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

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(dubenju@126.com)

  */

 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星之地形成本的实现

标签:

原文地址:http://my.oschina.net/u/660460/blog/472730

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!