- moved some collision code into the world class, since it only acts on world data
authorIngo Ruhnke <grumbel@gmx.de>
Sun, 11 Apr 2004 13:00:00 +0000 (13:00 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Sun, 11 Apr 2004 13:00:00 +0000 (13:00 +0000)
SVN-Revision: 471

src/collision.cpp
src/collision.h
src/gameloop.cpp
src/world.cpp
src/world.h

index de62b7a..373acd2 100644 (file)
@@ -39,9 +39,9 @@ bool collision_object_map(base_type* pbase)
   int h = (int)pbase->width / 16;
 
   if(issolid(pbase->x + 1, pbase->y + 1) ||
-      issolid(pbase->x + pbase->width -1, pbase->y + 1) ||
-      issolid(pbase->x +1, pbase->y + pbase->height -1) ||
-      issolid(pbase->x + pbase->width -1, pbase->y + pbase->height - 1))
+     issolid(pbase->x + pbase->width -1, pbase->y + 1) ||
+     issolid(pbase->x +1, pbase->y + pbase->height -1) ||
+     issolid(pbase->x + pbase->width -1, pbase->y + pbase->height - 1))
     return true;
 
   for(int i = 1; i < h; ++i)
@@ -203,86 +203,6 @@ void collision_swept_object_map(base_type* old, base_type* current)
   *old = *current;
 }
 
-void collision_handler()
-{
-  // CO_BULLET & CO_BADGUY check
-  for(unsigned int i = 0; i < world.bullets.size(); ++i)
-    {
-      for(unsigned int j = 0; j < world.bad_guys.size(); ++j)
-        {
-          if(world.bad_guys[j].dying != DYING_NOT)
-            continue;
-          if(rectcollision(&world.bullets[i].base, &world.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
-              world.bad_guys[j].collision(0, CO_BULLET);
-              bullet_collision(&world.bullets[i], CO_BADGUY);
-              break; // bullet is invalid now, so break
-            }
-        }
-    }
-
-  /* CO_BADGUY & CO_BADGUY check */
-  for(unsigned int i = 0; i < world.bad_guys.size(); ++i)
-    {
-      if(world.bad_guys[i].dying != DYING_NOT)
-        continue;
-      
-      for(unsigned int j = i+1; j < world.bad_guys.size(); ++j)
-        {
-          if(j == i || world.bad_guys[j].dying != DYING_NOT)
-            continue;
-
-          if(rectcollision(&world.bad_guys[i].base, &world.bad_guys[j].base))
-            {
-              // We have detected a collision and now call the
-              // collision functions of the collided objects.
-              world.bad_guys[j].collision(&world.bad_guys[i], CO_BADGUY);
-              world.bad_guys[i].collision(&world.bad_guys[j], CO_BADGUY);
-            }
-        }
-    }
-
-  if(tux.dying != DYING_NOT) return;
-    
-  // CO_BADGUY & CO_PLAYER check 
-  for(unsigned int i = 0; i < world.bad_guys.size(); ++i)
-    {
-      if(world.bad_guys[i].dying != DYING_NOT)
-        continue;
-      
-      if(rectcollision_offset(&world.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 
-              < world.bad_guys[i].base.y + world.bad_guys[i].base.height/2)
-            {
-              world.bad_guys[i].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
-            }
-          else
-            {
-              tux.collision(&world.bad_guys[i], CO_BADGUY);
-            }
-        }
-    }
-
-  // CO_UPGRADE & CO_PLAYER check
-  for(unsigned int i = 0; i < world.upgrades.size(); ++i)
-    {
-      if(rectcollision(&world.upgrades[i].base, &tux.base))
-        {
-          // We have detected a collision and now call the collision
-          // functions of the collided objects.
-          upgrade_collision(&world.upgrades[i], &tux, CO_PLAYER);
-        }
-    }
-}
-
 
 Tile* gettile(float x, float y)
 {
index fa165d7..deee4b8 100644 (file)
@@ -15,6 +15,7 @@
 #include "type.h"
 
 class Tile;
+class World;
 
 /* Collision objects */
 enum
@@ -32,13 +33,10 @@ enum CollisionType {
 
 bool rectcollision(base_type* one, base_type* two);
 bool rectcollision_offset(base_type* one, base_type* two, float off_x, float off_y);
+
 void collision_swept_object_map(base_type* old, base_type* current);
 bool collision_object_map(base_type* object);
 
-/* Checks for all possible collisions.
-   And calls the collision_handlers, which the collision_objects provide for this case (or not). */
-void collision_handler();
-
 /** Return a pointer to the tile at the given x/y coordinates */
 Tile* gettile(float x, float y);
 
index e18a80b..62dfa25 100644 (file)
@@ -432,16 +432,6 @@ GameSession::action()
 
   world->action();
 
-  /* update particle systems */
-  std::vector<ParticleSystem*>::iterator p;
-  for(p = world->particle_systems.begin(); p != world->particle_systems.end(); ++p)
-    {
-      (*p)->simulate(frame_ratio);
-    }
-
-  /* Handle all possible collisions. */
-  collision_handler();
-
   return -1;
 }
 
index cc51e5c..1cfac5e 100644 (file)
@@ -249,6 +249,98 @@ World::action()
 
   for (unsigned int i = 0; i < bad_guys.size(); i++)
     bad_guys[i].action();
+
+  /* update particle systems */
+  std::vector<ParticleSystem*>::iterator p;
+  for(p = particle_systems.begin(); p != particle_systems.end(); ++p)
+    {
+      (*p)->simulate(frame_ratio);
+    }
+
+  /* Handle all possible collisions. */
+  collision_handler();
+}
+
+
+void
+World::collision_handler()
+{
+  // CO_BULLET & CO_BADGUY check
+  for(unsigned int i = 0; i < bullets.size(); ++i)
+    {
+      for(unsigned int j = 0; j < bad_guys.size(); ++j)
+        {
+          if(bad_guys[j].dying != DYING_NOT)
+            continue;
+          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);
+              bullet_collision(&bullets[i], CO_BADGUY);
+              break; // bullet is invalid now, so break
+            }
+        }
+    }
+
+  /* CO_BADGUY & CO_BADGUY check */
+  for(unsigned int i = 0; i < bad_guys.size(); ++i)
+    {
+      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)
+            continue;
+
+          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);
+            }
+        }
+    }
+
+  if(tux.dying != DYING_NOT) return;
+    
+  // CO_BADGUY & CO_PLAYER check 
+  for(unsigned int i = 0; i < bad_guys.size(); ++i)
+    {
+      if(bad_guys[i].dying != DYING_NOT)
+        continue;
+      
+      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].collision(&tux, CO_PLAYER, COLLISION_SQUISH);
+            }
+          else
+            {
+              tux.collision(&bad_guys[i], CO_BADGUY);
+            }
+        }
+    }
+
+  // CO_UPGRADE & CO_PLAYER check
+  for(unsigned int i = 0; i < upgrades.size(); ++i)
+    {
+      if(rectcollision(&upgrades[i].base, &tux.base))
+        {
+          // We have detected a collision and now call the collision
+          // functions of the collided objects.
+          upgrade_collision(&upgrades[i], &tux, CO_PLAYER);
+        }
+    }
 }
 
 void
index 953e739..5c53c36 100644 (file)
@@ -58,6 +58,12 @@ class World
 
   void draw();
   void action();
+
+  /** Checks for all possible collisions. And calls the
+      collision_handlers, which the collision_objects provide for this
+      case (or not). */
+  void collision_handler();
+  
   void arrays_free();
 
   /** Load data for this level: