- made some arrays private
authorIngo Ruhnke <grumbel@gmx.de>
Thu, 22 Apr 2004 17:53:08 +0000 (17:53 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Thu, 22 Apr 2004 17:53:08 +0000 (17:53 +0000)
- store badguys in pointers instead of per value

SVN-Revision: 646

src/badguy.cpp
src/world.cpp
src/world.h

index 870ee42..c09678b 100644 (file)
@@ -408,14 +408,7 @@ BadGuy::fall()
 void
 BadGuy::remove_me()
 {
-  for(std::vector<BadGuy>::iterator i = World::current()->bad_guys.begin(); 
-      i != World::current()->bad_guys.end(); ++i) 
-    {
-      if( & (*i) == this) {
-        World::current()->bad_guys.erase(i);
-        return;
-      }
-    }
+  World::current()->remove_bad_guy(this);
 }
 
 void
index 8cdee76..27a06c2 100644 (file)
@@ -72,6 +72,11 @@ World::World(const std::string& subset, int level_nr)
 
 World::~World()
 {
+  for (unsigned int i = 0; i < bad_guys.size(); ++i)
+    {
+      delete bad_guys[i];
+    }
+
   halt_music(); // just to be sure (because levelmusic is freed now)
   delete level;
 }
@@ -168,7 +173,7 @@ World::draw()
     bouncy_bricks[i].draw();
 
   for (unsigned int i = 0; i < bad_guys.size(); ++i)
-    bad_guys[i].draw();
+    bad_guys[i]->draw();
 
   tux.draw();
 
@@ -240,7 +245,7 @@ World::action(double frame_ratio)
     upgrades[i].action(frame_ratio);
 
   for (unsigned int i = 0; i < bad_guys.size(); i++)
-    bad_guys[i].action(frame_ratio);
+    bad_guys[i]->action(frame_ratio);
 
   /* update particle systems */
   std::vector<ParticleSystem*>::iterator p;
@@ -262,15 +267,15 @@ World::collision_handler()
     {
       for(unsigned int j = 0; j < bad_guys.size(); ++j)
         {
-          if(bad_guys[j].dying != DYING_NOT)
+          if(bad_guys[j]->dying != DYING_NOT)
             continue;
-          if(rectcollision(&bullets[i].base, &bad_guys[j].base))
+          if(rectcollision(&bullets[i].base, &bad_guys[j]->base))
             {
               // We have detected a collision and now call the
               // collision functions of the collided objects.
               // collide with bad_guy first, since bullet_collision will
               // delete the bullet
-              bad_guys[j].collision(0, CO_BULLET);
+              bad_guys[j]->collision(0, CO_BULLET);
               bullets[i].collision(CO_BADGUY);
               break; // bullet is invalid now, so break
             }
@@ -280,20 +285,20 @@ World::collision_handler()
   /* CO_BADGUY & CO_BADGUY check */
   for(unsigned int i = 0; i < bad_guys.size(); ++i)
     {
-      if(bad_guys[i].dying != DYING_NOT)
+      if(bad_guys[i]->dying != DYING_NOT)
         continue;
       
       for(unsigned int j = i+1; j < bad_guys.size(); ++j)
         {
-          if(j == i || bad_guys[j].dying != DYING_NOT)
+          if(j == i || bad_guys[j]->dying != DYING_NOT)
             continue;
 
-          if(rectcollision(&bad_guys[i].base, &bad_guys[j].base))
+          if(rectcollision(&bad_guys[i]->base, &bad_guys[j]->base))
             {
               // We have detected a collision and now call the
               // collision functions of the collided objects.
-              bad_guys[j].collision(&bad_guys[i], CO_BADGUY);
-              bad_guys[i].collision(&bad_guys[j], CO_BADGUY);
+              bad_guys[j]->collision(&bad_guys[i], CO_BADGUY);
+              bad_guys[i]->collision(&bad_guys[j], CO_BADGUY);
             }
         }
     }
@@ -303,23 +308,23 @@ World::collision_handler()
   // CO_BADGUY & CO_PLAYER check 
   for(unsigned int i = 0; i < bad_guys.size(); ++i)
     {
-      if(bad_guys[i].dying != DYING_NOT)
+      if(bad_guys[i]->dying != DYING_NOT)
         continue;
       
-      if(rectcollision_offset(&bad_guys[i].base,&tux.base,0,0))
+      if(rectcollision_offset(&bad_guys[i]->base,&tux.base,0,0))
         {
           // We have detected a collision and now call the collision
           // functions of the collided objects.
           if (tux.previous_base.y < tux.base.y &&
               tux.previous_base.y + tux.previous_base.height 
-              < bad_guys[i].base.y + bad_guys[i].base.height/2)
+              < bad_guys[i]->base.y + bad_guys[i]->base.height/2)
             {
-              bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
+              bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_SQUISH);
             }
           else
             {
               tux.collision(&bad_guys[i], CO_BADGUY);
-              bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_NORMAL);
+              bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_NORMAL);
             }
         }
     }
