b32e5eded69147e0b6636eb54a2b36af151c29ab
[supertux.git] / src / trigger / switch.cpp
1 //  $Id$
2 //
3 //  SuperTux - Switch Trigger
4 //  Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
5 //
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.
10 //
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.
15 //
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.
19
20 #include <config.h>
21 #include <stdexcept>
22
23 #include "switch.hpp"
24 #include "object_factory.hpp"
25 #include "sprite/sprite.hpp"
26 #include "sprite/sprite_manager.hpp"
27 #include "sector.hpp"
28 #include "audio/sound_manager.hpp"
29
30 namespace {
31  const std::string SWITCH_SOUND = "sounds/switch.ogg";
32 }
33
34 Switch::Switch(const lisp::Lisp& reader)
35     : state(OFF)
36 {
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());
42
43   if (!reader.get("script", script)) throw std::runtime_error("no script set");
44   sound_manager->preload( SWITCH_SOUND );
45 }
46
47 Switch::~Switch()
48 {
49   delete sprite;
50 }
51
52 void
53 Switch::write(lisp::Writer& writer)
54 {
55   writer.start_list("switch");
56   writer.write("x", bbox.p1.x);
57   writer.write("y", bbox.p1.y);
58   writer.write("sprite", sprite_name);
59   writer.write("script", script);
60   writer.end_list("switch");
61 }
62
63 void
64 Switch::update(float )
65 {
66   switch (state) {
67     case OFF:
68       break;
69     case TURN_ON:
70       if(sprite->animation_done()) {
71     std::istringstream stream(script);
72     std::ostringstream location;
73     location << "switch" << bbox.p1;
74     Sector::current()->run_script(stream, location.str());
75
76     sprite->set_action("on", 1);
77     state = ON;
78       }
79       break;
80     case ON:
81       if(sprite->animation_done()) {
82     sprite->set_action("turnoff", 1);
83     state = TURN_OFF;
84       }
85       break;
86     case TURN_OFF:
87       if(sprite->animation_done()) {
88     sprite->set_action("off");
89     state = OFF;
90       }
91       break;
92   }
93 }
94
95 void
96 Switch::draw(DrawingContext& context)
97 {
98   sprite->draw(context, bbox.p1, LAYER_TILES);
99 }
100
101 void
102 Switch::event(Player& , EventType type)
103 {
104   if(type != EVENT_ACTIVATE) return;
105
106   switch (state) {
107     case OFF:
108     sprite->set_action("turnon", 1);
109         sound_manager->play( SWITCH_SOUND );
110     state = TURN_ON;
111       break;
112     case TURN_ON:
113       break;
114     case ON:
115       break;
116     case TURN_OFF:
117       break;
118   }
119
120 }
121
122 IMPLEMENT_FACTORY(Switch, "switch");