- More work on scripting interface
[supertux.git] / src / trigger / secretarea_trigger.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2005 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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include "secretarea_trigger.h"
23 #include "game_session.h"
24 #include "lisp/lisp.h"
25 #include "lisp/writer.h"
26 #include "object_factory.h"
27 #include "main.h"
28
29 static const float MESSAGE_TIME=3.5;
30
31 //TODO: Count numbers of triggered/total secret areas
32 SecretAreaTrigger::SecretAreaTrigger(const lisp::Lisp& reader)
33 {
34   reader.get("x", bbox.p1.x);
35   reader.get("y", bbox.p1.y);
36   float w = 32, h = 32;
37   reader.get("width", w);
38   reader.get("height", h);
39   bbox.set_size(w, h);
40
41   reader.get("message", message);
42   message_displayed = false;
43 }
44
45 SecretAreaTrigger::SecretAreaTrigger(const Rectangle& area)
46 {
47   bbox = area;
48   message = "You found a secret area!";
49   message_displayed = false;
50 }
51
52 SecretAreaTrigger::~SecretAreaTrigger()
53 {
54 }
55
56 void
57 SecretAreaTrigger::write(lisp::Writer& writer)
58 {
59   writer.start_list("secretarea");
60
61   writer.write_float("x", bbox.p1.x);
62   writer.write_float("y", bbox.p1.y);
63   writer.write_float("width", bbox.get_width());
64   writer.write_float("height", bbox.get_height());
65   writer.write_string("message", message);
66
67   writer.end_list("secretarea");
68 }
69
70 void
71 SecretAreaTrigger::draw(DrawingContext& context)
72 {
73    if (message_timer.started()) {
74       context.push_transform();
75       context.set_translation(Vector(0, 0));
76       Vector pos = Vector(0, SCREEN_HEIGHT/2 - gold_text->get_height()/2);
77       context.draw_center_text(gold_text, message, pos, LAYER_GUI);
78       context.pop_transform();
79    }
80    if (message_timer.check()) {
81       remove_me();
82    }
83 }
84
85 void
86 SecretAreaTrigger::event(Player& , EventType type)
87 {
88   if(type == EVENT_TOUCH) {
89     if (!message_displayed) {
90       message_timer.start(MESSAGE_TIME);
91       message_displayed = true;
92     }
93   }
94 }
95
96 IMPLEMENT_FACTORY(SecretAreaTrigger, "secretarea");