Replaced Ref and RefCounter with std::shared_ptr<>
[supertux.git] / src / object / block.cpp
index 494d718..1e115d9 100644 (file)
@@ -1,84 +1,92 @@
-//  $Id$
-// 
 //  SuperTux
-//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//  Copyright (C) 2006 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 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 3 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 "sprite/sprite.h"
-#include "sprite/sprite_manager.h"
-#include "video/drawing_context.h"
-#include "lisp/lisp.h"
-#include "gameobjs.h"
-#include "specialriser.h"
-#include "growup.h"
-#include "flower.h"
-#include "oneup.h"
-#include "star.h"
-#include "player_status.h"
-#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;
-static const float EPSILON = .0001;
-
-Block::Block(Sprite* newsprite)
-  : sprite(newsprite), bouncing(false), bounce_dir(0), bounce_offset(0)
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+#include "object/block.hpp"
+
+#include "audio/sound_manager.hpp"
+#include "badguy/badguy.hpp"
+#include "object/broken_brick.hpp"
+#include "object/coin.hpp"
+#include "object/flower.hpp"
+#include "object/growup.hpp"
+#include "object/player.hpp"
+#include "object/portable.hpp"
+#include "supertux/constants.hpp"
+#include "supertux/sector.hpp"
+
+static const float BOUNCY_BRICK_MAX_OFFSET = 8;
+static const float BOUNCY_BRICK_SPEED = 90;
+static const float BUMP_ROTATION_ANGLE = 10;
+
+Block::Block(SpritePtr newsprite) :
+  sprite(newsprite),
+  bouncing(false),
+  breaking(false),
+  bounce_dir(0),
+  bounce_offset(0),
+  original_y(-1)
 {
-  bbox.set_size(32, 32.1);
-  flags |= FLAG_SOLID;
+  bbox.set_size(32, 32.1f);
+  set_group(COLGROUP_STATIC);
+  SoundManager::current()->preload("sounds/upgrade.wav");
+  SoundManager::current()->preload("sounds/brick.wav");
 }
 
 Block::~Block()
 {
-  delete sprite;
 }
 
 HitResponse
