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 "moving_object.hpp"
37 #include "specialriser.hpp"
42 #include "player_status.hpp"
43 #include "badguy/badguy.hpp"
45 #include "object_factory.hpp"
46 #include "lisp/list_iterator.hpp"
47 #include "object_factory.hpp"
50 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
51 static const float BOUNCY_BRICK_SPEED = 90;
52 static const float EPSILON = .0001f;
53 static const float BUMP_ROTATION_ANGLE = 10;
55 Block::Block(Sprite* newsprite)
56 : sprite(newsprite), bouncing(false), breaking(false), bounce_dir(0), bounce_offset(0), original_y(-1)
58 bbox.set_size(32, 32.1f);
59 set_group(COLGROUP_STATIC);
60 sound_manager->preload("sounds/upgrade.wav");
61 sound_manager->preload("sounds/brick.wav");
70 Block::collision(GameObject& other, const CollisionHit& )
72 Player* player = dynamic_cast<Player*> (&other);
74 if(player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
79 // only interact with other objects if...
81 // 2) the object is not portable (either never or not currently)
82 // 3) the object is being hit from below (baguys don't get killed for activating boxes)
83 Portable* portable = dynamic_cast<Portable*> (&other);
84 MovingObject* moving_object = dynamic_cast<MovingObject*> (&other);
85 bool is_portable = ((portable != 0) && portable->is_portable());
86 bool hit_mo_from_below = ((moving_object == 0) || (moving_object->get_bbox().get_bottom() < (get_bbox().get_top() + 7.0)));
87 if(bouncing && !is_portable && hit_mo_from_below) {
90 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
95 // Coins get collected
96 Coin* coin = dynamic_cast<Coin*> (&other);
102 GrowUp* growup = dynamic_cast<GrowUp*> (&other);
113 Block::update(float elapsed_time)
118 float offset = original_y - get_pos().y;
119 if(offset > BOUNCY_BRICK_MAX_OFFSET) {
120 bounce_dir = BOUNCY_BRICK_SPEED;
121 movement = Vector(0, bounce_dir * elapsed_time);
125 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
126 movement = Vector(0, offset);
129 sprite->set_angle(0);
131 movement = Vector(0, bounce_dir * elapsed_time);
136 Block::draw(DrawingContext& context)
138 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
142 Block::start_bounce(GameObject* hitter)
144 if(original_y == -1){
145 original_y = bbox.p1.y;
148 bounce_dir = -BOUNCY_BRICK_SPEED;
151 MovingObject* hitter_mo = dynamic_cast<MovingObject*>(hitter);
153 float center_of_hitter = hitter_mo->get_bbox().get_middle().x;
154 float offset = (get_bbox().get_middle().x - center_of_hitter)*2 / get_bbox().get_width();
155 sprite->set_angle(BUMP_ROTATION_ANGLE*offset);
160 Block::start_break(GameObject* hitter)
162 start_bounce(hitter);
166 //---------------------------------------------------------------------------
168 BonusBlock::BonusBlock(const Vector& pos, int data)
169 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
172 sprite->set_action("normal");
174 case 1: contents = CONTENT_COIN; break;
175 case 2: contents = CONTENT_FIREGROW; break;
176 case 3: contents = CONTENT_STAR; break;
177 case 4: contents = CONTENT_1UP; break;
178 case 5: contents = CONTENT_ICEGROW; break;
180 log_warning << "Invalid box contents" << std::endl;
181 contents = CONTENT_COIN;
186 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
187 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
191 contents = CONTENT_COIN;
192 lisp::ListIterator iter(&lisp);
194 const std::string& token = iter.item();
196 iter.value()->get(pos.x);
197 } else if(token == "y") {
198 iter.value()->get(pos.y);
199 } else if(token == "contents") {
200 std::string contentstring;
201 iter.value()->get(contentstring);
202 if(contentstring == "coin") {
203 contents = CONTENT_COIN;
204 } else if(contentstring == "firegrow") {
205 contents = CONTENT_FIREGROW;
206 } else if(contentstring == "icegrow") {
207 contents = CONTENT_ICEGROW;
208 } else if(contentstring == "star") {
209 contents = CONTENT_STAR;
210 } else if(contentstring == "1up") {
211 contents = CONTENT_1UP;
212 } else if(contentstring == "custom") {
213 contents = CONTENT_CUSTOM;
215 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
218 if(contents == CONTENT_CUSTOM) {
219 GameObject* game_object = create_object(token, *(iter.lisp()));
220 object = dynamic_cast<MovingObject*> (game_object);
222 throw std::runtime_error(
223 "Only MovingObjects are allowed inside BonusBlocks");
225 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
230 if(contents == CONTENT_CUSTOM && object == 0)
231 throw std::runtime_error("Need to specify content object for custom block");
236 BonusBlock::~BonusBlock()
242 BonusBlock::hit(Player& )
248 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
249 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
251 // hit contains no information for collisions with blocks.
252 // Badguy's bottom has to be below the top of the bonusblock
253 // +7 is required to slide over one tile gaps.
254 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
258 Portable* portable = dynamic_cast<Portable*> (&other);
260 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
261 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
265 return Block::collision(other, hit);
269 BonusBlock::try_open()
271 if(sprite->get_action() == "empty") {
272 sound_manager->play("sounds/brick.wav");
276 Sector* sector = Sector::current();
278 assert(sector->player);
279 Player& player = *(sector->player);
280 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
284 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
285 player.get_status()->add_coins(1);
286 Sector::current()->get_level()->stats.coins++;
289 case CONTENT_FIREGROW:
290 if(player.get_status()->bonus == NO_BONUS) {
291 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
292 sector->add_object(riser);
294 SpecialRiser* riser = new SpecialRiser(
295 get_pos(), new Flower(FIRE_BONUS));
296 sector->add_object(riser);
298 sound_manager->play("sounds/upgrade.wav");
301 case CONTENT_ICEGROW:
302 if(player.get_status()->bonus == NO_BONUS) {
303 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
304 sector->add_object(riser);
306 SpecialRiser* riser = new SpecialRiser(
307 get_pos(), new Flower(ICE_BONUS));
308 sector->add_object(riser);
310 sound_manager->play("sounds/upgrade.wav");
314 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
318 sector->add_object(new OneUp(get_pos(), direction));
322 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
324 sector->add_object(riser);
325 sound_manager->play("sounds/upgrade.wav");
329 start_bounce(&player);
330 sprite->set_action("empty");
336 Sector* sector = Sector::current();
338 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
340 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
341 Vector(-150, -300)));
343 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
346 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
351 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
353 //---------------------------------------------------------------------------
355 Brick::Brick(const Vector& pos, int data)
356 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
367 Brick::hit(Player& player)
369 if(sprite->get_action() == "empty")
376 Brick::collision(GameObject& other, const CollisionHit& hit){
378 Player* player = dynamic_cast<Player*> (&other);
380 if (player->does_buttjump) try_break();
383 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
385 // hit contains no information for collisions with blocks.
386 // Badguy's bottom has to be below the top of the brick
387 // +7 is required to slide over one tile gaps.
388 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
392 Portable* portable = dynamic_cast<Portable*> (&other);
394 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
395 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
399 return Block::collision(other, hit);
403 Brick::try_break(Player* player)
405 if(sprite->get_action() == "empty")
408 sound_manager->play("sounds/brick.wav");
409 Sector* sector = Sector::current();
410 Player& player_one = *(sector->player);
411 if(coin_counter > 0) {
412 sector->add_object(new BouncyCoin(get_pos(),true));
414 player_one.get_status()->add_coins(1);
415 if(coin_counter == 0)
416 sprite->set_action("empty");
417 start_bounce(player);
418 } else if(breakable) {
420 if(player->is_big()){
424 start_bounce(player);
432 //IMPLEMENT_FACTORY(Brick, "brick");