Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / object / bouncy_coin.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 "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) :
28   position(pos), 
29   emerge_distance(0)
30 {
31   timer.start(LIFE_TIME);
32   sprite = sprite_manager->create("images/objects/coin/coin.sprite");
33
34   if(emerge) {
35     emerge_distance = sprite->get_height();
36   }
37 }
38
39 BouncyCoin::~BouncyCoin()
40 {
41 }
42
43 void
44 BouncyCoin::update(float elapsed_time)
45 {
46   float dist = -200 * elapsed_time;
47   position.y += dist;
48   emerge_distance += dist;
49
50   if(timer.check())
51     remove_me();
52 }
53
54 void
55 BouncyCoin::draw(DrawingContext& context)
56 {
57   float time_left = timer.get_timeleft();
58   bool fading = time_left < FADE_TIME;
59   if(fading) {
60     float alpha = time_left/FADE_TIME;
61     context.push_transform();
62     context.set_alpha(alpha);
63   }
64
65   int layer;
66   if(emerge_distance > 0) {
67     layer = LAYER_OBJECTS - 5;
68   } else {
69     layer = LAYER_OBJECTS + 5;
70   }
71   sprite->draw(context, position, layer);
72
73   if(fading) {
74     context.pop_transform();
75   }
76 }
77
78 /* EOF */