• <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∨大片久久| 久久青青草原精品国产软件| 三级三级久久三级久久 | 污污内射久久一区二区欧美日韩| 91精品国产91久久久久久青草| 久久99久久无码毛片一区二区 | 国产一区二区精品久久| 青青青青久久精品国产h| 久久婷婷五月综合成人D啪| 亚洲精品国产美女久久久| 国产精品激情综合久久| 麻豆一区二区99久久久久| 欧美麻豆久久久久久中文| 久久久噜噜噜久久熟女AA片| 久久亚洲国产精品123区| 久久精品国产亚洲av麻豆蜜芽| 久久综合中文字幕| 久久久久久亚洲AV无码专区| 久久91这里精品国产2020| 999久久久无码国产精品| 伊人久久无码中文字幕| 麻豆久久| 日韩中文久久| 色综合合久久天天给综看| 日本免费一区二区久久人人澡| 久久久噜噜噜www成人网| 免费久久人人爽人人爽av| 伊人色综合久久天天人守人婷| 国产午夜福利精品久久| 日本精品久久久久中文字幕8| 久久久婷婷五月亚洲97号色| 少妇久久久久久被弄高潮| 国内精品九九久久精品| 丁香色欲久久久久久综合网| 久久久久国产一区二区| 久久夜色撩人精品国产小说| 久久av免费天堂小草播放| 国产精品美女久久久久av爽| 久久久久久毛片免费看|