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;
52 static const float BUMP_ROTATION_ANGLE = 10;
54 Block::Block(Sprite* newsprite)
55 : sprite(newsprite), bouncing(false), breaking(false), bounce_dir(0), bounce_offset(0)
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");
69 Block::collision(GameObject& other, const CollisionHit& )
71 Player* player = dynamic_cast<Player*> (&other);
73 if(player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
78 // only interact with other objects if...
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()))) {
86 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
91 // Coins get collected
92 Coin* coin = dynamic_cast<Coin*> (&other);
103 Block::update(float elapsed_time)
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);
115 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
116 movement = Vector(0, offset);
119 sprite->set_angle(0);
121 movement = Vector(0, bounce_dir * elapsed_time);
126 Block::draw(DrawingContext& context)
128 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
132 Block::start_bounce(float center_of_hitter)
134 original_y = bbox.p1.y;
136 bounce_dir = -BOUNCY_BRICK_SPEED;
139 float offset = (get_bbox().get_middle().x - center_of_hitter)*2 / get_bbox().get_width();
140 sprite->set_angle(BUMP_ROTATION_ANGLE*offset);
144 Block::start_break(float center_of_hitter)
146 start_bounce(center_of_hitter);
150 //---------------------------------------------------------------------------
152 BonusBlock::BonusBlock(const Vector& pos, int data)
153 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
156 sprite->set_action("normal");
158 case 1: contents = CONTENT_COIN; break;
159 case 2: contents = CONTENT_FIREGROW; break;
160 case 3: contents = CONTENT_STAR; break;
161 case 4: contents = CONTENT_1UP; break;
162 case 5: contents = CONTENT_ICEGROW; break;
164 log_warning << "Invalid box contents" << std::endl;
165 contents = CONTENT_COIN;
170 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
171 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
175 contents = CONTENT_COIN;
176 lisp::ListIterator iter(&lisp);
178 const std::string& token = iter.item();
180 iter.value()->get(pos.x);
181 } else if(token == "y") {
182 iter.value()->get(pos.y);
183 } else if(token == "contents") {
184 std::string contentstring;
185 iter.value()->get(contentstring);
186 if(contentstring == "coin") {
187 contents = CONTENT_COIN;
188 } else if(contentstring == "firegrow") {
189 contents = CONTENT_FIREGROW;
190 } else if(contentstring == "icegrow") {
191 contents = CONTENT_ICEGROW;
192 } else if(contentstring == "star") {
193 contents = CONTENT_STAR;
194 } else if(contentstring == "1up") {
195 contents = CONTENT_1UP;
196 } else if(contentstring == "custom") {
197 contents = CONTENT_CUSTOM;
199 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
202 if(contents == CONTENT_CUSTOM) {
203 GameObject* game_object = create_object(token, *(iter.lisp()));
204 object = dynamic_cast<MovingObject*> (game_object);
206 throw std::runtime_error(
207 "Only MovingObjects are allowed inside BonusBlocks");
209 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
214 if(contents == CONTENT_CUSTOM && object == 0)
215 throw std::runtime_error("Need to specify content object for custom block");
220 BonusBlock::~BonusBlock()
226 BonusBlock::hit(Player& )
232 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
233 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
235 // hit contains no information for collisions with blocks.
236 // Badguy's bottom has to be below the top of the bonusblock
237 // +7 is required to slide over one tile gaps.
238 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
242 Portable* portable = dynamic_cast<Portable*> (&other);
244 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
245 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
249 return Block::collision(other, hit);
253 BonusBlock::try_open()
255 if(sprite->get_action() == "empty") {
256 sound_manager->play("sounds/brick.wav");
260 Sector* sector = Sector::current();
262 assert(sector->player);
263 Player& player = *(sector->player);
264 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
268 Sector::current()->add_object(new BouncyCoin(get_pos(), true));
269 player.get_status()->add_coins(1);
270 Sector::current()->get_level()->stats.coins++;
273 case CONTENT_FIREGROW:
274 if(player.get_status()->bonus == NO_BONUS) {
275 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
276 sector->add_object(riser);
278 SpecialRiser* riser = new SpecialRiser(
279 get_pos(), new Flower(FIRE_BONUS));
280 sector->add_object(riser);
282 sound_manager->play("sounds/upgrade.wav");
285 case CONTENT_ICEGROW:
286 if(player.get_status()->bonus == NO_BONUS) {
287 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
288 sector->add_object(riser);
290 SpecialRiser* riser = new SpecialRiser(
291 get_pos(), new Flower(ICE_BONUS));
292 sector->add_object(riser);
294 sound_manager->play("sounds/upgrade.wav");
298 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
302 sector->add_object(new OneUp(get_pos(), direction));
306 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
308 sector->add_object(riser);
309 sound_manager->play("sounds/upgrade.wav");
313 start_bounce(player.get_bbox().get_middle().x);
314 sprite->set_action("empty");
320 Sector* sector = Sector::current();
322 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
324 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
325 Vector(-150, -300)));
327 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
330 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
335 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
337 //---------------------------------------------------------------------------
339 Brick::Brick(const Vector& pos, int data)
340 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
351 Brick::hit(Player& player)
353 if(sprite->get_action() == "empty")
360 Brick::collision(GameObject& other, const CollisionHit& hit){
362 Player* player = dynamic_cast<Player*> (&other);
364 if (player->does_buttjump) try_break();
367 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
369 // hit contains no information for collisions with blocks.
370 // Badguy's bottom has to be below the top of the brick
371 // +7 is required to slide over one tile gaps.
372 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
376 Portable* portable = dynamic_cast<Portable*> (&other);
378 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
379 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
383 return Block::collision(other, hit);
387 Brick::try_break(Player* player)
389 if(sprite->get_action() == "empty")
392 sound_manager->play("sounds/brick.wav");
393 Sector* sector = Sector::current();
394 Player& player_one = *(sector->player);
395 if(coin_counter > 0) {
396 sector->add_object(new BouncyCoin(get_pos(),true));
398 player_one.get_status()->add_coins(1);
399 if(coin_counter == 0)
400 sprite->set_action("empty");
401 start_bounce(player->get_bbox().get_middle().x);
402 } else if(breakable) {
404 if(player->is_big()){
405 start_break(player->get_bbox().get_middle().x);
408 start_bounce(player->get_bbox().get_middle().x);
416 //IMPLEMENT_FACTORY(Brick, "brick");