* Update squirrel to 2.2.3
[supertux.git] / src / trigger / switch.cpp
index 49205fb..b32e5ed 100644 (file)
 #include "sprite/sprite.hpp"
 #include "sprite/sprite_manager.hpp"
 #include "sector.hpp"
+#include "audio/sound_manager.hpp"
+
+namespace {
+ const std::string SWITCH_SOUND = "sounds/switch.ogg";
+}
 
 Switch::Switch(const lisp::Lisp& reader)
-       : state(OFF)
+    : state(OFF)
 {
   if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
   if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
@@ -36,6 +41,7 @@ Switch::Switch(const lisp::Lisp& reader)
   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
 
   if (!reader.get("script", script)) throw std::runtime_error("no script set");
+  sound_manager->preload( SWITCH_SOUND );
 }
 
 Switch::~Switch()
@@ -47,10 +53,10 @@ void
 Switch::write(lisp::Writer& writer)
 {
   writer.start_list("switch");
-  writer.write_float("x", bbox.p1.x);
-  writer.write_float("y", bbox.p1.y);
-  writer.write_string("sprite", sprite_name);
-  writer.write_string("script", script);
+  writer.write("x", bbox.p1.x);
+  writer.write("y", bbox.p1.y);
+  writer.write("sprite", sprite_name);
+  writer.write("script", script);
   writer.end_list("switch");
 }
 
@@ -62,23 +68,25 @@ Switch::update(float )
       break;
     case TURN_ON:
       if(sprite->animation_done()) {
-       std::istringstream stream(script);
-       Sector::current()->run_script(stream, "Switch");
+    std::istringstream stream(script);
+    std::ostringstream location;
+    location << "switch" << bbox.p1;
+    Sector::current()->run_script(stream, location.str());
 
-       sprite->set_action("on", 1);
-       state = ON;
+    sprite->set_action("on", 1);
+    state = ON;
       }
       break;
     case ON:
       if(sprite->animation_done()) {
-       sprite->set_action("turnoff", 1);
-       state = TURN_OFF;
+    sprite->set_action("turnoff", 1);
+    state = TURN_OFF;
       }
       break;
     case TURN_OFF:
       if(sprite->animation_done()) {
-       sprite->set_action("off");
-       state = OFF;
+    sprite->set_action("off");
+    state = OFF;
       }
       break;
   }
@@ -97,8 +105,9 @@ Switch::event(Player& , EventType type)
 
   switch (state) {
     case OFF:
-       sprite->set_action("turnon", 1);
-       state = TURN_ON;
+    sprite->set_action("turnon", 1);
+        sound_manager->play( SWITCH_SOUND );
+    state = TURN_ON;
       break;
     case TURN_ON:
       break;