【cocos2dx 3.3】TileMap的A*演算法實現,cocos2dxtilemap
總結:
- cocos裡面的Vector比C++的vector容納的類型少很多,因此用標準庫的vector
- 地圖是320*480,瓦片是32*32像素
- 首先添加點擊事件,擷取終點的瓦片座標,並記錄下來
- 設定起始座標,並將起始座標作為【當前點】
- 每次對【當前點】的四周探索,放入優先隊列openList
- 將優先隊列的第一個元素,即F值最小的那個點,作為新的【當前點】,並放入closeList
- 繼續調用探索方法,直到終點座標也被放入openList
代碼:
HelloWorldScene.h
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__#include "cocos2d.h"#include <queue>#include <vector>USING_NS_CC;//定義【當前點】類,包含權值F和座標點class pointValue{public:pointValue(Point tem, Point dst , int total){tempPoint = tem;FValue = (abs((int)dst.x-(int)tem.x +(int)dst.y-(int)tem.y) + total) ;}//當前點座標,和F值Point tempPoint;int FValue;};//設定優先隊列比較函數,F值最小的【當前點】在優先隊列的Topstruct Compare{bool operator ()(pointValue a,pointValue b){return a.FValue > b.FValue;}};class HelloWorld : public cocos2d::Layer{public: static cocos2d::Scene* createScene();virtual bool init(); CREATE_FUNC(HelloWorld);void menuCloseCallback(cocos2d::Ref* pSender);//二維數組,防止點重複探索,初始化為0,探索過的設為1int arr[15][10];//TileMap的層TMXLayer *layer;//待檢測座標列表,優先隊列作為OpenListstd::priority_queue<pointValue, std::vector<pointValue>,Compare> p_quene;//關閉列表std::vector<pointValue> closeList;std::vector<pointValue>::iterator closeListStart; //起點瓦片座標Point beginPoint;//終點瓦片座標Point destination;//估算一個從起始點到終點的格子數目,每移動一次,則減30int total;//將最終的路徑所有點,存放在closeList中bool aStart();};#endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
#include "HelloWorldScene.h"USING_NS_CC;Scene* HelloWorld::createScene(){ auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene;}bool HelloWorld::init(){ if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin();//載入TileMap地圖auto map = TMXTiledMap::create("map.tmx");addChild(map);//擷取TileMap的層layer = map->getLayer("layer1");//確定起點瓦片座標和初始化二維數組beginPoint = Point(0,0);arr[0][0] = 1;//點擊事件監聽,記錄終點瓦片座標auto listener = EventListenerTouchOneByOne::create();listener->onTouchBegan = [this](Touch *t, Event *e){//設定終點瓦片座標int dx = (int)(t->getLocation().x)/32;int dy = 14 - (int)(t->getLocation().y)/32;this->destination = Point(dx,dy);CCLOG("%d,%d",dx,dy);//自己設定起點到終點的最短格子數估值,即G值total = 1000;//尋找最短路徑,座標存放在closeList裡面aStart();return true;};Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this); return true;}bool HelloWorld::aStart(){//每調用一次aStart相當於移動一步,每移動一步total減30,作為G值total = total - 30;if ((int)beginPoint.x == (int)destination.x && (int)beginPoint.y == (int)destination.y){return true;}//探索這個點的4周(上,右,下,左),若存在,則放進優先隊列openList中//判斷上方if ((int)beginPoint.x >= 0 && ((int)beginPoint.y-1) >= 0 && (int)beginPoint.x <= 9 && ((int)beginPoint.y-1) <= 14 ){Sprite *testUp = layer->getTileAt(Vec2((int)beginPoint.x,(int)beginPoint.y-1));if (testUp != NULL && (arr[(int)beginPoint.x][(int)beginPoint.y-1] != 1) ){CCLOG("up");//初始化探索的點,將它放進優先隊列openList中,並設定二維數組中的值為1,防止重複探索auto temp = new pointValue(Point(beginPoint.x,beginPoint.y-1),destination,total);p_quene.push(*temp);arr[(int)beginPoint.x][(int)beginPoint.y-1] = 1;}}//判斷左方if (((int)beginPoint.x-1) >= 0 && (int)beginPoint.y >= 0 && ((int)beginPoint.x-1) <= 9 && (int)beginPoint.y <= 14) {Sprite *testLeft = layer->getTileAt(Vec2((int)beginPoint.x-1,(int)beginPoint.y));if (testLeft != NULL && (arr[(int)beginPoint.x-1][(int)beginPoint.y] != 1) ){CCLOG("left");//初始化探索的點,將它放進優先隊列openList中,並設定二維數組中的值為1,防止重複探索auto temp2 = new pointValue(Point(beginPoint.x-1,beginPoint.y),destination,total);p_quene.push(*temp2);arr[(int)beginPoint.x-1][(int)beginPoint.y] = 1;}}//判斷下方if ((int)beginPoint.x >= 0 && ((int)beginPoint.y+1) >= 0 && (int)beginPoint.x <= 9 && ((int)beginPoint.y+1) <= 14) {Sprite *testDown = layer->getTileAt(Vec2((int)beginPoint.x,(int)beginPoint.y+1));if (testDown != NULL && (arr[(int)beginPoint.x][(int)beginPoint.y+1] != 1) ){CCLOG("down");//初始化探索的點,將它放進優先隊列openList中,並設定二維數組中的值為1,防止重複探索auto temp3 = new pointValue(Point(beginPoint.x,beginPoint.y+1),destination,total);p_quene.push(*temp3);arr[(int)beginPoint.x][(int)beginPoint.y+1] = 1;}}//判斷右方if (((int)beginPoint.x+1) >= 0 && (int)beginPoint.y >= 0 && ((int)beginPoint.x+1) <= 9 && (int)beginPoint.y <= 14){Sprite *testRight = layer->getTileAt(Vec2((int)beginPoint.x+1,(int)beginPoint.y));if (testRight != NULL && (arr[(int)beginPoint.x+1][(int)beginPoint.y] != 1)){CCLOG("right");//初始化探索的點,將它放進優先隊列openList中,並設定二維數組中的值為1,防止重複探索auto temp4 = new pointValue(Point(beginPoint.x+1,beginPoint.y),destination,total);p_quene.push(*temp4);arr[(int)beginPoint.x+1][(int)beginPoint.y] = 1;}}//重設當前位置beginPoint.x = p_quene.top().tempPoint.x;beginPoint.y = p_quene.top().tempPoint.y;CCLOG("%d,%d",(int)beginPoint.x,(int)beginPoint.y);//把F值最小的點,放進closeList中,並從優先隊列中pop掉closeList.push_back(p_quene.top());p_quene.pop(); //在路徑中添加精靈auto star = Sprite::create("star.png");star->setPosition(Point(beginPoint.x*32+10,480-beginPoint.y*32-10));addChild(star);//繼續遞迴 aStart();}void HelloWorld::menuCloseCallback(Ref* pSender){#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return;#endif Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif}
效果: