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), breaking(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);
102 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
103 movement = Vector(0, offset);
107 movement = Vector(0, bounce_dir * elapsed_time);
112 Block::draw(DrawingContext& context)
114 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
118 Block::start_bounce()
120 original_y = bbox.p1.y;
122 bounce_dir = -BOUNCY_BRICK_SPEED;
133 //---------------------------------------------------------------------------
135 BonusBlock::BonusBlock(const Vector& pos, int data)
136 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
139 sprite->set_action("normal");
141 case 1: contents = CONTENT_COIN; break;
142 case 2: contents = CONTENT_FIREGROW; break;
143 case 3: contents = CONTENT_STAR; break;
144 case 4: contents = CONTENT_1UP; break;
145 case 5: contents = CONTENT_ICEGROW; break;
147 log_warning << "Invalid box contents" << std::endl;
148 contents = CONTENT_COIN;
153 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
154 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
158 contents = CONTENT_COIN;
159 lisp::ListIterator iter(&lisp);
161 const std::string& token = iter.item();
163 iter.value()->get(pos.x);
164 } else if(token == "y") {
165 iter.value()->get(pos.y);
166 } else if(token == "contents") {
167 std::string contentstring;
168 iter.value()->get(contentstring);
169 if(contentstring == "coin") {
170 contents = CONTENT_COIN;
171 } else if(contentstring == "firegrow") {
172 contents = CONTENT_FIREGROW;
173 } else if(contentstring == "icegrow") {
174 contents = CONTENT_ICEGROW;
175 } else if(contentstring == "star") {
176 contents = CONTENT_STAR;
177 } else if(contentstring == "1up") {
178 contents = CONTENT_1UP;
179 } else if(contentstring == "custom") {
180 contents = CONTENT_CUSTOM;
182 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
185 if(contents == CONTENT_CUSTOM) {
186 GameObject* game_object = create_object(token, *(iter.lisp()));
187 object = dynamic_cast<MovingObject*> (game_object);
189 throw std::runtime_error(
190 "Only MovingObjects are allowed inside BonusBlocks");
192 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
197 if(contents == CONTENT_CUSTOM && object == 0)
198 throw std::runtime_error("Need to specify content object for custom block");
203 BonusBlock::~BonusBlock()
209 BonusBlock::hit(Player& )
215 BonusBlock::collision(GameObject& other, const CollisionHit& hit){
216 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
218 // hit contains no information for collisions with blocks.
219 // Badguy's bottom has to be below the top of the bonusblock
220 // +7 is required to slide over one tile gaps.
221 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0) ){
225 return Block::collision(other, hit);
229 BonusBlock::try_open()
231 if(sprite->get_action() == "empty") {
232 sound_manager->play("sounds/brick.wav");
236 Sector* sector = Sector::current();
238 assert(sector->player);
239 Player& player = *(sector->player);
240 Direction direction = (player.get_bbox().get_middle().x > get_bbox().get_middle().x) ? LEFT : RIGHT;
244 Sector::current()->add_object(new BouncyCoin(get_pos()));
245 player.get_status()->add_coins(1);
248 case CONTENT_FIREGROW:
249 if(player.get_status()->bonus == NO_BONUS) {
250 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
251 sector->add_object(riser);
253 SpecialRiser* riser = new SpecialRiser(
254 get_pos(), new Flower(FIRE_BONUS));
255 sector->add_object(riser);
257 sound_manager->play("sounds/upgrade.wav");
260 case CONTENT_ICEGROW:
261 if(player.get_status()->bonus == NO_BONUS) {
262 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp(direction));
263 sector->add_object(riser);
265 SpecialRiser* riser = new SpecialRiser(
266 get_pos(), new Flower(ICE_BONUS));
267 sector->add_object(riser);
269 sound_manager->play("sounds/upgrade.wav");
273 sector->add_object(new Star(get_pos() + Vector(0, -32), direction));
277 sector->add_object(new OneUp(get_pos(), direction));
281 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
283 sector->add_object(riser);
284 sound_manager->play("sounds/upgrade.wav");
289 sprite->set_action("empty");
295 Sector* sector = Sector::current();
297 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
299 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
300 Vector(-150, -300)));
302 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
305 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
310 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
312 //---------------------------------------------------------------------------
314 Brick::Brick(const Vector& pos, int data)
315 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
328 if(sprite->get_action() == "empty")
335 Brick::collision(GameObject& other, const CollisionHit& hit){
336 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
338 // hit contains no information for collisions with blocks.
339 // Badguy's bottom has to be below the top of the brick
340 // +7 is required to slide over one tile gaps.
341 if( badguy->can_break() && ( badguy->get_bbox().get_bottom() > get_bbox().get_top() + 7.0 ) ){
345 return Block::collision(other, hit);
349 Brick::try_break(bool playerhit)
351 if(sprite->get_action() == "empty")
354 sound_manager->play("sounds/brick.wav");
355 Sector* sector = Sector::current();
356 Player& player = *(sector->player);
357 if(coin_counter > 0) {
358 sector->add_object(new BouncyCoin(get_pos()));
360 player.get_status()->add_coins(1);
361 if(coin_counter == 0)
362 sprite->set_action("empty");
364 } else if(breakable) {
378 //IMPLEMENT_FACTORY(Brick, "brick");