merged new collision detection branch back into mainline
[supertux.git] / src / object / bullet.cpp
index a8b7cef..ef7b52f 100644 (file)
 #include "badguy/badguy.hpp"
 #include "main.hpp"
 
-static const float BULLET_XM = 600;
-static const float BULLET_STARTING_YM = 0;
+namespace {
+  const float BULLET_XM = 600;
+  const float BULLET_STARTING_YM = 0;
+}
 
-Bullet::Bullet(const Vector& pos, float xm, int dir, int kind_)
-  : kind(kind_), life_count(3), sprite(0)
+Bullet::Bullet(const Vector& pos, float xm, int dir)
+  : life_count(3)
 {
+  sprite.reset(sprite_manager->create("images/objects/bullets/firebullet.sprite"));
+  
   bbox.set_pos(pos);
-  bbox.set_size(4, 4);
+  bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
 
   float speed = dir == RIGHT ? BULLET_XM : -BULLET_XM;
   physic.set_velocity_x(speed + xm);
   physic.set_velocity_y(BULLET_STARTING_YM);
-
-  if (kind == ICE_BULLET) {
-    life_count = 6; //ice-bullets get "extra lives" for bumping off walls
-    sprite = sprite_manager->create("images/objects/bullets/icebullet.sprite");
-  } else if(kind == FIRE_BULLET) {
-    sprite = sprite_manager->create("images/objects/bullets/firebullet.sprite");
-  }
 }
 
 Bullet::~Bullet()
 {
-  delete sprite;
 }
 
 void
 Bullet::update(float elapsed_time)
 {
-  if(kind == FIRE_BULLET) {
-    // @not completely framerate independant :-/
-    physic.set_velocity_y(physic.get_velocity_y() + 50 * elapsed_time);
-  }
+  // @not completely framerate independant :-/
+  physic.set_velocity_y(physic.get_velocity_y() + 50 * elapsed_time);
+
   if(physic.get_velocity_y() > 900)
     physic.set_velocity_y(900);
   else if(physic.get_velocity_y() < -900)
@@ -88,30 +83,20 @@ Bullet::draw(DrawingContext& context)
   sprite->draw(context, get_pos(), LAYER_OBJECTS);
 }
 
-HitResponse
-Bullet::collision(GameObject& other, const CollisionHit& hit)
+void
+Bullet::collision_solid(const CollisionHit& hit)
 {
-  if(other.get_flags() & FLAG_SOLID) {
-    if(fabsf(hit.normal.y) > .5) { // roof or floor bump
-      physic.set_velocity_y(-physic.get_velocity_y());
-      life_count -= 1;
-    } else { // bumped left or right
-      if(kind == FIRE_BULLET)
-        remove_me();
-      else
-        physic.set_velocity_x(-physic.get_velocity_x());
-    }
-    
-    return CONTINUE;
-  }
-
-  // hit a Badguy
-  BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
-  if(badguy) {
+  if(hit.top || hit.bottom) {
+    physic.set_velocity_y(-physic.get_velocity_y());
+    life_count--;
+  } else if(hit.left || hit.right) {
     remove_me();
-    return FORCE_MOVE;
   }
+}
+
+HitResponse
+Bullet::collision(GameObject& , const CollisionHit& )
+{
   return FORCE_MOVE;
 }