Magic Blocks. (DrawingContext::get_light is not working yet.)
[supertux.git] / src / object / firefly.cpp
index c0875b5..49299fb 100644 (file)
 #include "object_factory.hpp"
 #include "game_session.hpp"
 #include "sector.hpp"
+#include "random_generator.hpp"
+#include "object/sprite_particle.hpp"
 
 Firefly::Firefly(const lisp::Lisp& lisp)
-  : ringing(false)
+       : MovingSprite(lisp, "images/objects/resetpoints/default-resetpoint.sprite", LAYER_TILES, COLGROUP_TOUCHABLE), activated(false)
 {
-  lisp.get("x", bbox.p1.x);
-  lisp.get("y", bbox.p1.y);
-  bbox.set_size(32, 32);
-  sprite = sprite_manager->create("images/objects/firefly/firefly.sprite");
-  set_group(COLGROUP_TOUCHABLE);
-}
 
-Firefly::~Firefly()
-{
-  delete sprite;
+  if( !lisp.get( "sprite", sprite_name ) ){
+    return;
+  }
+  if( sprite_name == "" ){
+    sprite_name = "images/objects/resetpoints/default-resetpoint.sprite";
+    return;
+  }
+  //Replace sprite
+  sprite = sprite_manager->create( sprite_name );
+  bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
 }
 
 void
@@ -49,35 +52,36 @@ Firefly::write(lisp::Writer& writer)
   writer.start_list("firefly");
   writer.write_float("x", bbox.p1.x);
   writer.write_float("y", bbox.p1.y);
-  writer.end_list("Firefly");
-}
-
-void
-Firefly::update(float )
-{
-}
-
-void
-Firefly::draw(DrawingContext& context)
-{
-  sprite->draw(context, get_pos(), LAYER_TILES);
+  writer.end_list("firefly");
 }
 
 HitResponse
 Firefly::collision(GameObject& other, const CollisionHit& )
 {
-  if(ringing)
+  if(activated)
     return ABORT_MOVE;
-  
+
   Player* player = dynamic_cast<Player*> (&other);
   if(player) {
-    ringing = true;
+    activated = true;
+// spawn some particles
+// 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 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));
+          }
     // TODO play sound
     sprite->set_action("ringing");
     GameSession::current()->set_reset_point(Sector::current()->get_name(),
         get_pos());
   }
-  
+
   return ABORT_MOVE;
 }