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"
30 const std::string SWITCH_SOUND = "sounds/switch.ogg";
33 Switch::Switch(const Reader& reader) :
39 if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
40 if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
41 if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
42 sprite = SpriteManager::current()->create(sprite_name);
43 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
45 if (!reader.get("script", script)) throw std::runtime_error("no script set");
46 SoundManager::current()->preload( SWITCH_SOUND );
54 Switch::update(float )
60 if(sprite->animation_done()) {
61 std::istringstream stream(script);
62 std::ostringstream location;
63 location << "switch" << bbox.p1;
64 Sector::current()->run_script(stream, location.str());
66 sprite->set_action("on", 1);
71 if(sprite->animation_done()) {
72 sprite->set_action("turnoff", 1);
77 if(sprite->animation_done()) {
78 sprite->set_action("off");
86 Switch::draw(DrawingContext& context)
88 sprite->draw(context, bbox.p1, LAYER_TILES);
92 Switch::event(Player& , EventType type)
94 if(type != EVENT_ACTIVATE) return;
98 sprite->set_action("turnon", 1);
99 SoundManager::current()->play( SWITCH_SOUND );