@@ -380,13 +385,29 @@ World::add_bouncy_brick(float x, float y)
   bouncy_bricks.push_back(new_bouncy_brick);
 }
 
-void
+BadGuy*
 World::add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform)
 {
-  bad_guys.push_back(BadGuy());
-  BadGuy& new_bad_guy = bad_guys.back();
+  bad_guys.push_back(new BadGuy());
+  BadGuy* new_bad_guy = bad_guys.back();
   
-  new_bad_guy.init(x,y,kind, stay_on_platform);
+  new_bad_guy->init(x,y,kind, stay_on_platform);
+  return new_bad_guy;
+}
+
+void
+World::remove_bad_guy(BadGuy* badguy)
+{
+  for(std::vector<BadGuy*>::iterator i = bad_guys.begin(); 
+      i != bad_guys.end(); ++i) 
+    {
+      if(*i == badguy)
+        {
+          World::current()->bad_guys.erase(i);
+          delete badguy;
+          return;
+        }
+    }
 }
 
 void
@@ -557,10 +578,10 @@ World::trybumpbadguy(float x, float y)
   // Bad guys: 
   for (unsigned int i = 0; i < bad_guys.size(); i++)
     {
-      if (bad_guys[i].base.x >= x - 32 && bad_guys[i].base.x <= x + 32 &&
-          bad_guys[i].base.y >= y - 16 && bad_guys[i].base.y <= y + 16)
+      if (bad_guys[i]->base.x >= x - 32 && bad_guys[i]->base.x <= x + 32 &&
+          bad_guys[i]->base.y >= y - 16 && bad_guys[i]->base.y <= y + 16)
         {
-          bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_BUMP);
+          bad_guys[i]->collision(&tux, CO_PLAYER, COLLISION_BUMP);
         }
     }
 
index ddbedd4..d7b8e97 100644 (file)
@@ -37,28 +37,27 @@ class Level;
     bouncy distros, etc) that are needed to run a game. */
 class World
 {
- public:
+private:
+  std::vector<BadGuy*> bad_guys;
   Level* level;
-
   Player tux;
-  
+
+  int distro_counter;
+  bool counting_distros;
+  int currentmusic;
+
+  static World* current_;
+public:
   std::vector<BouncyDistro> bouncy_distros;
   std::vector<BrokenBrick>  broken_bricks;
   std::vector<BouncyBrick>  bouncy_bricks;
   std::vector<FloatingScore> floating_scores;
 
-  std::vector<BadGuy> bad_guys;
   std::vector<Upgrade> upgrades;
   std::vector<Bullet> bullets;
   std::vector<ParticleSystem*> particle_systems;
 
-  int distro_counter;
-  bool counting_distros;
-
-  int currentmusic;
-
-  static World* current_;
- public:
+public:
   static World* current() { return current_; }
   static void set_current(World* w) { current_ = w; }
 
@@ -92,7 +91,10 @@ class World
   void add_broken_brick(Tile* tile, float x, float y);
   void add_broken_brick_piece(Tile* tile, float x, float y, float xm, float ym);
   void add_bouncy_brick(float x, float y);
-  void add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform = false);
+
+  BadGuy* add_bad_guy(float x, float y, BadGuyKind kind, bool stay_on_platform = false);
+  void    remove_bad_guy(BadGuy* badguy);
+
   void add_upgrade(float x, float y, Direction dir, UpgradeKind kind);
   void add_bullet(float x, float y, float xm, Direction dir);