Esempio n. 1
0
    void Astar()
    {
        searchingList = new List <MapPosition>();
        astarSearch   = new AStarScore[map.GetLength(0), map.GetLength(1)];

        astarSearch[startPoint.PosX, startPoint.PosY] = new AStarScore(0, 0);

        searchingList.Add(startPoint);

        MapPosition checkingPos = searchingList[0];

        searchingList.RemoveAt(0);

        while (!checkingPos.Equals(endPoint))
        {
            if (CheckPos(-1, 0))
            {
                return;
            }
            if (CheckPos(1, 0))
            {
                return;
            }
            if (CheckPos(0, -1))
            {
                return;
            }
            if (CheckPos(0, 1))
            {
                return;
            }

            searchingList.Sort((MapPosition pos1, MapPosition pos2) =>
            {
                AStarScore a1 = astarSearch[pos1.PosX, pos1.PosY];
                AStarScore a2 = astarSearch[pos2.PosX, pos2.PosY];

                return(a1.CompareTo(a2));
            });

            checkingPos = searchingList[0];
            searchingList.RemoveAt(0);
        }

        bool CheckPos(int offsetX, int offsetY)
        {
            int x = checkingPos.PosX + offsetX;
            int y = checkingPos.PosY + offsetY;

            if (x >= 0 && x < H && y >= 0 && y < W)
            {
                AStarScore tempScore = astarSearch[x, y];

                AStarScore  checkingScore = astarSearch[checkingPos.PosX, checkingPos.PosY];
                MapPosition tempPos       = new MapPosition(x, y);

                //////////////////if (map[x, y] == END)
                //////////////////{
                //////////////////    AScore a = new AScore(checkingScore._gScore + 1, 0);
                //////////////////    a.Parent = checkingPos;
                //////////////////    astarSearch[x, y] = a;

                //////////////////    searchBlocks[x, y].transform.GetChild(0).GetChild(0).GetComponent<Text>().text = astarSearch[x, y].g.ToString();
                //////////////////    searchBlocks[x, y].transform.GetChild(0).GetChild(1).GetComponent<Text>().text = astarSearch[x, y].h.ToString();
                //////////////////    searchBlocks[x, y].transform.GetChild(0).GetChild(2).GetComponent<Text>().text = astarSearch[x, y].F.ToString();
                //////////////////    searchBlocks[x, y].GetComponent<Renderer>().material = searchMat;
                //////////////////    searchBlocks[x, y].SetActive(true);

                //////////////////    isFound = true;
                //////////////////    searchingList.Clear();
                //////////////////    return true;
                //////////////////}
                //////////////////if (map[x, y] == EMPTY)
                //////////////////{
                //////////////////    if (tempScore == null)
                //////////////////    {
                //////////////////        AScore a = new AScore(checkingScore._gScore + 1, MapPosition.AStarDistance(tempPos, endPoint));
                //////////////////        a.Parent = checkingPos;
                //////////////////        astarSearch[x, y] = a;
                //////////////////        searchingList.Add(tempPos);
                //////////////////    }
                //////////////////    else if (tempScore._gScore > checkingScore._gScore + 1)
                //////////////////    {
                //////////////////        tempScore._gScore = checkingScore._gScore + 1;
                //////////////////        tempScore.Parent = checkingPos;

                //////////////////        if (!searchingList.Contains(tempPos))
                //////////////////        {
                //////////////////            searchingList.Add(tempPos);
                //////////////////        }
                //////////////////    }

                //////////////////    searchBlocks[x, y].transform.GetChild(0).GetChild(0).GetComponent<Text>().text = astarSearch[x, y].g.ToString();
                //////////////////    searchBlocks[x, y].transform.GetChild(0).GetChild(1).GetComponent<Text>().text = astarSearch[x, y].h.ToString();
                //////////////////    searchBlocks[x, y].transform.GetChild(0).GetChild(2).GetComponent<Text>().text = astarSearch[x, y].F.ToString();
                //////////////////    searchBlocks[x, y].GetComponent<Renderer>().material = searchMat;
                //////////////////    searchBlocks[x, y].SetActive(true);
                //////////////////}
            }

            return(false);
        }
    }
Esempio n. 2
0
 public int CompareTo(AStarScore aScore) => F.CompareTo(aScore.F);
Esempio n. 3
0
 public bool Equals(AStarScore aScore) => F.Equals(aScore.F);
Esempio n. 4
0
    public IEnumerator StartFinding(PathFinding pathFinding)
    {
        _searchingList = new List <MapPosition>();

        _searchingMap = new AStarScore[pathFinding.OriginMap.GetLength(0), pathFinding.OriginMap.GetLength(1)];
        _searchingMap[pathFinding.StartPoint.PosX, pathFinding.StartPoint.PosY] = new AStarScore(0, 0);

        _searchingList.Add(pathFinding.StartPoint);

        var checkingPos = _searchingList[0];

        _searchingList.RemoveAt(0);

        while (!checkingPos.Equals(pathFinding.EndPoint))
        {
            if (CheckPos(-1, 0))
            {
                yield break;
            }
            if (CheckPos(1, 0))
            {
                yield break;
            }
            if (CheckPos(0, -1))
            {
                yield break;
            }
            if (CheckPos(0, 1))
            {
                yield break;
            }

            _searchingList.Sort((MapPosition posA, MapPosition posB) =>
            {
                var aStarA = _searchingMap[posA.PosX, posA.PosY];
                var aStarB = _searchingMap[posB.PosX, posB.PosY];

                return(aStarA.CompareTo(aStarB));
            });

            checkingPos = _searchingList[0];
            _searchingList.RemoveAt(0);

            yield return(null);
        }

        bool CheckPos(int offsetX, int offsetY)
        {
            int nextX = checkingPos.PosX + offsetX;
            int nextY = checkingPos.PosY + offsetY;

            if (nextX < 0 || nextX >= PathFinding.MAP_HEIGHT)
            {
                return(false);
            }
            if (nextY < 0 || nextY >= PathFinding.MAP_WIDTH)
            {
                return(false);
            }

            var tempScore = _searchingMap[nextX, nextY];
            //if (tempScore != null) { return false; }
            var checkingScore = _searchingMap[checkingPos.PosX, checkingPos.PosY];
            var tempPos       = new MapPosition(nextX, nextY);

            if (pathFinding.OriginMap[nextX, nextY] == PathFinding.POINT_END)
            {
                var a = new AStarScore(checkingScore.G + 1, 0);
                a.SetParent(checkingPos);
                _searchingMap[nextX, nextY] = a;

                UpdateSearchBlock(pathFinding, nextX, nextY);

                pathFinding.SetIsFound(true);
                _searchingList.Clear();
                return(true);
            }

            if (pathFinding.OriginMap[nextX, nextY] == PathFinding.POINT_EMPTY)
            {
                if (tempScore == null)
                {
                    var a = new AStarScore(checkingScore.G + 1, MapPosition.AStarDistance(tempPos, pathFinding.EndPoint));
                    a.SetParent(checkingPos);
                    _searchingMap[nextX, nextY] = a;
                    _searchingList.Add(tempPos);
                }
                else if (tempScore.G > checkingScore.G + 1)
                {
                    tempScore.SetGScore(checkingScore.G + 1);
                    tempScore.SetParent(checkingPos);

                    if (!_searchingList.Contains(tempPos))
                    {
                        _searchingList.Add(tempPos);
                    }
                }

                UpdateSearchBlock(pathFinding, nextX, nextY);
            }

            return(false);
        }
    }