snow_particle_system.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. {
  35. init();
  36. }
  37. SnowParticleSystem::SnowParticleSystem(const ReaderMapping& reader) :
  38. ParticleSystem(reader),
  39. m_state(RELEASING),
  40. m_timer(),
  41. m_gust_onset(0),
  42. m_gust_current_velocity(0)
  43. {
  44. reader.get("state_length", m_state_length, 5.0f);
  45. reader.get("wind_speed", m_wind_speed, 30.0f);
  46. reader.get("spin_speed", m_spin_speed, 60.0f);
  47. reader.get("epsilon", m_epsilon, 0.5f);
  48. init();
  49. }
  50. SnowParticleSystem::~SnowParticleSystem()
  51. {
  52. }
  53. void
  54. SnowParticleSystem::init()
  55. {
  56. m_snowimages[0] = Surface::from_file("images/particles/snow2.png");
  57. m_snowimages[1] = Surface::from_file("images/particles/snow1.png");
  58. m_snowimages[2] = Surface::from_file("images/particles/snow0.png");
  59. virtual_width = static_cast<float>(SCREEN_WIDTH) * 2.0f;
  60. m_timer.start(.01f);
  61. // Create random snowflakes.
  62. int snowflakecount = static_cast<int>(virtual_width / 10.0f);
  63. for (int i = 0; i < snowflakecount; ++i)
  64. {
  65. auto particle = std::make_unique<SnowParticle>();
  66. int snowsize = graphicsRandom.rand(3);
  67. particle->pos.x = graphicsRandom.randf(virtual_width);
  68. particle->pos.y = graphicsRandom.randf(static_cast<float>(SCREEN_HEIGHT));
  69. particle->anchorx = particle->pos.x + (graphicsRandom.randf(-0.5, 0.5) * 16);
  70. // Drift will change with wind gusts.
  71. particle->drift_speed = graphicsRandom.randf(-0.5f, 0.5f) * 0.3f;
  72. particle->wobble = 0.0;
  73. particle->texture = m_snowimages[snowsize];
  74. particle->flake_size = static_cast<int>(powf(static_cast<float>(snowsize) + 3.0f, 4.0f)); // Since it ranges from 0 to 2.
  75. particle->speed = 6.32f * (1.0f + (2.0f - static_cast<float>(snowsize)) / 2.0f + graphicsRandom.randf(1.8f));
  76. // Spinning.
  77. particle->angle = graphicsRandom.randf(360.0);
  78. particle->spin_speed = graphicsRandom.randf(-m_spin_speed, m_spin_speed);
  79. particles.push_back(std::move(particle));
  80. }
  81. }
  82. ObjectSettings
  83. SnowParticleSystem::get_settings()
  84. {
  85. ObjectSettings result = ParticleSystem::get_settings();
  86. result.add_float(_("Epsilon"), &m_epsilon, "epsilon", 0.5f);
  87. result.add_float(_("Spin Speed"), &m_spin_speed, "spin_speed", 60.0f);
  88. result.add_float(_("State Length"), &m_state_length, "state_length", 5.0f);
  89. result.add_float(_("Wind Speed"), &m_wind_speed, "wind_speed", 30.0f);
  90. result.reorder({ "epsilon", "spin_speed", "state_length", "wind_speed", "enabled", "name" });
  91. return result;
  92. }
  93. void
  94. SnowParticleSystem::update(float dt_sec)
  95. {
  96. if (!enabled)
  97. return;
  98. // Simple ADSR wind gusts.
  99. if (m_timer.check())
  100. {
  101. // Change state
  102. m_state = static_cast<State>((m_state + 1) % MAX_STATE);
  103. if (m_state == RESTING)
  104. {
  105. // Stop wind.
  106. m_gust_current_velocity = 0;
  107. // New wind strength.
  108. m_gust_onset = -m_wind_speed;
  109. }
  110. m_timer.start(graphicsRandom.randf(m_state_length));
  111. }
  112. // Update velocities.
  113. switch (m_state)
  114. {
  115. case ATTACKING:
  116. m_gust_current_velocity += m_gust_onset * dt_sec;
  117. break;
  118. case DECAYING:
  119. m_gust_current_velocity -= m_gust_onset * dt_sec * DECAY_RATIO;
  120. break;
  121. case RELEASING:
  122. // Uses current time/velocity instead of constants.
  123. m_gust_current_velocity -= m_gust_current_velocity * dt_sec / m_timer.get_timeleft();
  124. break;
  125. case SUSTAINING:
  126. case RESTING:
  127. break;
  128. default:
  129. assert(false);
  130. }
  131. float sq_g = sqrtf(Sector::get().get_gravity());
  132. for (auto& part : particles)
  133. {
  134. auto particle = dynamic_cast<SnowParticle*>(part.get());
  135. if (!particle)
  136. continue;
  137. float anchor_delta;
  138. // Falling.
  139. particle->pos.y += particle->speed * dt_sec * sq_g;
  140. // Drifting (speed approaches wind at a rate dependent on flake size).
  141. particle->drift_speed += (m_gust_current_velocity - particle->drift_speed) / static_cast<float>(particle->flake_size) + graphicsRandom.randf(-m_epsilon, m_epsilon);
  142. particle->anchorx += particle->drift_speed * dt_sec;
  143. // Wobbling (particle approaches anchorx).
  144. particle->pos.x += particle->wobble * dt_sec * sq_g;
  145. anchor_delta = (particle->anchorx - particle->pos.x);
  146. particle->wobble += (WOBBLE_FACTOR * anchor_delta) + graphicsRandom.randf(-m_epsilon, m_epsilon);
  147. particle->wobble *= WOBBLE_DECAY;
  148. // Spinning.
  149. particle->angle += particle->spin_speed * dt_sec;
  150. particle->angle = fmodf(particle->angle, 360.0);
  151. }
  152. }
  153. /* EOF */