-Block::collision(GameObject& other, const CollisionHit& hitdata)
+Block::collision(GameObject& other, const CollisionHit& )
 {
   Player* player = dynamic_cast<Player*> (&other);
   if(player) {
-    // collided from below?
-    if(hitdata.normal.x == 0 && hitdata.normal.y < 0) {
+    if(player->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
       hit(*player);
     }
   }
 
-  if(bouncing) {
+  // only interact with other objects if...
+  //   1) we are bouncing
+  //   2) the object is not portable (either never or not currently)
+  //   3) the object is being hit from below (baguys don't get killed for activating boxes)
+  Portable* portable = dynamic_cast<Portable*> (&other);
+  MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
+  bool is_portable = ((portable != 0) && portable->is_portable());
+  bool hit_mo_from_below = ((moving_object == 0) || (moving_object->get_bbox().get_bottom() < (get_bbox().get_top() + SHIFT_DELTA)));
+  if(bouncing && !is_portable && hit_mo_from_below) {
+
+    // Badguys get killed
     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
     if(badguy) {
       badguy->kill_fall();
     }
+
+    // Coins get collected
     Coin* coin = dynamic_cast<Coin*> (&other);
     if(coin) {
       coin->collect();
     }
+
+    //Eggs get jumped
+    GrowUp* growup = dynamic_cast<GrowUp*> (&other);
+    if(growup) {
+      growup->do_jump();
+    }
+
   }
 
   return FORCE_MOVE;
@@ -89,15 +97,19 @@ Block::update(float elapsed_time)
 {
   if(!bouncing)
     return;
-  
+
   float offset = original_y - get_pos().y;
   if(offset > BOUNCY_BRICK_MAX_OFFSET) {
     bounce_dir = BOUNCY_BRICK_SPEED;
     movement = Vector(0, bounce_dir * elapsed_time);
+    if(breaking){
+      break_me();
+    }
   } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
     movement = Vector(0, offset);
     bounce_dir = 0;
     bouncing = false;
+    sprite->set_angle(0);
   } else {
     movement = Vector(0, bounce_dir * elapsed_time);
   }
@@ -110,218 +122,46 @@ Block::draw(DrawingContext& context)
 }
 
 void
-Block::start_bounce()
+Block::start_bounce(GameObject* hitter)
 {
-  original_y = bbox.p1.y;
+  if(original_y == -1){
+    original_y = bbox.p1.y;
+  }
   bouncing = true;
   bounce_dir = -BOUNCY_BRICK_SPEED;
   bounce_offset = 0;
-}
-
-//---------------------------------------------------------------------------
-
-BonusBlock::BonusBlock(const Vector& pos, int data)
-  : Block(sprite_manager->create("bonusblock")), object(0)
-{
-  bbox.set_pos(pos);
-  sprite->set_action("normal");
-  switch(data) {
-    case 1: contents = CONTENT_COIN; break;
-    case 2: contents = CONTENT_FIREGROW; break;
-    case 3: contents = CONTENT_STAR; break;
-    case 4: contents = CONTENT_1UP; break;
-    case 5: contents = CONTENT_ICEGROW; break;
-    default:
-      std::cerr << "Invalid box contents!\n";
-      contents = CONTENT_COIN;
-      break;
-  }          
-}
-
-BonusBlock::BonusBlock(const lisp::Lisp& lisp)
-  : Block(sprite_manager->create("bonusblock"))
-{
-  Vector pos;
-
-  contents = CONTENT_COIN;
-  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 {
-      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
-BonusBlock::hit(Player& )
-{
-  try_open();
-}
-
-void
-BonusBlock::try_open()
-{
-  if(sprite->get_action_name() == "empty") {
-    sound_manager->play_sound("brick");
-    return;
+  MovingObject* hitter_mo = dynamic_cast<MovingObject*>(hitter);
+  if (hitter_mo) {
+    float center_of_hitter = hitter_mo->get_bbox().get_middle().x;
+    float offset = (get_bbox().get_middle().x - center_of_hitter)*2 / get_bbox().get_width();
+    sprite->set_angle(BUMP_ROTATION_ANGLE*offset);
   }
-  
-  Sector* sector = Sector::current();
-  Player& player = *(sector->player);
-  switch(contents) {
-    case CONTENT_COIN:
-      Sector::current()->add_object(new BouncyCoin(get_pos()));
-      player.get_status()->incCoins();
-      break;
-
-    case CONTENT_FIREGROW:
-      if(player.get_status()->bonus == NO_BONUS) {
-        SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
-        sector->add_object(riser);
-      } else {
-        SpecialRiser* riser = new SpecialRiser(
-            get_pos(), new Flower(Flower::FIREFLOWER));
-        sector->add_object(riser);
-      }
-      sound_manager->play_sound("upgrade");
-      break;
-
-    case CONTENT_ICEGROW:
-      if(player.get_status()->bonus == NO_BONUS) {
-        SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
-        sector->add_object(riser);                                            
-      } else {
-        SpecialRiser* riser = new SpecialRiser(
-            get_pos(), new Flower(Flower::ICEFLOWER));
-        sector->add_object(riser);
-      }      
-      sound_manager->play_sound("upgrade");
-      break;
-
-    case CONTENT_STAR:
-      sector->add_object(new Star(get_pos() + Vector(0, -32)));
-      break;
-
-    case CONTENT_1UP:
-      sector->add_object(new OneUp(get_pos()));
-      break;
-
-    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");
-
-//---------------------------------------------------------------------------
-
-Brick::Brick(const Vector& pos, int data)
-  : Block(sprite_manager->create("brick")), breakable(false),
-    coin_counter(0)
-{
-  bbox.set_pos(pos);
-  if(data == 1)
-    coin_counter = 5;
-  else
-    breakable = true;
 }
 
 void
-Brick::hit(Player& )
+Block::start_break(GameObject* hitter)
 {
-  if(sprite->get_action_name() == "empty")
-    return;
-  
-  try_break(true);
+  start_bounce(hitter);
+  breaking = true;
 }
 
 void
-Brick::try_break(bool playerhit)
+Block::break_me()
 {
-  if(sprite->get_action_name() == "empty")
-    return;
-  
-  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();
-    if(coin_counter == 0)
-      sprite->set_action("empty");
-    start_bounce();
-  } else if(breakable) {
-    if(playerhit && !player.is_big()) {
-      start_bounce();
-      return;
-    }
-
-    sector->add_object(
-        new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
-    sector->add_object(
-        new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
-          Vector(-150, -300)));
-    sector->add_object(
-        new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
-          Vector(100, -400)));
-    sector->add_object(
-        new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
-          Vector(150, -300)));
-    remove_me();
-  }
+  sector->add_object(
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos(), Vector(-100, -400)));
+  sector->add_object(
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(0, 16),
+                                  Vector(-150, -300)));
+  sector->add_object(
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(16, 0),
+                                  Vector(100, -400)));
+  sector->add_object(
+    std::make_shared<BrokenBrick>(sprite->clone(), get_pos() + Vector(16, 16),
+                                  Vector(150, -300)));
+  remove_me();
 }
 
-//IMPLEMENT_FACTORY(Brick, "brick");
-
+/* EOF */