fc48dd53737e9604a26cdc4e14ea9122910406ab
[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/player.hpp"
25 #include "object/portable.hpp"
26 #include "sprite/sprite_manager.hpp"
27 #include "supertux/constants.hpp"
28 #include "supertux/sector.hpp"
29
30 Brick::Brick(const Vector& pos, int data, const std::string& spriteName)
31   : Block(SpriteManager::current()->create(spriteName)), breakable(false),
32     coin_counter(0)
33 {
34   bbox.set_pos(pos);
35   if(data == 1)
36     coin_counter = 5;
37   else
38     breakable = true;
39 }
40
41 void
42 Brick::hit(Player& player)
43 {
44   if(sprite->get_action() == "empty")
45     return;
46
47   try_break(&player);
48 }
49
50 HitResponse
51 Brick::collision(GameObject& other, const CollisionHit& hit_){
52
53   Player* player = dynamic_cast<Player*> (&other);
54   if (player) {
55     if (player->does_buttjump) try_break(player);
56     if (player->is_stone() && player->get_velocity().y >= 280) try_break(player); // stoneform breaks through bricks
57   }
58
59   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
60   if(badguy) {
61     // hit contains no information for collisions with blocks.
62     // Badguy's bottom has to be below the top of the brick
63     // SHIFT_DELTA is required to slide over one tile gaps.
64     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
65       try_break(player);
66     }
67   }
68   Portable* portable = dynamic_cast<Portable*> (&other);
69   if(portable) {
70     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
71     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
72       try_break(player);
73     }
74   }
75   Explosion* explosion = dynamic_cast<Explosion*> (&other);
76   if(explosion && explosion->hurts()) {
77     try_break(player);
78   }
79   return Block::collision(other, hit_);
80 }
81
82 void
83 Brick::try_break(Player* player)
84 {
85   if(sprite->get_action() == "empty")
86     return;
87
88   SoundManager::current()->play("sounds/brick.wav");
89   Sector* sector = Sector::current();
90   Player& player_one = *(sector->player);
91   if(coin_counter > 0 && !player->is_stone()) {
92     sector->add_object(std::make_shared<BouncyCoin>(get_pos(), true));
93     coin_counter--;
94     player_one.get_status()->add_coins(1);
95     if(coin_counter == 0)
96       sprite->set_action("empty");
97     start_bounce(player);
98   } else if(breakable) {
99     if(player){
100       if(player->is_big()){
101         start_break(player);
102         return;
103       } else {
104         start_bounce(player);
105         return;
106       }
107     }
108     break_me();
109   }
110 }
111
112 //IMPLEMENT_FACTORY(Brick, "brick");
113
114 /* EOF */