void SwarmGame::initialiseSwarm (EntityType type, int numEntities) { CCTextureCache* textureCache = CCTextureCache::sharedTextureCache(); CCSize size( float(m_map.width()), float(m_map.height()) ); EntityFactory& factory = EntityFactory::instance(); MobileEntity* entity = NULL, *mapEntity = NULL; MapNode* node = NULL; int x, y; int mapHeight = m_map.height() * c_blockSize; for ( int i = 0; i < numEntities; i++ ) { if ( (entity = static_cast<MobileEntity*>(factory.createEntity (type))) ) { // random position x = rand() % static_cast<int>(size.width); y = rand() % static_cast<int>(size.height); if ( (node = m_map.nodeAt(x, y)) ) { if ( node->isBlocking() ) { // if we chose a spot that already has an entity, try again... i--; delete entity; continue; } } // if we get here, we can place the new entity m_map.placeEntity<MobileEntity>(entity, x, y); // add it to the swarm CCTexture2D* entityTexture = NULL; if (entity->isHuman()) m_HumanSwarm.addEntity(entity); else { m_ZombieSwarm.addEntity(entity); m_HumanSwarm.addTarget(entity); } // create an entity sprite structure for the visualisation EntitySprite es; entityTexture = textureCache->textureForKey(xmlDataManager::instance().getEntitySprite (entity->type())); es.m_entity = dynamic_cast<MobileEntity*>(entity); es.m_sprite = SpriteEntity::spriteWithEntity(entity); es.m_sprite->setAnchorPoint( ccp(0.5, 0.5) ); es.m_sprite->setScaleX( c_blockSize/es.m_sprite->getContentSize().width ); es.m_sprite->setScaleY( c_blockSize/es.m_sprite->getContentSize().height ); es.m_sprite->setPosition( ccp(es.m_entity->x() * c_blockSize + c_blockSize/2, mapHeight - ((es.m_entity->y()+1) * c_blockSize + c_blockSize/2)) ); es.m_sprite->setMaxHealth( entity->hitPoints() ); m_mapLayer->addChild(es.m_sprite); m_entitySprites.push_back(es); } } }