2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
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 #include "util/reader.hpp"
25 Door::Door(const Reader& reader) :
32 reader.get("x", bbox.p1.x);
33 reader.get("y", bbox.p1.y);
34 reader.get("sector", target_sector);
35 reader.get("spawnpoint", target_spawnpoint);
37 sprite = sprite_manager->create("images/objects/door/door.sprite");
38 sprite->set_action("closed");
39 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
41 sound_manager->preload("sounds/door.wav");
44 Door::Door(int x, int y, std::string sector, std::string spawnpoint) :
51 bbox.set_pos(Vector(x, y));
52 target_sector = sector;
53 target_spawnpoint = spawnpoint;
55 sprite = sprite_manager->create("images/objects/door/door.sprite");
56 sprite->set_action("closed");
57 bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height());
59 sound_manager->preload("sounds/door.wav");
73 // if door has finished opening, start timer and keep door open
74 if(sprite->animation_done()) {
76 sprite->set_action("open");
77 stay_open_timer.start(1.0);
81 // if door was open long enough, start closing it
82 if (stay_open_timer.check()) {
84 sprite->set_action("closing", 1);
88 // if door has finished closing, keep it shut
89 if(sprite->animation_done()) {
91 sprite->set_action("closed");
98 Door::draw(DrawingContext& context)
100 sprite->draw(context, bbox.p1, LAYER_BACKGROUNDTILES+1);
104 Door::event(Player& , EventType type)
108 // if door was activated, start opening it
109 if (type == EVENT_ACTIVATE) {
111 sound_manager->play("sounds/door.wav");
112 sprite->set_action("opening", 1);
125 Door::collision(GameObject& other, const CollisionHit& hit)
134 // if door is open and was touched by a player, teleport the player
135 Player* player = dynamic_cast<Player*> (&other);
138 sprite->set_action("closing", 1);
139 GameSession::current()->respawn(target_sector, target_spawnpoint);
147 return TriggerBase::collision(other, hit);
150 IMPLEMENT_FACTORY(Door, "door");