Exemplo n.º 1
0
        public virtual bool CanAddAdjacentToReachable(PathFinding search, CellData adjacent)
        {
            // 如果已经在关闭集
            if (search.IsCellInExpored(adjacent))
            {
                return(false);
            }

            // 已经加入过开放集
            if (search.IsCellInReachable(adjacent))
            {
                return(false);
            }

            // 计算消耗 = 当前cell的消耗 + 邻居cell的消耗
            float g = search.currentCell.g + CalcGPerCell(search, adjacent);

            // 不在范围内
            if (g < 0f || g > search.range.y)
            {
                return(false);
            }

            adjacent.g = g;

            return(true);
        }
Exemplo n.º 2
0
        public override float CalcGPerCell(PathFinding search, CellData adjacent)
        {
            // 获取邻居的Tile
            SrpgTile tile = search.map.GetTile(adjacent.position);

            // 返回本格子的消耗
            return(search.GetMoveConsumption(tile.terrainType));
        }
Exemplo n.º 3
0
        public override float CalcH(PathFinding search, CellData adjacent)
        {
            Vector2 hVec;

            hVec.x = Mathf.Abs(adjacent.position.x - search.endCell.position.x);
            hVec.y = Mathf.Abs(adjacent.position.y - search.endCell.position.y);
            return(hVec.x + hVec.y);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 用H计算攻击距离
        /// </summary>
        /// <param name="search"></param>
        /// <param name="adjacent"></param>
        /// <returns></returns>
        public virtual float CalcH(PathFinding search, CellData adjacent)
        {
            CellData startCell = search.startCell ?? search.endCell;
            Vector2  hVec;

            hVec.x = Mathf.Abs(adjacent.position.x - startCell.position.x);
            hVec.y = Mathf.Abs(adjacent.position.y - startCell.position.y);
            return(hVec.x + hVec.y);
        }
Exemplo n.º 5
0
        public override void BuildResult(PathFinding search)
        {
            // 当没有达到目标点时,已经建立过结果
            if (search.result.Count > 0)
            {
                return;
            }

            search.BuildPath(search.endCell, true);
        }
Exemplo n.º 6
0
 public virtual void BuildResult(PathFinding search)
 {
     for (int i = 0; i < search.explored.Count; i++)
     {
         CellData cell = search.explored[i];
         if (cell.g >= search.range.x && cell.g <= search.range.y)
         {
             search.result.Add(cell);
         }
     }
 }
Exemplo n.º 7
0
        public override bool CanAddAdjacentToReachable(PathFinding search, CellData adjacent)
        {
            //// 没有Tile
            //if (!adjacent.hasTile)
            //{
            //    return false;
            //}

            //// 已经有对象了
            //if (adjacent.hasMapObject)
            //{
            //    return false;
            //}

            // 是否可移动
            if (!adjacent.canMove)
            {
                return(false);
            }

            // 如果已经在关闭集
            if (search.IsCellInExpored(adjacent))
            {
                return(false);
            }

            // 计算消耗 = 当前cell的消耗 + 邻居cell的消耗
            float g = search.currentCell.g + CalcGPerCell(search, adjacent);

            // 已经加入过开放集
            if (search.IsCellInReachable(adjacent))
            {
                // 如果新消耗更低
                if (g < adjacent.g)
                {
                    adjacent.g        = g;
                    adjacent.previous = search.currentCell;
                }
                return(false);
            }

            // 不在范围内
            if (g < 0f || g > search.range.y)
            {
                return(false);
            }

            adjacent.g        = g;
            adjacent.previous = search.currentCell;

            return(true);
        }
Exemplo n.º 8
0
        public virtual bool IsFinishedOnChose(PathFinding search)
        {
            if (search.currentCell == null)
            {
                return(true);
            }

            if (!search.IsCellInExpored(search.currentCell))
            {
                search.explored.Add(search.currentCell);
            }
            return(false);
        }
Exemplo n.º 9
0
        public virtual CellData ChoseCell(PathFinding search)
        {
            if (search.reachable.Count == 0)
            {
                return(null);
            }

            int      index = search.reachable.Count - 1;
            CellData chose = search.reachable[index];

            search.reachable.RemoveAt(index);
            return(chose);
        }
Exemplo n.º 10
0
        public override CellData ChoseCell(PathFinding search)
        {
            if (search.reachable.Count == 0)
            {
                return(null);
            }

            /// 取得f最小的节点(因为我们没有计算h,这里就是g)
            /// 当你在寻找路径有卡顿时,请一定使用更好的查找方式,
            /// 例如可以改用二叉树的方式,
            /// 也可将PathFinding里面reachable.Add(adjacent)的方法改成边排序边加入的方法
            search.reachable.Sort((cell1, cell2) => - cell1.f.CompareTo(cell2.f));
            int      index = search.reachable.Count - 1;
            CellData chose = search.reachable[index];

            search.reachable.RemoveAt(index);
            return(chose);
        }
Exemplo n.º 11
0
        public virtual bool CanAddAdjacentToReachable(PathFinding search, CellData adjacent)
        {
            // 如果已经在关闭集
            if (search.IsCellInExpored(adjacent))
            {
                return(false);
            }

            // 已经加入过开放集
            if (search.IsCellInReachable(adjacent))
            {
                return(false);
            }

            //// 计算消耗 = 当前cell的消耗 + 邻居cell的消耗
            //float g = search.currentCell.g + CalcGPerCell(search, adjacent);

            // 计算攻击距离
            float h = CalcH(search, adjacent);

            //// 不在范围内
            //if (g < 0f || g > search.range.y)
            //{
            //    return false;
            //}

            // 攻击距离不在范围内
            if (h < 0f || h > search.range.y)
            {
                return(false);
            }

            //adjacent.g = g;
            adjacent.h = h;

            return(true);
        }
Exemplo n.º 12
0
        public override bool IsFinishedOnChose(PathFinding search)
        {
            // 如果开放集中已经空了,则说明没有达到目标点
            if (search.currentCell == null)
            {
                // 使用h最小值建立结果
                CellData minHCell = search.explored.First(cell => cell.h == search.explored.Min(c => c.h));
                search.BuildPath(minHCell, true);
                return(true);
            }

            // 找到了目标点
            if (search.currentCell == search.endCell)
            {
                return(true);
            }

            if (!search.IsCellInExpored(search.currentCell))
            {
                search.explored.Add(search.currentCell);
            }

            return(false);
        }
Exemplo n.º 13
0
 public virtual float CalcGPerCell(PathFinding search, CellData adjacent)
 {
     return(1f);
 }
Exemplo n.º 14
0
 public virtual float CalcH(PathFinding search, CellData adjacent)
 {
     return(0f);
 }