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