particles.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <iostream>
  18. #include "irrlichttypes_extrabloated.h"
  19. #include "client/tile.h"
  20. #include "localplayer.h"
  21. #include "tileanimation.h"
  22. struct ClientEvent;
  23. class ParticleManager;
  24. class ClientEnvironment;
  25. struct MapNode;
  26. struct ContentFeatures;
  27. class Particle : public scene::ISceneNode
  28. {
  29. public:
  30. Particle(
  31. IGameDef* gamedef,
  32. LocalPlayer *player,
  33. ClientEnvironment *env,
  34. v3f pos,
  35. v3f velocity,
  36. v3f acceleration,
  37. float expirationtime,
  38. float size,
  39. bool collisiondetection,
  40. bool collision_removal,
  41. bool vertical,
  42. video::ITexture *texture,
  43. v2f texpos,
  44. v2f texsize,
  45. const struct TileAnimationParams &anim,
  46. u8 glow,
  47. video::SColor color = video::SColor(0xFFFFFFFF)
  48. );
  49. ~Particle() = default;
  50. virtual const aabb3f &getBoundingBox() const
  51. {
  52. return m_box;
  53. }
  54. virtual u32 getMaterialCount() const
  55. {
  56. return 1;
  57. }
  58. virtual video::SMaterial& getMaterial(u32 i)
  59. {
  60. return m_material;
  61. }
  62. virtual void OnRegisterSceneNode();
  63. virtual void render();
  64. void step(float dtime);
  65. bool get_expired ()
  66. { return m_expiration < m_time; }
  67. private:
  68. void updateLight();
  69. void updateVertices();
  70. video::S3DVertex m_vertices[4];
  71. float m_time = 0.0f;
  72. float m_expiration;
  73. ClientEnvironment *m_env;
  74. IGameDef *m_gamedef;
  75. aabb3f m_box;
  76. aabb3f m_collisionbox;
  77. video::SMaterial m_material;
  78. v2f m_texpos;
  79. v2f m_texsize;
  80. v3f m_pos;
  81. v3f m_velocity;
  82. v3f m_acceleration;
  83. LocalPlayer *m_player;
  84. float m_size;
  85. //! Color without lighting
  86. video::SColor m_base_color;
  87. //! Final rendered color
  88. video::SColor m_color;
  89. bool m_collisiondetection;
  90. bool m_collision_removal;
  91. bool m_vertical;
  92. v3s16 m_camera_offset;
  93. struct TileAnimationParams m_animation;
  94. float m_animation_time = 0.0f;
  95. int m_animation_frame = 0;
  96. u8 m_glow;
  97. };
  98. class ParticleSpawner
  99. {
  100. public:
  101. ParticleSpawner(IGameDef* gamedef,
  102. LocalPlayer *player,
  103. u16 amount,
  104. float time,
  105. v3f minp, v3f maxp,
  106. v3f minvel, v3f maxvel,
  107. v3f minacc, v3f maxacc,
  108. float minexptime, float maxexptime,
  109. float minsize, float maxsize,
  110. bool collisiondetection,
  111. bool collision_removal,
  112. u16 attached_id,
  113. bool vertical,
  114. video::ITexture *texture,
  115. u32 id,
  116. const struct TileAnimationParams &anim, u8 glow,
  117. ParticleManager* p_manager);
  118. ~ParticleSpawner() = default;
  119. void step(float dtime, ClientEnvironment *env);
  120. bool get_expired ()
  121. { return (m_amount <= 0) && m_spawntime != 0; }
  122. private:
  123. void spawnParticle(ClientEnvironment *env, float radius,
  124. bool is_attached, const v3f &attached_pos,
  125. float attached_yaw);
  126. ParticleManager *m_particlemanager;
  127. float m_time;
  128. IGameDef *m_gamedef;
  129. LocalPlayer *m_player;
  130. u16 m_amount;
  131. float m_spawntime;
  132. v3f m_minpos;
  133. v3f m_maxpos;
  134. v3f m_minvel;
  135. v3f m_maxvel;
  136. v3f m_minacc;
  137. v3f m_maxacc;
  138. float m_minexptime;
  139. float m_maxexptime;
  140. float m_minsize;
  141. float m_maxsize;
  142. video::ITexture *m_texture;
  143. std::vector<float> m_spawntimes;
  144. bool m_collisiondetection;
  145. bool m_collision_removal;
  146. bool m_vertical;
  147. u16 m_attached_id;
  148. struct TileAnimationParams m_animation;
  149. u8 m_glow;
  150. };
  151. /**
  152. * Class doing particle as well as their spawners handling
  153. */
  154. class ParticleManager
  155. {
  156. friend class ParticleSpawner;
  157. public:
  158. ParticleManager(ClientEnvironment* env);
  159. ~ParticleManager();
  160. void step (float dtime);
  161. void handleParticleEvent(ClientEvent *event, Client *client,
  162. LocalPlayer *player);
  163. void addDiggingParticles(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
  164. const MapNode &n, const ContentFeatures &f);
  165. void addNodeParticle(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
  166. const MapNode &n, const ContentFeatures &f);
  167. u32 getSpawnerId() const
  168. {
  169. for (u32 id = 0;; ++id) { // look for unused particlespawner id
  170. if (m_particle_spawners.find(id) == m_particle_spawners.end())
  171. return id;
  172. }
  173. }
  174. protected:
  175. void addParticle(Particle* toadd);
  176. private:
  177. void stepParticles (float dtime);
  178. void stepSpawners (float dtime);
  179. void clearAll ();
  180. std::vector<Particle*> m_particles;
  181. std::map<u32, ParticleSpawner*> m_particle_spawners;
  182. ClientEnvironment* m_env;
  183. std::mutex m_particle_list_lock;
  184. std::mutex m_spawner_list_lock;
  185. };