extend moving_object with a collides function that allows an early veto of collision...
authorMatthias Braun <matze@braunis.de>
Sat, 26 May 2007 14:15:22 +0000 (14:15 +0000)
committerMatthias Braun <matze@braunis.de>
Sat, 26 May 2007 14:15:22 +0000 (14:15 +0000)
SVN-Revision: 5018

src/object/invisible_block.cpp
src/object/invisible_block.hpp
src/object/magicblock.cpp
src/object/magicblock.hpp
src/sector.cpp

index 19922bd..2e802fc 100644 (file)
@@ -43,20 +43,27 @@ InvisibleBlock::draw(DrawingContext& context)
     sprite->draw(context, get_pos(), LAYER_OBJECTS);
 }
 
-HitResponse
-InvisibleBlock::collision(GameObject& other, const CollisionHit& hit)
+bool
+InvisibleBlock::collides(GameObject& other, const CollisionHit& )
 {
-  if (visible) return Block::collision(other, hit);
+  if(visible)
+    return true;
 
   // if we're not visible, only register a collision if this will make us visible
   Player* player = dynamic_cast<Player*> (&other);
   if ((player) 
           && (player->get_movement().y <= 0)
           && (player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0)) {
-    return Block::collision(other, hit);
+    return true;
   }
 
-  return PASSTHROUGH;
+  return false;
+}
+
+HitResponse
+InvisibleBlock::collision(GameObject& other, const CollisionHit& hit)
+{
+  return Block::collision(other, hit);
 }
 
 void
index 7b32be3..85fbb4b 100644 (file)
@@ -28,6 +28,7 @@ public:
   InvisibleBlock(const Vector& pos);
 
   virtual void draw(DrawingContext& context);
+  virtual bool collides(GameObject& other, const CollisionHit& hit);
   virtual HitResponse collision(GameObject& other, const CollisionHit& hit);
 
 protected:
index 9d0caf8..734186f 100644 (file)
@@ -144,14 +144,16 @@ MagicBlock::draw(DrawingContext& context){
   context.draw_filled_rect( get_bbox(), color, layer);
 }
 
+bool
+MagicBlock::collides(GameObject& /*other*/, const CollisionHit& /*hit*/)
+{
+  return is_solid;
+}
+
 HitResponse
 MagicBlock::collision(GameObject& /*other*/, const CollisionHit& /*hit*/)
 {
-  if(is_solid) {
-    return SOLID;
-  } else {
-    return PASSTHROUGH;
-  }
+  return SOLID;
 }
 
 IMPLEMENT_FACTORY(MagicBlock, "magicblock");
index 3509e73..64d9add 100644 (file)
@@ -35,6 +35,7 @@ class MagicBlock: public MovingSprite
 public:
   MagicBlock(const lisp::Lisp& reader);
 
+  bool collides(GameObject& other, const CollisionHit& hit);
   HitResponse collision(GameObject& other, const CollisionHit& hit);
   void update(float elapsed_time);
   void draw(DrawingContext& context);
