3 // SuperTux - Switch Trigger
4 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "object_factory.hpp"
25 #include "sprite/sprite.hpp"
26 #include "sprite/sprite_manager.hpp"
28 #include "audio/sound_manager.hpp"
31 const std::string SWITCH_SOUND = "sounds/switch.ogg";
34 Switch::Switch(const lisp::Lisp& reader)
37 if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
38 if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
39 if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
40 sprite = sprite_manager->create(sprite_name);
41 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
43 if (!reader.get("script", script)) throw std::runtime_error("no script set");
44 sound_manager->preload( SWITCH_SOUND );
53 Switch::write(lisp::Writer& writer)
55 writer.start_list("switch");
56 writer.write_float("x", bbox.p1.x);
57 writer.write_float("y", bbox.p1.y);
58 writer.write_string("sprite", sprite_name);
59 writer.write_string("script", script);
60 writer.end_list("switch");
64 Switch::update(float )
70 if(sprite->animation_done()) {
71 std::istringstream stream(script);
72 Sector::current()->run_script(stream, "Switch");
74 sprite->set_action("on", 1);
79 if(sprite->animation_done()) {
80 sprite->set_action("turnoff", 1);
85 if(sprite->animation_done()) {
86 sprite->set_action("off");
94 Switch::draw(DrawingContext& context)
96 sprite->draw(context, bbox.p1, LAYER_TILES);
100 Switch::event(Player& , EventType type)
102 if(type != EVENT_ACTIVATE) return;
106 sprite->set_action("turnon", 1);
107 sound_manager->play( SWITCH_SOUND );
120 IMPLEMENT_FACTORY(Switch, "switch");