New Object: rusty trampoline code by rgcjonas, graphics by ctdabomb, see test level...
[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   hit_counter(1)
44 {
45   bbox.set_pos(pos);
46   sprite->set_action("normal");
47   switch(data) {
48     case 1: contents = CONTENT_COIN; break;
49     case 2: contents = CONTENT_FIREGROW; break;
50     case 3: contents = CONTENT_STAR; break;
51     case 4: contents = CONTENT_1UP; break;
52     case 5: contents = CONTENT_ICEGROW; break;
53     default:
54       log_warning << "Invalid box contents" << std::endl;
55       contents = CONTENT_COIN;
56       break;
57   }
58 }
59
60 BonusBlock::BonusBlock(const Reader& lisp) :
61   Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
62   contents(),
63   object(0),
64   hit_counter(1)
65 {
66   Vector pos;
67
68   contents = CONTENT_COIN;
69   lisp::ListIterator iter(&lisp);
70   while(iter.next()) {
71     const std::string& token = iter.item();
72     if(token == "x") {
73       iter.value()->get(pos.x);
74     } else if(token == "y") {
75       iter.value()->get(pos.y);
76     } else if(token == "sprite") {
77       iter.value()->get(sprite_name);
78       sprite = sprite_manager->create(sprite_name);
79     } else if(token == "count") {
80       iter.value()->get(hit_counter);
81     } else if(token == "script") {
82       iter.value()->get(script);
83     } else if(token == "contents") {
84       std::string contentstring;
85       iter.value()->get(contentstring);
86       if(contentstring == "coin") {
87         contents = CONTENT_COIN;
88       } else if(contentstring == "firegrow") {
89         contents = CONTENT_FIREGROW;
90       } else if(contentstring == "icegrow") {
91         contents = CONTENT_ICEGROW;
92       } else if(contentstring == "star") {
93         contents = CONTENT_STAR;
94       } else if(contentstring == "1up") {
95         contents = CONTENT_1UP;
96       } else if(contentstring == "custom") {
97         contents = CONTENT_CUSTOM;
98       } else if(contentstring == "script") {
99         contents = CONTENT_SCRIPT;
100       } else {
101         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
102       }
103     } else {
104       if(contents == CONTENT_CUSTOM) {
105         GameObject* game_object = ObjectFactory::instance().create(token, *(iter.lisp()));
106         object = dynamic_cast<MovingObject*> (game_object);
107         if(object == 0)
108           throw std::runtime_error(
109             "Only MovingObjects are allowed inside BonusBlocks");
110       } else {
111         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
112       }
113     }
114   }
115
116   if(contents == CONTENT_CUSTOM && object == 0)
117     throw std::runtime_error("Need to specify content object for custom block");
118
119   bbox.set_pos(pos);
120 }
121
122 BonusBlock::~BonusBlock()
123 {
124   delete object;
125 }
126
127 void
128 BonusBlock::hit(Player & player)
129 {
130   try_open(&player);
131 }
132
133 HitResponse
134 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
135
136   Player* player = dynamic_cast<Player*> (&other);
137   if (player) {
138     if (player->does_buttjump)
139       try_open(player);
140   }
141
142   BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
143   if(badguy) {
144     // hit contains no information for collisions with blocks.
145     // Badguy's bottom has to be below the top of the block
146     // SHIFT_DELTA is required to slide over one tile gaps.
147     if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
148       try_open(player);
149     }
150   }
151   Portable* portable = dynamic_cast<Portable*> (&other);
152   if(portable) {
153     MovingObject* moving = dynamic_cast<MovingObject*> (&other);
154     if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
155       try_open(player);
156     }
157   }
158   return Block::collision(other, hit);
159 }
160
161 void
162 BonusBlock::try_open(Player *player)
163 {
164   if(sprite->get_action() == "empty") {
165     sound_manager->play("sounds/brick.wav");
166     return;
167   }
168
169   Sector* sector = Sector::current();
170   assert(sector);
171
172   if (player == NULL)
173     player = sector->player;
174   
175   if (player == NULL)
176     return;
177
178   Direction direction = (player->get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
179
180   switch(contents) {
181     case CONTENT_COIN:
182       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
183       player->get_status()->add_coins(1);
184       Sector::current()->get_level()->stats.coins++;
185       break;
186
187     case CONTENT_FIREGROW:
188       if(player->get_status()->bonus == NO_BONUS) {
189         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
190         sector->add_object(riser);
191       } else {
192         SpecialRiser* riser = new SpecialRiser(
193           get_pos(), new Flower(FIRE_BONUS));
194         sector->add_object(riser);
195       }
196       sound_manager->play("sounds/upgrade.wav");
197       break;
198
199     case CONTENT_ICEGROW:
200       if(player->get_status()->bonus == NO_BONUS) {
201         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
202         sector->add_object(riser);
203       } else {
204         SpecialRiser* riser = new SpecialRiser(
205           get_pos(), new Flower(ICE_BONUS));
206         sector->add_object(riser);
207       }
208       sound_manager->play("sounds/upgrade.wav");
209       break;
210
211     case CONTENT_STAR:
212       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
213       break;
214
215     case CONTENT_1UP:
216       sector->add_object(new OneUp(get_pos(), direction));
217       break;
218
219     case CONTENT_CUSTOM:
220       {
221         SpecialRiser* riser = new SpecialRiser(get_pos(), object);
222         object = 0;
223         sector->add_object(riser);
224         sound_manager->play("sounds/upgrade.wav");
225         break;
226       }
227
228     case CONTENT_SCRIPT:
229       if(script != "") {
230         std::istringstream stream(script);
231         Sector::current()->run_script(stream, "powerup-script");
232       }
233       break;
234   }
235
236   start_bounce(player);
237   if(hit_counter <= 0){ //use 0 to allow infinite hits
238   }else if(hit_counter == 1){
239     sprite->set_action("empty");
240   }else{
241     hit_counter--;
242   }
243 }
244
245 void
246 Block::break_me()
247 {
248   Sector* sector = Sector::current();
249   sector->add_object(
250     new BrokenBrick(sprite->clone(), get_pos(), Vector(-100, -400)));
251   sector->add_object(
252     new BrokenBrick(sprite->clone(), get_pos() + Vector(0, 16),
253                     Vector(-150, -300)));
254   sector->add_object(
255     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 0),
256                     Vector(100, -400)));
257   sector->add_object(
258     new BrokenBrick(sprite->clone(), get_pos() + Vector(16, 16),
259                     Vector(150, -300)));
260   remove_me();
261 }
262
263 /* EOF */