Resolves issue 0000304: Blocks don't return to original positions
[supertux.git] / src / object / block.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <config.h>
21
22 #include "block.hpp"
23 #include "log.hpp"
24
25 #include <stdexcept>
26
27 #include "resources.hpp"
28 #include "player.hpp"
29 #include "sector.hpp"
30 #include "sprite/sprite.hpp"
31 #include "sprite/sprite_manager.hpp"
32 #include "video/drawing_context.hpp"
33 #include "lisp/lisp.hpp"
34 #include "gameobjs.hpp"
35 #include "portable.hpp"
36 #include "specialriser.hpp"
37 #include "growup.hpp"
38 #include "flower.hpp"
39 #include "oneup.hpp"
40 #include "star.hpp"
41 #include "player_status.hpp"
42 #include "badguy/badguy.hpp"
43 #include "coin.hpp"
44 #include "object_factory.hpp"
45 #include "lisp/list_iterator.hpp"
46 #include "object_factory.hpp"
47 #include "level.hpp"
48
49 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
50 static const float BOUNCY_BRICK_SPEED = 90;
51 static const float EPSILON = .0001f;
52 static const float BUMP_ROTATION_ANGLE = 10;
53
54 Block::Block(Sprite* newsprite)
55   : sprite(newsprite), bouncing(false), breaking(false), bounce_dir(0), bounce_offset(0), original_y(-1)
56 {
57   bbox.set_size(32, 32.1f);
58   set_group(COLGROUP_STATIC);
59   sound_manager->preload("sounds/upgrade.wav");
60   sound_manager->preload("sounds/brick.wav");
61 }
62
63 Block::~Block()
64 {
65   delete sprite;
66 }
67
68 HitResponse
69 Block::collision(GameObject& other, const CollisionHit& )
70 {
71   Player* player = dynamic_cast<Player*> (&other);
72   if(player) {
73     if(player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
74       hit(*player);
75     }
76   }
77
78   // only interact with other objects if...
79   //   1) we are bouncing
80   // and
81   //   2) the object is not portable (either never or not currently)
82   Portable* portable = dynamic_cast<Portable*> (&other);
83   if(bouncing && (portable == 0 || (!portable->is_portable()))) {
84
85     // Badguys get killed
86     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
87     if(badguy) {
88       badguy->kill_fall();
89     }
90
91     // Coins get collected
92     Coin* coin = dynamic_cast<Coin*> (&other);
93     if(coin) {
94       coin->collect();
95     }
96
97   }
98
99   return SOLID;
100 }
101
102 void
103 Block::update(float elapsed_time)
104 {
105   if(!bouncing)
106     return;
107
108   float offset = original_y - get_pos().y;
109   if(offset > BOUNCY_BRICK_MAX_OFFSET) {
110     bounce_dir = BOUNCY_BRICK_SPEED;
111     movement = Vector(0, bounce_dir * elapsed_time);
112     if(breaking){
113       break_me();
114     }
115   } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
116     movement = Vector(0, offset);
117     bounce_dir = 0;
118     bouncing = false;
119     sprite->set_angle(0);
120   } else {
121     movement = Vector(0, bounce_dir * elapsed_time);
122   }
123 }
124
125 void
126 Block::draw(DrawingContext& context)
127 {
128   sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
129 }
130
131 void
132 Block::start_bounce(float center_of_hitter)
133 {
134   if(original_y == -1){
135     original_y = bbox.p1.y;
136   }
137   bouncing = true;
138   bounce_dir = -BOUNCY_BRICK_SPEED;
139   bounce_offset = 0;
140
141   float offset = (get_bbox().get_middle().x - center_of_hitter)*2 / get_bbox().get_width();
142   sprite->set_angle(BUMP_ROTATION_ANGLE*offset);
143 }
144
145 void
146 Block::start_break(float center_of_hitter)
147 {
148   start_bounce(center_of_hitter);
149   breaking = true;
150 }
151
152 //---------------------------------------------------------------------------
153
154 BonusBlock::BonusBlock(const Vector& pos, int data)
155   : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
156 {
157   bbox.set_pos(pos);
158   sprite->set_action("normal");
159   switch(data) {
160     case 1: contents = CONTENT_COIN; break;
161     case 2: contents = CONTENT_FIREGROW; break;
162     case 3: contents = CONTENT_STAR; break;
163     case 4: contents = CONTENT_1UP; break;
164     case 5: contents = CONTENT_ICEGROW; break;
165     default:
166       log_warning << "Invalid box contents" << std::endl;
167       contents = CONTENT_COIN;
168       break;
169   }
170 }
171
172 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
173   : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
174 {
175   Vector pos;
176
177   contents = CONTENT_COIN;
178   lisp::ListIterator iter(&lisp);
179   while(iter.next()) {
180     const std::string& token = iter.item();
181     if(token == "x") {
182       iter.value()->get(pos.x);
183     } else if(token == "y") {
184       iter.value()->get(pos.y);
185     } else if(token == "contents") {
186       std::string contentstring;
187       iter.value()->get(contentstring);
188       if(contentstring == "coin") {
189         contents = CONTENT_COIN;
190       } else if(contentstring == "firegrow") {
191         contents = CONTENT_FIREGROW;
192       } else if(contentstring == "icegrow") {
193         contents = CONTENT_ICEGROW;
194       } else if(contentstring == "star") {
195         contents = CONTENT_STAR;
196       } else if(contentstring == "1up") {
197         contents = CONTENT_1UP;
198       } else if(contentstring == "custom") {
199         contents = CONTENT_CUSTOM;
200       } else {
201         log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
202       }
203     } else {
204       if(contents == CONTENT_CUSTOM) {
205         GameObject* game_object = create_object(token, *(iter.lisp()));
206         object = dynamic_cast<MovingObject*> (game_object);
207         if(object == 0)
208           throw std::runtime_error(
209             "Only MovingObjects are allowed inside BonusBlocks");
210       } else {
211         log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
212       }
213     }
214   }
215
216   if(contents == CONTENT_CUSTOM && object == 0)
217     throw std::runtime_error("Need to specify content object for custom block");
218
219   bbox.set_pos(pos);
220 }
221
222 BonusBlock::~BonusBlock()
223 {
224   delete object;
225 }
226
227 void
228 BonusBlock::hit(Player& )
229 {
230   try_open();
231 }
232
233 HitResponse
234 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
235     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
236     if(badguy) {
237       // hit contains no information for collisions with blocks.
238       // Badguy's bottom has to be below the top of the bonusblock
239       // +7 is required to slide over one tile gaps.
240       if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
241         try_open();
242       }
243     }
244     Portable* portable = dynamic_cast<Portable*> (&other);
245     if(portable) {
246       MovingObject* moving = dynamic_cast<MovingObject*> (&other);
247       if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
248         try_open();
249       }
250     }
251     return Block::collision(other, hit);
252 }
253
254 void
255 BonusBlock::try_open()
256 {
257   if(sprite->get_action() == "empty") {
258     sound_manager->play("sounds/brick.wav");
259     return;
260   }
261
262   Sector* sector = Sector::current();
263   assert(sector);
264   assert(sector->player);
265   Player& player = *(sector->player);
266   Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
267
268   switch(contents) {
269     case CONTENT_COIN:
270       Sector::current()->add_object(new BouncyCoin(get_pos(), true));
271       player.get_status()->add_coins(1);
272       Sector::current()->get_level()->stats.coins++;
273       break;
274
275     case CONTENT_FIREGROW:
276       if(player.get_status()->bonus == NO_BONUS) {
277         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
278         sector->add_object(riser);
279       } else {
280         SpecialRiser* riser = new SpecialRiser(
281             get_pos(), new Flower(FIRE_BONUS));
282         sector->add_object(riser);
283       }
284       sound_manager->play("sounds/upgrade.wav");
285       break;
286
287     case CONTENT_ICEGROW:
288       if(player.get_status()->bonus == NO_BONUS) {
289         SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
290         sector->add_object(riser);
291       } else {
292         SpecialRiser* riser = new SpecialRiser(
293             get_pos(), new Flower(ICE_BONUS));
294         sector->add_object(riser);
295       }
296       sound_manager->play("sounds/upgrade.wav");
297       break;
298
299     case CONTENT_STAR:
300       sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
301       break;
302
303     case CONTENT_1UP:
304       sector->add_object(new OneUp(get_pos(), direction));
305       break;
306
307     case CONTENT_CUSTOM:
308       SpecialRiser* riser = new SpecialRiser(get_pos(), object);
309       object = 0;
310       sector->add_object(riser);
311       sound_manager->play("sounds/upgrade.wav");
312       break;
313   }
314
315   start_bounce(player.get_bbox().get_middle().x);
316   sprite->set_action("empty");
317 }
318
319 void
320 Block::break_me()
321 {
322   Sector* sector = Sector::current();
323   sector->add_object(
324       new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
325   sector->add_object(
326       new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
327         Vector(-150, -300)));
328   sector->add_object(
329       new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
330         Vector(100, -400)));
331   sector->add_object(
332       new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
333         Vector(150, -300)));
334   remove_me();
335 }
336
337 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
338
339 //---------------------------------------------------------------------------
340
341 Brick::Brick(const Vector& pos, int data)
342   : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
343     coin_counter(0)
344 {
345   bbox.set_pos(pos);
346   if(data == 1)
347     coin_counter = 5;
348   else
349     breakable = true;
350 }
351
352 void
353 Brick::hit(Player& player)
354 {
355   if(sprite->get_action() == "empty")
356     return;
357
358   try_break(&player);
359 }
360
361 HitResponse
362 Brick::collision(GameObject& other, const CollisionHit& hit){
363
364     Player* player = dynamic_cast<Player*> (&other);
365     if (player) {
366       if (player->does_buttjump) try_break();
367     }
368
369     BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
370     if(badguy) {
371       // hit contains no information for collisions with blocks.
372       // Badguy's bottom has to be below the top of the brick
373       // +7 is required to slide over one tile gaps.
374       if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
375         try_break();
376       }
377     }
378     Portable* portable = dynamic_cast<Portable*> (&other);
379     if(portable) {
380       MovingObject* moving = dynamic_cast<MovingObject*> (&other);
381       if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
382         try_break();
383       }
384     }
385    return Block::collision(other, hit);
386 }
387
388 void
389 Brick::try_break(Player* player)
390 {
391   if(sprite->get_action() == "empty")
392     return;
393
394   sound_manager->play("sounds/brick.wav");
395   Sector* sector = Sector::current();
396   Player& player_one = *(sector->player);
397   if(coin_counter > 0) {
398     sector->add_object(new BouncyCoin(get_pos(),true));
399     coin_counter--;
400     player_one.get_status()->add_coins(1);
401     if(coin_counter == 0)
402       sprite->set_action("empty");
403     start_bounce(player->get_bbox().get_middle().x);
404   } else if(breakable) {
405     if(player){
406       if(player->is_big()){
407         start_break(player->get_bbox().get_middle().x);
408         return;
409       } else {
410         start_bounce(player->get_bbox().get_middle().x);
411         return;
412       }
413     }
414    break_me();
415   }
416 }
417
418 //IMPLEMENT_FACTORY(Brick, "brick");