Updated addon repository URL and improved debug output on download
[supertux.git] / src / trigger / switch.cpp
1 //  SuperTux - Switch Trigger
2 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include <config.h>
18 #include <stdexcept>
19
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"
26
27 #include <sstream>
28
29 namespace {
30 const std::string SWITCH_SOUND = "sounds/switch.ogg";
31 }
32
33 Switch::Switch(const Reader& reader) :
34   sprite_name(),
35   sprite(),
36   script(),
37   state(OFF)
38 {
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());
44
45   if (!reader.get("script", script)) throw std::runtime_error("no script set");
46   SoundManager::current()->preload( SWITCH_SOUND );
47 }
48
49 Switch::~Switch()
50 {
51 }
52
53 void
54 Switch::update(float )
55 {
56   switch (state) {
57     case OFF:
58       break;
59     case TURN_ON:
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());
65
66         sprite->set_action("on", 1);
67         state = ON;
68       }
69       break;
70     case ON:
71       if(sprite->animation_done()) {
72         sprite->set_action("turnoff", 1);
73         state = TURN_OFF;
74       }
75       break;
76     case TURN_OFF:
77       if(sprite->animation_done()) {
78         sprite->set_action("off");
79         state = OFF;
80       }
81       break;
82   }
83 }
84
85 void
86 Switch::draw(DrawingContext& context)
87 {
88   sprite->draw(context, bbox.p1, LAYER_TILES);
89 }
90
91 void
92 Switch::event(Player& , EventType type)
93 {
94   if(type != EVENT_ACTIVATE) return;
95
96   switch (state) {
97     case OFF:
98       sprite->set_action("turnon", 1);
99       SoundManager::current()->play( SWITCH_SOUND );
100       state = TURN_ON;
101       break;
102     case TURN_ON:
103       break;
104     case ON:
105       break;
106     case TURN_OFF:
107       break;
108   }
109
110 }
111
112 /* EOF */