Пример #1
0
CCSprite* CMGameMap::TileMapLayerPosToTileSprite( CCPoint TileMapLayerPos)
{
	do 
	{
		CCPoint TileMapPos = TileMapLayerPosToTileMapPos(TileMapLayerPos);

		//获得地图的各个层
		CCTMXLayer* pCloudLayer = layerNamed("cloud");
		CC_BREAK_IF(pCloudLayer==NULL);
		CCTMXLayer* pBlockLayer = layerNamed("block");
		CC_BREAK_IF(pBlockLayer==NULL);
		CCTMXLayer* pPipeLayer = layerNamed("pipe");
		CC_BREAK_IF(pPipeLayer==NULL);
		CCTMXLayer* pLandLayer = layerNamed("land");
		CC_BREAK_IF(pLandLayer==NULL);
		CCTMXLayer* pTrapLayer = layerNamed("trap");
		CC_BREAK_IF(pTrapLayer==NULL);
		CCTMXObjectGroup* pObjectLayer = objectGroupNamed("objects");
		CC_BREAK_IF(pObjectLayer==NULL);
		CCTMXLayer* pCoinLayer = layerNamed("coin");
		CC_BREAK_IF(pCoinLayer==NULL);
		CCTMXLayer* pFlagpoleLayer = layerNamed("flagpole");
		CC_BREAK_IF(pFlagpoleLayer==NULL);

		//若马里奥超过上边界
		if(TileMapPos.y<0)return NULL;

		CCSprite* pLandSprite = pLandLayer->tileAt(ccp(TileMapPos.x,TileMapPos.y));
		if (pLandSprite!=NULL)
		{
			return pLandSprite;
		}
		CCSprite* pBlockSprite = pBlockLayer->tileAt(ccp(TileMapPos.x,TileMapPos.y));
		if (pBlockSprite!=NULL)
		{
			//遍历砖块数组,如果砖块数组中未找到,则说明已被顶坏,返回空
			CCObject *pObj = NULL;
			CCARRAY_FOREACH(m_pArrayBlocks,pObj)
			{
				CMItemBlock* pItem = dynamic_cast<CMItemBlock*>(pObj);
				CC_BREAK_IF(pItem==NULL);
				CCPoint CurBlockWorldPos = (pBlockSprite->getPosition());
				CCPoint TempBlockWorldPos = (pItem->getPosition());

				//找到则返回砖块精灵
				if (abs(CurBlockWorldPos.x==TempBlockWorldPos.x) && 
					abs(CurBlockWorldPos.y==TempBlockWorldPos.y))
				{
					return pBlockSprite;
				}
			}
			return NULL;
		}
CCArray* LoadLevelEnemies::getEnemiesListFromLayer(CCTMXTiledMap *tilemap,
		Player *player) {

	CCTMXLayer* layer = tilemap->layerNamed("enemies");

	CCSize layersize = layer->getLayerSize();

	CCLOG("%s \n", "setEnemiesTilesInformation");
	CCArray *enemieArray = CCArray::create();
	CCDictionary* enemiesDictionary = PersistenceAux::getEnemyData();
	CCLOG("Enemy data loaded");
	for (int x = 0; x < layersize.width; x++) {
		for (int y = 0; y < layersize.height; y++) {
			unsigned int tmpgid = layer->tileGIDAt(ccp(x, y));
			if (tmpgid != 0) {
				CCSprite* tile = layer->tileAt(ccp(x, y));
				CCRect box = tile->boundingBox();
				CCPoint boxsize = ccp(box.size.width, box.size.height);
				CCPoint tilePosition = ccpAdd(tile->getPosition(),
						ccpMult(boxsize, 0.5));
				layer->removeTileAt(ccp(x, y));

				// create the enemy directly
				CCDictionary* tileProperties = tilemap->propertiesForGID(
						tmpgid);
				CCLOG("%s \n", "dictionary for enemies properties");
				CCString* enemyName = (CCString*) tileProperties->objectForKey(
						"name");
				CCLOG("Enemy name %s \n", enemyName->getCString());
				CCDictionary* enemyProperties =
						(CCDictionary *) enemiesDictionary->objectForKey(
								enemyName->getCString());
				CCLOG("Enemy prop. Dictionary size %d \n",
						enemyProperties->count());
				CCLOG("Enemy Position x %d \n", tilePosition.x);

				CCLOG("************** PLIST ****************** \n");
				CCLOG("SPEED %s",
						((CCString*) enemyProperties->objectForKey("speed"))->getCString());
				CCLOG("JUMPFORCE %s",
						((CCString*) enemyProperties->objectForKey("jump"))->getCString());
				CCLOG("SIGHT %s",
						((CCString*) enemyProperties->objectForKey("sight"))->getCString());
				CCLOG("DIFICULT %s",
						((CCString*) enemyProperties->objectForKey("dificult"))->getCString());
				CCLOG("DEFAULT IMAGE %s",
						((CCString*) enemyProperties->objectForKey(
								"defaultimage"))->getCString());
				CCLOG("************** PLIST END ****************** \n");

				MeeleEnemy* enemy = MeeleEnemy::create(
						(CCString*) enemyProperties->objectForKey(
								"defaultimage"),
						(CCString*) enemyProperties->objectForKey("dificult"),
						(CCString*) enemyProperties->objectForKey("jump"),
						(CCString*) enemyProperties->objectForKey("sight"),
						(CCString*) enemyProperties->objectForKey("speed"),
						tilePosition, enemyName,
						(CCString*) enemyProperties->objectForKey("damage")
				);
				CCLOG("%s \n", "Initializing enemies");
				enemy->initMeeleEnemy(player);
				CCLOG("%s \n", "Add enemies to enemy array");
				enemieArray->addObject(enemy);
			}
		}
	}
	return enemieArray;
}