require './ellipsoid.rb' # One particle is basically a sprite, that moves itself by speed every tick along an angle. # It also has a concept of decay, in how quickly the particle dies. In this case, the draw # command takes into account decay to shrink the particles and make them more translucent. class Particle def initialize(window, x, y, ang, speed, decay) @x = x @y = y @angle = 90-ang @speed = speed @d_x = offset_x(@angle, @speed) @d_y = offset_y(@angle, @speed) @life = 1 @decay = decay radius = (rand * 10).floor @img = Gosu::Image.new(window, Ellipsoid.new(radius), false) end def update @x = @x + @d_x @y = @y + @d_y @life = @life - @decay @angle += (@speed / 2) end def dead? @life <= 0 end def draw_layer(layer, color, x = nil, y = nil) @img.draw_rot(x || @x, y || @y, layer, @angle, 0.5, 0.5, @life, @life, color, :default) end def draw return if dead? draw_layer(ZOrder::Shadow, Gosu::Color.new((@life * 128).floor,0,0,0), @x+2, @y+2) draw_layer(ZOrder::Shape, Gosu::Color.new((@life * 255).floor, 128,128,128)) draw_layer(ZOrder::Highlight, Gosu::Color.new((@life * 20).floor,200,200,200)) end end # This class keeps track of our moving particles. It loads in a few white png files, and removes # dead particles during every tick. They could probably be garbage collected less frequently. class Particles def initialize(window) @particles = [] @window = window end def update @particles.each {|p| p.update} @particles.delete_if {|p| p.dead?} end def draw @particles.each {|p| p.draw} end def create(x, y, angle, speed) @particles << Particle.new(@window, x, y, angle, speed, 0.01) end end