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