Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[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 namespace {
28 const std::string SWITCH_SOUND = "sounds/switch.ogg";
29 }
30
31 Switch::Switch(const Reader& reader) :
32   state(OFF)
33 {
34   if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
35   if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
36   if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
37   sprite = sprite_manager->create(sprite_name);
38   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
39
40   if (!reader.get("script", script)) throw std::runtime_error("no script set");
41   sound_manager->preload( SWITCH_SOUND );
42 }
43
44 Switch::~Switch()
45 {
46 }
47
48 void
49 Switch::update(float )
50 {
51   switch (state) {
52     case OFF:
53       break;
54     case TURN_ON:
55       if(sprite->animation_done()) {
56         std::istringstream stream(script);
57         std::ostringstream location;
58         location << "switch" << bbox.p1;
59         Sector::current()->run_script(stream, location.str());
60
61         sprite->set_action("on", 1);
62         state = ON;
63       }
64       break;
65     case ON:
66       if(sprite->animation_done()) {
67         sprite->set_action("turnoff", 1);
68         state = TURN_OFF;
69       }
70       break;
71     case TURN_OFF:
72       if(sprite->animation_done()) {
73         sprite->set_action("off");
74         state = OFF;
75       }
76       break;
77   }
78 }
79
80 void
81 Switch::draw(DrawingContext& context)
82 {
83   sprite->draw(context, bbox.p1, LAYER_TILES);
84 }
85
86 void
87 Switch::event(Player& , EventType type)
88 {
89   if(type != EVENT_ACTIVATE) return;
90
91   switch (state) {
92     case OFF:
93       sprite->set_action("turnon", 1);
94       sound_manager->play( SWITCH_SOUND );
95       state = TURN_ON;
96       break;
97     case TURN_ON:
98       break;
99     case ON:
100       break;
101     case TURN_OFF:
102       break;
103   }
104
105 }
106
107 IMPLEMENT_FACTORY(Switch, "switch");
108
109 /* EOF */