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