• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

               C++ 技術中心

               :: 首頁 :: 聯系 ::  :: 管理
              160 Posts :: 0 Stories :: 87 Comments :: 0 Trackbacks

            公告

            鄭重聲明:本BLOG所發表的原創文章,作者保留一切權利。必須經過作者本人同意后方可轉載,并注名作者(天空)和出處(CppBlog.com)。作者Email:coder@luckcoder.com

            留言簿(27)

            搜索

            •  

            最新隨筆

            最新評論

            評論排行榜

            #ifndef _ASTAR_FLY_H__
            #define _ASTAR_FLY_H__
            #include "Coordinate.h"
            #include <map>
            #include <set>

            using namespace std;



            typedef struct 
            {
                int x;
                int y;
                int f; // f = g + h
                int g;
                int h;

                void Init(int _x,int _y)
                {
                    f = 0;
                    g = 0;
                    h = 0;
                    x = _x;
                    y = _y;
                }
            } APoint, *PAPoint;



            class CAStarFly
            {
            public:
                typedef std::list<CIntPoint> PosList;


                CAStarFly();
                ~CAStarFly();

                bool CalcPath(PosList& pathList, const CIntPoint& from, const CIntPoint& to);
            protected:
                virtual int GetMapWidth();
                virtual int GetMapHeight();
                virtual CIntSize GetRoleSize();
                virtual bool GetNearPos(int curX,int curY,PosList &nearPos);
                virtual bool IsCanFly(int x,int y);
                virtual void OnAddBestPoint(int x, int y);
                virtual void OnUnAddBestPoint(int x, int y);
            private:
                PAPoint CalcNextPoint(PosList& pathList, PAPoint ptCalc); // 應用遞歸的辦法進行查詢

                void SetStartPoint(int x, int y);
                void SetEndPoint(int x, int y);
                void SetOpened(int x, int y);
                void SetClosed(int x, int y);
                void SetCurrent(int x, int y);

                int32 GetPosIndex(int x, int y);
                int32 GetFValue(const CIntPoint &pos);
                bool IsNotClosePos(int x, int y);
            private:
                APoint m_startPoint; //起始點
                APoint m_endPoint;   //結束點
                APoint m_curPoint;   //當前移到點
                set<int32> m_setClosePos;
                int m_curGValue;     //當前G值
            };

            #endif/*_ASTAR_FLY_H__*/



            #include "stdafx.h"
            #include "AStarFly.h"


            CAStarFly::CAStarFly()
            {
            }

            CAStarFly::~CAStarFly()
            {
            }

            bool CAStarFly::CalcPath(PosList& pathList, const CIntPoint& from, const CIntPoint& to)
            {
                //1.先設置開始與目標
                SetStartPoint(from.x, from.y);
                SetEndPoint(to.x, to.y);
                m_curPoint = m_startPoint;
                SetClosed(m_startPoint.x, m_startPoint.y); //路徑搜索不再經過
                m_curGValue = 0;

                //從起點開始計算路徑點
                return CalcNextPoint(pathList, nullptr)!=nullptr;
            }

            PAPoint CAStarFly::CalcNextPoint(PosList& pathList, PAPoint ptCalc)
            {
                if (nullptr == ptCalc)
                {
                    ptCalc = &m_startPoint;
                }
                int curX = ptCalc->x;
                int curY = ptCalc->y;
                int destX = m_endPoint.x;
                int destY = m_endPoint.y;

                //判斷是否已經到了最終位置
                if ((curX == destX && abs(curY - destY) == 1) || (curY == destY && abs(curX - destX) == 1))
                {
                    pathList.push_back(CIntPoint(m_endPoint.x, m_endPoint.y));
                    OnAddBestPoint(m_endPoint.x, m_endPoint.y);
                    return &m_endPoint;
                }

                // 最優步驟的坐標和值
                int xmin = curX;
                int ymin = curY;
                int fmin = 0;

                //獲得當前周邊點的的坐標
                PosList nearPos;
                if (GetNearPos(curX,curY,nearPos) == false)
                {
                    return nullptr;
                }

                //刪除不能飛的區塊
                
            //找出最優f值
                for (auto itPos : nearPos)
                {
                    int fValue = GetFValue(itPos);
                    if (fmin == 0 || fValue<fmin)
                    {
                        fmin = fValue;
                        xmin = itPos.x;
                        ymin = itPos.y;
                    }
                }

                if (fmin > 0)
                {
                    SetCurrent(xmin, ymin);
                    SetClosed(xmin, ymin); //路徑搜索不再經過
                    pathList.push_back(CIntPoint(xmin,ymin));
                    OnAddBestPoint(xmin, ymin);
                    PAPoint pAPoint= CalcNextPoint(pathList,&m_curPoint);
                    if (nullptr == pAPoint)
                    {
                        SetCurrent(curX, curY);
                        SetClosed(xmin, ymin); //路徑搜索不再經過

                        
            //將最后一次入的隊刪除
                        pathList.pop_back();
                        OnUnAddBestPoint(xmin, ymin);
                        return CalcNextPoint(pathList, &m_curPoint);
                    }

                    return pAPoint;
                }
                

                return nullptr;
            }

            CIntSize CAStarFly::GetRoleSize()
            {
                return CIntSize(1,1);
            }

            int CAStarFly::GetMapWidth()
            {
                return -1;
            }

            int CAStarFly::GetMapHeight()
            {
                return -1;
            }

            void CAStarFly::SetStartPoint(int x, int y)
            {
                m_startPoint.Init( x,y);
            }

            void CAStarFly::SetEndPoint(int x, int y)
            {
                m_endPoint.Init(x,y);
            }


            void CAStarFly::SetClosed(int x, int y)
            {
                m_setClosePos.insert(GetPosIndex(x,y));
            }

            void CAStarFly::SetCurrent(int x, int y)
            {
                m_curPoint.Init( x, y);
            }

            int32 CAStarFly::GetPosIndex(int x,int y)
            {
                return  y * GetMapWidth() + x;
            }


            bool CAStarFly::GetNearPos(int curX, int curY, PosList &nearPos)
            {
                int newX;
                int newY;
                //1.上
                if (curY > 0)
                {
                    newX = curX;
                    newY = curY - 1;

                    if (IsNotClosePos(newX, newY) && IsCanFly(newX, newY))
                    {
                        nearPos.push_back(CIntPoint(newX, newY));
                    }
                }
                
                 
                


                //3.左
                if (curX > 0)
                {
                    newX = curX - 1;
                    newY = curY;

                    if (IsNotClosePos(newX, newY) && IsCanFly(newX, newY))
                    {
                        nearPos.push_back(CIntPoint(newX, newY));
                    }
                }
                

                //4.右
                if (curX < GetMapWidth())
                {
                    newX = curX + 1;
                    newY = curY;

                    if (IsNotClosePos(newX, newY) && IsCanFly(newX, newY))
                    {
                        nearPos.push_back(CIntPoint(newX, newY));
                    }
                }
                
                //2.下
                if (curY < GetMapHeight())
                {
                    newX = curX;
                    newY = curY + 1;

                    if (IsNotClosePos(newX, newY) && IsCanFly(newX, newY))
                    {
                        nearPos.push_back(CIntPoint(newX, newY));
                    }
                }
                return nearPos.size()>0;
            }

            bool CAStarFly::IsCanFly(int x, int y)
            {
                return true;
            }

            int32 CAStarFly::GetFValue(const CIntPoint &pos)
            {
                int xDis = abs(pos.x - m_endPoint.x)*100;
                int yDis = abs(pos.y - m_endPoint.y)*100;
                return m_curGValue +  (int32)sqrt(xDis*xDis + yDis*yDis);
            }



            bool CAStarFly::IsNotClosePos(int x, int y)
            {
                return m_setClosePos.find(GetPosIndex(x, y)) == m_setClosePos.end();
            }

            void CAStarFly::OnAddBestPoint(int x, int y)
            {
                m_curGValue += 100;
            }

            void CAStarFly::OnUnAddBestPoint(int x, int y)
            {
                m_curGValue -= 100;
            }


            #include "AStarFly.h"
            #include <memory>

            class CNewMap;

            class CMobAStarFly : public CAStarFly
            {
            public:
                static CMobAStarFly& GetInstance();
                bool FindPath(const std::shared_ptr<CNewMap>& pMap, const CIntPoint& from, const CIntPoint& to, CAStarFly::PosList& pathList, const CIntSize& roleSize = CIntSize(1, 1));
                void setpath(int x, int y);
                void show();

                virtual int GetMapWidth() override;
                virtual int GetMapHeight() override;
                virtual CIntSize GetRoleSize() override;
                virtual bool IsCanFly(int x, int y) override;
                void OnAddBestPoint(int x, int y) override;
                void OnUnAddBestPoint(int x, int y) override;
            private:
                weak_ptr<CNewMap> m_pNewMap;
                CIntSize m_roleSize;
            };



            #include "stdafx.h"
            #include "MobFlyAStar.h"
            #include "NewMap.h"

            CMobAStarFly& CMobAStarFly::GetInstance()
            {
                static CMobAStarFly s_Instance;
                return s_Instance;
            }

            bool CMobAStarFly::FindPath(const std::shared_ptr<CNewMap>& pMap, const CIntPoint& from, const CIntPoint& to, CAStarFly::PosList& pathList, const CIntSize& roleSize)
            {
                m_pNewMap = pMap;
                m_roleSize = roleSize;

                return CalcPath(pathList, from, to);
            }

            int CMobAStarFly::GetMapWidth()
            {
                if (m_pNewMap.lock() == nullptr)
                    return 0;

                return m_pNewMap.lock()->GetWidth();
            }

            int CMobAStarFly::GetMapHeight()
            {
                if (m_pNewMap.lock() == nullptr)
                    return 0;

                return m_pNewMap.lock()->GetHeight();
            }

            CIntSize CMobAStarFly::GetRoleSize()
            {
                return m_roleSize;
            }

            bool CMobAStarFly::IsCanFly(int x, int y)
            {
                for (int w = 0; w < m_roleSize.width; ++w)
                {
                    for (int h = 0; h < m_roleSize.height; ++h)
                    {
                        if (m_pNewMap.lock()->IsPosBlock(m_pNewMap.lock()->PosToWorldX(x + w), m_pNewMap.lock()->PosToWorldY(y - h)))    {
                            return false;
                        }
                    }
                }

                return true;
            }

            void CMobAStarFly::setpath(int x, int y)
            {
                
            }

            void CMobAStarFly::show()
            {

            }

            void CMobAStarFly::OnAddBestPoint(int x, int y)
            {

            }

            void CMobAStarFly::OnUnAddBestPoint(int x, int y)
            {

            }
            posted on 2017-08-17 14:43 C++技術中心 閱讀(1452) 評論(1)  編輯 收藏 引用 所屬分類: 游戲開發

            Feedback

            # re: A*算法實現 2020-08-26 14:42 放屁阿狗
            我也寫了個astar 演示程序
            http://wallizard.com/eric/astar/  回復  更多評論
              

            久久久久国产一区二区三区| 一级A毛片免费观看久久精品| 亚洲AV无码一区东京热久久| 久久久久久精品免费免费自慰| 亚洲国产成人久久综合碰| 中文字幕亚洲综合久久菠萝蜜| 日韩久久久久久中文人妻| 好久久免费视频高清| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 久久国产精品成人免费| 狠狠色综合久久久久尤物| 久久久久久精品免费看SSS| 国产V综合V亚洲欧美久久| 久久人人爽人人爽人人片AV麻豆 | 久久精品国产WWW456C0M| 久久综合久久综合亚洲| 久久99国产精品久久| 精品国产青草久久久久福利| 久久久青草久久久青草| 久久亚洲国产最新网站| 国产亚洲精久久久久久无码AV| 欧美亚洲日本久久精品| 精品99久久aaa一级毛片| 久久久精品国产免大香伊| 国产精品99久久久久久猫咪| 精品久久久久成人码免费动漫| 激情伊人五月天久久综合| 亚洲一级Av无码毛片久久精品| 久久66热人妻偷产精品9| 久久久久久久久66精品片| 久久国产视频网| 色成年激情久久综合| 久久精品人人槡人妻人人玩AV | 精品久久久久久无码免费| 久久亚洲日韩精品一区二区三区| 久久精品国产亚洲av瑜伽| 一本一道久久精品综合| 亚洲天堂久久精品| 99国产精品久久| 国内精品九九久久久精品| 久久99国产综合精品免费|