Proper fix for waterfall tiles
[supertux.git] / src / object / firefly.cpp
index 792c69d..0077c9b 100644 (file)
 
 #include "object/firefly.hpp"
 
+#include <math.h>
+
+#include "audio/sound_manager.hpp"
 #include "math/random_generator.hpp"
 #include "object/player.hpp"
 #include "object/sprite_particle.hpp"
 #include "supertux/game_session.hpp"
 #include "supertux/object_factory.hpp"
 #include "supertux/sector.hpp"
-
-#include <math.h>
+#include "util/reader.hpp"
 
 Firefly::Firefly(const Reader& lisp) :
-   MovingSprite(lisp, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE), 
+   MovingSprite(lisp, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE),
    activated(false),
    initial_position()
 {
@@ -41,9 +43,17 @@ Firefly::Firefly(const Reader& lisp) :
     return;
   }
   //Replace sprite
-  sprite = sprite_manager->create( sprite_name );
+  sprite = SpriteManager::current()->create( sprite_name );
   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
   reactivate();
+
+  //Load sound
+    if( sprite_name.find("vbell", 0) == std::string::npos ) {
+      SoundManager::current()->preload("sounds/savebell_low.wav");
+    }
+    else {
+      SoundManager::current()->preload("sounds/savebell2.wav");
+    }
 }
 
 void
@@ -62,7 +72,8 @@ Firefly::reactivate()
 HitResponse
 Firefly::collision(GameObject& other, const CollisionHit& )
 {
-  if(activated)
+  // If the bell is already activated, don't ring it again!
+  if(activated || sprite->get_action() == "ringing")
     return ABORT_MOVE;
 
   Player* player = dynamic_cast<Player*> (&other);
@@ -72,15 +83,22 @@ Firefly::collision(GameObject& other, const CollisionHit& )
     // TODO: provide convenience function in MovingSprite or MovingObject?
     for (int i = 0; i < 5; i++) {
       Vector ppos = bbox.get_middle();
-      float angle = systemRandom.randf(-M_PI_2, M_PI_2);
-      float velocity = systemRandom.randf(450, 900);
+      float angle = graphicsRandom.randf(-M_PI_2, M_PI_2);
+      float velocity = graphicsRandom.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/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+      Sector::current()->add_object(std::make_shared<SpriteParticle>("images/objects/particles/reset.sprite", "default", ppos, ANCHOR_MIDDLE, pspeed, paccel, LAYER_OBJECTS-1));
+    }
+
+    if( sprite_name.find("vbell", 0) == std::string::npos ) {
+      SoundManager::current()->play("sounds/savebell2.wav");
     }
-    // TODO play sound
+    else {
+      SoundManager::current()->play("sounds/savebell_low.wav");
+    }
+
     sprite->set_action("ringing");
     GameSession::current()->set_reset_point(Sector::current()->get_name(),
                                             initial_position);
@@ -89,6 +107,4 @@ Firefly::collision(GameObject& other, const CollisionHit& )
   return ABORT_MOVE;
 }
 
-IMPLEMENT_FACTORY(Firefly, "firefly");
-
 /* EOF */