snow_particle_system.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // Copyright (C) 2024 bruhmoent
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "object/snow_particle_system.hpp"
  18. #include <assert.h>
  19. #include <math.h>
  20. #include "math/random.hpp"
  21. #include "supertux/sector.hpp"
  22. #include "util/reader_mapping.hpp"
  23. #include "video/surface.hpp"
  24. #include "video/video_system.hpp"
  25. #include "video/viewport.hpp"
  26. static const float DECAY_RATIO = 0.2f; // Ratio of attack speed to decay speed.
  27. static const float WOBBLE_DECAY = 0.99f; // Wobble decays exponentially by this much each tick.
  28. static const float WOBBLE_FACTOR = 4 * .005f; // Wobble approaches drift_speed by this much each tick.
  29. SnowParticleSystem::SnowParticleSystem() :
  30. m_state(RELEASING),
  31. m_timer(),
  32. m_gust_onset(0),
  33. m_gust_current_velocity(0),
  34. m_wind_speed(),
  35. m_epsilon(),
  36. m_spin_speed(),
  37. m_state_length(),
  38. m_snowimages()
  39. {
  40. init();
  41. }
  42. SnowParticleSystem::SnowParticleSystem(const ReaderMapping& reader) :
  43. ParticleSystem(reader),
  44. m_state(RELEASING),
  45. m_timer(),
  46. m_gust_onset(0),
  47. m_gust_current_velocity(0),
  48. m_wind_speed(),
  49. m_epsilon(),
  50. m_spin_speed(),
  51. m_state_length(),
  52. m_snowimages()
  53. {
  54. reader.get("state_length", m_state_length, 5.0f);
  55. reader.get("wind_speed", m_wind_speed, 30.0f);
  56. reader.get("spin_speed", m_spin_speed, 60.0f);
  57. reader.get("epsilon", m_epsilon, 0.5f);
  58. init();
  59. }
  60. SnowParticleSystem::~SnowParticleSystem()
  61. {
  62. }
  63. void
  64. SnowParticleSystem::init()
  65. {
  66. m_snowimages[0] = Surface::from_file("images/particles/snow2.png");
  67. m_snowimages[1] = Surface::from_file("images/particles/snow1.png");
  68. m_snowimages[2] = Surface::from_file("images/particles/snow0.png");
  69. virtual_width = static_cast<float>(SCREEN_WIDTH) * 2.0f;
  70. m_timer.start(.01f);
  71. // Create random snowflakes.
  72. int snowflakecount = static_cast<int>(virtual_width / 10.0f);
  73. for (int i = 0; i < snowflakecount; ++i)
  74. {
  75. auto particle = std::make_unique<SnowParticle>();
  76. int snowsize = graphicsRandom.rand(3);
  77. particle->pos.x = graphicsRandom.randf(virtual_width);
  78. particle->pos.y = graphicsRandom.randf(static_cast<float>(SCREEN_HEIGHT));
  79. particle->anchorx = particle->pos.x + (graphicsRandom.randf(-0.5, 0.5) * 16);
  80. // Drift will change with wind gusts.
  81. particle->drift_speed = graphicsRandom.randf(-0.5f, 0.5f) * 0.3f;
  82. particle->wobble = 0.0;
  83. particle->texture = m_snowimages[snowsize];
  84. particle->flake_size = static_cast<int>(powf(static_cast<float>(snowsize) + 3.0f, 4.0f)); // Since it ranges from 0 to 2.
  85. particle->speed = 6.32f * (1.0f + (2.0f - static_cast<float>(snowsize)) / 2.0f + graphicsRandom.randf(1.8f));
  86. // Spinning.
  87. particle->angle = graphicsRandom.randf(360.0);
  88. particle->spin_speed = graphicsRandom.randf(-m_spin_speed, m_spin_speed);
  89. particles.push_back(std::move(particle));
  90. }
  91. }
  92. ObjectSettings
  93. SnowParticleSystem::get_settings()
  94. {
  95. ObjectSettings result = ParticleSystem::get_settings();
  96. result.add_float(_("Epsilon"), &m_epsilon, "epsilon", 0.5f);
  97. result.add_float(_("Spin Speed"), &m_spin_speed, "spin_speed", 60.0f);
  98. result.add_float(_("State Length"), &m_state_length, "state_length", 5.0f);
  99. result.add_float(_("Wind Speed"), &m_wind_speed, "wind_speed", 30.0f);
  100. result.reorder({ "epsilon", "spin_speed", "state_length", "wind_speed", "enabled", "name" });
  101. return result;
  102. }
  103. void
  104. SnowParticleSystem::update(float dt_sec)
  105. {
  106. if (!enabled)
  107. return;
  108. // Simple ADSR wind gusts.
  109. if (m_timer.check())
  110. {
  111. // Change state
  112. m_state = static_cast<State>((m_state + 1) % MAX_STATE);
  113. if (m_state == RESTING)
  114. {
  115. // Stop wind.
  116. m_gust_current_velocity = 0;
  117. // New wind strength.
  118. m_gust_onset = -m_wind_speed;
  119. }
  120. m_timer.start(graphicsRandom.randf(m_state_length));
  121. }
  122. // Update velocities.
  123. switch (m_state)
  124. {
  125. case ATTACKING:
  126. m_gust_current_velocity += m_gust_onset * dt_sec;
  127. break;
  128. case DECAYING:
  129. m_gust_current_velocity -= m_gust_onset * dt_sec * DECAY_RATIO;
  130. break;
  131. case RELEASING:
  132. // Uses current time/velocity instead of constants.
  133. m_gust_current_velocity -= m_gust_current_velocity * dt_sec / m_timer.get_timeleft();
  134. break;
  135. case SUSTAINING:
  136. case RESTING:
  137. break;
  138. default:
  139. assert(false);
  140. }
  141. float sq_g = sqrtf(Sector::get().get_gravity());
  142. for (auto& part : particles)
  143. {
  144. auto particle = dynamic_cast<SnowParticle*>(part.get());
  145. if (!particle)
  146. continue;
  147. float anchor_delta;
  148. // Falling.
  149. particle->pos.y += particle->speed * dt_sec * sq_g;
  150. // Drifting (speed approaches wind at a rate dependent on flake size).
  151. particle->drift_speed += (m_gust_current_velocity - particle->drift_speed) / static_cast<float>(particle->flake_size) + graphicsRandom.randf(-m_epsilon, m_epsilon);
  152. particle->anchorx += particle->drift_speed * dt_sec;
  153. // Wobbling (particle approaches anchorx).
  154. particle->pos.x += particle->wobble * dt_sec * sq_g;
  155. anchor_delta = (particle->anchorx - particle->pos.x);
  156. particle->wobble += (WOBBLE_FACTOR * anchor_delta) + graphicsRandom.randf(-m_epsilon, m_epsilon);
  157. particle->wobble *= WOBBLE_DECAY;
  158. // Spinning.
  159. particle->angle += particle->spin_speed * dt_sec;
  160. particle->angle = fmodf(particle->angle, 360.0);
  161. }
  162. }
  163. /* EOF */