New trigger object "Switch" using public domain images from "mmiikkee12"
[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
22 #include "switch.hpp"
23 #include "object_factory.hpp"
24 #include "sprite/sprite.hpp"
25 #include "sprite/sprite_manager.hpp"
26 #include "sector.hpp"
27
28 Switch::Switch(const lisp::Lisp& reader)
29         : state(OFF)
30 {
31   if (!reader.get("x", bbox.p1.x)) throw std::runtime_error("no x position set");
32   if (!reader.get("y", bbox.p1.y)) throw std::runtime_error("no y position set");
33   if (!reader.get("sprite", sprite_name)) throw std::runtime_error("no sprite name set");
34   sprite = sprite_manager->create(sprite_name);
35   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
36
37   if (!reader.get("script", script)) throw std::runtime_error("no script set");
38 }
39
40 Switch::~Switch()
41 {
42   delete sprite;
43 }
44
45 void
46 Switch::write(lisp::Writer& writer)
47 {
48   writer.start_list("switch");
49   writer.write_float("x", bbox.p1.x);
50   writer.write_float("y", bbox.p1.y);
51   writer.write_string("sprite", sprite_name);
52   writer.write_string("script", script);
53   writer.end_list("switch");
54 }
55
56 void
57 Switch::update(float )
58 {
59   switch (state) {
60     case OFF:
61       break;
62     case TURN_ON:
63       if(sprite->animation_done()) {
64         std::istringstream stream(script);
65         Sector::current()->run_script(stream, "Switch");
66
67         sprite->set_action("on", 1);
68         state = ON;
69       }
70       break;
71     case ON:
72       if(sprite->animation_done()) {
73         sprite->set_action("turnoff", 1);
74         state = TURN_OFF;
75       }
76       break;
77     case TURN_OFF:
78       if(sprite->animation_done()) {
79         sprite->set_action("off");
80         state = OFF;
81       }
82       break;
83   }
84 }
85
86 void
87 Switch::draw(DrawingContext& context)
88 {
89   sprite->draw(context, bbox.p1, LAYER_TILES);
90 }
91
92 void
93 Switch::event(Player& , EventType type)
94 {
95   if(type != EVENT_ACTIVATE) return;
96
97   switch (state) {
98     case OFF:
99         sprite->set_action("turnon", 1);
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 IMPLEMENT_FACTORY(Switch, "switch");
113