Fixed a counting bug when using butt-jump on a multi-hit bonus block
[supertux.git] / src / object / bonus_block.cpp
index 37ad895..83cab76 100644 (file)
 #include "object/broken_brick.hpp"
 #include "object/flower.hpp"
 #include "object/bouncy_coin.hpp"
+#include "object/coin_explode.hpp"
+#include "object/coin_rain.hpp"
 #include "object/growup.hpp"
 #include "object/oneup.hpp"
 #include "object/player.hpp"
 #include "object/portable.hpp"
+#include "object/powerup.hpp"
 #include "object/specialriser.hpp"
 #include "object/star.hpp"
+#include "object/trampoline.hpp"
 #include "sprite/sprite_manager.hpp"
 #include "supertux/constants.hpp"
 #include "supertux/level.hpp"
 #include <stdexcept>
 
 BonusBlock::BonusBlock(const Vector& pos, int data) :
-  Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
+  Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
   contents(),
   object(0),
   hit_counter(1),
-  lightsprite(Surface::create("/images/objects/lightmap_light/bonusblock_light.png"))
+  lightsprite()
 {
   bbox.set_pos(pos);
   sprite->set_action("normal");
@@ -51,7 +55,24 @@ BonusBlock::BonusBlock(const Vector& pos, int data) :
     case 3: contents = CONTENT_STAR; break;
     case 4: contents = CONTENT_1UP; break;
     case 5: contents = CONTENT_ICEGROW; break;
-    case 6: contents = CONTENT_LIGHT; sound_manager->preload("sounds/switch.ogg"); break;
+    case 6: contents = CONTENT_LIGHT;
+      sound_manager->preload("sounds/switch.ogg");
+      lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
+      break;
+    case 7: contents = CONTENT_TRAMPOLINE;
+      //object = new Trampoline(get_pos(), false); //needed if this is to be moved to custom
+      break;
+    case 8: contents = CONTENT_CUSTOM;
+      object = new Trampoline(get_pos(), true);
+      break;
+    case 9: contents = CONTENT_CUSTOM;
+      object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
+      break;
+    case 10: contents = CONTENT_RAIN; break;
+    case 11: contents = CONTENT_EXPLODE; break;
+    case 12: contents = CONTENT_CUSTOM;
+      object = new PowerUp(get_pos(), "images/powerups/potions/red-potion.sprite");
+      break;
     default:
       log_warning << "Invalid box contents" << std::endl;
       contents = CONTENT_COIN;
@@ -64,7 +85,7 @@ BonusBlock::BonusBlock(const Reader& lisp) :
   contents(),
   object(0),
   hit_counter(1),
-  lightsprite(Surface::create("/images/objects/lightmap_light/bonusblock_light.png"))
+  lightsprite()
 {
   Vector pos;
 
@@ -98,11 +119,17 @@ BonusBlock::BonusBlock(const Reader& lisp) :
         contents = CONTENT_1UP;
       } else if(contentstring == "custom") {
         contents = CONTENT_CUSTOM;
-      } else if(contentstring == "script") {
+      } else if(contentstring == "script") { // use when bonusblock is to contain ONLY a script
         contents = CONTENT_SCRIPT;
       } else if(contentstring == "light") {
         contents = CONTENT_LIGHT;
         sound_manager->preload("sounds/switch.ogg");
+      } else if(contentstring == "trampoline") {
+        contents = CONTENT_TRAMPOLINE;
+      } else if(contentstring == "rain") {
+        contents = CONTENT_RAIN;
+      } else if(contentstring == "explode") {
+        contents = CONTENT_EXPLODE;
       } else {
         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
       }
@@ -121,6 +148,8 @@ BonusBlock::BonusBlock(const Reader& lisp) :
 
   if(contents == CONTENT_CUSTOM && object == 0)
     throw std::runtime_error("Need to specify content object for custom block");
+  if(contents == CONTENT_LIGHT)
+    lightsprite = Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
 
   bbox.set_pos(pos);
 }
@@ -142,7 +171,7 @@ BonusBlock::collision(GameObject& other, const CollisionHit& hit){
   Player* player = dynamic_cast<Player*> (&other);
   if (player) {
     if (player->does_buttjump)
-      try_open(player);
+      try_drop(player);
   }
 
   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
@@ -177,7 +206,7 @@ BonusBlock::try_open(Player *player)
 
   if (player == NULL)
     player = sector->player;
-  
+
   if (player == NULL)
     return;
 
@@ -224,12 +253,14 @@ BonusBlock::try_open(Player *player)
     case CONTENT_STAR:
     {
       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
+      sound_manager->play("sounds/upgrade.wav");
       break;
     }
 
     case CONTENT_1UP:
     {
       sector->add_object(new OneUp(get_pos(), direction));
+      sound_manager->play("sounds/upgrade.wav");
       break;
     }
 
@@ -243,13 +274,8 @@ BonusBlock::try_open(Player *player)
     }
 
     case CONTENT_SCRIPT:
