MapLayer.h实现
#include <cocos2d.h>
class MapLayer : public cocos2d::CCLayer
{
public:
~MapLayer();
bool createMapInit(const char * mapFile, const char * bgFile);
static MapLayer * currentMap();
static MapLayer * _instance;
//存储tileMap地图,并可获取
CC_SYNTHESIZE_READONLY(cocos2d::CCTMXTiledMap *, map, TMXTiledMap);
//加载怪物移动点
std::vector<cocos2d::CCPoint> loadMonsterPoints();
//加载炮塔安置点
std::vector<cocos2d::CCPoint> loadTowerPoints();
//获取类型道具安置点
std::vector<cocos2d::CCPoint> loadPropPoints(int type);
private:
cocos2d::CCTMXObjectGroup * objectsLayer;
std::string mapFile;
std::string bgFile;
//加载地图
void loadMap(const char * fileName);
//加载map背景图
void loadBackGround(const char * bgFile);
};
MapLayer.cpp
#include "MapLayer.h"
USING_NS_CC;
MapLayer * MapLayer::_instance = NULL;
MapLayer::~MapLayer()
{
CC_SAFE_RELEASE(map);
CC_SAFE_RELEASE(objectsLayer);
CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrameByName(mapFile.c_str());
CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFrameByName(bgFile.c_str());
}
MapLayer * MapLayer::currentMap()
{
if (!_instance)
{
_instance = new MapLayer();
}
return _instance;
}
void MapLayer::loadBackGround(const char * bgFile)
{
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(bgFile);
auto path = CCSprite::createWithSpriteFrameName("Path.png");
this->addChild(path);
path->setPosition(CCPoint(960 / 2.0, 640 / 2.0));
}
bool MapLayer::createMapInit(const char * mapFile, const char * bgFile)
{
if (!CCLayer::init())
{
return false;
}
this->mapFile = mapFile;
this->bgFile = bgFile;
this->loadMap(mapFile);
this->loadBackGround(bgFile);
return true;
}
//加载地图
void MapLayer::loadMap(const char * fileName)
{
map = CCTMXTiledMap::create(fileName);
this->map->retain();
this->addChild(map);
objectsLayer = map->objectGroupNamed("PATH");
this->objectsLayer->retain();
loadMonsterPoints();
}
//获取类型道具安置点
std::vector<cocos2d::CCPoint> MapLayer::loadPropPoints(int type)
{
std::string str = "%dOb%d";
int count = 1;
std::vector<cocos2d::CCPoint> propPoints;
auto objPoint = objectsLayer->objectNamed(CCString::createWithFormat(str.c_str(), type, count)->getCString());
while (objPoint)
{
float x = objPoint->valueForKey("x")->floatValue();
float y = objPoint->valueForKey("y")->floatValue();
float width = objPoint->valueForKey("width")->floatValue();
float height = objPoint->valueForKey("height")->floatValue();
propPoints.push_back(CCPoint(x + width / 2.0f, y + height / 2.0f));
count++;
objPoint = objectsLayer->objectNamed(CCString::createWithFormat(str.c_str(), type, count)->getCString());
}
return propPoints;
}
//加载炮塔安置点
std::vector<cocos2d::CCPoint> MapLayer::loadTowerPoints()
{
std::string str = "Obj%d";
int count = 1;
std::vector<cocos2d::CCPoint> towerPoints;
auto objPoint = objectsLayer->objectNamed(CCString::createWithFormat(str.c_str(), count)->getCString());
while (objPoint)
{
//取出来瓦片的每一个openGL坐标,这里y+80,位置在左上角,转换成map的坐标,否则差一个瓦片高度
float x = objPoint->valueForKey("x")->floatValue();
float y = objPoint->valueForKey("y")->floatValue() + 80;
float width = objPoint->valueForKey("width")->floatValue();
float height = objPoint->valueForKey("height")->floatValue();
for (int row = 0; row < height / 80; row++)
{
for (int col = 0; col < width / 80; col++)
{
towerPoints.push_back(CCPoint(x + col * 80, y + row * 80));
}
}
count++;
objPoint = objectsLayer->objectNamed(CCString::createWithFormat(str.c_str(), count)->getCString());
}
return towerPoints;
}
//加载怪物移动点
std::vector<cocos2d::CCPoint> MapLayer::loadMonsterPoints()
{
std::string str = "PT%d";
int count = 1;
std::vector<cocos2d::CCPoint> monsterPoints;
auto objPoint = objectsLayer->objectNamed(CCString::createWithFormat(str.c_str(), count)->getCString());
while (objPoint)
{
float x = objPoint->valueForKey("x")->floatValue();
float y = objPoint->valueForKey("y")->floatValue();
monsterPoints.push_back(CCPoint(x, y));
count++;
objPoint = objectsLayer->objectNamed(CCString::createWithFormat(str.c_str(), count)->getCString());
}
return monsterPoints;
}