When creating a brick object to handle tiles with attribute brick the iced image...
[supertux.git] / src / object / brick.cpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmx.de>
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/flower.hpp"
22 #include "object/bouncy_coin.hpp"
23 #include "object/player.hpp"
24 #include "object/portable.hpp"
25 #include "sprite/sprite_manager.hpp"
26 #include "supertux/constants.hpp"
27 #include "supertux/sector.hpp"
28
29 Brick::Brick(const Vector& pos, int data, const std::string& spriteName)
30   : Block(sprite_manager->create(spriteName)), breakable(false),
31     coin_counter(0)
32 {
33   bbox.set_pos(pos);
34   if(data == 1)
35     coin_counter = 5;
36   else
37     breakable = true;
38 }
39
40 void
41 Brick::hit(Player& player)
42 {
43   if(sprite->get_action() == "empty")
44     return;
45
46   try_break(&player);
47 }
48
49 HitResponse
50 Brick::collision(GameObject& other, const CollisionHit& hit){
51
52   Player* player = dynamic_cast<Player*> (&other);
53   if (player) {
54     if (player->does_buttjump) try_break(player);
55   }
56
57   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
58   if(badguy) {
59     // hit contains no information for collisions with blocks.
60     // Badguy's bottom has to be below the top of the brick
61     // SHIFT_DELTA is required to slide over one tile gaps.
62     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
63       try_break(player);
64     }
65   }
66   Portable* portable = dynamic_cast<Portable*> (&other);
67   if(portable) {
68     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
69     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
70       try_break(player);
71     }
72   }
73   return Block::collision(other, hit);
74 }
75
76 void
77 Brick::try_break(Player* player)
78 {
79   if(sprite->get_action() == "empty")
80     return;
81
82   sound_manager->play("sounds/brick.wav");
83   Sector* sector = Sector::current();
84   Player& player_one = *(sector->player);
85   if(coin_counter > 0) {
86     sector->add_object(new BouncyCoin(get_pos(),true));
87     coin_counter--;
88     player_one.get_status()->add_coins(1);
89     if(coin_counter == 0)
90       sprite->set_action("empty");
91     start_bounce(player);
92   } else if(breakable) {
93     if(player){
94       if(player->is_big()){
95         start_break(player);
96         return;
97       } else {
98         start_bounce(player);
99         return;
100       }
101     }
102     break_me();
103   }
104 }
105
106 //IMPLEMENT_FACTORY(Brick, "brick");
107
108 /* EOF */