From: Matthias Braun Date: Thu, 5 May 2005 17:17:40 +0000 (+0000) Subject: Here we go, the first script embedded in a level says hello to the world :) X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=11ea32de7fcc2d667b13f95bb7097d808e6b0dd4;p=supertux.git Here we go, the first script embedded in a level says hello to the world :) SVN-Revision: 2410 --- diff --git a/data/levels/test/script.stl b/data/levels/test/script.stl new file mode 100644 index 000000000..3c207ea05 --- /dev/null +++ b/data/levels/test/script.stl @@ -0,0 +1,111 @@ +;; Generated by Flexlay Editor +(supertux-level + (version 2) + (name (_ "Scripting Test")) + (author "Matthias Braun") + (sector + (name "main") + (music "salcon.mod") + (gravity 10.000000) + (init-script "wait(2); +print(\"Hello from the script\\n\"); +") + (tilemap + (layer "background") + (solid #f) + (speed 1.000000) + (width 25) + (height 20) + (tiles + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 124 210 210 125 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 124 210 210 125 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 124 210 210 125 0 0 0 0 0 0 0 0 0 + 0 0 124 125 0 0 0 0 0 0 0 0 124 210 210 125 0 0 0 0 0 0 0 0 0 + 0 0 124 125 0 0 0 0 0 0 0 0 124 210 210 125 0 0 124 125 0 0 0 0 0 + 0 0 124 125 0 0 25 0 0 0 24 0 124 210 210 125 0 0 124 125 0 0 24 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + )) + (tilemap + (layer "interactive") + (solid #t) + (speed 1.000000) + (width 25) + (height 20) + (tiles + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 122 209 209 123 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 122 123 0 0 0 0 0 0 0 0 0 0 402 403 404 405 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 406 407 408 409 122 123 0 0 0 0 0 + 0 403 404 0 0 0 0 0 0 0 0 0 0 0 410 451 412 413 0 0 0 0 0 0 0 + 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 + 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 + 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 + 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 + 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 + )) + (tilemap + (layer "foreground") + (solid #f) + (speed 1.000000) + (width 25) + (height 20) + (tiles + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 402 403 404 405 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 406 407 408 409 0 0 0 0 0 0 0 + 0 403 404 0 0 0 0 0 0 0 0 0 0 0 410 411 412 413 0 0 0 0 0 0 0 + 0 407 408 0 0 0 0 0 0 0 0 0 0 0 414 423 416 425 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + )) + (camera + (mode "normal") + ) + + (background + (image "semi_arctic.jpg") + (speed 0.500000) + ) + (spawnpoint (name "main") (x 320) (y 384)) + (particles-snow + ) + ) + ) + +;; EOF ;; diff --git a/src/sector.cpp b/src/sector.cpp index 46927eb60..657996f33 100644 --- a/src/sector.cpp +++ b/src/sector.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "sector.h" @@ -56,6 +57,7 @@ #include "badguy/spike.h" #include "trigger/sequence_trigger.h" #include "player_status.h" +#include "scripting/script_interpreter.h" //#define USE_GRID @@ -63,7 +65,7 @@ Sector* Sector::_current = 0; Sector::Sector() : gravity(10), player(0), solids(0), camera(0), - currentmusic(LEVEL_MUSIC) + interpreter(0), currentmusic(LEVEL_MUSIC) { song_title = "Mortimers_chipdisko.mod"; player = new Player(&player_status); @@ -147,6 +149,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 +415,25 @@ Sector::activate(const std::string& spawnpoint) } else { activate(sp->pos); } + + // Run init script + if(init_script != "") { + try { + // TODO we should keep the interpreter across sessions (or some variables) + // so that you can store information across levels/sectors... + delete interpreter; + interpreter = 0; + interpreter = new ScriptInterpreter(); + std::string sourcename = std::string("Sector(") + name + ") - init"; + std::istringstream in(init_script); + printf("Load script.\n"); + interpreter->load_script(in, sourcename); + printf("run script.\n"); + interpreter->run_script(); + } catch(std::exception& e) { + std::cerr << "Couldn't execute init script: " << e.what() << "\n"; + } + } } void diff --git a/src/sector.h b/src/sector.h index c2a8f3c47..8cec44ceb 100644 --- a/src/sector.h +++ b/src/sector.h @@ -40,6 +40,7 @@ class Camera; class TileMap; class Bullet; class CollisionGrid; +class ScriptInterpreter; class SpawnPoint { @@ -134,6 +135,9 @@ public: private: std::vector bullets; + ScriptInterpreter* interpreter; + std::string init_script; + public: // TODO make this private again typedef std::vector GameObjects; GameObjects gameobjects;