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 "specialriser.hpp"
40 #include "player_status.hpp"
41 #include "badguy/badguy.hpp"
43 #include "object_factory.hpp"
44 #include "lisp/list_iterator.hpp"
45 #include "object_factory.hpp"
48 static const float BOUNCY_BRICK_MAX_OFFSET = 8;
49 static const float BOUNCY_BRICK_SPEED = 90;
50 static const float EPSILON = .0001f;
52 Block::Block(Sprite* newsprite)
53 : sprite(newsprite), bouncing(false), breaking(false), bounce_dir(0), bounce_offset(0)
55 bbox.set_size(32, 32.1f);
56 set_group(COLGROUP_STATIC);
57 sound_manager->preload("sounds/upgrade.wav");
58 sound_manager->preload("sounds/brick.wav");
67 Block::collision(GameObject& other, const CollisionHit& )
69 Player* player = dynamic_cast<Player*> (&other);
71 if(player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
77 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
81 Coin* coin = dynamic_cast<Coin*> (&other);
91 Block::update(float elapsed_time)
96 float offset = original_y - get_pos().y;
97 if(offset > BOUNCY_BRICK_MAX_OFFSET) {
98 bounce_dir = BOUNCY_BRICK_SPEED;
99 movement = Vector(0, bounce_dir * elapsed_time);
103 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
104 movement = Vector(0, offset);
108 movement = Vector(0, bounce_dir * elapsed_time);
113 Block::draw(DrawingContext& context)
115 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
119 Block::start_bounce()
121 original_y = bbox.p1.y;
123 bounce_dir = -BOUNCY_BRICK_SPEED;
134 //---------------------------------------------------------------------------
136 BonusBlock::BonusBlock(const Vector& pos, int data)
137 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
140 sprite->set_action("normal");
142 case 1: contents = CONTENT_COIN; break;
143 case 2: contents = CONTENT_FIREGROW; break;
144 case 3: contents = CONTENT_STAR; break;
145 case 4: contents = CONTENT_1UP; break;
146 case 5: contents = CONTENT_ICEGROW; break;
148 log_warning << "Invalid box contents" << std::endl;
149 contents = CONTENT_COIN;
154 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
155 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
159 contents = CONTENT_COIN;
160 lisp::ListIterator iter(&lisp);
162 const std::string& token = iter.item();
164 iter.value()->get(pos.x);
165 } else if(token == "y") {
166 iter.value()->get(pos.y);
167 } else if(token == "contents") {
168 std::string contentstring;
169 iter.value()->get(contentstring);
170 if(contentstring == "coin") {
171 contents = CONTENT_COIN;
172 } else if(contentstring == "firegrow") {
173 contents = CONTENT_FIREGROW;
174 } else if(contentstring == "icegrow") {
175 contents = CONTENT_ICEGROW;
176 } else if(contentstring == "star") {
177 contents = CONTENT_STAR;
178 } else if(contentstring == "1up") {
179 contents = CONTENT_1UP;
180 } else if(contentstring == "custom") {
181 contents = CONTENT_CUSTOM;
183 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
186 if(contents == CONTENT_CUSTOM) {
187 GameObject* game_object = create_object(token, *(iter.lisp()));
188 object = dynamic_cast<MovingObject*> (game_object);
190 throw std::runtime_error(
191 "Only MovingObjects are allowed inside BonusBlocks");
193 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
198 if(contents == CONTENT_CUSTOM && object == 0)
199 throw std::runtime_error("Need to specify content object for custom block");
204 BonusBlock::~BonusBlock()
210 BonusBlock::hit(Player& )
216 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
217 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
219 // hit contains no information for collisions with blocks.
220 // Badguy's bottom has to be below the top of the bonusblock
221 // +7 is required to slide over one tile gaps.
222 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
226 return Block::collision(other, hit);
230 BonusBlock::try_open()
232 if(sprite->get_action() == "empty") {
233 sound_manager->play("sounds/brick.wav");
237 Sector* sector = Sector::current();
239 assert(sector->player);
240 Player& player = *(sector->player);
241 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
245 Sector::current()->add_object(new BouncyCoin(get_pos()));
246 player.get_status()->add_coins(1);
247 Sector::current()->get_level()->stats.coins++;
250 case CONTENT_FIREGROW:
251 if(player.get_status()->bonus == NO_BONUS) {
252 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
253 sector->add_object(riser);
255 SpecialRiser* riser = new SpecialRiser(
256 get_pos(), new Flower(FIRE_BONUS));
257 sector->add_object(riser);
259 sound_manager->play("sounds/upgrade.wav");
262 case CONTENT_ICEGROW:
263 if(player.get_status()->bonus == NO_BONUS) {
264 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
265 sector->add_object(riser);
267 SpecialRiser* riser = new SpecialRiser(
268 get_pos(), new Flower(ICE_BONUS));
269 sector->add_object(riser);
271 sound_manager->play("sounds/upgrade.wav");
275 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
279 sector->add_object(new OneUp(get_pos(), direction));
283 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
285 sector->add_object(riser);
286 sound_manager->play("sounds/upgrade.wav");
291 sprite->set_action("empty");
297 Sector* sector = Sector::current();
299 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
301 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
302 Vector(-150, -300)));
304 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
307 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
312 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
314 //---------------------------------------------------------------------------
316 Brick::Brick(const Vector& pos, int data)
317 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
330 if(sprite->get_action() == "empty")
337 Brick::collision(GameObject& other, const CollisionHit& hit){
338 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
340 // hit contains no information for collisions with blocks.
341 // Badguy's bottom has to be below the top of the brick
342 // +7 is required to slide over one tile gaps.
343 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
347 return Block::collision(other, hit);
351 Brick::try_break(bool playerhit)
353 if(sprite->get_action() == "empty")
356 sound_manager->play("sounds/brick.wav");
357 Sector* sector = Sector::current();
358 Player& player = *(sector->player);
359 if(coin_counter > 0) {
360 sector->add_object(new BouncyCoin(get_pos()));
362 player.get_status()->add_coins(1);
363 if(coin_counter == 0)
364 sprite->set_action("empty");
366 } else if(breakable) {
380 //IMPLEMENT_FACTORY(Brick, "brick");