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