coin_rain.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SuperTux
  2. // Copyright (C) 2013 LMH <lmh.0013@gmail.com>
  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/coin_rain.hpp"
  17. #include "math/random.hpp"
  18. #include "object/coin.hpp"
  19. #include "sprite/sprite.hpp"
  20. #include "sprite/sprite_manager.hpp"
  21. #include "supertux/sector.hpp"
  22. static const float DROP_TIME = .1f; // Time duration between "drops" of coin rain.
  23. CoinRain::CoinRain(const Vector& pos, bool emerge, bool count_stats, const std::string& sprite_path) :
  24. sprite(SpriteManager::current()->create(sprite_path)),
  25. m_sprite_path(sprite_path),
  26. position(pos),
  27. emerge_distance(0),
  28. timer(),
  29. counter(0),
  30. drop(0),
  31. m_count_stats(count_stats)
  32. {
  33. if (emerge) {
  34. emerge_distance = static_cast<float>(sprite->get_height());
  35. }
  36. }
  37. void
  38. CoinRain::update(float dt_sec)
  39. {
  40. // First a single (untouchable) coin flies up above the sector.
  41. if (position.y > -32){
  42. float dist = -500 * dt_sec;
  43. position.y += dist;
  44. emerge_distance += dist;
  45. } // Then the first collectable coin drops from one of ten random positions.
  46. else if (counter==0){
  47. drop = gameRandom.rand(10);
  48. Sector::get().add<HeavyCoin>(Vector(position.x + 32.0f * static_cast<float>((drop < 5) ? -drop - 1 : drop - 4), -32.0f),
  49. Vector(0, 0), m_count_stats, m_sprite_path);
  50. counter++;
  51. timer.start(DROP_TIME);
  52. } // Finally, the remaining coins drop in a determined but seemingly random order.
  53. else if (timer.check()){
  54. if (counter<10){
  55. drop += 7;
  56. if (drop >= 10) drop -=10;
  57. Sector::get().add<HeavyCoin>(Vector(position.x + 32.0f * static_cast<float>((drop < 5) ? -drop - 1 : drop - 4), -32.0f),
  58. Vector(0, 0), m_count_stats, m_sprite_path);
  59. counter++;
  60. timer.start(DROP_TIME);
  61. } else {
  62. remove_me();
  63. }
  64. }
  65. }
  66. void
  67. CoinRain::draw(DrawingContext& context)
  68. {
  69. int layer;
  70. if (emerge_distance > 0) {
  71. layer = LAYER_OBJECTS - 5;
  72. } else {
  73. layer = LAYER_OBJECTS + 5;
  74. }
  75. sprite->draw(context.color(), position, layer);
  76. }
  77. /* EOF */