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