Removed some obsolete stuff from supertux/globals.hpp
[supertux.git] / src / object / bonus_block.cpp
index 6f4c057..dbcc20c 100644 (file)
@@ -1,5 +1,5 @@
 //  SuperTux
-//  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
+//  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
 //
 //  This program is free software: you can redistribute it and/or modify
 //  it under the terms of the GNU General Public License as published by
@@ -19,7 +19,6 @@
 #include "audio/sound_manager.hpp"
 #include "badguy/badguy.hpp"
 #include "lisp/list_iterator.hpp"
-#include "object/broken_brick.hpp"
 #include "object/flower.hpp"
 #include "object/bouncy_coin.hpp"
 #include "object/coin_explode.hpp"
 #include <stdexcept>
 
 BonusBlock::BonusBlock(const Vector& pos, int data) :
-  Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
+  Block(SpriteManager::current()->create("images/objects/bonus_block/bonusblock.sprite")),
   contents(),
   object(0),
   hit_counter(1),
+  sprite_name(),
+  script(),
   lightsprite()
 {
   bbox.set_pos(pos);
@@ -55,8 +56,8 @@ 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"); 
+    case 6: contents = CONTENT_LIGHT;
+      SoundManager::current()->preload("sounds/switch.ogg");
       lightsprite=Surface::create("/images/objects/lightmap_light/bonusblock_light.png");
       break;
     case 7: contents = CONTENT_TRAMPOLINE;
@@ -66,7 +67,7 @@ BonusBlock::BonusBlock(const Vector& pos, int data) :
       object = new Trampoline(get_pos(), true);
       break;
     case 9: contents = CONTENT_CUSTOM;
-      object = new Rock(get_pos(), "images/objects/rock/rock.sprite"); 
+      object = new Rock(get_pos(), "images/objects/rock/rock.sprite");
       break;
     case 10: contents = CONTENT_RAIN; break;
     case 11: contents = CONTENT_EXPLODE; break;
@@ -81,10 +82,12 @@ BonusBlock::BonusBlock(const Vector& pos, int data) :
 }
 
 BonusBlock::BonusBlock(const Reader& lisp) :
-  Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
+  Block(SpriteManager::current()->create("images/objects/bonus_block/bonusblock.sprite")),
   contents(),
   object(0),
   hit_counter(1),
+  sprite_name(),
+  script(),
   lightsprite()
 {
   Vector pos;
@@ -99,7 +102,7 @@ BonusBlock::BonusBlock(const Reader& lisp) :
       iter.value()->get(pos.y);
     } else if(token == "sprite") {
       iter.value()->get(sprite_name);
-      sprite = sprite_manager->create(sprite_name);
+      sprite = SpriteManager::current()->create(sprite_name);
     } else if(token == "count") {
       iter.value()->get(hit_counter);
     } else if(token == "script") {
@@ -123,7 +126,7 @@ BonusBlock::BonusBlock(const Reader& lisp) :
         contents = CONTENT_SCRIPT;
       } else if(contentstring == "light") {
         contents = CONTENT_LIGHT;
-        sound_manager->preload("sounds/switch.ogg");
+        SoundManager::current()->preload("sounds/switch.ogg");
       } else if(contentstring == "trampoline") {
         contents = CONTENT_TRAMPOLINE;
       } else if(contentstring == "rain") {
@@ -166,7 +169,7 @@ BonusBlock::hit(Player & player)
 }
 
 HitResponse
-BonusBlock::collision(GameObject& other, const CollisionHit& hit){
+BonusBlock::collision(GameObject& other, const CollisionHit& hit_){
 
   Player* player = dynamic_cast<Player*> (&other);
   if (player) {
@@ -190,14 +193,14 @@ BonusBlock::collision(GameObject& other, const CollisionHit& hit){
       try_open(player);
     }
   }
-  return Block::collision(other, hit);
+  return Block::collision(other, hit_);
 }
 
 void
 BonusBlock::try_open(Player *player)
 {
   if(sprite->get_action() == "empty") {
-    sound_manager->play("sounds/brick.wav");
+    SoundManager::current()->play("sounds/brick.wav");
     return;
   }
 
@@ -232,7 +235,7 @@ BonusBlock::try_open(Player *player)
           get_pos(), new Flower(FIRE_BONUS));
         sector->add_object(riser);
       }
