Class: ParticleFX2D::Particle
- Inherits:
-
Object
- Object
- ParticleFX2D::Particle
- Defined in:
- lib/particlefx2d/particle.rb
Overview
A single 2D particle.
Instance Attribute Summary collapse
-
#angle ⇒ Object
readonly
Returns the value of attribute angle.
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#end_color ⇒ Object
readonly
Returns the value of attribute end_color.
-
#end_scale ⇒ Object
readonly
Returns the value of attribute end_scale.
-
#gravity_x ⇒ Object
readonly
Returns the value of attribute gravity_x.
-
#gravity_y ⇒ Object
readonly
Returns the value of attribute gravity_y.
-
#life ⇒ Object
readonly
Returns the value of attribute life.
-
#radial_accel ⇒ Object
readonly
Returns the value of attribute radial_accel.
-
#renderer ⇒ Object
readonly
Returns the value of attribute renderer.
-
#scale ⇒ Object
readonly
Returns the value of attribute scale.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#speed ⇒ Object
readonly
Returns the value of attribute speed.
-
#tangent_accel ⇒ Object
readonly
Returns the value of attribute tangent_accel.
-
#velocity ⇒ Object
readonly
Returns the value of attribute velocity.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
Instance Method Summary collapse
-
#alive? ⇒ Boolean
Returns true if the particle is considered alive.
-
#renderer!(renderer) ⇒ Object
Set the rendering peer for the particle.
-
#reset!(opts = {}) ⇒ Object
Used by the Emitter when re-using particles from the particle pool.
-
#update(frame_time) ⇒ Object
Used by the Emitter to update the particle by
frame_time
seconds.
Instance Attribute Details
#angle ⇒ Object (readonly)
Returns the value of attribute angle.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def angle @angle end |
#color ⇒ Object (readonly)
Returns the value of attribute color.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def color @color end |
#end_color ⇒ Object (readonly)
Returns the value of attribute end_color.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def end_color @end_color end |
#end_scale ⇒ Object (readonly)
Returns the value of attribute end_scale.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def end_scale @end_scale end |
#gravity_x ⇒ Object (readonly)
Returns the value of attribute gravity_x.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def gravity_x @gravity_x end |
#gravity_y ⇒ Object (readonly)
Returns the value of attribute gravity_y.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def gravity_y @gravity_y end |
#life ⇒ Object (readonly)
Returns the value of attribute life.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def life @life end |
#radial_accel ⇒ Object (readonly)
Returns the value of attribute radial_accel.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def radial_accel @radial_accel end |
#renderer ⇒ Object (readonly)
Returns the value of attribute renderer.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def renderer @renderer end |
#scale ⇒ Object (readonly)
Returns the value of attribute scale.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def scale @scale end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def size @size end |
#speed ⇒ Object (readonly)
Returns the value of attribute speed.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def speed @speed end |
#tangent_accel ⇒ Object (readonly)
Returns the value of attribute tangent_accel.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def tangent_accel @tangent_accel end |
#velocity ⇒ Object (readonly)
Returns the value of attribute velocity.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def velocity @velocity end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
8 9 10 |
# File 'lib/particlefx2d/particle.rb', line 8 def y @y end |
Instance Method Details
#alive? ⇒ Boolean
Returns true if the particle is considered alive
67 68 69 |
# File 'lib/particlefx2d/particle.rb', line 67 def alive? @life.positive? end |
#renderer!(renderer) ⇒ Object
Set the rendering peer for the particle.
-
show_particle(p) The specified particle is visible.
-
hide_particle(p) The specified particle is no longer visible.
-
draw_particle(p) Render the specified particle; this method is called only if a particle is visible.
60 61 62 |
# File 'lib/particlefx2d/particle.rb', line 60 def renderer!(renderer) @renderer = renderer end |
#reset!(opts = {}) ⇒ Object
Used by the Emitter when re-using particles from the particle pool.
-
x
defaults to 0 -
y
defaults to 0 -
color
orcolour
array of particle’s color components [r, g, b, a]; default is _[0, 1.0, 0, 1.0]_ (green) -
end_color
orend_colour
array of particle’s end color [r, g, b, a]; default is _[1.0, 0, 0, 1.0]_ (red) -
angle
in degrees; default is 0 -
speed
in pixels per second; default is 100 -
life_time
in seconds; default is 100.0 -
size
in pixels; default is 5 -
scale
relative to size, default is 1 -
end_scale
particle’s end scale relative to size; default isscale
-
gravity_x
in pixels/second squared along the x axis, default is 0 -
gravity_y
in pixels/second squared along the y axis, default is 0 -
radial_acceleration
in pixel/seconds squared, default is 0 -
tangential_acceleration
in pixel/seconds squared, default is 0
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/particlefx2d/particle.rb', line 39 def reset!(opts = {}) @initial_life = @life = value_from(opts, :life_time, default: 100).to_f @initial_size = @size = value_from(opts, :size, default: 5) @gravity = Vector2D.new value_from(opts, :gravity_x, default: 0), value_from(opts, :gravity_y, default: 0) # following may depend on initial life and size reset_forces opts reset_scale_from opts reset_position_from opts reset_color_from opts reset_velocity_from opts end |
#update(frame_time) ⇒ Object
Used by the Emitter to update the particle by frame_time
seconds.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/particlefx2d/particle.rb', line 76 def update(frame_time) @life -= frame_time return unless alive? @scale += @delta_scale * frame_time @size = @initial_size * @scale @color.add!(@delta_color, each_times: frame_time) update_forces update_motion frame_time @renderer&.draw_particle self end |