- Refactored worldmap a bit to reuse GameObject from the rest of the game
[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 \r
25 #include "scripttrigger.h"\r
26 #include "game_session.h"\r
27 #include "lisp/lisp.h"\r
28 #include "lisp/writer.h"\r
29 #include "object_factory.h"\r
30 #include "scripting/script_interpreter.h"\r
31 #include "sector.h"\r
32 \r
33 ScriptTrigger::ScriptTrigger(const lisp::Lisp& reader)\r
34 {\r
35   bool must_activate;\r
36   \r
37   reader.get("x", bbox.p1.x);\r
38   reader.get("y", bbox.p1.y);\r
39   float w, h;\r
40   reader.get("width", w);\r
41   reader.get("height", h);\r
42   bbox.set_size(w, h);\r
43   reader.get("script", script);\r
44   reader.get("button", must_activate);\r
45   \r
46   if (must_activate)\r
47     triggerevent = EVENT_ACTIVATE;\r
48   else\r
49     triggerevent = EVENT_TOUCH;\r
50 }\r
51 \r
52 ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script)\r
53 {\r
54   bbox.set_pos(pos);\r
55   bbox.set_size(32, 32);\r
56   this->script = script;\r
57   triggerevent = EVENT_TOUCH;\r
58 }\r
59 \r
60 ScriptTrigger::~ScriptTrigger()\r
61 {\r
62 }\r
63 \r
64 void\r
65 ScriptTrigger::write(lisp::Writer& writer)\r
66 {\r
67   writer.start_list("scripttrigger");\r
68 \r
69   writer.write_float("x", bbox.p1.x);\r
70   writer.write_float("y", bbox.p1.y);\r
71   writer.write_float("width", bbox.get_width());\r
72   writer.write_float("height", bbox.get_height());\r
73   writer.write_string("script", script);\r
74   writer.write_bool("button", (triggerevent == EVENT_ACTIVATE) ? true : false);\r
75 \r
76   writer.end_list("scripttrigger");\r
77 }\r
78 \r
79 void\r
80 ScriptTrigger::event(Player& , EventType type)\r
81 {\r
82   if(type == triggerevent)\r
83   {\r
84     if (script != "")\r
85     {\r
86       try\r
87       {\r
88         ScriptInterpreter* interpreter = new ScriptInterpreter(Sector::current());\r
89         std::istringstream in(script);\r
90         interpreter->load_script(in, "trigger-script");\r
91         interpreter->start_script();\r
92         Sector::current()->add_object(interpreter);\r
93       }\r
94       catch(std::exception& e)\r
95       {\r
96           std::cerr << "Couldn't execute trigger script: " << e.what() << "\n";\r
97       }\r
98     }\r
99     else\r
100     {\r
101       std::cerr << "... and where's the trigger script I'm supposed to run?\n";\r
102     }\r
103   }\r
104 }\r
105 \r
106 IMPLEMENT_FACTORY(ScriptTrigger, "scripttrigger");\r
107 \r