* Fixed a typographic mistake in scripting.xml (closed a bracket)
authorOndřej Hošek <ondra.hosek@gmail.com>
Wed, 11 May 2005 05:22:46 +0000 (05:22 +0000)
committerOndřej Hošek <ondra.hosek@gmail.com>
Wed, 11 May 2005 05:22:46 +0000 (05:22 +0000)
* Added a pretty simple but versatile script-firing trigger.

SVN-Revision: 2465

docs/scripting/scripting.xml
src/trigger/scripttrigger.cpp [new file with mode: 0644]
src/trigger/scripttrigger.h [new file with mode: 0644]

index 69b8043..efbf97c 100644 (file)
@@ -253,7 +253,7 @@ WOOT.set_animation("left");
 
 <sect3><title>set_visible</title>
 <para>Usage: <code>Text.set_visible(bool visible)</code></para>
-<para>Effect: Shows or hides the text abruptly (drastic counterpart to <code>fade_in</code> and <code>fade_out</code>.</para>
+<para>Effect: Shows or hides the text abruptly (drastic counterpart to <code>fade_in</code> and <code>fade_out</code>).</para>
 </sect3>
 
 </sect2>
diff --git a/src/trigger/scripttrigger.cpp b/src/trigger/scripttrigger.cpp
new file mode 100644 (file)
index 0000000..1d2ccb8
--- /dev/null
@@ -0,0 +1,105 @@
+//  $Id$\r
+// \r
+//  SuperTux\r
+//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>\r
+//\r
+//  This program is free software; you can redistribute it and/or\r
+//  modify it under the terms of the GNU General Public License\r
+//  as published by the Free Software Foundation; either version 2\r
+//  of the License, or (at your option) any later version.\r
+//\r
+//  This program is distributed in the hope that it will be useful,\r
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+//  GNU General Public License for more details.\r
+// \r
+//  You should have received a copy of the GNU General Public License\r
+//  along with this program; if not, write to the Free Software\r
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA\r
+//  02111-1307, USA.\r
+//\r
+\r
+#include <config.h>\r
+#include <sstream>\r
+\r
+#include "scripttrigger.h"\r
+#include "game_session.h"\r
+#include "lisp/lisp.h"\r
+#include "lisp/writer.h"\r
+#include "object_factory.h"\r
+#include "scripting/script_interpreter.h"\r
+#include "sector.h"\r
+\r
+ScriptTrigger::ScriptTrigger(const lisp::Lisp& reader)\r
+{\r
+  bool must_activate;\r
+  \r
+  reader.get("x", bbox.p1.x);\r
+  reader.get("y", bbox.p1.y);\r
+  float w, h;\r
+  reader.get("width", w);\r
+  reader.get("height", h);\r
+  bbox.set_size(w, h);\r
+  reader.get("script", script);\r
+  reader.get("button", must_activate);\r
+  \r
+  if (must_activate)\r
+    triggerevent = EVENT_ACTIVATE;\r
+  else\r
+    triggerevent = EVENT_TOUCH;\r
+}\r
+\r
+ScriptTrigger::ScriptTrigger(const Vector& pos, const std::string& script)\r
+{\r
+  bbox.set_pos(pos);\r
+  bbox.set_size(32, 32);\r
+  this->script = script;\r
+  triggerevent = EVENT_TOUCH;\r
+}\r
+\r
+ScriptTrigger::~ScriptTrigger()\r
+{\r
+}\r
+\r
+void\r
+ScriptTrigger::write(lisp::Writer& writer)\r
+{\r
+  writer.start_list("scripttrigger");\r
+\r
+  writer.write_float("x", bbox.p1.x);\r
+  writer.write_float("y", bbox.p1.y);\r
+  writer.write_float("width", bbox.get_width());\r
+  writer.write_float("height", bbox.get_height());\r
+  writer.write_string("script", script);\r
+\r
+  writer.end_list("scripttrigger");\r
+}\r
+\r
+void\r
+ScriptTrigger::event(Player& , EventType type)\r
+{\r
+  if(type == triggerevent)\r
+  {\r
+    if (script != "")\r
+    {\r
+      try\r
+      {\r
+        ScriptInterpreter* interpreter = new ScriptInterpreter(Sector::current());\r
+        std::istringstream in(script);\r
+        interpreter->load_script(in, "trigger-script");\r
+        interpreter->start_script();\r
+        Sector::current()->add_object(interpreter);\r
+      }\r
+      catch(std::exception& e)\r
+      {\r
+          std::cerr << "Couldn't execute trigger script: " << e.what() << "\n";\r
+      }\r
+    }\r
+    else\r
+    {\r
+      std::cerr << "... and where's the trigger script I'm supposed to run?\n";\r
+    }\r
+  }\r
+}\r
+\r
+IMPLEMENT_FACTORY(ScriptTrigger, "scripttrigger")\r
diff --git a/src/trigger/scripttrigger.h b/src/trigger/scripttrigger.h
new file mode 100644 (file)
index 0000000..321be31
--- /dev/null
@@ -0,0 +1,42 @@
+//  $Id$
+//
+//  SuperTux
+//  Copyright (C) 2005 Matthias Braun <matze@braunis.de>
+//
+//  This program is free software; you can redistribute it and/or
+//  modify it under the terms of the GNU General Public License
+//  as published by the Free Software Foundation; either version 2
+//  of the License, or (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program; if not, write to the Free Software
+//  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+//  02111-1307, USA.
+
+#ifndef __SCRIPTTRIGGER_H__
+#define __SCRIPTTRIGGER_H__
+
+#include "trigger_base.h"
+#include "serializable.h"
+
+class ScriptTrigger : public TriggerBase, public Serializable
+{
+public:
+  ScriptTrigger(const lisp::Lisp& reader);
+  ScriptTrigger(const Vector& pos, const std::string& script);
+  ~ScriptTrigger();
+  
+  void write(lisp::Writer& writer);
+  void event(Player& player, EventType type);
+  
+private:
+  EventType triggerevent;
+  std::string script;
+};
+
+#endif