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 "object/bouncy_coin.hpp" |
18 | |
19 | #include "sprite/sprite.hpp" |
20 | #include "sprite/sprite_manager.hpp" |
21 | |
22 | /** this controls the time over which a bouncy coin fades */ |
23 | static const float FADE_TIME = .2f; |
24 | /** this is the total life time of a bouncy coin */ |
25 | static const float LIFE_TIME = .5f; |
26 | |
27 | BouncyCoin::BouncyCoin(const Vector& pos, bool emerge, const std::string& sprite_path) : |
28 | sprite(SpriteManager::current()->create(sprite_path)), |
29 | position(pos), |
30 | timer(), |
31 | emerge_distance(0) |
32 | { |
33 | timer.start(LIFE_TIME); |
34 | |
35 | if (emerge) { |
36 | emerge_distance = static_cast<float>(sprite->get_height()); |
37 | } |
38 | } |
39 | |
40 | void |
41 | BouncyCoin::update(float dt_sec) |
42 | { |
43 | float dist = -200 * dt_sec; |
44 | position.y += dist; |
45 | emerge_distance += dist; |
46 | |
47 | if (timer.check()) |
48 | remove_me(); |
49 | } |
50 | |
51 | void |
52 | BouncyCoin::draw(DrawingContext& context) |
53 | { |
54 | float time_left = timer.get_timeleft(); |
55 | bool fading = time_left < FADE_TIME; |
56 | if (fading) { |
57 | float alpha = time_left/FADE_TIME; |
58 | context.push_transform(); |
59 | context.set_alpha(alpha); |
60 | } |
61 | |
62 | int layer; |
63 | if (emerge_distance > 0) { |
64 | layer = LAYER_OBJECTS - 5; |
65 | } else { |
66 | layer = LAYER_OBJECTS + 5; |
67 | } |
68 | sprite->draw(context.color(), position, layer); |
69 | |
70 | if (fading) { |
71 | context.pop_transform(); |
72 | } |
73 | } |
74 | |
75 | /* EOF */ |
76 | |