]> git.lizzy.rs Git - minetest.git/blob - src/particles.h
Adding particle blend, glow and animation (#4705)
[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 #define DIGGING_PARTICLES_AMOUNT 10
24
25 #include <iostream>
26 #include "irrlichttypes_extrabloated.h"
27 #include "client/tile.h"
28 #include "localplayer.h"
29 #include "environment.h"
30
31 struct ClientEvent;
32 class ParticleManager;
33 class ClientEnvironment;
34
35 class Particle : public scene::ISceneNode
36 {
37         public:
38         Particle(
39                 IGameDef* gamedef,
40                 scene::ISceneManager* mgr,
41                 LocalPlayer *player,
42                 ClientEnvironment *env,
43                 v3f pos,
44                 v3f velocity,
45                 v3f acceleration,
46                 float expirationtime,
47                 float size,
48                 bool collisiondetection,
49                 bool collision_removal,
50                 bool vertical,
51                 video::ITexture *texture,
52                 v2f texpos,
53                 v2f texsize,
54                 u32 material_type_param,
55                 u16 vertical_frame_num,
56                 u16 horizontal_frame_num,
57                 u16 first_frame,
58                 float frame_length,
59                 bool loop_animation,
60                 u8 glow
61         );
62         ~Particle();
63
64         virtual const aabb3f &getBoundingBox() const
65         {
66                 return m_box;
67         }
68
69         virtual u32 getMaterialCount() const
70         {
71                 return 1;
72         }
73
74         virtual video::SMaterial& getMaterial(u32 i)
75         {
76                 return m_material;
77         }
78
79         virtual void OnRegisterSceneNode();
80         virtual void render();
81
82         void step(float dtime);
83
84         bool get_expired ()
85         { return m_expiration < m_time; }
86
87 private:
88         void updateLight();
89         void updateVertices();
90
91         video::S3DVertex m_vertices[4];
92         float m_time;
93         float m_expiration;
94
95         ClientEnvironment *m_env;
96         IGameDef *m_gamedef;
97         aabb3f m_box;
98         aabb3f m_collisionbox;
99         video::SMaterial m_material;
100         v2f m_texpos;
101         v2f m_texsize;
102         v3f m_pos;
103         v3f m_velocity;
104         v3f m_acceleration;
105         LocalPlayer *m_player;
106         float m_size;
107         u8 m_light;
108         bool m_collisiondetection;
109         bool m_collision_removal;
110         bool m_vertical;
111         v3s16 m_camera_offset;
112         u16 m_vertical_frame_num;
113         u16 m_horizontal_frame_num;
114         u16 m_first_frame;
115         float m_frame_length;
116         bool m_loop_animation;
117         u8 m_glow;
118 };
119
120 class ParticleSpawner
121 {
122         public:
123         ParticleSpawner(IGameDef* gamedef,
124                 scene::ISceneManager *smgr,
125                 LocalPlayer *player,
126                 u16 amount,
127                 float time,
128                 v3f minp, v3f maxp,
129                 v3f minvel, v3f maxvel,
130                 v3f minacc, v3f maxacc,
131                 float minexptime, float maxexptime,
132                 float minsize, float maxsize,
133                 bool collisiondetection,
134                 bool collision_removal,
135                 u16 attached_id,
136                 bool vertical,
137                 video::ITexture *texture,
138                 u32 id,
139                 u32 material_type_param,
140                 u16 vertical_frame_num,
141                 u16 horizontal_frame_num,
142                 u16 min_first_frame,
143                 u16 max_first_frame,
144                 float frame_length,
145                 bool loop_animation,
146                 u8 glow,
147                 ParticleManager* p_manager);
148         ~ParticleSpawner();
149
150         void step(float dtime, ClientEnvironment *env);
151
152         bool get_expired ()
153         { return (m_amount <= 0) && m_spawntime != 0; }
154
155         private:
156         ParticleManager* m_particlemanager;
157         float m_time;
158         IGameDef *m_gamedef;
159         scene::ISceneManager *m_smgr;
160         LocalPlayer *m_player;
161         u16 m_amount;
162         float m_spawntime;
163         v3f m_minpos;
164         v3f m_maxpos;
165         v3f m_minvel;
166         v3f m_maxvel;
167         v3f m_minacc;
168         v3f m_maxacc;
169         float m_minexptime;
170         float m_maxexptime;
171         float m_minsize;
172         float m_maxsize;
173         video::ITexture *m_texture;
174         std::vector<float> m_spawntimes;
175         bool m_collisiondetection;
176         bool m_collision_removal;
177         bool m_vertical;
178         u16 m_attached_id;
179         u32 m_material_type_param;
180         u16 m_vertical_frame_num;
181         u16 m_horizontal_frame_num;
182         u16 m_min_first_frame;
183         u16 m_max_first_frame;
184         float m_frame_length;
185         bool m_loop_animation;
186         u8 m_glow;
187 };
188
189 /**
190  * Class doing particle as well as their spawners handling
191  */
192 class ParticleManager
193 {
194 friend class ParticleSpawner;
195 public:
196         ParticleManager(ClientEnvironment* env);
197         ~ParticleManager();
198
199         void step (float dtime);
200
201         void handleParticleEvent(ClientEvent *event,IGameDef *gamedef,
202                         scene::ISceneManager* smgr, LocalPlayer *player);
203
204         void addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
205                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
206
207         void addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
208                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
209
210         void addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr,
211                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
212
213 protected:
214         void addParticle(Particle* toadd);
215
216 private:
217
218         void stepParticles (float dtime);
219         void stepSpawners (float dtime);
220
221         void clearAll ();
222
223         std::vector<Particle*> m_particles;
224         std::map<u32, ParticleSpawner*> m_particle_spawners;
225
226         ClientEnvironment* m_env;
227         Mutex m_particle_list_lock;
228         Mutex m_spawner_list_lock;
229 };
230
231 #endif