55481d7e0863eef849c7183facb6b3c1d5d89ebe
[supertux.git] / src / object / bonus_block.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/bonus_block.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/flower.hpp"
24 #include "object/bouncy_coin.hpp"
25 #include "object/growup.hpp"
26 #include "object/oneup.hpp"
27 #include "object/player.hpp"
28 #include "object/portable.hpp"
29 #include "object/specialriser.hpp"
30 #include "object/star.hpp"
31 #include "sprite/sprite_manager.hpp"
32 #include "supertux/constants.hpp"
33 #include "supertux/level.hpp"
34 #include "supertux/object_factory.hpp"
35 #include "supertux/sector.hpp"
36
37 BonusBlock::BonusBlock(const Vector& pos, int data) :
38   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
39   contents(),
40   object(0)
41 {
42   bbox.set_pos(pos);
43   sprite->set_action("normal");
44   switch(data) {
45     case 1: contents = CONTENT_COIN; break;
46     case 2: contents = CONTENT_FIREGROW; break;
47     case 3: contents = CONTENT_STAR; break;
48     case 4: contents = CONTENT_1UP; break;
49     case 5: contents = CONTENT_ICEGROW; break;
50     default:
51       log_warning << "Invalid box contents" << std::endl;
52       contents = CONTENT_COIN;
53       break;
54   }
55 }
56
57 BonusBlock::BonusBlock(const Reader& lisp) :
58   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
59   contents(),
60   object(0)
61 {
62   Vector pos;
63
64   contents = CONTENT_COIN;
65   lisp::ListIterator iter(&lisp);
66   while(iter.next()) {
67     const std::string& token = iter.item();
68     if(token == "x") {
69       iter.value()->get(pos.x);
70     } else if(token == "y") {
71       iter.value()->get(pos.y);
72     } else if(token == "contents") {
73       std::string contentstring;
74       iter.value()->get(contentstring);
75       if(contentstring == "coin") {
76         contents = CONTENT_COIN;
77       } else if(contentstring == "firegrow") {
78         contents = CONTENT_FIREGROW;
79       } else if(contentstring == "icegrow") {
80         contents = CONTENT_ICEGROW;
81       } else if(contentstring == "star") {
82         contents = CONTENT_STAR;
83       } else if(contentstring == "1up") {
84         contents = CONTENT_1UP;
85       } else if(contentstring == "custom") {
86         contents = CONTENT_CUSTOM;
87       } else {
88         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
89       }
90     } else {
91       if(contents == CONTENT_CUSTOM) {
92         GameObject* game_object = create_object(token, *(iter.lisp()));
93         object = dynamic_cast<MovingObject*> (game_object);
94         if(object == 0)
95           throw std::runtime_error(
96             "Only MovingObjects are allowed inside BonusBlocks");
97       } else {
98         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
99       }
100     }
101   }
102
103   if(contents == CONTENT_CUSTOM && object == 0)
104     throw std::runtime_error("Need to specify content object for custom block");
105
106   bbox.set_pos(pos);
107 }
108
109 BonusBlock::~BonusBlock()
110 {
111   delete object;
112 }
113
114 void
115 BonusBlock::hit(Player& )
116 {
117   try_open();
118 }
119
120 HitResponse
121 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
122
123   Player* player = dynamic_cast<Player*> (&other);
124   if (player) {
125     if (player->does_buttjump) try_open();
126   }
127
128   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
129   if(badguy) {
130     // hit contains no information for collisions with blocks.
131     // Badguy's bottom has to be below the top of the block
132     // SHIFT_DELTA is required to slide over one tile gaps.
133     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
134       try_open();
135     }
136   }
137   Portable* portable = dynamic_cast<Portable*> (&other);
138   if(portable) {
139     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
140     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
141       try_open();
142     }
143   }
144   return Block::collision(other, hit);
145 }
146
147 void
148 BonusBlock::try_open()
149 {
150   if(sprite->get_action() == "empty") {
151     sound_manager->play("sounds/brick.wav");
152     return;
153   }
154
155   Sector* sector = Sector::current();
156   assert(sector);
157   assert(sector->player);
158   Player& player = *(sector->player);
159   Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
160
161   switch(contents) {
162     case CONTENT_COIN:
163       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
164       player.get_status()->add_coins(1);
165       Sector::current()->get_level()->stats.coins++;
166       break;
167
168     case CONTENT_FIREGROW:
169       if(player.get_status()->bonus == NO_BONUS) {
170         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
171         sector->add_object(riser);
172       } else {
173         SpecialRiser* riser = new SpecialRiser(
174           get_pos(), new Flower(FIRE_BONUS));
175         sector->add_object(riser);
176       }
177       sound_manager->play("sounds/upgrade.wav");
178       break;
179
180     case CONTENT_ICEGROW:
181       if(player.get_status()->bonus == NO_BONUS) {
182         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
183         sector->add_object(riser);
184       } else {
185         SpecialRiser* riser = new SpecialRiser(
186           get_pos(), new Flower(ICE_BONUS));
187         sector->add_object(riser);
188       }
189       sound_manager->play("sounds/upgrade.wav");
190       break;
191
192     case CONTENT_STAR:
193       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
194       break;
195
196     case CONTENT_1UP:
197       sector->add_object(new OneUp(get_pos(), direction));
198       break;
199
200     case CONTENT_CUSTOM:
201       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
202       object = 0;
203       sector->add_object(riser);
204       sound_manager->play("sounds/upgrade.wav");
205       break;
206   }
207
208   start_bounce(&player);
209   sprite->set_action("empty");
210 }
211
212 void
213 Block::break_me()
214 {
215   Sector* sector = Sector::current();
216   sector->add_object(
217     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos(), Vector(-100, -400)));
218   sector->add_object(
219     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(0, 16),
220                     Vector(-150, -300)));
221   sector->add_object(
222     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 0),
223                     Vector(100, -400)));
224   sector->add_object(
225     new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 16),
226                     Vector(150, -300)));
227   remove_me();
228 }
229
230 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
231
232 /* EOF */