]> git.lizzy.rs Git - minetest.git/blob - src/particles.h
Environment & IGameDef code refactoring (#4985)
[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         );
55         ~Particle();
56
57         virtual const aabb3f &getBoundingBox() const
58         {
59                 return m_box;
60         }
61
62         virtual u32 getMaterialCount() const
63         {
64                 return 1;
65         }
66
67         virtual video::SMaterial& getMaterial(u32 i)
68         {
69                 return m_material;
70         }
71
72         virtual void OnRegisterSceneNode();
73         virtual void render();
74
75         void step(float dtime);
76
77         bool get_expired ()
78         { return m_expiration < m_time; }
79
80 private:
81         void updateLight();
82         void updateVertices();
83
84         video::S3DVertex m_vertices[4];
85         float m_time;
86         float m_expiration;
87
88         ClientEnvironment *m_env;
89         IGameDef *m_gamedef;
90         aabb3f m_box;
91         aabb3f m_collisionbox;
92         video::SMaterial m_material;
93         v2f m_texpos;
94         v2f m_texsize;
95         v3f m_pos;
96         v3f m_velocity;
97         v3f m_acceleration;
98         LocalPlayer *m_player;
99         float m_size;
100         u8 m_light;
101         bool m_collisiondetection;
102         bool m_collision_removal;
103         bool m_vertical;
104         v3s16 m_camera_offset;
105 };
106
107 class ParticleSpawner
108 {
109         public:
110         ParticleSpawner(IGameDef* gamedef,
111                 scene::ISceneManager *smgr,
112                 LocalPlayer *player,
113                 u16 amount,
114                 float time,
115                 v3f minp, v3f maxp,
116                 v3f minvel, v3f maxvel,
117                 v3f minacc, v3f maxacc,
118                 float minexptime, float maxexptime,
119                 float minsize, float maxsize,
120                 bool collisiondetection,
121                 bool collision_removal,
122                 u16 attached_id,
123                 bool vertical,
124                 video::ITexture *texture,
125                 u32 id,
126                 ParticleManager* p_manager);
127
128         ~ParticleSpawner();
129
130         void step(float dtime, ClientEnvironment *env);
131
132         bool get_expired ()
133         { return (m_amount <= 0) && m_spawntime != 0; }
134
135         private:
136         ParticleManager* m_particlemanager;
137         float m_time;
138         IGameDef *m_gamedef;
139         scene::ISceneManager *m_smgr;
140         LocalPlayer *m_player;
141         u16 m_amount;
142         float m_spawntime;
143         v3f m_minpos;
144         v3f m_maxpos;
145         v3f m_minvel;
146         v3f m_maxvel;
147         v3f m_minacc;
148         v3f m_maxacc;
149         float m_minexptime;
150         float m_maxexptime;
151         float m_minsize;
152         float m_maxsize;
153         video::ITexture *m_texture;
154         std::vector<float> m_spawntimes;
155         bool m_collisiondetection;
156         bool m_collision_removal;
157         bool m_vertical;
158         u16 m_attached_id;
159 };
160
161 /**
162  * Class doing particle as well as their spawners handling
163  */
164 class ParticleManager
165 {
166 friend class ParticleSpawner;
167 public:
168         ParticleManager(ClientEnvironment* env);
169         ~ParticleManager();
170
171         void step (float dtime);
172
173         void handleParticleEvent(ClientEvent *event, Client *client,
174                         scene::ISceneManager* smgr, LocalPlayer *player);
175
176         void addDiggingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
177                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
178
179         void addPunchingParticles(IGameDef* gamedef, scene::ISceneManager* smgr,
180                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
181
182         void addNodeParticle(IGameDef* gamedef, scene::ISceneManager* smgr,
183                 LocalPlayer *player, v3s16 pos, const TileSpec tiles[]);
184
185 protected:
186         void addParticle(Particle* toadd);
187
188 private:
189
190         void stepParticles (float dtime);
191         void stepSpawners (float dtime);
192
193         void clearAll ();
194
195         std::vector<Particle*> m_particles;
196         std::map<u32, ParticleSpawner*> m_particle_spawners;
197
198         ClientEnvironment* m_env;
199         Mutex m_particle_list_lock;
200         Mutex m_spawner_list_lock;
201 };
202
203 #endif