00001 // SuperTux 00002 // Copyright (C) 2006 Matthias Braun <matze@braunis.de> 00003 // 00004 // This program is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 00017 #include "object/bouncy_coin.hpp" 00018 00019 #include "sprite/sprite.hpp" 00020 #include "sprite/sprite_manager.hpp" 00021 00023 static const float FADE_TIME = .2f; 00025 static const float LIFE_TIME = .5f; 00026 00027 BouncyCoin::BouncyCoin(const Vector& pos, bool emerge) : 00028 sprite(), 00029 position(pos), 00030 timer(), 00031 emerge_distance(0) 00032 { 00033 timer.start(LIFE_TIME); 00034 sprite = sprite_manager->create("images/objects/coin/coin.sprite"); 00035 00036 if(emerge) { 00037 emerge_distance = sprite->get_height(); 00038 } 00039 } 00040 00041 BouncyCoin::~BouncyCoin() 00042 { 00043 } 00044 00045 void 00046 BouncyCoin::update(float elapsed_time) 00047 { 00048 float dist = -200 * elapsed_time; 00049 position.y += dist; 00050 emerge_distance += dist; 00051 00052 if(timer.check()) 00053 remove_me(); 00054 } 00055 00056 void 00057 BouncyCoin::draw(DrawingContext& context) 00058 { 00059 float time_left = timer.get_timeleft(); 00060 bool fading = time_left < FADE_TIME; 00061 if(fading) { 00062 float alpha = time_left/FADE_TIME; 00063 context.push_transform(); 00064 context.set_alpha(alpha); 00065 } 00066 00067 int layer; 00068 if(emerge_distance > 0) { 00069 layer = LAYER_OBJECTS - 5; 00070 } else { 00071 layer = LAYER_OBJECTS + 5; 00072 } 00073 sprite->draw(context, position, layer); 00074 00075 if(fading) { 00076 context.pop_transform(); 00077 } 00078 } 00079 00080 /* EOF */