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 "trigger/secretarea_trigger.hpp"
18
19#include "audio/sound_manager.hpp"
20#include "editor/editor.hpp"
21#include "object/tilemap.hpp"
22#include "supertux/debug.hpp"
23#include "supertux/level.hpp"
24#include "supertux/resources.hpp"
25#include "supertux/sector.hpp"
26#include "util/reader_mapping.hpp"
27#include "video/drawing_context.hpp"
28#include "video/video_system.hpp"
29#include "video/viewport.hpp"
30
31static const float MESSAGE_TIME=3.5;
32
33SecretAreaTrigger::SecretAreaTrigger(const ReaderMapping& reader) :
34 TriggerBase(reader),
35 message_timer(),
36 message_displayed(false),
37 message(),
38 fade_tilemap(),
39 script(),
40 new_size()
41{
42 reader.get("x", m_col.m_bbox.get_left());
43 reader.get("y", m_col.m_bbox.get_top());
44 float w,h;
45 reader.get("width", w, 32.0f);
46 reader.get("height", h, 32.0f);
47 m_col.m_bbox.set_size(w, h);
48 new_size.x = w;
49 new_size.y = h;
50 reader.get("fade-tilemap", fade_tilemap);
51 reader.get("message", message);
52 if (message.empty() && !Editor::is_active()) {
53 message = _("You found a secret area!");
54 }
55 reader.get("script", script);
56}
57
58SecretAreaTrigger::SecretAreaTrigger(const Rectf& area, const std::string& fade_tilemap_) :
59 message_timer(),
60 message_displayed(false),
61 message(_("You found a secret area!")),
62 fade_tilemap(fade_tilemap_),
63 script(),
64 new_size()
65{
66 m_col.m_bbox = area;
67}
68
69ObjectSettings
70SecretAreaTrigger::get_settings()
71{
72 new_size.x = m_col.m_bbox.get_width();
73 new_size.y = m_col.m_bbox.get_height();
74
75 ObjectSettings result = TriggerBase::get_settings();
76
77 result.add_text(_("Name"), &m_name);
78 result.add_text(_("Fade tilemap"), &fade_tilemap, "fade-tilemap");
79 result.add_translatable_text(_("Message"), &message, "message");
80 result.add_script(_("Script"), &script, "script");
81
82 result.reorder({"fade-tilemap", "script", "sprite", "message", "region", "name", "x", "y"});
83
84 return result;
85}
86
87void
88SecretAreaTrigger::after_editor_set()
89{
90 m_col.m_bbox.set_size(new_size.x, new_size.y);
91}
92
93std::string
94SecretAreaTrigger::get_fade_tilemap_name() const
95{
96 return fade_tilemap;
97}
98
99void
100SecretAreaTrigger::draw(DrawingContext& context)
101{
102 if (message_timer.started()) {
103 context.push_transform();
104 context.set_translation(Vector(0, 0));
105 Vector pos = Vector(0, static_cast<float>(SCREEN_HEIGHT) / 2.0f - Resources::normal_font->get_height() / 2.0f);
106 context.color().draw_center_text(Resources::normal_font, message, pos, LAYER_HUD, SecretAreaTrigger::text_color);
107 context.pop_transform();
108 }
109 if (Editor::is_active() || g_debug.show_collision_rects) {
110 context.color().draw_filled_rect(m_col.m_bbox, Color(0.0f, 1.0f, 0.0f, 0.6f),
111 0.0f, LAYER_OBJECTS);
112 } else if (message_timer.check()) {
113 remove_me();
114 }
115}
116
117void
118SecretAreaTrigger::event(Player& , EventType type)
119{
120 if (type == EVENT_TOUCH) {
121 if (!message_displayed) {
122 message_timer.start(MESSAGE_TIME);
123 message_displayed = true;
124 Sector::get().get_level().m_stats.m_secrets++;
125 SoundManager::current()->play("sounds/excellent.wav");
126
127 if (!fade_tilemap.empty()) {
128 // fade away tilemaps
129 for (auto& tm : Sector::get().get_objects_by_type<TileMap>()) {
130 if (tm.get_name() == fade_tilemap) {
131 tm.fade(0.0, 1.0);
132 }
133 }
134 }
135
136 if (!script.empty()) {
137 Sector::get().run_script(script, "SecretAreaScript");
138 }
139 }
140 }
141}
142
143/* EOF */
144