]> git.lizzy.rs Git - minetest.git/blob - src/particles.h
Use C++11 mutexes only (remove compat code) (#5922)
[minetest.git] / src / particles.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef PARTICLES_HEADER
21 #define PARTICLES_HEADER
22
23 #include <iostream>
24 #include "irrlichttypes_extrabloated.h"
25 #include "client/tile.h"
26 #include "localplayer.h"
27 #include "environment.h"
28 #include "tileanimation.h"
29
30 struct ClientEvent;
31 class ParticleManager;
32 class ClientEnvironment;
33 struct MapNode;
34 struct ContentFeatures;
35
36 class Particle : public scene::ISceneNode
37 {
38         public:
39         Particle(
40                 IGameDef* gamedef,
41                 scene::ISceneManager* mgr,
42                 LocalPlayer *player,
43                 ClientEnvironment *env,
44                 v3f pos,
45                 v3f velocity,
46                 v3f acceleration,
47                 float expirationtime,
48                 float size,
49                 bool collisiondetection,
50                 bool collision_removal,
51                 bool vertical,
52                 video::ITexture *texture,
53                 v2f texpos,
54                 v2f texsize,
55                 const struct TileAnimationParams &anim,
56                 u8 glow,
57                 video::SColor color = video::SColor(0xFFFFFFFF)
58         );
59         ~Particle();
60
61         virtual const aabb3f &getBoundingBox() const
62         {
63                 return m_box;
64         }
65
66         virtual u32 getMaterialCount() const
67         {
68                 return 1;
69         }
70
71         virtual video::SMaterial& getMaterial(u32 i)
72         {
73                 return m_material;
74         }
75
76         virtual void OnRegisterSceneNode();
77         virtual void render();
78
79         void step(float dtime);
80
81         bool get_expired ()
82         { return m_expiration < m_time; }
83
84 private:
85         void updateLight();
86         void updateVertices();
87
88         video::S3DVertex m_vertices[4];
89         float m_time;
90         float m_expiration;
91
92         ClientEnvironment *m_env;
93         IGameDef *m_gamedef;
94         aabb3f m_box;
95         aabb3f m_collisionbox;
96         video::SMaterial m_material;
97         v2f m_texpos;
98         v2f m_texsize;
99         v3f m_pos;
100         v3f m_velocity;
101         v3f m_acceleration;
102         LocalPlayer *m_player;
103         float m_size;
104         //! Color without lighting
105         video::SColor m_base_color;
106         //! Final rendered color
107         video::SColor m_color;
108         bool m_collisiondetection;
109         bool m_collision_removal;
110         bool m_vertical;
111         v3s16 m_camera_offset;
112         struct TileAnimationParams m_animation;
113         float m_animation_time;
114         int m_animation_frame;
115         u8 m_glow;
116 };
117
118 class ParticleSpawner
119 {
120         public:
121         ParticleSpawner(IGameDef* gamedef,
122                 scene::ISceneManager *smgr,
123                 LocalPlayer *player,
124                 u16 amount,
125                 float time,
126                 v3f minp, v3f maxp,
127                 v3f minvel, v3f maxvel,
128                 v3f minacc, v3f maxacc,
129                 float minexptime, float maxexptime,
130                 float minsize, float maxsize,
131                 bool collisiondetection,
132                 bool collision_removal,
133                 u16 attached_id,
134                 bool vertical,
135                 video::ITexture *texture,
136                 u32 id,
137                 const struct TileAnimationParams &anim, u8 glow,
138                 ParticleManager* p_manager);
139
140         ~ParticleSpawner();
141
142         void step(float dtime, ClientEnvironment *env);
143
144         bool get_expired ()
145         { return (m_amount <= 0) && m_spawntime != 0; }
146
147         private:
148         ParticleManager* m_particlemanager;
149         float m_time;
150         IGameDef *m_gamedef;
151         scene::ISceneManager *m_smgr;
152         LocalPlayer *m_player;
153         u16 m_amount;
154         float m_spawntime;
155         v3f m_minpos;
156         v3f m_maxpos;
157         v3f m_minvel;
158         v3f m_maxvel;
159         v3f m_minacc;
160         v3f m_maxacc;
161         float m_minexptime;
162         float m_maxexptime;
163         float m_minsize;
164         float m_maxsize;
165         video::ITexture *m_texture;
166         std::vector<float> m_spawntimes;
167         bool m_collisiondetection;
168         bool m_collision_removal;
169         bool m_vertical;
170         u16 m_attached_id;
171         struct TileAnimationParams m_animation;
172         u8 m_glow;
173 };
174
175 /**
176  * Class doing particle as well as their spawners handling
177  */
178 class ParticleManager
179 {
180 friend class ParticleSpawner;
181 public:
182         ParticleManager(ClientEnvironment* env);
183         ~ParticleManager();
184
185         void step (float dtime);
186
187         void handleParticleEvent(ClientEvent *event, Client *client,
188                         scene::ISceneManager* smgr, LocalPlayer *player);
189
190         void addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
191                 LocalPlayer *player, v3s16 pos, const MapNode &n,
192                 const ContentFeatures &f);
193
194         void addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
195                 LocalPlayer *player, v3s16 pos, const MapNode &n,
196                 const ContentFeatures &f);
197
198         void addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr,
199                 LocalPlayer *player, v3s16 pos, const MapNode &n,
200                 const ContentFeatures &f);
201
202 protected:
203         void addParticle(Particle* toadd);
204
205 private:
206
207         void stepParticles (float dtime);
208         void stepSpawners (float dtime);
209
210         void clearAll ();
211
212         std::vector<Particle*> m_particles;
213         std::map<u32, ParticleSpawner*> m_particle_spawners;
214
215         ClientEnvironment* m_env;
216         std::mutex m_particle_list_lock;
217         std::mutex m_spawner_list_lock;
218 };
219
220 #endif