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& hitdata)
69 Player* player = dynamic_cast<Player*> (&other);
71 // collided from below?
72 if(hitdata.normal.x == 0 && hitdata.normal.y < 0) {
78 BadGuy* badguy = dynamic_cast<BadGuy*> (&other);
82 Coin* coin = dynamic_cast<Coin*> (&other);
92 Block::update(float elapsed_time)
97 float offset = original_y - get_pos().y;
98 if(offset > BOUNCY_BRICK_MAX_OFFSET) {
99 bounce_dir = BOUNCY_BRICK_SPEED;
100 movement = Vector(0, bounce_dir * elapsed_time);
101 } else if(offset < BOUNCY_BRICK_SPEED * elapsed_time && bounce_dir > 0) {
102 movement = Vector(0, offset);
106 movement = Vector(0, bounce_dir * elapsed_time);
111 Block::draw(DrawingContext& context)
113 sprite->draw(context, get_pos(), LAYER_OBJECTS+1);
117 Block::start_bounce()
119 original_y = bbox.p1.y;
121 bounce_dir = -BOUNCY_BRICK_SPEED;
125 //---------------------------------------------------------------------------
127 BonusBlock::BonusBlock(const Vector& pos, int data)
128 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite")), object(0)
131 sprite->set_action("normal");
133 case 1: contents = CONTENT_COIN; break;
134 case 2: contents = CONTENT_FIREGROW; break;
135 case 3: contents = CONTENT_STAR; break;
136 case 4: contents = CONTENT_1UP; break;
137 case 5: contents = CONTENT_ICEGROW; break;
139 log_warning << "Invalid box contents" << std::endl;
140 contents = CONTENT_COIN;
145 BonusBlock::BonusBlock(const lisp::Lisp& lisp)
146 : Block(sprite_manager->create("images/objects/bonus_block/bonusblock.sprite"))
150 contents = CONTENT_COIN;
151 lisp::ListIterator iter(&lisp);
153 const std::string& token = iter.item();
155 iter.value()->get(pos.x);
156 } else if(token == "y") {
157 iter.value()->get(pos.y);
158 } else if(token == "contents") {
159 std::string contentstring;
160 iter.value()->get(contentstring);
161 if(contentstring == "coin") {
162 contents = CONTENT_COIN;
163 } else if(contentstring == "firegrow") {
164 contents = CONTENT_FIREGROW;
165 } else if(contentstring == "icegrow") {
166 contents = CONTENT_ICEGROW;
167 } else if(contentstring == "star") {
168 contents = CONTENT_STAR;
169 } else if(contentstring == "1up") {
170 contents = CONTENT_1UP;
171 } else if(contentstring == "custom") {
172 contents = CONTENT_CUSTOM;
174 log_warning << "Invalid box contents '" << contentstring << "'" << std::endl;
177 if(contents == CONTENT_CUSTOM) {
178 GameObject* game_object = create_object(token, *(iter.lisp()));
179 object = dynamic_cast<MovingObject*> (game_object);
181 throw std::runtime_error(
182 "Only MovingObjects are allowed inside BonusBlocks");
184 log_warning << "Invalid element '" << token << "' in bonusblock" << std::endl;
189 if(contents == CONTENT_CUSTOM && object == 0)
190 throw std::runtime_error("Need to specify content object for custom block");
195 BonusBlock::~BonusBlock()
201 BonusBlock::hit(Player& )
207 BonusBlock::try_open()
209 if(sprite->get_action() == "empty") {
210 sound_manager->play("sounds/brick.wav");
214 Sector* sector = Sector::current();
215 Player& player = *(sector->player);
218 Sector::current()->add_object(new BouncyCoin(get_pos()));
219 player.get_status()->add_coins(1);
222 case CONTENT_FIREGROW:
223 if(player.get_status()->bonus == NO_BONUS) {
224 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
225 sector->add_object(riser);
227 SpecialRiser* riser = new SpecialRiser(
228 get_pos(), new Flower(Flower::FIREFLOWER));
229 sector->add_object(riser);
231 sound_manager->play("sounds/upgrade.wav");
234 case CONTENT_ICEGROW:
235 if(player.get_status()->bonus == NO_BONUS) {
236 SpecialRiser* riser = new SpecialRiser(get_pos(), new GrowUp());
237 sector->add_object(riser);
239 SpecialRiser* riser = new SpecialRiser(
240 get_pos(), new Flower(Flower::ICEFLOWER));
241 sector->add_object(riser);
243 sound_manager->play("sounds/upgrade.wav");
247 sector->add_object(new Star(get_pos() + Vector(0, -32)));
251 sector->add_object(new OneUp(get_pos()));
255 SpecialRiser* riser = new SpecialRiser(get_pos(), object);
257 sector->add_object(riser);
258 sound_manager->play("sounds/upgrade.wav");
266 sprite->set_action("empty");
269 IMPLEMENT_FACTORY(BonusBlock, "bonusblock");
271 //---------------------------------------------------------------------------
273 Brick::Brick(const Vector& pos, int data)
274 : Block(sprite_manager->create("images/objects/bonus_block/brick.sprite")), breakable(false),
287 if(sprite->get_action() == "empty")
294 Brick::try_break(bool playerhit)
296 if(sprite->get_action() == "empty")
299 sound_manager->play("sounds/brick.wav");
300 Sector* sector = Sector::current();
301 Player& player = *(sector->player);
302 if(coin_counter > 0) {
303 sector->add_object(new BouncyCoin(get_pos()));
305 player.get_status()->add_coins(1);
306 if(coin_counter == 0)
307 sprite->set_action("empty");
309 } else if(breakable) {
310 if(playerhit && !player.is_big()) {
316 new BrokenBrick(new Sprite(*sprite), get_pos(), Vector(-100, -400)));
318 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(0, 16),
319 Vector(-150, -300)));
321 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 0),
324 new BrokenBrick(new Sprite(*sprite), get_pos() + Vector(16, 16),
330 //IMPLEMENT_FACTORY(Brick, "brick");