Exemplo n.º 1
0
void GameLayer::importGroundData(cocos2d::TMXTiledMap* data)
{
	TMXObjectGroup *objects = m_pTiledMap->getObjectGroup("Grounds");
	const ValueVector _objects = objects->getObjects();
	if (!_objects.empty())
	{
		for (const auto& v : _objects)
		{
			const ValueMap& dict = v.asValueMap();
			if (dict.find("name") != dict.end())
			{
				if (dict.at("name").asString() == "Ground" || dict.at("name").asString() == "Box")
				{
					Size boxSize(dict.at("width").asFloat(), dict.at("height").asFloat());
					auto ground = Ground::create();
					if(dict.find("rotation") != dict.end())
						ground->initPhysics(boxSize, Point(dict.at("x").asFloat(), dict.at("y").asFloat()), dict.at("rotation").asInt());
					else
						ground->initPhysics(boxSize, Point(dict.at("x").asFloat(), dict.at("y").asFloat()), 0);
					this->addChild(ground);
				}
			}
		}
	}
}
Exemplo n.º 2
0
bool MultiPriceMatch::readFile(const char* fileName,const char* levelID) {

	bool bRet = false;	
	string id = levelID;
	do{
		CC_BREAK_IF(!FileUtils::getInstance()->isFileExist(fileName));

		ValueMap root = FileUtils::getInstance()->getValueMapFromFile(fileName);
		CC_BREAK_IF(root.empty());
		
		ValueVector pl = root["PRODUCT_LIST"].asValueVector();
		CC_BREAK_IF(pl.empty());
		int nSize = pl.size();
		std::vector<int> levelNum_all;
		std::vector<int> levelNum;

		levelNum_all.clear();
		levelNum.clear();

		for(int i = 0; i < nSize; i++){
			levelNum_all.push_back(i);
		}

		std::random_device rd1;
		std::mt19937 g1(rd1());
 		std::shuffle(std::begin(levelNum_all), std::end(levelNum_all), g1);

		for(int n=0; n< _TOTAL_PRODUCTS; ++n){
			levelNum.push_back(levelNum_all[n]);// generate numbers
		}

		for (int i = 0; i < levelNum.size(); i++){
			ValueMap pd = pl[levelNum[i]].asValueMap();
			PRODUCT_INFO p;
			p.id = pd[PD_ID].asString().c_str();
			p.name = pd[PD_NAME].asString().c_str();
			p.image = pd[PD_IMAGE].asString().c_str();
			p.price = pd[PD_PRICE].asString().c_str();
			p.description = pd[PD_DESCRIPTION].asString().c_str();
			p.sku = pd[PD_SKU].asString().c_str();
			_product_list.push_back(p);
		}

	bRet = true;
	}while(0);

	return bRet;
}
    void startElement(void *ctx, const char *name, const char **atts)
    {
        CC_UNUSED_PARAM(ctx);
        CC_UNUSED_PARAM(atts);
        const std::string sName(name);
        if( sName == "dict" )
        {
            if(_resultType == SAX_RESULT_DICT && _rootDict.empty())
            {
                _curDict = &_rootDict;
            }

            _state = SAX_DICT;

            SAXState preState = SAX_NONE;
            if (! _stateStack.empty())
            {
                preState = _stateStack.top();
            }

            if (SAX_ARRAY == preState)
            {
                // add a new dictionary into the array
                _curArray->push_back(Value(ValueMap()));
                _curDict = &(_curArray->rbegin())->asValueMap();
            }
            else if (SAX_DICT == preState)
            {
                // add a new dictionary into the pre dictionary
                CCASSERT(! _dictStack.empty(), "The state is wrong!");
                ValueMap* preDict = _dictStack.top();
                (*preDict)[_curKey] = Value(ValueMap());
                _curDict = &(*preDict)[_curKey].asValueMap();
            }

            // record the dict state
            _stateStack.push(_state);
            _dictStack.push(_curDict);
        }
        else if(sName == "key")
        {
            _state = SAX_KEY;
        }
        else if(sName == "integer")
        {
            _state = SAX_INT;
        }
        else if(sName == "real")
        {
            _state = SAX_REAL;
        }
        else if(sName == "string")
        {
            _state = SAX_STRING;
        }
        else if (sName == "array")
        {
            _state = SAX_ARRAY;

            if (_resultType == SAX_RESULT_ARRAY && _rootArray.empty())
            {
                _curArray = &_rootArray;
            }
            SAXState preState = SAX_NONE;
            if (! _stateStack.empty())
            {
                preState = _stateStack.top();
            }

            if (preState == SAX_DICT)
            {
                (*_curDict)[_curKey] = Value(ValueVector());
                _curArray = &(*_curDict)[_curKey].asValueVector();
            }
            else if (preState == SAX_ARRAY)
            {
                CCASSERT(! _arrayStack.empty(), "The state is wrong!");
                ValueVector* preArray = _arrayStack.top();
                preArray->push_back(Value(ValueVector()));
                _curArray = &(_curArray->rbegin())->asValueVector();
            }
            // record the array state
            _stateStack.push(_state);
            _arrayStack.push(_curArray);
        }
        else
        {
            _state = SAX_NONE;
        }
    }
