Class: ParticleFX2D::Emitter

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

Overview

A particle effect emitter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Emitter

Create a new particle emitter

  • quantity Number of total particles in the emitter’s pool

  • emission_rate Number of particles to emit per second

  • particle_config Options are used to configure each particle when it is emitted, which are as follows:

    • x Initial x-axis position of emission

    • x_range optional range from which a random value is chosen to add to the initial x; default is 0

    • y Initial y-axis position of emission

    • y_range optional range from which a random value is chosen to add to the initial y; default is 0

    • start_color optional initial colour of the emitted particle. See ‘Particle` for default

    • end_color optional end colour of the particle by the end of its life. See ‘Particle` for default

    • start_scale optional initial size scale factor of the emitted particle relative to its size; default is 1

    • end_scale optional end size scale factor of the particle by the end of its life; default is 1

    • angle optional angle at which the particle is emitted, in degrees. See ‘Particle` for default

    • angle_range optional range from which a random value is chosen to add to the angle; default is 0

    • speed optional speed at which the particle is emitted, in pixels/s. See ‘Particle` for default

    • speed_range optional range from which a random value is chosen to add to the speed; default is 0

    • size optional size of the particle when emitted, in pixels. See ‘Particle` for default

    • size_range optional range from which a random value is chosen to add to the size; default is 0

    • gravity_x optional linear acceleration in pixels/second squared along the x axis, default is 0

    • gravity_y optional linear acceleration in pixels/second squared along the y axis, default is 0

    • radial_acceleration optional radial accelation in pixel/seconds squared, default is 0

    • tangential_acceleration optional tangential accelation in pixel/seconds squared, default is 0

    • life_time of each particle in seconds. See ‘Particle` for default

    • life_time_range optional range from which a random value is chosen to add to the life_time; default is 0

Parameters:

  • opts (defaults to: {})

    Options to configure the emitter as follows:



38
39
40
41
42
43
44
45
# File 'lib/particlefx2d/emitter.rb', line 38

def initialize(opts = {})
  @quantity = (opts[:quantity] || 128).to_i
  @emission_rate = opts[:emission_rate].to_f
  @emission = 0
  @particle_config = opts[:particle_config]
  @renderer_factory = opts[:renderer_factory]
  setup_particle_pool
end

Instance Attribute Details

#emission_rateObject

Returns the value of attribute emission_rate.



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

def emission_rate
  @emission_rate
end

#particle_configObject

Returns the value of attribute particle_config.



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

def particle_config
  @particle_config
end

Instance Method Details

#statsObject

Retrieve statistics about the emitter’s current status

  • quantity The total number of particles in the emitter

  • emission_rate The emission rate (particles per second)

  • active The number of active particles

  • unused The number of inactive particles in the pool

Returns:

  • A hash with the following properties:



71
72
73
74
75
76
77
78
# File 'lib/particlefx2d/emitter.rb', line 71

def stats
  {
    quantity: @quantity,
    emission_rate: @emission_rate,
    active: @active.count,
    unused: @pool.count
  }
end

#update(frame_time) ⇒ Object

Update the particle effect emission, called for each frame of the animation cycle.

Parameters:

  • frame_time (Float)

    in seconds (essentially, 1 / fps)



52
53
54
55
56
57
58
59
60
# File 'lib/particlefx2d/emitter.rb', line 52

def update(frame_time)
  @renderer_factory.on_update_start
  emit_particles frame_time
  @active.each do |p|
    p.update frame_time
    free_particle p unless p.alive?
  end
  @renderer_factory.on_update_end
end