4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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.
27 #include "resources.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"
41 #include "player_status.hpp"
42 #include "badguy/badguy.hpp"
44 #include "object_factory.hpp"
45 #include "lisp/list_iterator.hpp"
46 #include "object_factory.hpp"
49 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
50 static const float BOUNCY_BRICK_SPEED = 90;
51 static const float EPSILON = .0001f;
53 Block::Block(Sprite* newsprite)
54 : sprite(newsprite), bouncing(false), breaking(false), bounce_dir(0), bounce_offset(0)
56 bbox.set_size(32, 32.1f);
57 set_group(COLGROUP_STATIC);
58 sound_manager->preload("sounds/upgrade.wav");
59 sound_manager->preload("sounds/brick.wav");
68 Block::collision(GameObject& other, const CollisionHit& )
70 Player* player = dynamic_cast<Player*> (&other);
72 if(player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
77 // only interact with other objects if...
80 // 2) the object is not portable (either never or not currently)
81 Portable* portable = dynamic_cast<Portable*> (&other);
82 if(bouncing && (portable == 0 || (!portable->is_portable()))) {
85 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
90 // Coins get collected
91 Coin* coin = dynamic_cast<Coin*> (&other);
102 Block::update(float elapsed_time)
107 float offset = original_y - get_pos().y;
108 if(offset > BOUNCY_BRICK_MAX_OFFSET) {
109 bounce_dir = BOUNCY_BRICK_SPEED;
110 movement = Vector(0, bounce_dir * elapsed_time);
114 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
115 movement = Vector(0, offset);
119 movement = Vector(0, bounce_dir * elapsed_time);
124 Block::draw(DrawingContext& context)
126 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
130 Block::start_bounce()
132 original_y = bbox.p1.y;
134 bounce_dir = -BOUNCY_BRICK_SPEED;
145 //---------------------------------------------------------------------------
147 BonusBlock::BonusBlock(const Vector& pos, int data)
148 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
151 sprite->set_action("normal");
153 case 1: contents = CONTENT_COIN; break;
154 case 2: contents = CONTENT_FIREGROW; break;
155 case 3: contents = CONTENT_STAR; break;
156 case 4: contents = CONTENT_1UP; break;
157 case 5: contents = CONTENT_ICEGROW; break;
159 log_warning << "Invalid box contents" << std::endl;
160 contents = CONTENT_COIN;
165 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
166 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
170 contents = CONTENT_COIN;
171 lisp::ListIterator iter(&lisp);
173 const std::string& token = iter.item();
175 iter.value()->get(pos.x);
176 } else if(token == "y") {
177 iter.value()->get(pos.y);
178 } else if(token == "contents") {
179 std::string contentstring;
180 iter.value()->get(contentstring);
181 if(contentstring == "coin") {
182 contents = CONTENT_COIN;
183 } else if(contentstring == "firegrow") {
184 contents = CONTENT_FIREGROW;
185 } else if(contentstring == "icegrow") {
186 contents = CONTENT_ICEGROW;
187 } else if(contentstring == "star") {
188 contents = CONTENT_STAR;
189 } else if(contentstring == "1up") {
190 contents = CONTENT_1UP;
191 } else if(contentstring == "custom") {
192 contents = CONTENT_CUSTOM;
194 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
197 if(contents == CONTENT_CUSTOM) {
198 GameObject* game_object = create_object(token, *(iter.lisp()));
199 object = dynamic_cast<MovingObject*> (game_object);
201 throw std::runtime_error(
202 "Only MovingObjects are allowed inside BonusBlocks");
204 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
209 if(contents == CONTENT_CUSTOM && object == 0)
210 throw std::runtime_error("Need to specify content object for custom block");
215 BonusBlock::~BonusBlock()
221 BonusBlock::hit(Player& )
227 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
228 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
230 // hit contains no information for collisions with blocks.
231 // Badguy's bottom has to be below the top of the bonusblock
232 // +7 is required to slide over one tile gaps.
233 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
237 Portable* portable = dynamic_cast<Portable*> (&other);
239 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
240 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
244 return Block::collision(other, hit);
248 BonusBlock::try_open()
250 if(sprite->get_action() == "empty") {
251 sound_manager->play("sounds/brick.wav");
255 Sector* sector = Sector::current();
257 assert(sector->player);
258 Player& player = *(sector->player);
259 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
263 Sector::current()->add_object(new BouncyCoin(get_pos()));
264 player.get_status()->add_coins(1);
265 Sector::current()->get_level()->stats.coins++;
268 case CONTENT_FIREGROW:
269 if(player.get_status()->bonus == NO_BONUS) {
270 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
271 sector->add_object(riser);
273 SpecialRiser* riser = new SpecialRiser(
274 get_pos(), new Flower(FIRE_BONUS));
275 sector->add_object(riser);
277 sound_manager->play("sounds/upgrade.wav");
280 case CONTENT_ICEGROW:
281 if(player.get_status()->bonus == NO_BONUS) {
282 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
283 sector->add_object(riser);
285 SpecialRiser* riser = new SpecialRiser(
286 get_pos(), new Flower(ICE_BONUS));
287 sector->add_object(riser);
289 sound_manager->play("sounds/upgrade.wav");
293 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
297 sector->add_object(new OneUp(get_pos(), direction));
301 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
303 sector->add_object(riser);
304 sound_manager->play("sounds/upgrade.wav");
309 sprite->set_action("empty");
315 Sector* sector = Sector::current();
317 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
319 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
320 Vector(-150, -300)));
322 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
325 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
330 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
332 //---------------------------------------------------------------------------
334 Brick::Brick(const Vector& pos, int data)
335 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
346 Brick::hit(Player& player)
348 if(sprite->get_action() == "empty")
355 Brick::collision(GameObject& other, const CollisionHit& hit){
357 Player* player = dynamic_cast<Player*> (&other);
359 if (player->butt_jump) try_break();
362 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
364 // hit contains no information for collisions with blocks.
365 // Badguy's bottom has to be below the top of the brick
366 // +7 is required to slide over one tile gaps.
367 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
371 Portable* portable = dynamic_cast<Portable*> (&other);
373 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
374 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
378 return Block::collision(other, hit);
382 Brick::try_break(Player* player)
384 if(sprite->get_action() == "empty")
387 sound_manager->play("sounds/brick.wav");
388 Sector* sector = Sector::current();
389 Player& player_one = *(sector->player);
390 if(coin_counter > 0) {
391 sector->add_object(new BouncyCoin(get_pos()));
393 player_one.get_status()->add_coins(1);
394 if(coin_counter == 0)
395 sprite->set_action("empty");
397 } else if(breakable) {
399 if(player->is_big()){
411 //IMPLEMENT_FACTORY(Brick, "brick");