Beispiel #1
0
 public bool Equals(AScore a)
 {
     if (a.F == F)
     {
         return(true);
     }
     return(false);
 }
Beispiel #2
0
 public int CompareTo(AScore a2)
 {
     if (F == a2.F)
     {
         return(0);
     }
     if (F > a2.F)
     {
         return(1);
     }
     return(-1);
 }
Beispiel #3
0
        IEnumerator AStar()
        {
            astar_search = new AScore[map.GetLength(0), map.GetLength(1)];

            List <Pos> list = new List <Pos>();

            astar_search[startPos.y, startPos.x] = new AScore(0, 0);
            list.Add(startPos);

            // 每一个点的处理函数
            Func func = (Pos cur, int ox, int oy) =>
            {
                var o_score = astar_search[cur.y + oy, cur.x + ox];
                if (o_score != null && o_score.closed)
                {
                    return(false);
                }
                var cur_score = astar_search[cur.y, cur.x];
                Pos o_pos     = new Pos(cur.x + ox, cur.y + oy);
                if (map[cur.y + oy, cur.x + ox] == END)
                {
                    var a = new AScore(cur_score.G + 1, 0);
                    a.parent = cur;
                    astar_search[cur.y + oy, cur.x + ox] = a;
                    Debug.Log("寻路完成");
                    return(true);
                }
                if (map[cur.y + oy, cur.x + ox] == 0)
                {
                    if (o_score == null)
                    {
                        var a = new AScore(cur_score.G + 1, Pos.AStarDistance(o_pos, endPos));
                        a.parent = cur;
                        astar_search[cur.y + oy, cur.x + ox] = a;
                        list.Add(o_pos);
                    }
                    else if (o_score.G > cur_score.G + 1)
                    {
                        o_score.G      = cur_score.G + 1;
                        o_score.parent = cur;
                        if (!list.Contains(o_pos))
                        {
                            list.Add(o_pos);
                        }
                    }
                }
                return(false);
            };


            while (list.Count > 0)
            {
                list.Sort((Pos p1, Pos p2) =>
                {
                    AScore a1 = astar_search[p1.y, p1.x];
                    AScore a2 = astar_search[p2.y, p2.x];
                    //return a1.H.CompareTo(a2.H);
                    return(a1.CompareTo(a2));
                });
                Pos cur = list[0];
                list.RemoveAt(0);
                astar_search[cur.y, cur.x].closed = true;

                // 上
                if (cur.y > 0)
                {
                    if (func(cur, 0, -1))
                    {
                        break;
                    }
                }
                // 下
                if (cur.y < H - 1)
                {
                    if (func(cur, 0, 1))
                    {
                        break;
                    }
                }
                // 左
                if (cur.x > 0)
                {
                    if (func(cur, -1, 0))
                    {
                        break;
                    }
                }
                // 右
                if (cur.x < W - 1)
                {
                    if (func(cur, 1, 0))
                    {
                        break;
                    }
                }

                short[,] temp_map = new short[map.GetLength(0), map.GetLength(1)];
                for (int i = 0; i < H; ++i)
                {
                    for (int j = 0; j < W; ++j)
                    {
                        temp_map[i, j] = short.MaxValue;
                        //if (map_search[i,j] != null && map_search[i,j].closed)
                        if (astar_search[i, j] != null)
                        {
                            temp_map[i, j] = (short)astar_search[i, j].F;
                        }
                    }
                }
                RefreshPath(temp_map);
                yield return(0);
            }
            gameState = GameState.ShowPath;
            yield return(null);
        }