Class: Dopairb::Fx::Particles
- Inherits:
-
Object
- Object
- Dopairb::Fx::Particles
- Defined in:
- lib/dopairb/fx.rb
Instance Attribute Summary collapse
-
#list ⇒ Object
readonly
Returns the value of attribute list.
Instance Method Summary collapse
- #add(x, y, vx, vy, at:, life:, palette:, glyph: nil, gravity: 0.0, drag: 0.0) ⇒ Object
- #alive?(t) ⇒ Boolean
-
#burst(x, y, n, at:, speed: 20.0, spread: 1.0, life: 0.6, palette: Color::FIRE, glyphs: nil, gravity: 18.0, angle: nil, arc: Math::PI * 2, drag: 1.5) ⇒ Object
speed in cells/sec; y velocity is squashed to respect the cell aspect.
- #clear ⇒ Object
- #draw(canvas, t) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(rng = Random.new) ⇒ Particles
constructor
A new instance of Particles.
- #position(p, age) ⇒ Object
- #prune(t) ⇒ Object
Constructor Details
#initialize(rng = Random.new) ⇒ Particles
Returns a new instance of Particles.
12 13 14 15 |
# File 'lib/dopairb/fx.rb', line 12 def initialize(rng = Random.new) @rng = rng @list = [] end |
Instance Attribute Details
#list ⇒ Object (readonly)
Returns the value of attribute list.
10 11 12 |
# File 'lib/dopairb/fx.rb', line 10 def list @list end |
Instance Method Details
#add(x, y, vx, vy, at:, life:, palette:, glyph: nil, gravity: 0.0, drag: 0.0) ⇒ Object
33 34 35 |
# File 'lib/dopairb/fx.rb', line 33 def add(x, y, vx, vy, at:, life:, palette:, glyph: nil, gravity: 0.0, drag: 0.0) @list << Particle.new(x, y, vx, vy, at, life, palette, glyph, gravity, drag) end |
#alive?(t) ⇒ Boolean
41 42 43 |
# File 'lib/dopairb/fx.rb', line 41 def alive?(t) @list.any? { |p| t - p.born <= p.life } end |
#burst(x, y, n, at:, speed: 20.0, spread: 1.0, life: 0.6, palette: Color::FIRE, glyphs: nil, gravity: 18.0, angle: nil, arc: Math::PI * 2, drag: 1.5) ⇒ Object
speed in cells/sec; y velocity is squashed to respect the cell aspect
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/dopairb/fx.rb', line 21 def burst(x, y, n, at:, speed: 20.0, spread: 1.0, life: 0.6, palette: Color::FIRE, glyphs: nil, gravity: 18.0, angle: nil, arc: Math::PI * 2, drag: 1.5) n.times do a = angle ? angle + (@rng.rand - 0.5) * arc : @rng.rand * Math::PI * 2 s = speed * (0.35 + @rng.rand * 0.65 * spread) g = glyphs && glyphs[@rng.rand(glyphs.size)] @list << Particle.new(x, y, Math.cos(a) * s, Math.sin(a) * s * 0.5, at, life * (0.6 + @rng.rand * 0.4), palette, g, gravity, drag) end self end |
#clear ⇒ Object
17 |
# File 'lib/dopairb/fx.rb', line 17 def clear = @list.clear |
#draw(canvas, t) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dopairb/fx.rb', line 45 def draw(canvas, t) @list.each do |p| age = t - p.born next if age < 0 || age > p.life x, y = position(p, age) col = Color.ramp(p.palette, age / p.life) if p.glyph canvas.put(x, y, p.glyph, col, bold: age < p.life * 0.3) else canvas.dot(x * 2, y * 4, col) end end end |
#empty? ⇒ Boolean
18 |
# File 'lib/dopairb/fx.rb', line 18 def empty? = @list.empty? |
#position(p, age) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/dopairb/fx.rb', line 59 def position(p, age) if p.drag > 0 k = (1 - Math.exp(-p.drag * age)) / p.drag [p.x + p.vx * k, p.y + p.vy * k + 0.5 * p.gravity * age * age * 0.25] else [p.x + p.vx * age, p.y + p.vy * age + 0.5 * p.gravity * age * age * 0.25] end end |
#prune(t) ⇒ Object
37 38 39 |
# File 'lib/dopairb/fx.rb', line 37 def prune(t) @list.reject! { |p| t - p.born > p.life } end |