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