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