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 Portable* portable = dynamic_cast<Portable*> (&other);
78 if(bouncing && (portable == 0 || portable->is_portable())) {
79 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
83 Coin* coin = dynamic_cast<Coin*> (&other);
93 Block::update(float elapsed_time)
98 float offset = original_y - get_pos().y;
99 if(offset > BOUNCY_BRICK_MAX_OFFSET) {
100 bounce_dir = BOUNCY_BRICK_SPEED;
101 movement = Vector(0, bounce_dir * elapsed_time);
105 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
106 movement = Vector(0, offset);
110 movement = Vector(0, bounce_dir * elapsed_time);
115 Block::draw(DrawingContext& context)
117 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
121 Block::start_bounce()
123 original_y = bbox.p1.y;
125 bounce_dir = -BOUNCY_BRICK_SPEED;
136 //---------------------------------------------------------------------------
138 BonusBlock::BonusBlock(const Vector& pos, int data)
139 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
142 sprite->set_action("normal");
144 case 1: contents = CONTENT_COIN; break;
145 case 2: contents = CONTENT_FIREGROW; break;
146 case 3: contents = CONTENT_STAR; break;
147 case 4: contents = CONTENT_1UP; break;
148 case 5: contents = CONTENT_ICEGROW; break;
150 log_warning << "Invalid box contents" << std::endl;
151 contents = CONTENT_COIN;
156 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
157 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
161 contents = CONTENT_COIN;
162 lisp::ListIterator iter(&lisp);
164 const std::string& token = iter.item();
166 iter.value()->get(pos.x);
167 } else if(token == "y") {
168 iter.value()->get(pos.y);
169 } else if(token == "contents") {
170 std::string contentstring;
171 iter.value()->get(contentstring);
172 if(contentstring == "coin") {
173 contents = CONTENT_COIN;
174 } else if(contentstring == "firegrow") {
175 contents = CONTENT_FIREGROW;
176 } else if(contentstring == "icegrow") {
177 contents = CONTENT_ICEGROW;
178 } else if(contentstring == "star") {
179 contents = CONTENT_STAR;
180 } else if(contentstring == "1up") {
181 contents = CONTENT_1UP;
182 } else if(contentstring == "custom") {
183 contents = CONTENT_CUSTOM;
185 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
188 if(contents == CONTENT_CUSTOM) {
189 GameObject* game_object = create_object(token, *(iter.lisp()));
190 object = dynamic_cast<MovingObject*> (game_object);
192 throw std::runtime_error(
193 "Only MovingObjects are allowed inside BonusBlocks");
195 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
200 if(contents == CONTENT_CUSTOM && object == 0)
201 throw std::runtime_error("Need to specify content object for custom block");
206 BonusBlock::~BonusBlock()
212 BonusBlock::hit(Player& )
218 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
219 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
221 // hit contains no information for collisions with blocks.
222 // Badguy's bottom has to be below the top of the bonusblock
223 // +7 is required to slide over one tile gaps.
224 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
228 Portable* portable = dynamic_cast<Portable*> (&other);
230 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
231 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
235 return Block::collision(other, hit);
239 BonusBlock::try_open()
241 if(sprite->get_action() == "empty") {
242 sound_manager->play("sounds/brick.wav");
246 Sector* sector = Sector::current();
248 assert(sector->player);
249 Player& player = *(sector->player);
250 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
254 Sector::current()->add_object(new BouncyCoin(get_pos()));
255 player.get_status()->add_coins(1);
256 Sector::current()->get_level()->stats.coins++;
259 case CONTENT_FIREGROW:
260 if(player.get_status()->bonus == NO_BONUS) {
261 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
262 sector->add_object(riser);
264 SpecialRiser* riser = new SpecialRiser(
265 get_pos(), new Flower(FIRE_BONUS));
266 sector->add_object(riser);
268 sound_manager->play("sounds/upgrade.wav");
271 case CONTENT_ICEGROW:
272 if(player.get_status()->bonus == NO_BONUS) {
273 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
274 sector->add_object(riser);
276 SpecialRiser* riser = new SpecialRiser(
277 get_pos(), new Flower(ICE_BONUS));
278 sector->add_object(riser);
280 sound_manager->play("sounds/upgrade.wav");
284 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
288 sector->add_object(new OneUp(get_pos(), direction));
292 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
294 sector->add_object(riser);
295 sound_manager->play("sounds/upgrade.wav");
300 sprite->set_action("empty");
306 Sector* sector = Sector::current();
308 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
310 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
311 Vector(-150, -300)));
313 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
316 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
321 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
323 //---------------------------------------------------------------------------
325 Brick::Brick(const Vector& pos, int data)
326 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
339 if(sprite->get_action() == "empty")
346 Brick::collision(GameObject& other, const CollisionHit& hit){
347 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
349 // hit contains no information for collisions with blocks.
350 // Badguy's bottom has to be below the top of the brick
351 // +7 is required to slide over one tile gaps.
352 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
356 Portable* portable = dynamic_cast<Portable*> (&other);
358 MovingObject* moving = dynamic_cast<MovingObject*> (&other);
359 if(moving->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
363 return Block::collision(other, hit);
367 Brick::try_break(bool playerhit)
369 if(sprite->get_action() == "empty")
372 sound_manager->play("sounds/brick.wav");
373 Sector* sector = Sector::current();
374 Player& player = *(sector->player);
375 if(coin_counter > 0) {
376 sector->add_object(new BouncyCoin(get_pos()));
378 player.get_status()->add_coins(1);
379 if(coin_counter == 0)
380 sprite->set_action("empty");
382 } else if(breakable) {
396 //IMPLEMENT_FACTORY(Brick, "brick");