1731a3954770f6eec1f1c0511b80d635b285c228
[supertux.git] / src / object / invisible_block.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21
22 #include "invisible_block.hpp"
23 #include "resources.hpp"
24 #include "sprite/sprite.hpp"
25 #include "sprite/sprite_manager.hpp"
26 #include "video/drawing_context.hpp"
27 #include "audio/sound_manager.hpp"
28 #include "object_factory.hpp"
29 #include "object/player.hpp"
30 #include "constants.hpp"
31
32 InvisibleBlock::InvisibleBlock(const Vector& pos)
33   : Block(sprite_manager->create("images/objects/bonus_block/invisibleblock.sprite")), visible(false)
34 {
35   bbox.set_pos(pos);
36   sound_manager->preload("sounds/brick.wav");
37 }
38
39 void
40 InvisibleBlock::draw(DrawingContext& context)
41 {
42   if(visible)
43     sprite->draw(context, get_pos(), LAYER_OBJECTS);
44 }
45
46 bool
47 InvisibleBlock::collides(GameObject& other, const CollisionHit& )
48 {
49   if(visible)
50     return true;
51
52   // if we're not visible, only register a collision if this will make us visible
53   Player* player = dynamic_cast<Player*> (&other);
54   if ((player) 
55           && (player->get_movement().y <= 0)
56           && (player->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA)) {
57     return true;
58   }
59
60   return false;
61 }
62
63 HitResponse
64 InvisibleBlock::collision(GameObject& other, const CollisionHit& hit)
65 {
66   return Block::collision(other, hit);
67 }
68
69 void
70 InvisibleBlock::hit(Player& player)
71 {
72   sound_manager->play("sounds/brick.wav");
73
74   if(visible)
75     return;
76
77   sprite->set_action("empty");
78   start_bounce(&player);
79   set_group(COLGROUP_STATIC);
80   visible = true;
81 }
82
83 //IMPLEMENT_FACTORY(InvisibleBlock, "invisible_block");