11extends Node2D
22
3- export (int ) var min_particles_number = 400
3+ export (int ) var min_particles_number = 200
44export (int ) var max_particles_number = 400
55
6- export (float ) var min_particles_gravity = 200
7- export (float ) var max_particles_gravity = 600
6+ export (float ) var min_particles_gravity = 200.0
7+ export (float ) var max_particles_gravity = 600.0
88
9- export (float ) var min_particles_velocity = 200
10- export (float ) var max_particles_velocity = 600
9+ export (float ) var min_particles_velocity = 200.0
10+ export (float ) var max_particles_velocity = 600.0
1111
1212export (int ) var max_particles_position_x = ProjectSettings .get_setting ("display/window/size/width" )
1313export (int ) var max_particles_position_y = ProjectSettings .get_setting ("display/window/size/height" )
1414
15- export (int ) var min_particles_size = 2
16- export (int ) var max_particles_size = 6
15+ export (int ) var min_particles_size = 1
16+ export (int ) var max_particles_size = 4
1717
1818export (bool ) var get_random_position = false
1919export (bool ) var start_timer = false
20+ export (float ) var timer_wait_time = 1.0
2021export (bool ) var particles_explode = false
2122export (String ) var group_name = "fake_explosion_particles"
2223
2324var particles = []
2425var particles_number
2526var particles_initial_position
2627
27- # var particles_colors = [
28- # Color("#ffffff"),
29- # Color("#000000"),
30- # Color("#ff004d"),
31- # Color("#ffa300"),
32- # Color("#ffec27")
33- # ]
34-
3528var particles_colors_with_weights = [
3629 [4 , Color ("#ffffff" )],
3730 [2 , Color ("#000000" )],
@@ -40,25 +33,21 @@ var particles_colors_with_weights = [
4033 [10 , Color ("#ffec27" )]
4134]
4235
43-
4436var particles_timer
45- var particles_timer_wait_time = 1
4637
4738func _ready ():
4839 # Add to a group so it can be found from anywhere.
4940 add_to_group (group_name )
5041
5142 # Create the initial particles.
52- particles_initial_position = _get_random_position () if get_random_position else position
53- particles_number = _get_random_number ()
5443 _create_particles ()
5544
5645 # Create a timer.
5746 particles_timer = Timer .new ()
5847 particles_timer .one_shot = false
59- particles_timer .wait_time = particles_timer_wait_time
48+ particles_timer .wait_time = timer_wait_time
6049 particles_timer .set_timer_process_mode (1 )
61- particles_timer .connect ("timeout" , self , "_on_timer_timeout " )
50+ particles_timer .connect ("timeout" , self , "_on_particles_timer_timeout " )
6251
6352 add_child (particles_timer , true )
6453
@@ -76,7 +65,7 @@ func _process(delta):
7665 update ()
7766
7867 # If there are no particles in the particles array, free the node.
79- if particles .size () == 0 :
68+ if particles .size () == 0 and not start_timer :
8069 queue_free ()
8170
8271
@@ -93,7 +82,7 @@ func _particles_explode(delta):
9382 particle .position += (particle .velocity + particle .gravity ) * delta
9483
9584 particle .time += delta
96- # print(particle.time)
85+
9786 if particle .time > _get_random_time ():
9887 # Fade out the particles.
9988 if particle .color .a > 0 :
@@ -113,6 +102,13 @@ func _particles_explode(delta):
113102
114103
115104func _create_particles ():
105+ # Set the node's position to (0,0) to get proper random position values.
106+ if get_random_position : position = Vector2 .ZERO
107+
108+ # Set initial values.
109+ particles_initial_position = _get_random_position () if get_random_position else Vector2 .ZERO
110+ particles_number = _get_random_number ()
111+
116112 # Empty the particles array.
117113 particles .clear ()
118114
@@ -148,7 +144,6 @@ func _get_random_alpha():
148144
149145func _get_random_color ():
150146 randomize ()
151- # var random_color = particles_colors[randi() % particles_colors.size()]
152147 var random_color = _rand_array (particles_colors_with_weights )
153148 return random_color
154149
@@ -218,24 +213,22 @@ func _get_random_time():
218213
219214func _rand_array (array ):
220215 # Code from @CowThing (https://pastebin.com/HhdBuUzT).
221- # Arrays must be [weight, value].
222-
223- var sum_of_weights = 0
224- for t in array :
225- sum_of_weights += t [0 ]
226-
227- var x = randf () * sum_of_weights
228-
229- var cumulative_weight = 0
230- for t in array :
231- cumulative_weight += t [0 ]
232-
233- if x < cumulative_weight :
234- return t [1 ]
235-
236-
237- func _on_timer_timeout ():
216+ # Arrays must be [weight, value].
217+
218+ var sum_of_weights = 0
219+ for t in array :
220+ sum_of_weights += t [0 ]
221+
222+ var x = randf () * sum_of_weights
223+
224+ var cumulative_weight = 0
225+ for t in array :
226+ cumulative_weight += t [0 ]
227+
228+ if x < cumulative_weight :
229+ return t [1 ]
230+
231+
232+ func _on_particles_timer_timeout ():
238233 # Create new particles every time the timer times out.
239- particles_initial_position = _get_random_position () if get_random_position else position
240- particles_number = _get_random_number ()
241234 _create_particles ()
0 commit comments