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