bool GameMap::loadMap(const int& level)
{
	string fileName = StringUtils::format("map/map_%03d.tmx", level);
	TMXTiledMap* map = TMXTiledMap::create(fileName);
	m_map = map;
	float scale = 1 / Director::getInstance()->getContentScaleFactor();

	//GridPos
	TMXObjectGroup* group = map->getObjectGroup("TowerPos");
	ValueVector vec = group->getObjects();
	m_gridPos.clear();
	m_gridPos.resize(vec.size());
	if (!vec.empty())
	{
		int i = 0;
		for (const auto& v : vec)
		{
			const ValueMap& dict = v.asValueMap();

			m_gridPos[i] = new GridPos( 
				i,
				Rect(dict.at("x").asFloat(),
				dict.at("y").asFloat(),
				dict.at("width").asFloat(),
				dict.at("height").asFloat()));
			i++;
		}
	}
	//计算临近的塔的ID
	float h = group->getProperty("TowerPosHeight").asFloat() *scale;
	float w = group->getProperty("TowerPosWidth").asFloat() *scale;
	float dis = (h + 2)*(h + 2) + (w + 2)*(w + 2);
	vector<int> GridPosID;
	for (auto t1 : m_gridPos)
	{
		GridPosID.clear();
		Point pos = t1->getPos();
		for (auto t2 : m_gridPos)
		{
			if (t1 != t2 && pos.distanceSquared(t2->getPos())<=dis)
			{
				GridPosID.push_back(t2->ID);
			}
		}

		int around[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
		for (auto tid : GridPosID)
		{
			Rect rect = m_gridPos[tid]->getRect();
			if		(rect.containsPoint( Point(pos.x	, pos.y + h	)))around[North] = tid;
			else if (rect.containsPoint( Point(pos.x - w, pos.y + h	)))around[NorthWest] = tid;
			else if (rect.containsPoint( Point(pos.x - w, pos.y		)))around[West] = tid;
			else if (rect.containsPoint( Point(pos.x - w, pos.y - h	)))around[SouthWest] = tid;
			else if (rect.containsPoint( Point(pos.x	, pos.y - h	)))around[South] = tid;
			else if (rect.containsPoint( Point(pos.x + w, pos.y - h	)))around[SouthEast] = tid;
			else if (rect.containsPoint( Point(pos.x + w, pos.y		)))around[East] = tid;
			else if (rect.containsPoint( Point(pos.x + w, pos.y + h	)))around[NorthEast] = tid;
		}
		t1->setAroundGridPosID(around);
	}


	//MonsterPath
	group = map->getObjectGroup("Path");
	vec = group->getObjects();
	MonsterPath.clear();
	if (!vec.empty())
	{
		vector<Point> posvec;
		for (const auto& var : vec)
		{
			posvec.clear();
			const ValueMap& dict = var.asValueMap();
			const ValueVector& vec2 = dict.at("polylinePoints").asValueVector();
			Point pos = Point(dict.at("x").asFloat(), dict.at("y").asFloat());

			for (const auto& v : vec2)
			{
				const ValueMap& dict = v.asValueMap();
				posvec.push_back(Point(pos.x + dict.at("x").asFloat()*scale, pos.y - dict.at("y").asFloat()*scale));
				//posvec.push_back(Point(pos.x + dict.at("x").asFloat(), pos.y - dict.at("y").asFloat()));
			}
			MonsterPath.push_back(MapPath(dict.at("LoopTo").asInt(), posvec));
		}
	}

	//WaveData
	int waveCount= map->getProperty("WaveCount").asInt();
	std::stringstream ss;
	string propertyName;
	WaveList.clear();
	for (int i = 1; i <= waveCount; i++)
	{
		propertyName = StringUtils::format("Wave%03d", i);
		group = map->getObjectGroup(propertyName);
		CCASSERT(group != nullptr, string("propertyName :" + propertyName +" NOT found").c_str());

		Wave wave;
		wave.WaveTime = group->getProperty("waveTime").asInt();
		
		int momsterCount = group->getProperty("momsterCount").asInt();
		for (int j = 1; j <=momsterCount; j++)
		{
			propertyName = StringUtils::format("m%03d", j);
			ss.clear();
			ss << group->getProperty(propertyName).asString();

			WaveData data;
			ss >> data.MonsterID;
			ss >> data.PathID;
			ss >> data.NextDalay;

			wave.wavedata.push_back(data);
		}
		WaveList.push_back(wave);
	}

	return true;
}
Exemplo n.º 5
0
void Face::initFaceForKey(std::string key, __Array* faceStates) {
    // load file plist
    auto frameCache = SpriteFrameCache::getInstance();
    char plistFile[200];
    sprintf(plistFile, "%s.plist", key.data());
    frameCache->addSpriteFramesWithFile(plistFile);
    
    // load animation
    auto animationCache = AnimationCache::getInstance();
    char animationFile[200];
    std::string firstSpriteFrameName = "";
    sprintf(animationFile, "ani-%s.plist", key.data());
    std::string path = FileUtils::getInstance()->fullPathForFilename(animationFile);
    ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(path);
    ValueVector animations = dict.at("Animations").asValueVector();
    __Array* animationNames = __Array::create();
    
    for (int i = 0; i < animations.size(); i++) {
        ValueMap animationDict = animations.at(i).asValueMap();
        ValueVector frames = animationDict.at("Frames").asValueVector();
        ValueMap properties = animationDict.at("Properties").asValueMap();
        std::string name = properties.at("Animation").asString();
        animationNames->addObject(__String::create(name.data()));
        float delayPerUnit = 1 / properties.at("FPS").asFloat();
        Animation* animation = NULL;
        
        if (frames.empty()) {
            continue;
        }
        
        ssize_t frameSize = frames.size();
        Vector<AnimationFrame*> animationFrames(frameSize);
        
        for (auto& frame : frames) {
            ValueMap frameValueMap = frame.asValueMap();
            std::string frameString = frameValueMap.at("FrameName").asString();
            if (firstSpriteFrameName == "") {
                firstSpriteFrameName = frameString;
            }
            float duration = frameValueMap.at("Duration").asFloat();
            SpriteFrame* spriteFrame = frameCache->getSpriteFrameByName(frameString);
            
            if (!spriteFrame) {
                continue;
            }
            
            AnimationFrame* animFrame = AnimationFrame::create(spriteFrame, duration, ValueMap());
            animationFrames.pushBack(animFrame);
        }
        
        if (frames.empty()) {
            assert(NULL);
            continue;
        } else if (frames.size() != frameSize) {
            assert(NULL);
        }
        
        animation = Animation::create(animationFrames, delayPerUnit);
        animationCache->addAnimation(animation, name);
    }
    
    this->initWithSpriteFrameName(firstSpriteFrameName);
    this->getTexture()->setAliasTexParameters();
    
    for (int i = 0; i < animationNames->count(); i++) {
        __String* animationName = (__String*) animationNames->getObjectAtIndex(i);
        std::string animationNameStr = animationName->getCString();
        for (int j = 0; j < faceStates->count(); j++) {
            FaceState* state = (FaceState*) faceStates->getObjectAtIndex(j);
            std::string stateString = state->getStateName();
            if (animationNameStr.find(stateString) != std::string::npos) {
                auto animation = animationCache->getAnimation(animationNameStr);
                auto animate = Animate::create(animation);
                if (state->isLoop()) {
                    Action* action = RepeatForever::create(animate);
                    this->registerState(stateString, action);
                } else {
                    Action* action = (Action*) animate;
                    this->registerState(stateString, action);
                }
                break;
            }
        }
    }
}