-      sound_manager->play("sounds/upgrade.wav");
+      SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
 
@@ -246,21 +249,21 @@ BonusBlock::try_open(Player *player)
           get_pos(), new Flower(ICE_BONUS));
         sector->add_object(riser);
       }
-      sound_manager->play("sounds/upgrade.wav");
+      SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
 
     case CONTENT_STAR:
     {
       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
-      sound_manager->play("sounds/upgrade.wav");
+      SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
 
     case CONTENT_1UP:
     {
       sector->add_object(new OneUp(get_pos(), direction));
-      sound_manager->play("sounds/upgrade.wav");
+      SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
 
@@ -269,7 +272,7 @@ BonusBlock::try_open(Player *player)
       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
       object = 0;
       sector->add_object(riser);
-      sound_manager->play("sounds/upgrade.wav");
+      SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
 
@@ -282,28 +285,28 @@ BonusBlock::try_open(Player *player)
         sprite->set_action("off");
       else
         sprite->set_action("on");
-      sound_manager->play("sounds/switch.ogg");
+      SoundManager::current()->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");
+      SoundManager::current()->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");
+      SoundManager::current()->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");
+      SoundManager::current()->play("sounds/upgrade.wav");
       break;
     }
   }
@@ -326,7 +329,7 @@ void
 BonusBlock::try_drop(Player *player)
 {
   if(sprite->get_action() == "empty") {
-    sound_manager->play("sounds/brick.wav");
+    SoundManager::current()->play("sounds/brick.wav");
     return;
   }
 
@@ -334,12 +337,12 @@ BonusBlock::try_drop(Player *player)
   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)) {
+  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;
   }
@@ -352,6 +355,8 @@ BonusBlock::try_drop(Player *player)
 
   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
 
+  bool countdown = false;
+
   switch(contents) {
     case CONTENT_COIN:
     {
@@ -362,38 +367,43 @@ BonusBlock::try_drop(Player *player)
     case CONTENT_FIREGROW:
     {
       sector->add_object(new PowerUp(get_pos() + Vector(0, 32), "images/powerups/fireflower/fireflower.sprite"));
-      sound_manager->play("sounds/upgrade.wav");
+      SoundManager::current()->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");
+      SoundManager::current()->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");
+      SoundManager::current()->play("sounds/upgrade.wav");
+      countdown = true;
       break;
     }
 
     case CONTENT_1UP:
     {
       sector->add_object(new OneUp(get_pos(), DOWN));
-      sound_manager->play("sounds/upgrade.wav");
+      SoundManager::current()->play("sounds/upgrade.wav");
+      countdown = true;
       break;
     }
 
     case CONTENT_CUSTOM:
     {
-      //TODO: non-portable trampolines could be moved to CONTENT_CUSTOM, but they should not drop
+      //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");
+      SoundManager::current()->play("sounds/upgrade.wav");
+      countdown = true;
       break;
     }
 
@@ -419,7 +429,8 @@ BonusBlock::try_drop(Player *player)
     {
       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");
+      SoundManager::current()->play("sounds/upgrade.wav");
+      countdown = true;
       break;
     }
   }
@@ -429,39 +440,22 @@ BonusBlock::try_drop(Player *player)
     Sector::current()->run_script(stream, "powerup-script");
   }
 
-  if(hit_counter <= 0 || contents == CONTENT_LIGHT){ //use 0 to allow infinite hits
-  }else if(hit_counter == 1){
-    sprite->set_action("empty");
-  }else{
-    hit_counter--;
+  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();
-  sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
-  sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
-                    Vector(-150, -300)));
-  sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
-                    Vector(100, -400)));
-  sector->add_object(
-    new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
-                    Vector(150, -300)));
-  remove_me();
-}
-
-void
 BonusBlock::draw(DrawingContext& context){
   // 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;
+    Vector pos = get_pos() + (bbox.get_size().as_vector() - lightsprite->get_size()) / 2;
     context.push_target();
     context.set_target(DrawingContext::LIGHTMAP);
     context.draw_surface(lightsprite, pos, 10);