• <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++技術中心 閱讀(1453) 評論(1)  編輯 收藏 引用 所屬分類: 游戲開發

            Feedback

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

            亚洲国产另类久久久精品小说| 久久国产高潮流白浆免费观看| 国产99久久久国产精免费| 精品免费tv久久久久久久| 国产69精品久久久久9999| 午夜视频久久久久一区| 久久99精品国产麻豆| 国产精品狼人久久久久影院| 偷窥少妇久久久久久久久| 久久精品国产99国产精品澳门| 亚洲国产婷婷香蕉久久久久久| 国产色综合久久无码有码| www亚洲欲色成人久久精品| 亚洲国产精品无码久久98| 亚洲狠狠久久综合一区77777| 国产精品久久久久久久久久影院| 色婷婷综合久久久久中文 | 色综合合久久天天综合绕视看 | 久久精品桃花综合| 久久精品嫩草影院| 五月丁香综合激情六月久久| 久久国产精品无码网站| 久久精品国产亚洲AV久| 国产99久久久久久免费看| 波多野结衣AV无码久久一区| 久久精品成人影院| 大美女久久久久久j久久| 精品一区二区久久久久久久网站| 久久天天躁夜夜躁狠狠躁2022| 嫩草影院久久99| 婷婷综合久久狠狠色99h| 久久久免费精品re6| 久久亚洲精品成人av无码网站| 国内精品久久久久影院老司| 欧美性大战久久久久久| 久久亚洲国产精品123区| 久久国产视屏| 奇米影视7777久久精品人人爽| 色88久久久久高潮综合影院| 久久久久亚洲av毛片大| 久久综合色区|