Fixed a Segmentation Fault when mr_iceblock was kicked into a brick containing coins...
[supertux.git] / src / object / brick.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #include "object/brick.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "badguy/badguy.hpp"
21 #include "object/bouncy_coin.hpp"
22 #include "object/explosion.hpp"
23 #include "object/flower.hpp"
24 #include "object/icecrusher.hpp"
25 #include "object/player.hpp"
26 #include "object/portable.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "supertux/constants.hpp"
29 #include "supertux/sector.hpp"
30
31 Brick::Brick(const Vector& pos, int data, const std::string& spriteName)
32   : Block(SpriteManager::current()->create(spriteName)), breakable(false),
33     coin_counter(0)
34 {
35   bbox.set_pos(pos);
36   if(data == 1)
37     coin_counter = 5;
38   else
39     breakable = true;
40 }
41
42 void
43 Brick::hit(Player& player)
44 {
45   if(sprite->get_action() == "empty")
46     return;
47
48   try_break(&player);
49 }
50
51 HitResponse
52 Brick::collision(GameObject& other, const CollisionHit& hit_){
53
54   Player* player = dynamic_cast<Player*> (&other);
55   if (player) {
56     if (player->does_buttjump) try_break(player);
57     if (player->is_stone() && player->get_velocity().y >= 280) try_break(player); // stoneform breaks through bricks
58   }
59
60   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
61   if(badguy) {
62     // hit contains no information for collisions with blocks.
63     // Badguy's bottom has to be below the top of the brick
64     // SHIFT_DELTA is required to slide over one tile gaps.
65     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
66       try_break(player);
67     }
68   }
69   Portable* portable = dynamic_cast<Portable*> (&other);
70   if(portable) {
71     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
72     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
73       try_break(player);
74     }
75   }
76   Explosion* explosion = dynamic_cast<Explosion*> (&other);
77   if(explosion && explosion->hurts()) {
78     try_break(player);
79   }
80   IceCrusher* icecrusher = dynamic_cast<IceCrusher*> (&other);
81   if(icecrusher && coin_counter == 0)
82     try_break(player);
83   return Block::collision(other, hit_);
84 }
85
86 void
87 Brick::try_break(Player* player)
88 {
89   if(sprite->get_action() == "empty")
90     return;
91
92   SoundManager::current()->play("sounds/brick.wav");
93   Sector* sector = Sector::current();
94   Player& player_one = *(sector->player);
95   if(coin_counter > 0 ){
96     sector->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
97     coin_counter--;
98     player_one.get_status()->add_coins(1);
99     if(coin_counter == 0)
100       sprite->set_action("empty");
101     start_bounce(player);
102   } else if(breakable) {
103     if(player){
104       if(player->is_big()){
105         start_break(player);
106         return;
107       } else {
108         start_bounce(player);
109         return;
110       }
111     }
112     break_me();
113   }
114 }
115
116 //IMPLEMENT_FACTORY(Brick, "brick");
117
118 /* EOF */