particlesystem.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef HEADER_SUPERTUX_OBJECT_PARTICLESYSTEM_HPP
  17. #define HEADER_SUPERTUX_OBJECT_PARTICLESYSTEM_HPP
  18. #include <vector>
  19. #include "math/vector.hpp"
  20. #include "squirrel/exposed_object.hpp"
  21. #include "scripting/particlesystem.hpp"
  22. #include "supertux/game_object.hpp"
  23. #include "video/surface_ptr.hpp"
  24. class ReaderMapping;
  25. /**
  26. This is the base class for particle systems. It is responsible for
  27. storing a set of particles with each having an x- and y-coordinate
  28. the number of the layer where it should be drawn and a texture.
  29. The coordinate system used here is a virtual one. It would be a bad
  30. idea to populate whole levels with particles. So we're using a
  31. virtual rectangle here that is tiled onto the level when drawing.
  32. This rect.has the size (virtual_width, virtual_height). We're using
  33. modulo on the particle coordinates, so when a particle leaves left,
  34. it'll reenter at the right side.
  35. Classes that implement a particle system should subclass from this
  36. class, initialize particles in the constructor and move them in the
  37. simulate function.
  38. */
  39. class ParticleSystem : public GameObject,
  40. public ExposedObject<ParticleSystem, scripting::ParticleSystem>
  41. {
  42. public:
  43. ParticleSystem(const ReaderMapping& reader, float max_particle_size = 60);
  44. ParticleSystem(float max_particle_size = 60);
  45. ~ParticleSystem() override;
  46. virtual void draw(DrawingContext& context) override;
  47. static std::string class_name() { return "particle-system"; }
  48. virtual std::string get_class_name() const override { return class_name(); }
  49. static std::string display_name() { return _("Particle system"); }
  50. virtual std::string get_display_name() const override { return display_name(); }
  51. virtual ObjectSettings get_settings() override;
  52. void set_enabled(bool enabled_);
  53. bool get_enabled() const;
  54. int get_layer() const { return z_pos; }
  55. protected:
  56. class Particle
  57. {
  58. public:
  59. Particle() :
  60. pos(0.0f, 0.0f),
  61. angle(),
  62. texture(),
  63. alpha(),
  64. scale(1.f) // This currently only works in the custom particle system
  65. {}
  66. virtual ~Particle()
  67. {}
  68. Vector pos;
  69. // angle at which to draw particle
  70. float angle;
  71. SurfacePtr texture;
  72. float alpha;
  73. float scale; // see initializer
  74. private:
  75. Particle(const Particle&) = delete;
  76. Particle& operator=(const Particle&) = delete;
  77. };
  78. protected:
  79. float max_particle_size;
  80. int z_pos;
  81. std::vector<std::unique_ptr<Particle> > particles;
  82. float virtual_width;
  83. float virtual_height;
  84. bool enabled;
  85. private:
  86. ParticleSystem(const ParticleSystem&) = delete;
  87. ParticleSystem& operator=(const ParticleSystem&) = delete;
  88. };
  89. #endif
  90. /* EOF */