-    {
-      if(script != "") {
-        std::istringstream stream(script);
-        Sector::current()->run_script(stream, "powerup-script");
-      }
-      break;
-    }
+    { break; } // because scripts always run, this prevents default contents from being assumed
+
     case CONTENT_LIGHT:
     {
       if(sprite->get_action() == "on")
@@ -257,9 +283,36 @@ BonusBlock::try_open(Player *player)
       else
         sprite->set_action("on");
       sound_manager->play("sounds/switch.ogg");
+      break;
+    }
+    case CONTENT_TRAMPOLINE:
+    {
+      SpecialRiser* riser = new SpecialRiser(get_pos(), new Trampoline(get_pos(), false));
+      sector->add_object(riser);
+      sound_manager->play("sounds/upgrade.wav");
+      break;
+    }
+    case CONTENT_RAIN:
+    {
+      hit_counter = 1; // multiple hits of coin rain is not allowed
+      Sector::current()->add_object(new CoinRain(get_pos(), true));
+      sound_manager->play("sounds/upgrade.wav");
+      break;
+    }
+    case CONTENT_EXPLODE:
+    {
+      hit_counter = 1; // multiple hits of coin explode is not allowed
+      Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, -40)));
+      sound_manager->play("sounds/upgrade.wav");
+      break;
     }
   }
 
+  if(script != "") { // scripts always run if defined
+    std::istringstream stream(script);
+    Sector::current()->run_script(stream, "BonusBlockScript");
+  }
+
   start_bounce(player);
   if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
   }else if(hit_counter == 1){
@@ -270,6 +323,130 @@ BonusBlock::try_open(Player *player)
 }
 
 void
+BonusBlock::try_drop(Player *player)
+{
+  if(sprite->get_action() == "empty") {
+    sound_manager->play("sounds/brick.wav");
+    return;
+  }
+
+  Sector* sector = Sector::current();
+  assert(sector);
+
+  // First what's below the bonus block, if solid send it up anyway (excepting doll)
+  Rectf dest;
+  dest.p1.x = bbox.get_left() + 1;
+  dest.p1.y = bbox.get_bottom() + 1;
+  dest.p2.x = bbox.get_right() - 1;
+  dest.p2.y = dest.p1.y + 30;
+  if (!Sector::current()->is_free_of_statics(dest, this, true) && !(contents == CONTENT_1UP)) {
+    try_open(player);
+    return;
+  }
+
+  if (player == NULL)
+    player = sector->player;
+
+  if (player == NULL)
+    return;
+
+  Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
+
+  bool countdown = false;
+
+  switch(contents) {
+    case CONTENT_COIN:
+    {
+      try_open(player);
+      break;
+    }
+
+    case CONTENT_FIREGROW:
+    {
+      sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
+      sound_manager->play("sounds/upgrade.wav");
+      countdown = true;
+      break;
+    }
+
+    case CONTENT_ICEGROW:
+    {
+      sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/iceflower/iceflower.sprite"));
+      sound_manager->play("sounds/upgrade.wav");
+      countdown = true;
+      break;
+    }
+
+    case CONTENT_STAR:
+    {
+      sector->add_object(new Star(get_pos() + Vector(0, 32), direction));
+      sound_manager->play("sounds/upgrade.wav");
+      countdown = true;
+      break;
+    }
+
+    case CONTENT_1UP:
+    {
+      sector->add_object(new OneUp(get_pos(), DOWN));
+      sound_manager->play("sounds/upgrade.wav");
+      countdown = true;
+      break;
+    }
+
+    case CONTENT_CUSTOM:
+    {
+      //NOTE: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
+      object->set_pos(get_pos() +  Vector(0, 32));
+      sector->add_object(object);
+      object = 0;
+      sound_manager->play("sounds/upgrade.wav");
+      countdown = true;
+      break;
+    }
+
+    case CONTENT_SCRIPT:
+    { break; } // because scripts always run, this prevents default contents from being assumed
+
+    case CONTENT_LIGHT:
+    {
+      try_open(player);
+      break;
+    }
+    case CONTENT_TRAMPOLINE:
+    {
+      try_open(player);
+      break;
+    }
+    case CONTENT_RAIN:
+    {
+      try_open(player);
+      break;
+    }
+    case CONTENT_EXPLODE:
+    {
+      hit_counter = 1; // multiple hits of coin explode is not allowed
+      Sector::current()->add_object(new CoinExplode(get_pos() + Vector (0, 40)));
+      sound_manager->play("sounds/upgrade.wav");
+      countdown = true;
+      break;
+    }
+  }
+
+  if(script != "") { // scripts always run if defined
+    std::istringstream stream(script);
+    Sector::current()->run_script(stream, "powerup-script");
+  }
+
+  if(countdown){ // only decrease hit counter if try_open was not called
+    if(hit_counter == 1){
+      sprite->set_action("empty");
+    }else{
+      hit_counter--;
+    }
+  }
+}
+
+void
 Block::break_me()
 {
   Sector* sector = Sector::current();
@@ -289,9 +466,9 @@ Block::break_me()
 
 void
 BonusBlock::draw(DrawingContext& context){
-  // draw regular sprite
-  sprite->draw(context, get_pos(), 10);
-  //Draw light if on.
+  // do the regular drawing first
+  Block::draw(context);
+  // then Draw the light if on.
   if(sprite->get_action() == "on") {
     Vector pos = get_pos() + (bbox.get_size() - lightsprite->get_size()) / 2;
     context.push_target();