2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #include "object/block.hpp"
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"
38 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
39 static const float BOUNCY_BRICK_SPEED = 90;
40 static const float EPSILON = .0001f;
41 static const float BUMP_ROTATION_ANGLE = 10;
43 Block::Block(std::auto_ptr<Sprite> newsprite) :
51 bbox.set_size(32, 32.1f);
52 set_group(COLGROUP_STATIC);
53 sound_manager->preload("sounds/upgrade.wav");
54 sound_manager->preload("sounds/brick.wav");
62 Block::collision(GameObject& other, const CollisionHit& )
64 Player* player = dynamic_cast<Player*> (&other);
66 if(player->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
71 // only interact with other objects if...
73 // 2) the object is not portable (either never or not currently)
74 // 3) the object is being hit from below (baguys don't get killed for activating boxes)
75 Portable* portable = dynamic_cast<Portable*> (&other);
76 MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
77 bool is_portable = ((portable != 0) && portable->is_portable());
78 bool hit_mo_from_below = ((moving_object == 0) || (moving_object->get_bbox().get_bottom() < (get_bbox().get_top() + SHIFT_DELTA)));
79 if(bouncing && !is_portable && hit_mo_from_below) {
82 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
87 // Coins get collected
88 Coin* coin = dynamic_cast<Coin*> (&other);
94 GrowUp* growup = dynamic_cast<GrowUp*> (&other);
105 Block::update(float elapsed_time)
110 float offset = original_y - get_pos().y;
111 if(offset > BOUNCY_BRICK_MAX_OFFSET) {
112 bounce_dir = BOUNCY_BRICK_SPEED;
113 movement = Vector(0, bounce_dir * elapsed_time);
117 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
118 movement = Vector(0, offset);
121 sprite->set_angle(0);
123 movement = Vector(0, bounce_dir * elapsed_time);
128 Block::draw(DrawingContext& context)
130 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
134 Block::start_bounce(GameObject* hitter)
136 if(original_y == -1){
137 original_y = bbox.p1.y;
140 bounce_dir = -BOUNCY_BRICK_SPEED;
143 MovingObject* hitter_mo = dynamic_cast<MovingObject*>(hitter);
145 float center_of_hitter = hitter_mo->get_bbox().get_middle().x;
146 float offset = (get_bbox().get_middle().x - center_of_hitter)*2 / get_bbox().get_width();
147 sprite->set_angle(BUMP_ROTATION_ANGLE*offset);
152 Block::start_break(GameObject* hitter)
154 start_bounce(hitter);
158 //---------------------------------------------------------------------------
160 BonusBlock::BonusBlock(const Vector& pos, int data) :
161 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")),
165 sprite->set_action("normal");
167 case 1: contents = CONTENT_COIN; break;
168 case 2: contents = CONTENT_FIREGROW; break;
169 case 3: contents = CONTENT_STAR; break;
170 case 4: contents = CONTENT_1UP; break;
171 case 5: contents = CONTENT_ICEGROW; break;
173 log_warning << "Invalid box contents" << std::endl;
174 contents = CONTENT_COIN;
179 BonusBlock::BonusBlock(const Reader& lisp) :
180 Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
184 contents = CONTENT_COIN;
185 lisp::ListIterator iter(&lisp);
187 const std::string& token = iter.item();
189 iter.value()->get(pos.x);
190 } else if(token == "y") {
191 iter.value()->get(pos.y);
192 } else if(token == "contents") {
193 std::string contentstring;
194 iter.value()->get(contentstring);
195 if(contentstring == "coin") {
196 contents = CONTENT_COIN;
197 } else if(contentstring == "firegrow") {
198 contents = CONTENT_FIREGROW;
199 } else if(contentstring == "icegrow") {
200 contents = CONTENT_ICEGROW;
201 } else if(contentstring == "star") {
202 contents = CONTENT_STAR;
203 } else if(contentstring == "1up") {
204 contents = CONTENT_1UP;
205 } else if(contentstring == "custom") {
206 contents = CONTENT_CUSTOM;
208 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
211 if(contents == CONTENT_CUSTOM) {
212 GameObject* game_object = create_object(token, *(iter.lisp()));
213 object = dynamic_cast<MovingObject*> (game_object);
215 throw std::runtime_error(
216 "Only MovingObjects are allowed inside BonusBlocks");
218 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
223 if(contents == CONTENT_CUSTOM && object == 0)
224 throw std::runtime_error("Need to specify content object for custom block");
229 BonusBlock::~BonusBlock()
235 BonusBlock::hit(Player& )
241 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
243 Player* player = dynamic_cast<Player*> (&other);
245 if (player->does_buttjump) try_open();
248 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
250 // hit contains no information for collisions with blocks.
251 // Badguy's bottom has to be below the top of the block
252 // SHIFT_DELTA is required to slide over one tile gaps.
253 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
257 Portable* portable = dynamic_cast<Portable*> (&other);
259 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
260 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
264 return Block::collision(other, hit);
268 BonusBlock::try_open()
270 if(sprite->get_action() == "empty") {
271 sound_manager->play("sounds/brick.wav");
275 Sector* sector = Sector::current();
277 assert(sector->player);
278 Player& player = *(sector->player);
279 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
283 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
284 player.get_status()->add_coins(1);
285 Sector::current()->get_level()->stats.coins++;
288 case CONTENT_FIREGROW:
289 if(player.get_status()->bonus == NO_BONUS) {
290 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
291 sector->add_object(riser);
293 SpecialRiser* riser = new SpecialRiser(
294 get_pos(), new Flower(FIRE_BONUS));
295 sector->add_object(riser);
297 sound_manager->play("sounds/upgrade.wav");
300 case CONTENT_ICEGROW:
301 if(player.get_status()->bonus == NO_BONUS) {
302 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
303 sector->add_object(riser);
305 SpecialRiser* riser = new SpecialRiser(
306 get_pos(), new Flower(ICE_BONUS));
307 sector->add_object(riser);
309 sound_manager->play("sounds/upgrade.wav");
313 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
317 sector->add_object(new OneUp(get_pos(), direction));
321 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
323 sector->add_object(riser);
324 sound_manager->play("sounds/upgrade.wav");
328 start_bounce(&player);
329 sprite->set_action("empty");
335 Sector* sector = Sector::current();
337 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos(), Vector(-100, -400)));
339 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(0, 16),
340 Vector(-150, -300)));
342 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 0),
345 new BrokenBrick(std::auto_ptr<Sprite>(new Sprite(*sprite)), get_pos() + Vector(16, 16),
350 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
352 //---------------------------------------------------------------------------
354 Brick::Brick(const Vector& pos, int data)
355 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
366 Brick::hit(Player& player)
368 if(sprite->get_action() == "empty")
375 Brick::collision(GameObject& other, const CollisionHit& hit){
377 Player* player = dynamic_cast<Player*> (&other);
379 if (player->does_buttjump) try_break();
382 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
384 // hit contains no information for collisions with blocks.
385 // Badguy's bottom has to be below the top of the brick
386 // SHIFT_DELTA is required to slide over one tile gaps.
387 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + SHIFT_DELTA ) ){
391 Portable* portable = dynamic_cast<Portable*> (&other);
393 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
394 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA) {
398 return Block::collision(other, hit);
402 Brick::try_break(Player* player)
404 if(sprite->get_action() == "empty")
407 sound_manager->play("sounds/brick.wav");
408 Sector* sector = Sector::current();
409 Player& player_one = *(sector->player);
410 if(coin_counter > 0) {
411 sector->add_object(new BouncyCoin(get_pos(),true));
413 player_one.get_status()->add_coins(1);
414 if(coin_counter == 0)
415 sprite->set_action("empty");
416 start_bounce(player);
417 } else if(breakable) {
419 if(player->is_big()){
423 start_bounce(player);
431 //IMPLEMENT_FACTORY(Brick, "brick");