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 "audio/sound_manager.hpp" |
20 | #include "object/player.hpp" |
21 | #include "sprite/sprite.hpp" |
22 | #include "sprite/sprite_manager.hpp" |
23 | #include "supertux/fadetoblack.hpp" |
24 | #include "supertux/game_session.hpp" |
25 | #include "supertux/screen_manager.hpp" |
26 | #include "supertux/sector.hpp" |
27 | #include "util/reader_mapping.hpp" |
28 | |
29 | Door::Door(const ReaderMapping& mapping) : |
30 | TriggerBase(mapping), |
31 | state(CLOSED), |
32 | target_sector(), |
33 | target_spawnpoint(), |
34 | script(), |
35 | sprite(SpriteManager::current()->create("images/objects/door/door.sprite" )), |
36 | stay_open_timer() |
37 | { |
38 | mapping.get("x" , m_col.m_bbox.get_left()); |
39 | mapping.get("y" , m_col.m_bbox.get_top()); |
40 | mapping.get("sector" , target_sector); |
41 | mapping.get("spawnpoint" , target_spawnpoint); |
42 | |
43 | mapping.get("script" , script); |
44 | |
45 | sprite->set_action("closed" ); |
46 | m_col.m_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, const std::string& sector, const std::string& spawnpoint) : |
52 | TriggerBase(), |
53 | state(CLOSED), |
54 | target_sector(sector), |
55 | target_spawnpoint(spawnpoint), |
56 | script(), |
57 | sprite(SpriteManager::current()->create("images/objects/door/door.sprite" )), |
58 | stay_open_timer() |
59 | { |
60 | m_col.m_bbox.set_pos(Vector(static_cast<float>(x), static_cast<float>(y))); |
61 | |
62 | sprite->set_action("closed" ); |
63 | m_col.m_bbox.set_size(sprite->get_current_hitbox_width(), sprite->get_current_hitbox_height()); |
64 | |
65 | SoundManager::current()->preload("sounds/door.wav" ); |
66 | } |
67 | |
68 | ObjectSettings |
69 | Door::get_settings() |
70 | { |
71 | ObjectSettings result = TriggerBase::get_settings(); |
72 | |
73 | result.add_script(_("Script" ), &script, "script" ); |
74 | result.add_text(_("Sector" ), &target_sector, "sector" ); |
75 | result.add_text(_("Spawn point" ), &target_spawnpoint, "spawnpoint" ); |
76 | |
77 | result.reorder({"sector" , "spawnpoint" , "name" , "x" , "y" }); |
78 | |
79 | return result; |
80 | } |
81 | |
82 | Door::~Door() |
83 | { |
84 | } |
85 | |
86 | void |
87 | Door::update(float ) |
88 | { |
89 | switch (state) { |
90 | case CLOSED: |
91 | break; |
92 | case OPENING: |
93 | // if door has finished opening, start timer and keep door open |
94 | if (sprite->animation_done()) { |
95 | state = OPEN; |
96 | sprite->set_action("open" ); |
97 | stay_open_timer.start(1.0); |
98 | } |
99 | break; |
100 | case OPEN: |
101 | // if door was open long enough, start closing it |
102 | if (stay_open_timer.check()) { |
103 | state = CLOSING; |
104 | sprite->set_action("closing" , 1); |
105 | } |
106 | break; |
107 | case CLOSING: |
108 | // if door has finished closing, keep it shut |
109 | if (sprite->animation_done()) { |
110 | state = CLOSED; |
111 | sprite->set_action("closed" ); |
112 | } |
113 | break; |
114 | } |
115 | } |
116 | |
117 | void |
118 | Door::draw(DrawingContext& context) |
119 | { |
120 | sprite->draw(context.color(), m_col.m_bbox.p1(), LAYER_BACKGROUNDTILES+1); |
121 | } |
122 | |
123 | void |
124 | Door::event(Player& , EventType type) |
125 | { |
126 | switch (state) { |
127 | case CLOSED: |
128 | // if door was activated, start opening it |
129 | if (type == EVENT_ACTIVATE) { |
130 | state = OPENING; |
131 | SoundManager::current()->play("sounds/door.wav" ); |
132 | sprite->set_action("opening" , 1); |
133 | ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEOUT, 1)); |
134 | } |
135 | break; |
136 | case OPENING: |
137 | break; |
138 | case OPEN: |
139 | break; |
140 | case CLOSING: |
141 | break; |
142 | } |
143 | } |
144 | |
145 | HitResponse |
146 | Door::collision(GameObject& other, const CollisionHit& hit_) |
147 | { |
148 | switch (state) { |
149 | case CLOSED: |
150 | break; |
151 | case OPENING: |
152 | break; |
153 | case OPEN: |
154 | { |
155 | // if door is open and was touched by a player, teleport the player |
156 | Player* player = dynamic_cast<Player*> (&other); |
157 | |
158 | if (player) { |
159 | bool invincible = player->is_invincible(); |
160 | int invincibilityperiod = static_cast<int>(player->m_invincible_timer.get_timeleft()); |
161 | state = CLOSING; |
162 | sprite->set_action("closing" , 1); |
163 | if (!script.empty()) { |
164 | Sector::get().run_script(script, "Door" ); |
165 | } |
166 | |
167 | if (!target_sector.empty()) { |
168 | GameSession::current()->respawn(target_sector, target_spawnpoint, |
169 | invincible, invincibilityperiod); |
170 | ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEIN, 1)); |
171 | } |
172 | } |
173 | } |
174 | break; |
175 | case CLOSING: |
176 | break; |
177 | } |
178 | |
179 | return TriggerBase::collision(other, hit_); |
180 | } |
181 | |
182 | /* EOF */ |
183 | |