made the phone (infoblock) ringing until tux picks it up
[supertux.git] / src / object / block.cpp
index 4bc3434..d61e420 100644 (file)
@@ -1,11 +1,33 @@
+//  $Id$
+// 
+//  SuperTux
+//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//
+//  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 the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+// 
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+//  02111-1307, USA.
 #include <config.h>
 
 #include "block.h"
+
+#include <stdexcept>
+
 #include "resources.h"
 #include "player.h"
 #include "sector.h"
-#include "special/sprite.h"
-#include "special/sprite_manager.h"
+#include "sprite/sprite.h"
+#include "sprite/sprite_manager.h"
 #include "video/drawing_context.h"
 #include "lisp/lisp.h"
 #include "gameobjs.h"
@@ -18,6 +40,8 @@
 #include "badguy/badguy.h"
 #include "coin.h"
 #include "object_factory.h"
+#include "lisp/list_iterator.h"
+#include "object_factory.h"
 
 static const float BOUNCY_BRICK_MAX_OFFSET=8;
 static const float BOUNCY_BRICK_SPEED=90;
@@ -61,7 +85,7 @@ Block::collision(GameObject& other, const CollisionHit& hitdata)
 }
 
 void
-Block::action(float elapsed_time)
+Block::update(float elapsed_time)
 {
   if(!bouncing)
     return;
@@ -97,10 +121,10 @@ Block::start_bounce()
 //---------------------------------------------------------------------------
 
 BonusBlock::BonusBlock(const Vector& pos, int data)
-  : Block(sprite_manager->create("bonusblock"))
+  : Block(sprite_manager->create("bonusblock")), object(0)
 {
   bbox.set_pos(pos);
-  sprite->set_action("default");
+  sprite->set_action("normal");
   switch(data) {
     case 1: contents = CONTENT_COIN; break;
     case 2: contents = CONTENT_FIREGROW; break;
@@ -118,27 +142,55 @@ BonusBlock::BonusBlock(const lisp::Lisp& lisp)
   : Block(sprite_manager->create("bonusblock"))
 {
   Vector pos;
-  lisp.get("x", pos.x);
-  lisp.get("y", pos.y);
-  bbox.set_pos(pos);
 
-  std::string contentstring;
   contents = CONTENT_COIN;
-  if(lisp.get("contents", contentstring)) {
-    if(contentstring == "coin") {
-      contents = CONTENT_COIN;
-    } else if(contentstring == "firegrow") {
-      contents = CONTENT_FIREGROW;
-    } else if(contentstring == "icegrow") {
-      contents = CONTENT_ICEGROW;
-    } else if(contentstring == "star") {
-      contents = CONTENT_STAR;
-    } else if(contentstring == "1up") {
-      contents = CONTENT_1UP;
+  lisp::ListIterator iter(&lisp);
+  while(iter.next()) {
+    const std::string& token = iter.item();
+    if(token == "x") {
+      iter.value()->get(pos.x);
+    } else if(token == "y") {
+      iter.value()->get(pos.y);
+    } else if(token == "contents") {
+      std::string contentstring;
+      iter.value()->get(contentstring);
+      if(contentstring == "coin") {
+        contents = CONTENT_COIN;
+      } else if(contentstring == "firegrow") {
+        contents = CONTENT_FIREGROW;
+      } else if(contentstring == "icegrow") {
+        contents = CONTENT_ICEGROW;
+      } else if(contentstring == "star") {
+        contents = CONTENT_STAR;
+      } else if(contentstring == "1up") {
+        contents = CONTENT_1UP;
+      } else if(contentstring == "custom") {
+        contents = CONTENT_CUSTOM;
+      } else {
+        std::cerr << "Invalid box contents '" << contentstring << "'.\n";
+      }
     } else {
-      std::cerr << "Invalid box contents '" << contentstring << "'.\n";
-    }
+      if(contents == CONTENT_CUSTOM) {
+        GameObject* game_object = create_object(token, *(iter.lisp()));
+        object = dynamic_cast<MovingObject*> (game_object);
+        if(object == 0)
+          throw std::runtime_error(
+            "Only MovingObjects are allowed inside BonusBlocks");
+      } else {
+        std::cerr << "Invalid element '" << token << "' in bonusblock.\n";
+      }
+    }  
   }
+
+  if(contents == CONTENT_CUSTOM && object == 0)
+    throw std::runtime_error("Need to specify content object for custom block");
+  
+  bbox.set_pos(pos);
+}
+
+BonusBlock::~BonusBlock()
+{
+  delete object;
 }
 
 void
@@ -151,7 +203,7 @@ void
 BonusBlock::try_open()
 {
   if(sprite->get_action_name() == "empty") {
-    SoundManager::get()->play_sound(IDToSound(SND_BRICK));
+    sound_manager->play_sound("brick");
     return;
   }
   
@@ -160,33 +212,31 @@ BonusBlock::try_open()
   switch(contents) {
     case CONTENT_COIN:
       Sector::current()->add_object(new BouncyCoin(get_pos()));
-      player.get_status().incCoins();
+      player.get_status()->incCoins();
       break;
 
     case CONTENT_FIREGROW:
-      if(player.size == SMALL) {
-        SpecialRiser* riser = new SpecialRiser(
-            new GrowUp(get_pos() + Vector(0, -32)));
+      if(player.get_status()->bonus == NO_BONUS) {
+        SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
         sector->add_object(riser);
       } else {
         SpecialRiser* riser = new SpecialRiser(
-            new Flower(get_pos() + Vector(0, -32), Flower::FIREFLOWER));
+            get_pos(), new Flower(Flower::FIREFLOWER));
         sector->add_object(riser);
       }
-      SoundManager::get()->play_sound(IDToSound(SND_UPGRADE));
+      sound_manager->play_sound("upgrade");
       break;
 
     case CONTENT_ICEGROW:
-      if(player.size == SMALL) {
-        SpecialRiser* riser = new SpecialRiser(
-            new GrowUp(get_pos() + Vector(0, -32)));
+      if(player.get_status()->bonus == NO_BONUS) {
+        SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
         sector->add_object(riser);                                            
       } else {
-        SpecialRiser* riser = new SpecialRiser(                               
-            new Flower(get_pos() + Vector(0, -32), Flower::ICEFLOWER));
+        SpecialRiser* riser = new SpecialRiser(
+            get_pos(), new Flower(Flower::ICEFLOWER));
         sector->add_object(riser);
       }      
-      SoundManager::get()->play_sound(IDToSound(SND_UPGRADE));
+      sound_manager->play_sound("upgrade");
       break;
 
     case CONTENT_STAR:
@@ -197,15 +247,22 @@ BonusBlock::try_open()
       sector->add_object(new OneUp(get_pos()));
       break;
 
-    default:
-      assert(false);
+    case CONTENT_CUSTOM:
+      SpecialRiser* riser = new SpecialRiser(get_pos(), object);
+      object = 0;
+      sector->add_object(riser);
+      sound_manager->play_sound("upgrade");
+      break;
+
+    //default:
+      //assert(false);
   }
 
   start_bounce();
   sprite->set_action("empty");
 }
 
-IMPLEMENT_FACTORY(BonusBlock, "bonusblock")
+IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
 
 //---------------------------------------------------------------------------
 
@@ -235,18 +292,18 @@ Brick::try_break(bool playerhit)
   if(sprite->get_action_name() == "empty")
     return;
   
-  SoundManager::get()->play_sound(IDToSound(SND_BRICK));
+  sound_manager->play_sound("brick");
   Sector* sector = Sector::current();
   Player& player = *(sector->player);
   if(coin_counter > 0) {
     sector->add_object(new BouncyCoin(get_pos()));
     coin_counter--;
-    player.get_status().incCoins();
+    player.get_status()->incCoins();
     if(coin_counter == 0)
       sprite->set_action("empty");
     start_bounce();
   } else if(breakable) {
-    if(playerhit && player.size == SMALL) {
+    if(playerhit && !player.is_big()) {
       start_bounce();
       return;
     }
@@ -266,4 +323,5 @@ Brick::try_break(bool playerhit)
   }
 }
 
-//IMPLEMENT_FACTORY(Brick, "brick")
+//IMPLEMENT_FACTORY(Brick, "brick");
+