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);
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);
100 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
101 movement = Vector(0, offset);
105 movement = Vector(0, bounce_dir * elapsed_time);
110 Block::draw(DrawingContext& context)
112 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
116 Block::start_bounce()
118 original_y = bbox.p1.y;
120 bounce_dir = -BOUNCY_BRICK_SPEED;
124 //---------------------------------------------------------------------------
126 BonusBlock::BonusBlock(const Vector& pos, int data)
127 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
130 sprite->set_action("normal");
132 case 1: contents = CONTENT_COIN; break;
133 case 2: contents = CONTENT_FIREGROW; break;
134 case 3: contents = CONTENT_STAR; break;
135 case 4: contents = CONTENT_1UP; break;
136 case 5: contents = CONTENT_ICEGROW; break;
138 log_warning << "Invalid box contents" << std::endl;
139 contents = CONTENT_COIN;
144 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
145 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
149 contents = CONTENT_COIN;
150 lisp::ListIterator iter(&lisp);
152 const std::string& token = iter.item();
154 iter.value()->get(pos.x);
155 } else if(token == "y") {
156 iter.value()->get(pos.y);
157 } else if(token == "contents") {
158 std::string contentstring;
159 iter.value()->get(contentstring);
160 if(contentstring == "coin") {
161 contents = CONTENT_COIN;
162 } else if(contentstring == "firegrow") {
163 contents = CONTENT_FIREGROW;
164 } else if(contentstring == "icegrow") {
165 contents = CONTENT_ICEGROW;
166 } else if(contentstring == "star") {
167 contents = CONTENT_STAR;
168 } else if(contentstring == "1up") {
169 contents = CONTENT_1UP;
170 } else if(contentstring == "custom") {
171 contents = CONTENT_CUSTOM;
173 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
176 if(contents == CONTENT_CUSTOM) {
177 GameObject* game_object = create_object(token, *(iter.lisp()));
178 object = dynamic_cast<MovingObject*> (game_object);
180 throw std::runtime_error(
181 "Only MovingObjects are allowed inside BonusBlocks");
183 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
188 if(contents == CONTENT_CUSTOM && object == 0)
189 throw std::runtime_error("Need to specify content object for custom block");
194 BonusBlock::~BonusBlock()
200 BonusBlock::hit(Player& )
206 BonusBlock::try_open()
208 if(sprite->get_action() == "empty") {
209 sound_manager->play("sounds/brick.wav");
213 Sector* sector = Sector::current();
214 Player& player = *(sector->player);
217 Sector::current()->add_object(new BouncyCoin(get_pos()));
218 player.get_status()->add_coins(1);
221 case CONTENT_FIREGROW:
222 if(player.get_status()->bonus == NO_BONUS) {
223 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
224 sector->add_object(riser);
226 SpecialRiser* riser = new SpecialRiser(
227 get_pos(), new Flower(FIRE_BONUS));
228 sector->add_object(riser);
230 sound_manager->play("sounds/upgrade.wav");
233 case CONTENT_ICEGROW:
234 if(player.get_status()->bonus == NO_BONUS) {
235 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
236 sector->add_object(riser);
238 SpecialRiser* riser = new SpecialRiser(
239 get_pos(), new Flower(ICE_BONUS));
240 sector->add_object(riser);
242 sound_manager->play("sounds/upgrade.wav");
246 sector->add_object(new Star(get_pos() + Vector(0, -32)));
250 sector->add_object(new OneUp(get_pos()));
254 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
256 sector->add_object(riser);
257 sound_manager->play("sounds/upgrade.wav");
262 sprite->set_action("empty");
265 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
267 //---------------------------------------------------------------------------
269 Brick::Brick(const Vector& pos, int data)
270 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
283 if(sprite->get_action() == "empty")
290 Brick::try_break(bool playerhit)
292 if(sprite->get_action() == "empty")
295 sound_manager->play("sounds/brick.wav");
296 Sector* sector = Sector::current();
297 Player& player = *(sector->player);
298 if(coin_counter > 0) {
299 sector->add_object(new BouncyCoin(get_pos()));
301 player.get_status()->add_coins(1);
302 if(coin_counter == 0)
303 sprite->set_action("empty");
305 } else if(breakable) {
306 if(playerhit && !player.is_big()) {
312 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
314 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
315 Vector(-150, -300)));
317 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
320 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
326 //IMPLEMENT_FACTORY(Brick, "brick");