Class: ParticleFX2D::Particle

Inherits:
Object
  • Object
show all
Defined in:
lib/particlefx2d/particle.rb

Overview

A single 2D particle.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#angleObject (readonly)

Returns the value of attribute angle.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def angle
  @angle
end

#colorObject (readonly)

Returns the value of attribute color.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def color
  @color
end

#end_colorObject (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_scaleObject (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_xObject (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_yObject (readonly)

Returns the value of attribute gravity_y.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def gravity_y
  @gravity_y
end

#lifeObject (readonly)

Returns the value of attribute life.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def life
  @life
end

#radial_accelObject (readonly)

Returns the value of attribute radial_accel.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def radial_accel
  @radial_accel
end

#rendererObject (readonly)

Returns the value of attribute renderer.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def renderer
  @renderer
end

#scaleObject (readonly)

Returns the value of attribute scale.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def scale
  @scale
end

#sizeObject (readonly)

Returns the value of attribute size.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def size
  @size
end

#speedObject (readonly)

Returns the value of attribute speed.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def speed
  @speed
end

#tangent_accelObject (readonly)

Returns the value of attribute tangent_accel.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def tangent_accel
  @tangent_accel
end

#velocityObject (readonly)

Returns the value of attribute velocity.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def velocity
  @velocity
end

#xObject (readonly)

Returns the value of attribute x.



8
9
10
# File 'lib/particlefx2d/particle.rb', line 8

def x
  @x
end

#yObject (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

Returns:

  • (Boolean)


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.

Parameters:

  • renderer

    The renderer must implement following methods:



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 or colour array of particle’s color components [r, g, b, a]; default is _[0, 1.0, 0, 1.0]_ (green)

  • end_color or end_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 is scale

  • 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

Parameters:

  • opts (defaults to: {})

    Options describing the particle as follows:



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.

Parameters:

  • frame_time (Float)

    in 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