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"
47 static const float BOUNCY_BRICK_MAX_OFFSET=8;
48 static const float BOUNCY_BRICK_SPEED=90;
49 static const float EPSILON = .0001;
51 Block::Block(Sprite* newsprite)
52 : sprite(newsprite), bouncing(false), bounce_dir(0), bounce_offset(0)
54 bbox.set_size(32, 32.1);
55 set_group(COLGROUP_STATIC);
56 sound_manager->preload("sounds/upgrade.wav");
57 sound_manager->preload("sounds/brick.wav");
66 Block::collision(GameObject& other, const CollisionHit& )
68 Player* player = dynamic_cast<Player*> (&other);
70 if(player->get_bbox().get_top() > get_bbox().get_bottom() - 7.0) {
76 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
80 Coin* coin = dynamic_cast<Coin*> (&other);
90 Block::update(float elapsed_time)
95 float offset = original_y - get_pos().y;
96 if(offset > BOUNCY_BRICK_MAX_OFFSET) {
97 bounce_dir = BOUNCY_BRICK_SPEED;
98 movement = Vector(0, bounce_dir * elapsed_time);
99 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
100 movement = Vector(0, offset);
104 movement = Vector(0, bounce_dir * elapsed_time);
109 Block::draw(DrawingContext& context)
111 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
115 Block::start_bounce()
117 original_y = bbox.p1.y;
119 bounce_dir = -BOUNCY_BRICK_SPEED;
123 //---------------------------------------------------------------------------
125 BonusBlock::BonusBlock(const Vector& pos, int data)
126 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
129 sprite->set_action("normal");
131 case 1: contents = CONTENT_COIN; break;
132 case 2: contents = CONTENT_FIREGROW; break;
133 case 3: contents = CONTENT_STAR; break;
134 case 4: contents = CONTENT_1UP; break;
135 case 5: contents = CONTENT_ICEGROW; break;
137 log_warning << "Invalid box contents" << std::endl;
138 contents = CONTENT_COIN;
143 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
144 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
148 contents = CONTENT_COIN;
149 lisp::ListIterator iter(&lisp);
151 const std::string& token = iter.item();
153 iter.value()->get(pos.x);
154 } else if(token == "y") {
155 iter.value()->get(pos.y);
156 } else if(token == "contents") {
157 std::string contentstring;
158 iter.value()->get(contentstring);
159 if(contentstring == "coin") {
160 contents = CONTENT_COIN;
161 } else if(contentstring == "firegrow") {
162 contents = CONTENT_FIREGROW;
163 } else if(contentstring == "icegrow") {
164 contents = CONTENT_ICEGROW;
165 } else if(contentstring == "star") {
166 contents = CONTENT_STAR;
167 } else if(contentstring == "1up") {
168 contents = CONTENT_1UP;
169 } else if(contentstring == "custom") {
170 contents = CONTENT_CUSTOM;
172 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
175 if(contents == CONTENT_CUSTOM) {
176 GameObject* game_object = create_object(token, *(iter.lisp()));
177 object = dynamic_cast<MovingObject*> (game_object);
179 throw std::runtime_error(
180 "Only MovingObjects are allowed inside BonusBlocks");
182 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
187 if(contents == CONTENT_CUSTOM && object == 0)
188 throw std::runtime_error("Need to specify content object for custom block");
193 BonusBlock::~BonusBlock()
199 BonusBlock::hit(Player& )
205 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
206 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
208 // hit contains no information for collisions with blocks.
209 // Badguy's bottom has to be below the top of the bonusblock
210 // +7 is required to slide over one tile gaps.
211 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
215 return Block::collision(other, hit);
219 BonusBlock::try_open()
221 if(sprite->get_action() == "empty") {
222 sound_manager->play("sounds/brick.wav");
226 Sector* sector = Sector::current();
228 assert(sector->player);
229 Player& player = *(sector->player);
230 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
234 Sector::current()->add_object(new BouncyCoin(get_pos()));
235 player.get_status()->add_coins(1);
238 case CONTENT_FIREGROW:
239 if(player.get_status()->bonus == NO_BONUS) {
240 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
241 sector->add_object(riser);
243 SpecialRiser* riser = new SpecialRiser(
244 get_pos(), new Flower(FIRE_BONUS));
245 sector->add_object(riser);
247 sound_manager->play("sounds/upgrade.wav");
250 case CONTENT_ICEGROW:
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(ICE_BONUS));
257 sector->add_object(riser);
259 sound_manager->play("sounds/upgrade.wav");
263 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
267 sector->add_object(new OneUp(get_pos(), direction));
271 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
273 sector->add_object(riser);
274 sound_manager->play("sounds/upgrade.wav");
279 sprite->set_action("empty");
282 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
284 //---------------------------------------------------------------------------
286 Brick::Brick(const Vector& pos, int data)
287 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
300 if(sprite->get_action() == "empty")
307 Brick::collision(GameObject& other, const CollisionHit& hit){
308 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
310 // hit contains no information for collisions with blocks.
311 // Badguy's bottom has to be below the top of the brick
312 // +7 is required to slide over one tile gaps.
313 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
317 return Block::collision(other, hit);
321 Brick::try_break(bool playerhit)
323 if(sprite->get_action() == "empty")
326 sound_manager->play("sounds/brick.wav");
327 Sector* sector = Sector::current();
328 Player& player = *(sector->player);
329 if(coin_counter > 0) {
330 sector->add_object(new BouncyCoin(get_pos()));
332 player.get_status()->add_coins(1);
333 if(coin_counter == 0)
334 sprite->set_action("empty");
336 } else if(breakable) {
337 if(playerhit && !player.is_big()) {
343 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
345 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
346 Vector(-150, -300)));
348 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
351 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
357 //IMPLEMENT_FACTORY(Brick, "brick");