1 // SuperTux - Switch Trigger
2 // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "audio/sound_manager.hpp"
21 #include "sprite/sprite.hpp"
22 #include "sprite/sprite_manager.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/sector.hpp"
25 #include "trigger/switch.hpp"
28 const std::string SWITCH_SOUND = "sounds/switch.ogg";
31 Switch::Switch(const Reader& 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 );
52 Switch::update(float )
58 if(sprite->animation_done()) {
59 std::istringstream stream(script);
60 std::ostringstream location;
61 location << "switch" << bbox.p1;
62 Sector::current()->run_script(stream, location.str());
64 sprite->set_action("on", 1);
69 if(sprite->animation_done()) {
70 sprite->set_action("turnoff", 1);
75 if(sprite->animation_done()) {
76 sprite->set_action("off");
84 Switch::draw(DrawingContext& context)
86 sprite->draw(context, bbox.p1, LAYER_TILES);
90 Switch::event(Player& , EventType type)
92 if(type != EVENT_ACTIVATE) return;
96 sprite->set_action("turnon", 1);
97 sound_manager->play( SWITCH_SOUND );
110 IMPLEMENT_FACTORY(Switch, "switch");