fix cr/lfs and remove trailing whitespaces...
[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
29 Switch::Switch(const lisp::Lisp& reader)
30         : state(OFF)
31 {
32   if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
33   if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
34   if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
35   sprite = sprite_manager->create(sprite_name);
36   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
37
38   if (!reader.get("script", script)) throw std::runtime_error("no script set");
39 }
40
41 Switch::~Switch()
42 {
43   delete sprite;
44 }
45
46 void
47 Switch::write(lisp::Writer& writer)
48 {
49   writer.start_list("switch");
50   writer.write_float("x", bbox.p1.x);
51   writer.write_float("y", bbox.p1.y);
52   writer.write_string("sprite", sprite_name);
53   writer.write_string("script", script);
54   writer.end_list("switch");
55 }
56
57 void
58 Switch::update(float )
59 {
60   switch (state) {
61     case OFF:
62       break;
63     case TURN_ON:
64       if(sprite->animation_done()) {
65         std::istringstream stream(script);
66         Sector::current()->run_script(stream, "Switch");
67
68         sprite->set_action("on", 1);
69         state = ON;
70       }
71       break;
72     case ON:
73       if(sprite->animation_done()) {
74         sprite->set_action("turnoff", 1);
75         state = TURN_OFF;
76       }
77       break;
78     case TURN_OFF:
79       if(sprite->animation_done()) {
80         sprite->set_action("off");
81         state = OFF;
82       }
83       break;
84   }
85 }
86
87 void
88 Switch::draw(DrawingContext& context)
89 {
90   sprite->draw(context, bbox.p1, LAYER_TILES);
91 }
92
93 void
94 Switch::event(Player& , EventType type)
95 {
96   if(type != EVENT_ACTIVATE) return;
97
98   switch (state) {
99     case OFF:
100         sprite->set_action("turnon", 1);
101         state = TURN_ON;
102       break;
103     case TURN_ON:
104       break;
105     case ON:
106       break;
107     case TURN_OFF:
108       break;
109   }
110
111 }
112
113 IMPLEMENT_FACTORY(Switch, "switch");