b35643032bea31e7d5a9203ee3ab1c41429814d0
[supertux.git] / src / trigger / secretarea_trigger.cpp
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 "object/tilemap.hpp"
20 #include "supertux/level.hpp"
21 #include "supertux/globals.hpp"
22 #include "supertux/object_factory.hpp"
23 #include "supertux/resources.hpp"
24 #include "supertux/sector.hpp"
25 #include "util/gettext.hpp"
26 #include "util/reader.hpp"
27 #include "util/writer.hpp"
28
29 static const float MESSAGE_TIME=3.5;
30
31 SecretAreaTrigger::SecretAreaTrigger(const Reader& reader) :
32   message_timer(),
33   message_displayed(),
34   message(),
35   fade_tilemap(),
36   script()
37 {
38   reader.get("x", bbox.p1.x);
39   reader.get("y", bbox.p1.y);
40   float w = 32, h = 32;
41   reader.get("width", w);
42   reader.get("height", h);
43   bbox.set_size(w, h);
44   reader.get("fade-tilemap", fade_tilemap);
45   reader.get("message", message);
46   if(message == "") {
47     message = _("You found a secret area!");
48   }
49   reader.get("script", script);
50
51   message_displayed = false;
52 }
53
54 SecretAreaTrigger::SecretAreaTrigger(const Rectf& area, std::string fade_tilemap) :
55   message_timer(),
56   message_displayed(),
57   message(_("You found a secret area!")),
58   fade_tilemap(fade_tilemap),
59   script()
60 {
61   bbox = area;
62   message_displayed = false;
63 }
64
65 SecretAreaTrigger::~SecretAreaTrigger()
66 {
67 }
68
69 void
70 SecretAreaTrigger::draw(DrawingContext& context)
71 {
72   if (message_timer.started()) {
73     context.push_transform();
74     context.set_translation(Vector(0, 0));
75     Vector pos = Vector(0, SCREEN_HEIGHT/2 - Resources::normal_font->get_height()/2);
76     context.draw_center_text(Resources::normal_font, message, pos, LAYER_HUD, SecretAreaTrigger::text_color);
77     context.pop_transform();
78   }
79   if (message_timer.check()) {
80     remove_me();
81   }
82 }
83
84 void
85 SecretAreaTrigger::event(Player& , EventType type)
86 {
87   if(type == EVENT_TOUCH) {
88     if (!message_displayed) {
89       message_timer.start(MESSAGE_TIME);
90       message_displayed = true;
91       Sector::current()->get_level()->stats.secrets++;
92
93       if (fade_tilemap != "") {
94         // fade away tilemaps
95         Sector& sector = *Sector::current();
96         for(Sector::GameObjects::iterator i = sector.gameobjects.begin(); i != sector.gameobjects.end(); ++i) {
97           TileMap* tm = dynamic_cast<TileMap*>(*i);
98           if (!tm) continue;
99           if (tm->get_name() != fade_tilemap) continue;
100           tm->fade(0.0, 1.0);
101         }
102       }
103
104       if(script != "") {
105         std::istringstream stream(script);
106         Sector::current()->run_script(stream, "SecretAreaScript");
107       }
108     }
109   }
110 }
111
112 /* EOF */