Replaced Ref and RefCounter with std::shared_ptr<>
[supertux.git] / src / object / block.cpp
index c75c990..1e115d9 100644 (file)
-#include <config.h>
-
-#include "block.h"
-#include "resources.h"
-#include "player.h"
-#include "sector.h"
-#include "special/sprite.h"
-#include "special/sprite_manager.h"
-#include "video/drawing_context.h"
-#include "gameobjs.h"
-#include "specialriser.h"
-#include "growup.h"
-#include "flower.h"
-#include "oneup.h"
-#include "star.h"
-
-static const float BOUNCY_BRICK_MAX_OFFSET=8;
-static const float BOUNCY_BRICK_SPEED=90;
-static const float EPSILON = .0001;
-
-Block::Block(const Vector& pos, Sprite* newsprite)
-  : sprite(newsprite), bouncing(false), bounce_dir(0), bounce_offset(0)
+//  SuperTux
+//  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 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, 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_pos(pos);
-  bbox.set_size(32, 32);
-  flags |= FLAG_SOLID;
-  original_y = pos.y;
+  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& )
 {
-  // TODO kill badguys when bumping them...
-  
   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);
     }
   }
 
+  // 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;
 }
 
 void
-Block::action(float elapsed_time)
+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);
   }
@@ -74,123 +122,46 @@ Block::draw(DrawingContext& context)
 }
 
 void
-Block::start_bounce()
+Block::start_bounce(GameObject* hitter)
 {
+  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 newdata)
-  : Block(pos, sprite_manager->create("bonusblock")), data(newdata)
-{
-  sprite->set_action("default");
-}
 
-void
-BonusBlock::hit(Player& player)
-{
-  if(sprite->get_action_name() == "empty") {
-    SoundManager::get()->play_sound(IDToSound(SND_BRICK));
-    return;
-  }
-  
-  Sector* sector = Sector::current();
-  switch(data) {
-    case 1: // coin
-      Sector::current()->add_object(new BouncyCoin(get_pos()));
-      player.get_status().incCoins();
-      break;
-
-    case 2: // grow/fireflower
-      if(player.size == SMALL) {
-        SpecialRiser* riser = new SpecialRiser(
-            new GrowUp(get_pos() + Vector(0, -32)));
-        sector->add_object(riser);
-      } else {
-        SpecialRiser* riser = new SpecialRiser(
-            new Flower(get_pos() + Vector(0, -32), Flower::FIREFLOWER));
-        sector->add_object(riser);
-      }
-      SoundManager::get()->play_sound(IDToSound(SND_UPGRADE));
-      break;
-
-    case 5: // grow/iceflower
-      if(player.size == SMALL) {
-        SpecialRiser* riser = new SpecialRiser(
-            new GrowUp(get_pos() + Vector(0, -32)));
-        sector->add_object(riser);                                            
-      } else {
-        SpecialRiser* riser = new SpecialRiser(                               
-            new Flower(get_pos() + Vector(0, -32), Flower::ICEFLOWER));
-        sector->add_object(riser);
-      }      
-      SoundManager::get()->play_sound(IDToSound(SND_UPGRADE));
-      break;
-
-    case 3: // star
-      sector->add_object(new Star(get_pos() + Vector(0, -32)));
-      break;
-
-    case 4: // 1up
-      sector->add_object(new OneUp(get_pos()));
-      break;
-
-    default:
-      assert(false);
+  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);
   }
-
-  start_bounce();
-  sprite->set_action("empty");
 }
 
-//---------------------------------------------------------------------------
-
-Brick::Brick(const Vector& pos, int data)
-  : Block(pos, sprite_manager->create("brick")), breakable(false),
-    coin_counter(0)
+void
+Block::start_break(GameObject* hitter)
 {
-  if(data == 1)
-    coin_counter = 5;
-  else
-    breakable = true;
+  start_bounce(hitter);
+  breaking = true;
 }
 
 void
-Brick::hit(Player& player)
+Block::break_me()
 {
-  if(sprite->get_action_name() == "empty")
-    return;
-  
-  SoundManager::get()->play_sound(IDToSound(SND_BRICK));
   Sector* sector = Sector::current();
-  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(player.size == SMALL) {
-      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();
 }
 
+/* EOF */