Merged back changes from 0.3.x branch
[supertux.git] / src / badguy / bomb.cpp
index e065086..670baa5 100644 (file)
 #include <config.h>
 
 #include "bomb.hpp"
+#include "random_generator.hpp"
+#include "object/sprite_particle.hpp"
 
-static const float TICKINGTIME = 1;
-static const float EXPLOSIONTIME = 1;
-
-Bomb::Bomb(const Vector& pos, Direction dir)
+Bomb::Bomb(const Vector& pos, Direction dir, std::string custom_sprite /*= "images/creatures/mr_bomb/mr_bomb.sprite"*/ )
+       : BadGuy( pos, dir, custom_sprite )
 {
-  start_position = pos;
-  bbox.set_pos(pos);
-  sprite = sprite_manager->create("images/creatures/mr_bomb/bomb.sprite");
-  bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
   state = STATE_TICKING;
-  timer.start(TICKINGTIME);
-  this->dir = dir;
-  sprite->set_action(dir == LEFT ? "ticking-left" : "ticking-right");
+  set_action(dir == LEFT ? "ticking-left" : "ticking-right", 1);
   countMe = false;
+
+  ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
+  ticking->set_position(get_pos());
+  ticking->set_looping(true);
+  ticking->set_gain(2.0);
+  ticking->set_reference_distance(32);
+  ticking->play();
+}
+
+Bomb::Bomb(const Bomb& other)
+       : BadGuy(other), state(other.state)
+{
+  if (state == STATE_TICKING) {
+    ticking.reset(sound_manager->create_sound_source("sounds/fizz.wav"));
+    ticking->set_position(get_pos());
+    ticking->set_looping(true);
+    ticking->set_gain(2.0);
+    ticking->set_reference_distance(32);
+    ticking->play();
+  }
 }
 
 void
@@ -43,13 +57,11 @@ Bomb::write(lisp::Writer& )
   // bombs are only temporarily so don't write them out...
 }
 
-HitResponse
-Bomb::collision_solid(GameObject& , const CollisionHit& hit)
+void
+Bomb::collision_solid(const CollisionHit& hit)
 {
-  if(fabsf(hit.normal.y) > .5)
+  if(hit.bottom)
     physic.set_velocity_y(0);
-
-  return CONTINUE;
 }
 
 HitResponse
@@ -74,26 +86,41 @@ Bomb::active_update(float )
 {
   switch(state) {
     case STATE_TICKING:
-      if(timer.check()) {
+      ticking->set_position(get_pos());
+      if(sprite->animation_done()) {
         explode();
       }
       break;
     case STATE_EXPLODING:
-      if(timer.check()) {
+      if(sprite->animation_done()) {
         remove_me();
       }
       break;
-  } 
+  }
 }
 
 void
 Bomb::explode()
 {
+  ticking->stop();
   state = STATE_EXPLODING;
   set_group(COLGROUP_TOUCHABLE);
-  sprite->set_action("explosion");
   sound_manager->play("sounds/explosion.wav", get_pos());
-  timer.start(EXPLOSIONTIME);
+  set_action("explosion", 1, ANCHOR_BOTTOM);
+
+  // spawn some particles
+  // TODO: provide convenience function in MovingSprite or MovingObject?
+  for (int i = 0; i < 100; i++) {
+    Vector ppos = bbox.get_middle();
+    float angle = systemRandom.randf(-M_PI_2, M_PI_2);
+    float velocity = systemRandom.randf(450, 900);
+    float vx = sin(angle)*velocity;
+    float vy = -cos(angle)*velocity;
+    Vector pspeed = Vector(vx, vy);
+    Vector paccel = Vector(0, 1000);
+    Sector::current()->add_object(new SpriteParticle("images/objects/particles/explosion.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+  }
+
 }
 
 void
@@ -102,4 +129,3 @@ Bomb::kill_fall()
   if (state != STATE_EXPLODING)  // we don't want it exploding again
     explode();
 }
-