Ejemplo n.º 1
0
   void PlatformGenerator::addObjectsToPlatform(Platform& p, const Engine& e)
   {
      // Some constants
      const int MaxYounglingClusters = 4;
      const int MinYounglingClusters = 2;
      const int MaxYounglingsInCluster = 3;
      const int MinYounglingsInCluster = 3;

      if(p.x() > m_minBlockLength * 16) // don't add objects to the very first platform
      {
         // Add younglings.
         // Younglings come in groups of 1, 2 or 3.
         // There can be up to 4 groups of younglings on one platform
         // There's at least two groups on each platform.
         // They are positioned randomly.
         int clusters = MinYounglingClusters + rand() % (MaxYounglingClusters - MinYounglingClusters + 1);
         GameObjectPtr next_youngling(new Youngling(e, 0, 0));
         int max_cluster_width = next_youngling -> width() * MaxYounglingsInCluster;
         int yng_x = rand() % (p.width() - max_cluster_width); // X coordinate offset from the start of the platform
         int yng_y = p.y() - next_youngling -> height(); // Y coordinate

         // Place groups one after another, leaving some space between them. 
         // If we exceed the length of the platform, wrap around.
         for(int c = 0; c < clusters; ++c) // Generate younglings for each group
         {
            int yng_count = MinYounglingsInCluster + rand() % (MaxYounglingsInCluster - MinYounglingsInCluster + 1); 
            yng_x = (yng_x + max_cluster_width) % (p.width() - max_cluster_width);

            // Place each youngling in the group
            for(int c = 0; c < yng_count; ++c)
            {
               next_youngling.reset(new Youngling(e, p.x() + yng_x, yng_y));
               p.addObject(GameObjectPtr(next_youngling));
               yng_x += next_youngling -> width();
            }
         }

         // Add meanies.
         // Meanies come in the following combinations:
         // a single Walker
         // a single Jumper
         // Jumper - Walker
         // Jumper - Flyer
         // Walker - Flyer
         int r = rand() % 5;
         switch(r)
         {
         case 0:
            p.addObject(GameObjectPtr(new MeanWalker(e, p.x() + 16, p.y() - 9, p.x(), p.x() + p.width()))); 
            break;

         case 1:
            p.addObject(GameObjectPtr(new MeanJumper(e, p.x() + p.width()/2, p.y() - 10,  p.x(), p.x() + p.width())));
            break;

         case 2:
            p.addObject(GameObjectPtr(new MeanWalker(e, p.x() + 16, p.y() - 9, p.x(), p.x() + p.width()))); 
            p.addObject(GameObjectPtr(new MeanJumper(e, p.x() + p.width()/2, p.y() - 10,  p.x(), p.x() + p.width())));
            break;

         case 3:
            p.addObject(GameObjectPtr(new MeanFlyer(e, p.x() + p.width()/4, p.y() - 32,  p.x(), p.x() + p.width())));
            p.addObject(GameObjectPtr(new MeanWalker(e, p.x() + 16, p.y() - 9, p.x(), p.x() + p.width()))); 
            break;

         case 4:
            p.addObject(GameObjectPtr(new MeanFlyer(e, p.x() + p.width()/4, p.y() - 32,  p.x(), p.x() + p.width())));
            p.addObject(GameObjectPtr(new MeanJumper(e, p.x() + p.width()/2, p.y() - 10,  p.x(), p.x() + p.width())));
            break;
         }

         // Add a heart with 1/3 probability
         if(rand() % 3 == 0)
         {
            p.addObject(GameObjectPtr(new Heart(e, p.x() + rand() % p.width(), p.y() - (16 + rand() % 5))));
         }
      }
   }