- Change ScriptInterpreter to a gameobject, so we can now have several script
[supertux.git] / src / sector.cpp
index 46927eb..80f76c8 100644 (file)
@@ -16,7 +16,6 @@
 //  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.
-
 #include <config.h>
 
 #include <memory>
@@ -24,6 +23,7 @@
 #include <stdexcept>
 #include <iostream>
 #include <fstream>
+#include <sstream>
 #include <stdexcept>
 
 #include "sector.h"
 #include "object/block.h"
 #include "object/invisible_block.h"
 #include "object/bullet.h"
+#include "object/text_object.h"
 #include "badguy/jumpy.h"
 #include "badguy/spike.h"
 #include "trigger/sequence_trigger.h"
 #include "player_status.h"
+#include "scripting/script_interpreter.h"
+#include "scripting/sound.h"
+#include "scripting/scripted_object.h"
+#include "scripting/text.h"
 
 //#define USE_GRID
 
@@ -147,6 +152,8 @@ Sector::parse(const lisp::Lisp& sector)
       spawnpoint_lisp->get("x", sp->pos.x);
       spawnpoint_lisp->get("y", sp->pos.y);
       spawnpoints.push_back(sp);
+    } else if(token == "init-script") {
+      iter.value()->get(init_script);
     } else {
       GameObject* object = parse_object(token, *(iter.lisp()));
       if(object) {
@@ -411,6 +418,21 @@ Sector::activate(const std::string& spawnpoint)
   } else {
     activate(sp->pos);
   }
+
+  // Run init script
+  if(init_script != "") {
+    try {
+      ScriptInterpreter* interpreter = new ScriptInterpreter(this);
+      std::string sourcename = std::string("Sector(") + name + ") - init";
+      std::istringstream in(init_script);
+      interpreter->load_script(in, sourcename);
+      interpreter->start_script();
+      add_object(interpreter);
+      init_script = "";
+    } catch(std::exception& e) {
+      std::cerr << "Couldn't execute init script: " << e.what() << "\n";
+    }
+  }
 }
 
 void