* Fixed a few own mistakes from a previous commit
[supertux.git] / src / trigger / scripttrigger.cpp
1 //  $Id$\r
2 // \r
3 //  SuperTux\r
4 //  Copyright (C) 2005 Matthias Braun <matze@braunis.de>\r
5 //\r
6 //  This program is free software; you can redistribute it and/or\r
7 //  modify it under the terms of the GNU General Public License\r
8 //  as published by the Free Software Foundation; either version 2\r
9 //  of the License, or (at your option) any later version.\r
10 //\r
11 //  This program is distributed in the hope that it will be useful,\r
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 //  GNU General Public License for more details.\r
15 // \r
16 //  You should have received a copy of the GNU General Public License\r
17 //  along with this program; if not, write to the Free Software\r
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA\r
19 //  02111-1307, USA.\r
20 //\r
21 #include <config.h>\r
22 \r
23 #include <sstream>\r
24 #include <stdexcept>\r
25 #include <memory>\r
26 \r
27 #include "scripttrigger.hpp"\r
28 #include "game_session.hpp"\r
29 #include "lisp/lisp.hpp"\r
30 #include "lisp/writer.hpp"\r
31 #include "object_factory.hpp"\r
32 #include "scripting/script_interpreter.hpp"\r
33 #include "sector.hpp"\r
34 \r
35 ScriptTrigger::ScriptTrigger(const lisp::Lisp& reader)\r
36 {\r
37   bool must_activate = false;\r
38   \r
39   reader.get("x", bbox.p1.x);\r
40   reader.get("y", bbox.p1.y);\r
41   float w = 0, h = 0;\r
42   reader.get("width", w);\r
43   reader.get("height", h);\r
44   bbox.set_size(w, h);\r
45   reader.get("script", script);\r
46   reader.get("button", must_activate);\r
47   if(script == "") {\r
48     throw std::runtime_error("Need to specify a script for trigger object");\r
49   }\r
50   \r
51   if (must_activate)\r
52     triggerevent = EVENT_ACTIVATE;\r
53   else\r
54     triggerevent = EVENT_TOUCH;\r
55 }\r
56 \r
57 ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script)\r
58 {\r
59   bbox.set_pos(pos);\r
60   bbox.set_size(32, 32);\r
61   this->script = script;\r
62   triggerevent = EVENT_TOUCH;\r
63 }\r
64 \r
65 ScriptTrigger::~ScriptTrigger()\r
66 {\r
67 }\r
68 \r
69 void\r
70 ScriptTrigger::write(lisp::Writer& writer)\r
71 {\r
72   writer.start_list("scripttrigger");\r
73 \r
74   writer.write_float("x", bbox.p1.x);\r
75   writer.write_float("y", bbox.p1.y);\r
76   writer.write_float("width", bbox.get_width());\r
77   writer.write_float("height", bbox.get_height());\r
78   writer.write_string("script", script);\r
79   writer.write_bool("button", (triggerevent == EVENT_ACTIVATE) ? true : false);\r
80 \r
81   writer.end_list("scripttrigger");\r
82 }\r
83 \r
84 void\r
85 ScriptTrigger::event(Player& , EventType type)\r
86 {\r
87   if(type != triggerevent)\r
88     return;\r
89 \r
90   ScriptInterpreter::add_script_object(Sector::current(), "trigger - scritp",\r
91       script);\r
92 }\r
93 \r
94 IMPLEMENT_FACTORY(ScriptTrigger, "scripttrigger");\r
95 \r