bouncy_coin.cpp 1.9 KB

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