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;
}
Ejemplo n.º 2
0
void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent)
{
	CCNode* node = this->getChildByTag(TileMapNode);	
	//NSAssert([node isKindOfClass:[CCTMXTiledMap class]], @"not a CCTMXTiledMap");
	CCTMXTiledMap* tileMap = (CCTMXTiledMap*)node;
	
	// get the position in tile coordinates from the touch location
	CCPoint touchLocation = this->locationFromTouch((cocos2d::CCTouch *)pTouches->anyObject());	
	CCPoint tilePos = this->tilePosFromLocation(touchLocation, tileMap);	

	//// move tilemap so that touched tiles is at center of screen
	this->centerTileMapOnTileCoord(tilePos, tileMap);

	// Check if the touch was on water (eg. tiles with isWater property drawn in GameEventLayer)
	bool isTouchOnWater = false;	
	CCTMXLayer* eventLayer = tileMap->layerNamed("GameEventLayer");
	int tileGID = eventLayer->tileGIDAt(tilePos);	
	//
	if (tileGID != 0) {
		CCDictionary* properties = tileMap->propertiesForGID(tileGID);
		if (properties) {
			//CCLOG(@"NSDictionary 'properties' contains:\n%@", properties);	
			const CCString * isWaterProperty = properties->valueForKey("isWater");
			isTouchOnWater = isWaterProperty->boolValue();			
		}
	}
	
	// Check if the touch was within one of the rectangle objects
	CCTMXObjectGroup* objectLayer = tileMap->objectGroupNamed("ObjectLayer");
	//NSAssert([objectLayer isKindOfClass:[CCTMXObjectGroup class]], 
	//		 @"ObjectLayer not found or not a CCTMXObjectGroup");
	
	bool isTouchInRectangle = false;
	int numObjects = objectLayer->getObjects()->count();
	for (int i = 0; i < numObjects; i++){
		CCDictionary* properties = (CCDictionary*)objectLayer->getObjects()->objectAtIndex(i);
		CCRect rect = this->getRectFromObjectProperties(properties, tileMap);
		//	
		if (CCRect::CCRectContainsPoint(rect, touchLocation)) {
			isTouchInRectangle = true;
			break;
		}
	}
	
	// decide what to do depending on where the touch was ...
	if (isTouchOnWater) {
		//[[SimpleAudioEngine sharedEngine] playEffect:@"alien-sfx.caf"];
		CCLog("touchOnWater");
	}
	else if (isTouchInRectangle) {
		CCLog("touchObject");
		CCParticleSystem* system = CCParticleSystemQuad::particleWithFile("fx-explosion.plist");
		system->setAutoRemoveOnFinish(true);
		system->setPosition(touchLocation);
		this->addChild(system, 1);
	}
	else {

#if 0
		// get the winter layer and toggle its visibility
		CCTMXLayer* winterLayer = tileMap->layerNamed("WinterLayer");
		winterLayer->setVisible(!winterLayer->isVisible());
		
		// other options you might be interested in are:	

		// remove the touched tile		
		winterLayer->removeTileAt(tilePos);		
		// add a specific tile
		tileGID = winterLayer->tileGIDAt(CCPointMake(0, 19));
		winterLayer->setTileGID(tileGID, tilePos);
#endif

	}
}