您的位置:首页 > 移动开发 > Cocos引擎

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: