ghost_particle_system.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // SuperTux
  2. // Copyright (C) 2009 Ingo Ruhnke <grumbel@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/ghost_particle_system.hpp"
  17. #include <algorithm>
  18. #include <math.h>
  19. #include "math/random.hpp"
  20. #include "supertux/globals.hpp"
  21. #include "video/surface.hpp"
  22. #include "video/video_system.hpp"
  23. #include "video/viewport.hpp"
  24. //FIXME: Sometimes both ghosts have the same image
  25. // Ghosts don't change their movement pattern - not random.
  26. GhostParticleSystem::GhostParticleSystem()
  27. {
  28. init();
  29. }
  30. GhostParticleSystem::GhostParticleSystem(const ReaderMapping& reader) :
  31. ParticleSystem(reader)
  32. {
  33. init();
  34. }
  35. GhostParticleSystem::~GhostParticleSystem()
  36. {
  37. }
  38. void
  39. GhostParticleSystem::init()
  40. {
  41. ghosts[0] = Surface::from_file("images/particles/ghost0.png");
  42. ghosts[1] = Surface::from_file("images/particles/ghost1.png");
  43. virtual_width = static_cast<float>(SCREEN_WIDTH) * 2.0f;
  44. // Create two ghosts.
  45. size_t ghostcount = 2;
  46. for (size_t i=0; i<ghostcount; ++i) {
  47. auto particle = std::make_unique<GhostParticle>();
  48. particle->pos.x = graphicsRandom.randf(virtual_width);
  49. particle->pos.y = graphicsRandom.randf(static_cast<float>(SCREEN_HEIGHT));
  50. int size = graphicsRandom.rand(2);
  51. particle->texture = ghosts[size];
  52. particle->speed = graphicsRandom.randf(std::max(50.0f, static_cast<float>(size) * 10.0f),
  53. 180.0f + static_cast<float>(size) * 10.0f);
  54. particles.push_back(std::move(particle));
  55. }
  56. }
  57. void
  58. GhostParticleSystem::update(float dt_sec)
  59. {
  60. if (!enabled)
  61. return;
  62. for (const auto& part : particles) {
  63. const auto& particle = dynamic_cast<GhostParticle*>(part.get());
  64. particle->pos.y -= particle->speed * dt_sec;
  65. particle->pos.x -= particle->speed * dt_sec;
  66. if (particle->pos.y > static_cast<float>(SCREEN_HEIGHT)) {
  67. particle->pos.y = fmodf(particle->pos.y , virtual_height);
  68. particle->pos.x = graphicsRandom.randf(virtual_width);
  69. }
  70. }
  71. }
  72. /* EOF */