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