1// SuperTux
2// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "object/invisible_block.hpp"
18
19#include "audio/sound_manager.hpp"
20#include "editor/editor.hpp"
21#include "object/player.hpp"
22#include "sprite/sprite.hpp"
23#include "sprite/sprite_manager.hpp"
24#include "supertux/constants.hpp"
25
26InvisibleBlock::InvisibleBlock(const Vector& pos) :
27 Block(SpriteManager::current()->create("images/objects/bonus_block/invisibleblock.sprite")),
28 visible(false)
29{
30 m_col.m_bbox.set_pos(pos);
31 SoundManager::current()->preload("sounds/brick.wav");
32 m_sprite->set_action("default-editor");
33}
34
35InvisibleBlock::InvisibleBlock(const ReaderMapping& mapping) :
36 Block(mapping, "images/objects/bonus_block/invisibleblock.sprite"),
37 visible(false)
38{
39 SoundManager::current()->preload("sounds/brick.wav");
40}
41
42void
43InvisibleBlock::draw(DrawingContext& context)
44{
45 if (visible || Editor::is_active())
46 m_sprite->draw(context.color(), get_pos(), LAYER_OBJECTS);
47}
48
49bool
50InvisibleBlock::collides(GameObject& other, const CollisionHit& ) const
51{
52 if (visible)
53 return true;
54
55 // if we're not visible, only register a collision if this will make us visible
56 auto player = dynamic_cast<Player*> (&other);
57 if ((player)
58 && (player->get_movement().y <= 0)
59 && (player->get_bbox().get_top() > get_bbox().get_bottom() - SHIFT_DELTA)) {
60 return true;
61 }
62
63 return false;
64}
65
66HitResponse
67InvisibleBlock::collision(GameObject& other, const CollisionHit& hit_)
68{
69 return Block::collision(other, hit_);
70}
71
72void
73InvisibleBlock::hit(Player& player)
74{
75 SoundManager::current()->play("sounds/brick.wav");
76
77 if (visible)
78 return;
79
80 m_sprite->set_action("empty");
81 start_bounce(&player);
82 set_group(COLGROUP_STATIC);
83 visible = true;
84}
85
86/* EOF */
87