index 5ced57b..29a0aa1 100644 (file)
@@ -889,6 +889,10 @@ void check_collisions(collision::Constraints* constraints,
   if(!collision::intersects(r1, r2))
     return;
 
+  CollisionHit dummy;
+  if(other != NULL && !other->collides(*object, dummy))
+    return;
+
   // calculate intersection
   float itop = r1.get_bottom() - r2.get_top();
   float ibottom = r2.get_bottom() - r1.get_top();
@@ -915,10 +919,10 @@ void check_collisions(collision::Constraints* constraints,
   }
 
   if(other != NULL) {
-    CollisionHit dummy;
     HitResponse response = other->collision(*object, dummy);
     if(response == PASSTHROUGH)
       return;
+
     if(other->get_movement() != Vector(0, 0)) {
       // TODO what todo when we collide with 2 moving objects?!?
       constraints->ground_movement = other->get_movement();
@@ -1072,12 +1076,19 @@ Sector::collision_object(MovingObject* object1, MovingObject* object2) const
     Vector normal;
     get_hit_normal(r1, r2, hit, normal);
 
+    if(!object1->collides(*object2, hit))
+      return;
+    std::swap(hit.left, hit.right);
+    std::swap(hit.top, hit.bottom);
+    if(!object2->collides(*object1, hit))
+      return;
+    std::swap(hit.left, hit.right);
+    std::swap(hit.top, hit.bottom);
+
     HitResponse response1 = object1->collision(*object2, hit);
     std::swap(hit.left, hit.right);
     std::swap(hit.top, hit.bottom);
     HitResponse response2 = object2->collision(*object1, hit);
-    assert( response1 != SOLID && response1 != PASSTHROUGH );
-    assert( response2 != SOLID && response2 != PASSTHROUGH );
     if(response1 == CONTINUE && response2 == CONTINUE) {
       normal *= (0.5 + DELTA);
       object1->dest.move(-normal);
@@ -1168,8 +1179,10 @@ Sector::collision_static_constrains(MovingObject& object)
     if(constraints.right < infinity) {
       float width = constraints.right - constraints.left;
       if(width + SHIFT_DELTA < owidth) {
+#if 0
         printf("Object %p crushed horizontally... L:%f R:%f\n", &object,
             constraints.left, constraints.right);
+#endif
         CollisionHit h;
         h.left = true;
         h.right = true;
@@ -1198,7 +1211,9 @@ Sector::collision_static_constrains(MovingObject& object)
   if(constraints.bottom < infinity) {
     float height = constraints.bottom - constraints.top;
     if(height + SHIFT_DELTA < oheight) {
+#if 0
       printf("Object %p crushed vertically...\n", &object);
+#endif
       CollisionHit h;
       h.top = true;
       h.bottom = true;
@@ -1284,6 +1299,11 @@ Sector::handle_collisions()
         CollisionHit hit;
         get_hit_normal(moving_object->dest, moving_object_2->dest,
                        hit, normal);
+        if(!moving_object->collides(*moving_object_2, hit))
+          return;
+        if(!moving_object_2->collides(*moving_object, hit))
+          return;
+
         moving_object->collision(*moving_object_2, hit);
         moving_object_2->collision(*moving_object, hit);
       }
@@ -1472,7 +1492,9 @@ Sector::inside(const Rect& rect) const
     TileMap* solids = *i;
     bool horizontally = ((rect.p2.x >= 0 + solids->get_x_offset()) && (rect.p1.x <= solids->get_width() * 32 + solids->get_x_offset()));
     bool vertically = (rect.p1.y <= solids->get_height() * 32 + solids->get_y_offset());
-    if (horizontally && vertically) return true;
+
+    if (horizontally && vertically)
+      return true;
   }
   return false;
 }
@@ -1481,10 +1503,14 @@ float
 Sector::get_width() const
 {
   float width = 0;
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin();
+      i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
-    if ((solids->get_width() * 32 + solids->get_x_offset()) > width) width = (solids->get_width() * 32 + solids->get_x_offset());
+    if ((solids->get_width() * 32 + solids->get_x_offset()) > width) {
+      width = solids->get_width() * 32 + solids->get_x_offset();
+    }
   }
+
   return width;
 }
 
@@ -1492,10 +1518,14 @@ float
 Sector::get_height() const
 {
   float height = 0;
-  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin(); i != solid_tilemaps.end(); i++) {
+  for(std::list<TileMap*>::const_iterator i = solid_tilemaps.begin();
+      i != solid_tilemaps.end(); i++) {
     TileMap* solids = *i;
-    if ((solids->get_height() * 32 + solids->get_y_offset()) > height) height = (solids->get_height() * 32 + solids->get_y_offset());
+    if ((solids->get_height() * 32 + solids->get_y_offset()) > height) {
+      height = solids->get_height() * 32 + solids->get_y_offset();
+    }
   }
+
   return height;
 }