fix cr/lfs and remove trailing whitespaces...
[supertux.git] / src / trigger / door.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.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 "door.hpp"
23 #include "game_session.hpp"
24 #include "resources.hpp"
25 #include "object_factory.hpp"
26 #include "sprite/sprite.hpp"
27 #include "sprite/sprite_manager.hpp"
28 #include "video/drawing_context.hpp"
29 #include "lisp/lisp.hpp"
30 #include "lisp/writer.hpp"
31 #include "audio/sound_manager.hpp"
32
33 Door::Door(const lisp::Lisp& reader)
34         : state(CLOSED)
35 {
36   reader.get("x", bbox.p1.x);
37   reader.get("y", bbox.p1.y);
38   reader.get("sector", target_sector);
39   reader.get("spawnpoint", target_spawnpoint);
40
41   sprite = sprite_manager->create("images/objects/door/door.sprite");
42   sprite->set_action("closed");
43   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
44 }
45
46 Door::Door(int x, int y, std::string sector, std::string spawnpoint)
47         : state(CLOSED)
48 {
49   bbox.set_pos(Vector(x, y));
50   target_sector = sector;
51   target_spawnpoint = spawnpoint;
52
53   sprite = sprite_manager->create("images/objects/door/door.sprite");
54   sprite->set_action("closed");
55   bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
56 }
57
58 Door::~Door()
59 {
60   delete sprite;
61 }
62
63 void
64 Door::write(lisp::Writer& writer)
65 {
66   writer.start_list("door");
67
68   writer.write_float("x", bbox.p1.x);
69   writer.write_float("y", bbox.p1.y);
70   writer.write_float("width", bbox.get_width());
71   writer.write_float("height", bbox.get_height());
72
73   writer.write_string("sector", target_sector);
74   writer.write_string("spawnpoint", target_spawnpoint);
75   sound_manager->preload("sounds/door.wav");
76   writer.end_list("door");
77 }
78
79 void
80 Door::update(float )
81 {
82   switch (state) {
83     case CLOSED:
84       break;
85     case OPENING:
86       // if door has finished opening, start timer and keep door open
87       if(sprite->animation_done()) {
88         state = OPEN;
89         sprite->set_action("open");
90        stay_open_timer.start(1.0);
91       }
92       break;
93     case OPEN:
94       // if door was open long enough, start closing it
95       if (stay_open_timer.check()) {
96         state = CLOSING;
97         sprite->set_action("closing", 1);
98       }
99       break;
100     case CLOSING:
101       // if door has finished closing, keep it shut
102       if(sprite->animation_done()) {
103         state = CLOSED;
104         sprite->set_action("closed");
105       }
106       break;
107   }
108 }
109
110 void
111 Door::draw(DrawingContext& context)
112 {
113   sprite->draw(context, bbox.p1, LAYER_BACKGROUNDTILES+1);
114 }
115
116 void
117 Door::event(Player& , EventType type)
118 {
119   switch (state) {
120     case CLOSED:
121       // if door was activated, start opening it
122       if (type == EVENT_ACTIVATE) {
123         state = OPENING;
124         sound_manager->play("sounds/door.wav");
125         sprite->set_action("opening", 1);
126       }
127       break;
128     case OPENING:
129       break;
130     case OPEN:
131       break;
132     case CLOSING:
133       break;
134   }
135 }
136
137 HitResponse
138 Door::collision(GameObject& other, const CollisionHit& hit)
139 {
140   switch (state) {
141     case CLOSED:
142       break;
143     case OPENING:
144       break;
145     case OPEN:
146       {
147         // if door is open and was touched by a player, teleport the player
148         Player* player = dynamic_cast<Player*> (&other);
149         if (player) {
150           state = CLOSING;
151           sprite->set_action("closing", 1);
152           GameSession::current()->respawn(target_sector, target_spawnpoint);
153         }
154       }
155       break;
156     case CLOSING:
157       break;
158   }
159
160   return TriggerBase::collision(other, hit);
161 }
162
163 IMPLEMENT_FACTORY(Door, "door");