cocos2d实现2D地图A*广度路径算法
2017-08-24 17:17
288 查看
.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
enum PatchFront
{
Uper = 1,
Down = 2,
Left = 3,
Right = 4,
};
struct SinglePatch;
static SinglePatch* patchvector[32][32];
struct SinglePatch
{
public:
static SinglePatch* CreatePatch(int row, int field)
{
SinglePatch* TempPatch = new SinglePatch(row, field);
return TempPatch;
}
Sprite* GetVision() const { return pSprite; }
SinglePatch* GetPatchNear(PatchFront f)
{
SinglePatch* returnpatch = nullptr;
switch (f)
{
case Uper:
if (Row + 1 >= 32)
return nullptr;
returnpatch = patchvector[Row + 1][Field];
break;
case Down:
if (Row - 1 < 0)
return nullptr;
returnpatch = patchvector[Row - 1][Field];
break;
case Left:
if (Field - 1 < 0)
return nullptr;
returnpatch = patchvector[Row][Field - 1];
break;
case Right:
if (Field + 1 >= 32)
return nullptr;
returnpatch = patchvector[Row][Field + 1];
break;
}
if (returnpatch->IsMarked)
return nullptr;
return returnpatch;
}
void Marked()
{
pSprite->setTexture("Finded.png");
}
void SetStep(int step)
{
Step = step;
char msg[20];
snprintf(msg, 20, "%d", step);
pText->setString(msg);
pSprite->setTexture("Marked.png");
}
void _draw()
{
pSprite->setTexture("WayPoint.png");
}
void SetBefore(SinglePatch* before) { BeforeStep = before; }
void DrawWayPoint()
{
_draw();
SinglePatch* Before = BeforeStep;
while (Before)
{
Before->_draw();
Before = Before->BeforeStep;
}
}
int Step;
bool IsMarked;
private:
SinglePatch(int row, int field) : Row(row), Field(field), Step(0), IsMarked(false), BeforeStep(nullptr)
{
pSprite = Sprite::create("SinglePatch.png");
pSprite->setAnchorPoint(Vec2(0, 0));
pSprite->setPosition(row * 30, field * 30);
char msg[255];
snprintf(msg, 255, "%d|%d", row, field);
pText = LabelTTF::create();
pText->setString(msg);
pText->setHorizontalAlignment(TextHAlignment::CENTER);
pText->setVerticalAlignment(TextVAlignment::CENTER);
pText->setPosition(pSprite->getContentSize() / 2);
pText->setColor(ccc3(0, 0, 0));
pText->setFontSize(18);
pSprite->addChild(pText);
}
~SinglePatch();
int Row;
int Field;
Sprite* pSprite;
LabelTTF* pText;
SinglePatch* BeforeStep;
};
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void FindPatch(SinglePatch* _patch, int Step);
void Begin();
private:
SinglePatch* StartPoint;
SinglePatch* EndPoint;
bool Finded;
std::list<SinglePatch*> ArrayBefore;
std::list<SinglePatch*> ArrayAfter;
};
#endif
.cpp
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
layer->setTag(159);
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 32; j++)
{
SinglePatch* patch = SinglePatch::CreatePatch(i, j);
addChild(patch->GetVision());
patchvector[i][j] = patch;
}
}
EndPoint = patchvector[rand() % 31][rand() % 31];
Finded = false;
StartPoint = patchvector[15][15];
StartPoint->SetStep(0);
StartPoint->IsMarked = true;
ArrayBefore.push_back(StartPoint);
std::thread th(&HelloWorld::Begin, this);
th.detach();
return true;
}
void HelloWorld::Begin()
{
//_begin:
while (ArrayBefore.size())
{
if (Finded)
break;
SinglePatch* TempSreach = *ArrayBefore.begin();
ArrayBefore.pop_front();
FindPatch(TempSreach, TempSreach->Step);
}
//goto _begin;
}
void HelloWorld::FindPatch(SinglePatch* _StartPatch, int Step)
{
for (int i = Uper; i != Right + 1; i++)
{
SinglePatch* tempPatch = _StartPatch->GetPatchNear((PatchFront)i);
if (tempPatch == EndPoint)
{
tempPatch->Marked();
_StartPatch->DrawWayPoint();
Finded = true;
return;
}
if (tempPatch)
{
tempPatch->SetBefore(_StartPatch);
tempPatch->SetStep(Step + 1);
tempPatch->IsMarked = true;
ArrayBefore.push_back(tempPatch);
Sleep(100);
}
}
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
enum PatchFront
{
Uper = 1,
Down = 2,
Left = 3,
Right = 4,
};
struct SinglePatch;
static SinglePatch* patchvector[32][32];
struct SinglePatch
{
public:
static SinglePatch* CreatePatch(int row, int field)
{
SinglePatch* TempPatch = new SinglePatch(row, field);
return TempPatch;
}
Sprite* GetVision() const { return pSprite; }
SinglePatch* GetPatchNear(PatchFront f)
{
SinglePatch* returnpatch = nullptr;
switch (f)
{
case Uper:
if (Row + 1 >= 32)
return nullptr;
returnpatch = patchvector[Row + 1][Field];
break;
case Down:
if (Row - 1 < 0)
return nullptr;
returnpatch = patchvector[Row - 1][Field];
break;
case Left:
if (Field - 1 < 0)
return nullptr;
returnpatch = patchvector[Row][Field - 1];
break;
case Right:
if (Field + 1 >= 32)
return nullptr;
returnpatch = patchvector[Row][Field + 1];
break;
}
if (returnpatch->IsMarked)
return nullptr;
return returnpatch;
}
void Marked()
{
pSprite->setTexture("Finded.png");
}
void SetStep(int step)
{
Step = step;
char msg[20];
snprintf(msg, 20, "%d", step);
pText->setString(msg);
pSprite->setTexture("Marked.png");
}
void _draw()
{
pSprite->setTexture("WayPoint.png");
}
void SetBefore(SinglePatch* before) { BeforeStep = before; }
void DrawWayPoint()
{
_draw();
SinglePatch* Before = BeforeStep;
while (Before)
{
Before->_draw();
Before = Before->BeforeStep;
}
}
int Step;
bool IsMarked;
private:
SinglePatch(int row, int field) : Row(row), Field(field), Step(0), IsMarked(false), BeforeStep(nullptr)
{
pSprite = Sprite::create("SinglePatch.png");
pSprite->setAnchorPoint(Vec2(0, 0));
pSprite->setPosition(row * 30, field * 30);
char msg[255];
snprintf(msg, 255, "%d|%d", row, field);
pText = LabelTTF::create();
pText->setString(msg);
pText->setHorizontalAlignment(TextHAlignment::CENTER);
pText->setVerticalAlignment(TextVAlignment::CENTER);
pText->setPosition(pSprite->getContentSize() / 2);
pText->setColor(ccc3(0, 0, 0));
pText->setFontSize(18);
pSprite->addChild(pText);
}
~SinglePatch();
int Row;
int Field;
Sprite* pSprite;
LabelTTF* pText;
SinglePatch* BeforeStep;
};
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void FindPatch(SinglePatch* _patch, int Step);
void Begin();
private:
SinglePatch* StartPoint;
SinglePatch* EndPoint;
bool Finded;
std::list<SinglePatch*> ArrayBefore;
std::list<SinglePatch*> ArrayAfter;
};
#endif
.cpp
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
layer->setTag(159);
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
for (int i = 0; i < 32; i++)
{
for (int j = 0; j < 32; j++)
{
SinglePatch* patch = SinglePatch::CreatePatch(i, j);
addChild(patch->GetVision());
patchvector[i][j] = patch;
}
}
EndPoint = patchvector[rand() % 31][rand() % 31];
Finded = false;
StartPoint = patchvector[15][15];
StartPoint->SetStep(0);
StartPoint->IsMarked = true;
ArrayBefore.push_back(StartPoint);
std::thread th(&HelloWorld::Begin, this);
th.detach();
return true;
}
void HelloWorld::Begin()
{
//_begin:
while (ArrayBefore.size())
{
if (Finded)
break;
SinglePatch* TempSreach = *ArrayBefore.begin();
ArrayBefore.pop_front();
FindPatch(TempSreach, TempSreach->Step);
}
//goto _begin;
}
void HelloWorld::FindPatch(SinglePatch* _StartPatch, int Step)
{
for (int i = Uper; i != Right + 1; i++)
{
SinglePatch* tempPatch = _StartPatch->GetPatchNear((PatchFront)i);
if (tempPatch == EndPoint)
{
tempPatch->Marked();
_StartPatch->DrawWayPoint();
Finded = true;
return;
}
if (tempPatch)
{
tempPatch->SetBefore(_StartPatch);
tempPatch->SetStep(Step + 1);
tempPatch->IsMarked = true;
ArrayBefore.push_back(tempPatch);
Sleep(100);
}
}
}
相关文章推荐
- Cocos2d-x《雷电大战》(6) 智能敌机AI来袭--飞行路径算法设计与实现(下)
- Cocos2d-x《雷电大战》(6) 智能敌机AI来袭--飞行路径算法设计与实现(上)
- 算法基础(六):广度优先探索迷宫路径(队列实现!)—超详细版!
- Cocos2d-x 地图行走的实现3:A* 算法
- (UWP)通过编写算法实现在地图中的渐变路径
- Cocos2d-x《雷电大战》(6) 智能敌机AI来袭–飞行路径算法设计与实现(上)
- 广度优先搜索迷宫路径算法的实现
- cocos2d-x 2D砖块地图实现技术之一
- Cocos2d-x《雷电大战》(6) 智能敌机AI来袭--飞行路径算法设计与实现(上)
- Cocos2d-x 地图行走的实现3:A*算法
- cocos2d-x 2D砖块地图实现技术之二
- 【Cocos2d-x】地图滚动算法的实现
- cocos2d-x 2D砖块地图实现技术之三
- cocos2d-x 2D砖块地图实现技术之四
- Cocos2d-x 地图行走的实现3:A*算法
- (UWP)通过编写算法实现在地图中的渐变路径
- 练练看游戏JavaScript搜索路径的核心算法实现
- PHP实现的蚂蚁爬杆路径算法代码
- 带权图的最短路径算法(Dijkstra)实现
- Java实现利用广度优先遍历(BFS)计算最短路径的方法