Example #1
0
    public virtual float CalG()
    {
        BaseCell parentCell = CellManager.Instance.GetCellByID(parentID);

        //if (parentCell == null)
        //{
        //    // 无父节点
        //    return 0;
        //}
        //gVal = parentCell.gVal + 1;
        //return gVal;

        if (parentCell == null)
        {
            // 无父节点
            return(PassableDifficulty);
        }
        return(parentCell.CalG() + PassableDifficulty);
    }
Example #2
0
    private int CalF(int targetCellID, int presentCellID)
    {
        BaseCell presentCell = CellManager.Instance.GetCellByID(presentCellID);

        if (presentCell == null)
        {
            Debug.LogError("当前网格ID不合法");
            return(-1);
        }
        int g = presentCell.CalG();
        int h = CalH(targetCellID, presentCellID);

        if (h == -1)
        {
            return(-1);
        }

        return(g + h);
    }
Example #3
0
    private List <int> AStarSearch(int startCellId, int endCellId)
    {
        if (!CheckCanSearch(startCellId, endCellId))
        {
            return(null);
        }

        BaseCell searchingCell = OnSearchStart(startCellId);

        if (searchingCell == null)
        {
            return(null);
        }

        bool keepSearching = true;

        while (keepSearching)
        {
            if (searchingCell.ID == endCellId)
            {
                keepSearching = false;
            }

            m_CellsToSearch.Remove(searchingCell);
            m_CellsSearched.Add(searchingCell);

            foreach (BaseCell neighbor in searchingCell.neighbors)
            {
                if (neighbor == null || neighbor.block)
                {
                    continue;
                }
                if (m_CellsToSearch.Contains(neighbor))
                {
                    continue;
                }
                if (m_CellsSearched.Contains(neighbor))
                {
                    if (neighbor.CalG() > neighbor.PassableDifficulty + searchingCell.CalG())
                    {
                        neighbor.SetParent(searchingCell.ID);
                    }
                    continue;
                }

                m_CellsToSearch.Add(neighbor);
                neighbor.SetParent(searchingCell.ID);
                if (neighbor.ID == endCellId)
                {
                    // 走到了
                    m_Path.Add(neighbor.ID);
                    if (needDemonstration)
                    {
                        neighbor.GetComponent <SpriteRenderer>().color = Color.blue;
                    }

                    BaseCell traceNeighbor = neighbor;
                    int      i             = 0;
                    while (CellManager.Instance.GetCellByID(traceNeighbor.parentID) != null && i < 100)
                    {
                        traceNeighbor = CellManager.Instance.GetCellByID(traceNeighbor.parentID);

                        if (needDemonstration)
                        {
                            traceNeighbor.GetComponent <SpriteRenderer>().color = Color.blue;
                        }

                        m_Path.Add(traceNeighbor.ID);
                        i++;
                    }

                    m_Path.Reverse();

                    return(m_Path);
                }
            }

            BaseCell newSearchingCell = null;
            float    minF             = -1;
            foreach (var cell in m_CellsToSearch)
            {
                float f = CalF(endCellId, cell.ID);
                if (f < 0)
                {
                    Debug.Log("这货距离小于零啊,有问题");
                    continue;
                }
                else if (f <= minF || minF < 0)
                {
                    minF             = f;
                    newSearchingCell = cell;
                }
            }

            if (m_CellsToSearch.Count == 0)
            {
                Debug.Log("Open列表为空,搜索结束");
                break;
            }
            if (newSearchingCell == null)
            {
                Debug.Log("找不到下一个搜索节点,搜索结束");
                break;
            }

            if (needDemonstration)
            {
                newSearchingCell.GetComponent <SpriteRenderer>().color = Color.yellow;
            }
            searchingCell = newSearchingCell;
        }
        return(null);
    }