particles.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 "../particles.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. const ParticleParameters &p,
  35. video::ITexture *texture,
  36. v2f texpos,
  37. v2f texsize,
  38. video::SColor color
  39. );
  40. ~Particle() = default;
  41. virtual const aabb3f &getBoundingBox() const
  42. {
  43. return m_box;
  44. }
  45. virtual u32 getMaterialCount() const
  46. {
  47. return 1;
  48. }
  49. virtual video::SMaterial& getMaterial(u32 i)
  50. {
  51. return m_material;
  52. }
  53. virtual void OnRegisterSceneNode();
  54. virtual void render();
  55. void step(float dtime);
  56. bool get_expired ()
  57. { return m_expiration < m_time; }
  58. private:
  59. void updateLight();
  60. void updateVertices();
  61. video::S3DVertex m_vertices[4];
  62. float m_time = 0.0f;
  63. float m_expiration;
  64. ClientEnvironment *m_env;
  65. IGameDef *m_gamedef;
  66. aabb3f m_box;
  67. aabb3f m_collisionbox;
  68. video::SMaterial m_material;
  69. v2f m_texpos;
  70. v2f m_texsize;
  71. v3f m_pos;
  72. v3f m_velocity;
  73. v3f m_acceleration;
  74. LocalPlayer *m_player;
  75. float m_size;
  76. //! Color without lighting
  77. video::SColor m_base_color;
  78. //! Final rendered color
  79. video::SColor m_color;
  80. bool m_collisiondetection;
  81. bool m_collision_removal;
  82. bool m_object_collision;
  83. bool m_vertical;
  84. v3s16 m_camera_offset;
  85. struct TileAnimationParams m_animation;
  86. float m_animation_time = 0.0f;
  87. int m_animation_frame = 0;
  88. u8 m_glow;
  89. };
  90. class ParticleSpawner
  91. {
  92. public:
  93. ParticleSpawner(IGameDef* gamedef,
  94. LocalPlayer *player,
  95. const ParticleSpawnerParameters &p,
  96. u16 attached_id,
  97. video::ITexture *texture,
  98. ParticleManager* p_manager);
  99. ~ParticleSpawner() = default;
  100. void step(float dtime, ClientEnvironment *env);
  101. bool get_expired ()
  102. { return p.amount <= 0 && p.time != 0; }
  103. private:
  104. void spawnParticle(ClientEnvironment *env, float radius,
  105. const core::matrix4 *attached_absolute_pos_rot_matrix);
  106. ParticleManager *m_particlemanager;
  107. float m_time;
  108. IGameDef *m_gamedef;
  109. LocalPlayer *m_player;
  110. ParticleSpawnerParameters p;
  111. video::ITexture *m_texture;
  112. std::vector<float> m_spawntimes;
  113. u16 m_attached_id;
  114. };
  115. /**
  116. * Class doing particle as well as their spawners handling
  117. */
  118. class ParticleManager
  119. {
  120. friend class ParticleSpawner;
  121. public:
  122. ParticleManager(ClientEnvironment* env);
  123. ~ParticleManager();
  124. void step (float dtime);
  125. void handleParticleEvent(ClientEvent *event, Client *client,
  126. LocalPlayer *player);
  127. void addDiggingParticles(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
  128. const MapNode &n, const ContentFeatures &f);
  129. void addNodeParticle(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
  130. const MapNode &n, const ContentFeatures &f);
  131. /**
  132. * This function is only used by client particle spawners
  133. *
  134. * We don't need to check the particle spawner list because client ID will
  135. * never overlap (u64)
  136. * @return new id
  137. */
  138. u64 generateSpawnerId()
  139. {
  140. return m_next_particle_spawner_id++;
  141. }
  142. protected:
  143. static bool getNodeParticleParams(const MapNode &n, const ContentFeatures &f,
  144. ParticleParameters &p, video::ITexture **texture, v2f &texpos,
  145. v2f &texsize, video::SColor *color, u8 tilenum = 0);
  146. void addParticle(Particle* toadd);
  147. private:
  148. void addParticleSpawner(u64 id, ParticleSpawner *toadd);
  149. void deleteParticleSpawner(u64 id);
  150. void stepParticles(float dtime);
  151. void stepSpawners(float dtime);
  152. void clearAll();
  153. std::vector<Particle*> m_particles;
  154. std::unordered_map<u64, ParticleSpawner*> m_particle_spawners;
  155. // Start the particle spawner ids generated from here after u32_max. lower values are
  156. // for server sent spawners.
  157. u64 m_next_particle_spawner_id = U32_MAX + 1;
  158. ClientEnvironment* m_env;
  159. std::mutex m_particle_list_lock;
  160. std::mutex m_spawner_list_lock;
  161. };