2fb6bc8a72da4dd238b3c2da8f4abf5f90470994
[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 #include <stdexcept>
38
39 BonusBlock::BonusBlock(const Vector& pos, int data) :
40   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), 
41   contents(),
42   object(0)
43 {
44   bbox.set_pos(pos);
45   sprite->set_action("normal");
46   switch(data) {
47     case 1: contents = CONTENT_COIN; break;
48     case 2: contents = CONTENT_FIREGROW; break;
49     case 3: contents = CONTENT_STAR; break;
50     case 4: contents = CONTENT_1UP; break;
51     case 5: contents = CONTENT_ICEGROW; break;
52     default:
53       log_warning << "Invalid box contents" << std::endl;
54       contents = CONTENT_COIN;
55       break;
56   }
57 }
58
59 BonusBlock::BonusBlock(const Reader& lisp) :
60   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
61   contents(),
62   object(0)
63 {
64   Vector pos;
65
66   contents = CONTENT_COIN;
67   lisp::ListIterator iter(&lisp);
68   while(iter.next()) {
69     const std::string& token = iter.item();
70     if(token == "x") {
71       iter.value()->get(pos.x);
72     } else if(token == "y") {
73       iter.value()->get(pos.y);
74     } else if(token == "contents") {
75       std::string contentstring;
76       iter.value()->get(contentstring);
77       if(contentstring == "coin") {
78         contents = CONTENT_COIN;
79       } else if(contentstring == "firegrow") {
80         contents = CONTENT_FIREGROW;
81       } else if(contentstring == "icegrow") {
82         contents = CONTENT_ICEGROW;
83       } else if(contentstring == "star") {
84         contents = CONTENT_STAR;
85       } else if(contentstring == "1up") {
86         contents = CONTENT_1UP;
87       } else if(contentstring == "custom") {
88         contents = CONTENT_CUSTOM;
89       } else {
90         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
91       }
92     } else {
93       if(contents == CONTENT_CUSTOM) {
94         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
95         object = dynamic_cast<MovingObject*> (game_object);
96         if(object == 0)
97           throw std::runtime_error(
98             "Only MovingObjects are allowed inside BonusBlocks");
99       } else {
100         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
101       }
102     }
103   }
104
105   if(contents == CONTENT_CUSTOM && object == 0)
106     throw std::runtime_error("Need to specify content object for custom block");
107
108   bbox.set_pos(pos);
109 }
110
111 BonusBlock::~BonusBlock()
112 {
113   delete object;
114 }
115
116 void
117 BonusBlock::hit(Player & player)
118 {
119   try_open(player);
120 }
121
122 HitResponse
123 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
124
125   Player* player = dynamic_cast<Player*> (&other);
126   if (player) {
127     if (player->does_buttjump) try_open(*player);
128   }
129
130   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
131   if(badguy) {
132     // hit contains no information for collisions with blocks.
133     // Badguy's bottom has to be below the top of the block
134     // SHIFT_DELTA is required to slide over one tile gaps.
135     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
136       try_open(*player);
137     }
138   }
139   Portable* portable = dynamic_cast<Portable*> (&other);
140   if(portable) {
141     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
142     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
143       try_open(*player);
144     }
145   }
146   return Block::collision(other, hit);
147 }
148
149 void
150 BonusBlock::try_open(Player & player)
151 {
152   if(sprite->get_action() == "empty") {
153     sound_manager->play("sounds/brick.wav");
154     return;
155   }
156
157   Sector* sector = Sector::current();
158   assert(sector);
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(sprite->clone(), get_pos(), Vector(-100, -400)));
218   sector->add_object(
219     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
220                     Vector(-150, -300)));
221   sector->add_object(
222     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
223                     Vector(100, -400)));
224   sector->add_object(
225     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
226                     Vector(150, -300)));
227   remove_me();
228 }
229
230 /* EOF */