1 /*************************************************************************/
2 /*  particles_2d.h                                                       */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md)    */
10 /*                                                                       */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the       */
13 /* "Software"), to deal in the Software without restriction, including   */
14 /* without limitation the rights to use, copy, modify, merge, publish,   */
15 /* distribute, sublicense, and/or sell copies of the Software, and to    */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions:                                             */
18 /*                                                                       */
19 /* The above copyright notice and this permission notice shall be        */
20 /* included in all copies or substantial portions of the Software.       */
21 /*                                                                       */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
29 /*************************************************************************/
30 #ifndef PARTICLES_FRAME_H
31 #define PARTICLES_FRAME_H
32 
33 #include "scene/2d/node_2d.h"
34 #include "scene/resources/color_ramp.h"
35 #include "scene/resources/texture.h"
36 
37 class Particles2D;
38 class ParticleAttractor2D : public Node2D {
39 
40 	OBJ_TYPE(ParticleAttractor2D, Node2D);
41 
42 	friend class Particles2D;
43 	bool enabled;
44 	float radius;
45 	float disable_radius;
46 	float gravity;
47 	float absorption;
48 	NodePath path;
49 
50 	Particles2D *owner;
51 
52 	void _update_owner();
53 	void _owner_exited();
54 	void _set_owner(Particles2D *p_owner);
55 
56 	void _notification(int p_what);
57 	static void _bind_methods();
58 
59 public:
60 	void set_enabled(bool p_enabled);
61 	bool is_enabled() const;
62 
63 	void set_radius(float p_radius);
64 	float get_radius() const;
65 
66 	void set_disable_radius(float p_disable_radius);
67 	float get_disable_radius() const;
68 
69 	void set_gravity(float p_gravity);
70 	float get_gravity() const;
71 
72 	void set_absorption(float p_absorption);
73 	float get_absorption() const;
74 
75 	void set_particles_path(NodePath p_path);
76 	NodePath get_particles_path() const;
77 
78 	virtual String get_configuration_warning() const;
79 
80 	ParticleAttractor2D();
81 };
82 
83 class Particles2D : public Node2D {
84 
85 	OBJ_TYPE(Particles2D, Node2D);
86 
87 public:
88 	enum Parameter {
89 		PARAM_DIRECTION,
90 		PARAM_SPREAD,
91 		PARAM_LINEAR_VELOCITY,
92 		PARAM_SPIN_VELOCITY,
93 		PARAM_ORBIT_VELOCITY,
94 		PARAM_GRAVITY_DIRECTION,
95 		PARAM_GRAVITY_STRENGTH,
96 		PARAM_RADIAL_ACCEL,
97 		PARAM_TANGENTIAL_ACCEL,
98 		PARAM_DAMPING,
99 		PARAM_INITIAL_ANGLE,
100 		PARAM_INITIAL_SIZE,
101 		PARAM_FINAL_SIZE,
102 		PARAM_HUE_VARIATION,
103 		PARAM_ANIM_SPEED_SCALE,
104 		PARAM_ANIM_INITIAL_POS,
105 		PARAM_MAX
106 	};
107 
108 	enum {
109 		MAX_COLOR_PHASES = 4
110 	};
111 
112 	enum ProcessMode {
113 		PROCESS_FIXED,
114 		PROCESS_IDLE,
115 	};
116 
117 private:
118 	float param[PARAM_MAX];
119 	float randomness[PARAM_MAX];
120 
121 	struct Particle {
122 
123 		bool active;
124 		Point2 pos;
125 		Vector2 velocity;
126 		float rot;
127 		float frame;
128 		uint32_t seed;
ParticleParticle129 		Particle() {
130 			active = false;
131 			seed = 123465789;
132 			rot = 0;
133 			frame = 0;
134 		}
135 	};
136 
137 	Vector<Particle> particles;
138 
139 	struct AttractorCache {
140 
141 		Vector2 pos;
142 		ParticleAttractor2D *attractor;
143 	};
144 
145 	Vector<AttractorCache> attractor_cache;
146 
147 	float explosiveness;
148 	float preprocess;
149 	float lifetime;
150 	bool emitting;
151 	bool local_space;
152 	float emit_timeout;
153 	float time_to_live;
154 	float time_scale;
155 	bool flip_h;
156 	bool flip_v;
157 	int h_frames;
158 	int v_frames;
159 	Point2 emissor_offset;
160 	Vector2 initial_velocity;
161 	Vector2 extents;
162 	DVector<Vector2> emission_points;
163 
164 	ProcessMode process_mode;
165 
166 	float time;
167 	int active_count;
168 
169 	Ref<Texture> texture;
170 
171 	//If no color ramp is set then default color is used. Created as simple alternative to color_ramp.
172 	Color default_color;
173 	Ref<ColorRamp> color_ramp;
174 
175 	void testee(int a, int b, int c, int d, int e);
176 	void _process_particles(float p_delta);
177 	friend class ParticleAttractor2D;
178 
179 	Set<ParticleAttractor2D *> attractors;
180 
181 protected:
182 	void _notification(int p_what);
183 	static void _bind_methods();
184 
185 public:
186 	void set_emitting(bool p_emitting);
187 	bool is_emitting() const;
188 
189 	void set_process_mode(ProcessMode p_mode);
190 	ProcessMode get_process_mode() const;
191 
192 	void set_amount(int p_amount);
193 	int get_amount() const;
194 
195 	void set_lifetime(float p_lifetime);
196 	float get_lifetime() const;
197 
198 	void set_time_scale(float p_time_scale);
199 	float get_time_scale() const;
200 
201 	void set_pre_process_time(float p_pre_process_time);
202 	float get_pre_process_time() const;
203 
204 	void set_emit_timeout(float p_timeout);
205 	float get_emit_timeout() const;
206 
207 	void set_emission_half_extents(const Vector2 &p_extents);
208 	Vector2 get_emission_half_extents() const;
209 
210 	void set_param(Parameter p_param, float p_value);
211 	float get_param(Parameter p_param) const;
212 
213 	void set_randomness(Parameter p_randomness, float p_value);
214 	float get_randomness(Parameter p_randomness) const;
215 
216 	void set_explosiveness(float p_value);
217 	float get_explosiveness() const;
218 
219 	void set_flip_h(bool p_flip);
220 	bool is_flipped_h() const;
221 
222 	void set_flip_v(bool p_flip);
223 	bool is_flipped_v() const;
224 
225 	void set_h_frames(int p_frames);
226 	int get_h_frames() const;
227 
228 	void set_v_frames(int p_frames);
229 	int get_v_frames() const;
230 
231 	void set_color_phases(int p_phases);
232 	int get_color_phases() const;
233 
234 	void set_color_phase_color(int p_phase, const Color &p_color);
235 	Color get_color_phase_color(int p_phase) const;
236 
237 	void set_color_phase_pos(int p_phase, float p_pos);
238 	float get_color_phase_pos(int p_phase) const;
239 
240 	void set_texture(const Ref<Texture> &p_texture);
241 	Ref<Texture> get_texture() const;
242 
243 	void set_color(const Color &p_color);
244 	Color get_color() const;
245 
246 	void set_color_ramp(const Ref<ColorRamp> &p_texture);
247 	Ref<ColorRamp> get_color_ramp() const;
248 
249 	void set_emissor_offset(const Point2 &p_offset);
250 	Point2 get_emissor_offset() const;
251 
252 	void set_use_local_space(bool p_use);
253 	bool is_using_local_space() const;
254 
255 	void set_initial_velocity(const Vector2 &p_velocity);
256 	Vector2 get_initial_velocity() const;
257 
258 	void set_emission_points(const DVector<Vector2> &p_points);
259 	DVector<Vector2> get_emission_points() const;
260 
261 	void pre_process(float p_delta);
262 	void reset();
263 
264 	Particles2D();
265 };
266 
267 VARIANT_ENUM_CAST(Particles2D::ProcessMode);
268 VARIANT_ENUM_CAST(Particles2D::Parameter);
269 
270 #endif // PARTICLES_FRAME_H
271