Class: MiniGL::Particles::Particle

Inherits:
Object
  • Object
show all
Defined in:
lib/minigl/particles.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(x:, y:, duration:, shape: nil, img: nil, **options) ⇒ Particle

Returns a new instance of Particle.



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/minigl/particles.rb', line 212

def initialize(x:, y:, duration:, shape: nil, img: nil, **options)
  @x = x
  @y = y
  @duration = duration
  @shape = shape
  @img = img
  @options = DEFAULT_OPTIONS.slice(*PARTICLE_OPTIONS).merge(options)
  @elapsed_time = 0

  if @options[:angle].is_a?(Range)
    @angle = rand(@options[:angle])
  elsif @options[:angle].is_a?(Numeric)
    @angle = @options[:angle]
  end

  if @options[:speed].is_a?(Hash)
    speed_x = @options[:speed][:x].is_a?(Range) ? rand(@options[:speed][:x]) : (@options[:speed][:x] || 0)
    speed_y = @options[:speed][:y].is_a?(Range) ? rand(@options[:speed][:y]) : (@options[:speed][:y] || 0)
    @speed = Vector.new(speed_x, speed_y)
  elsif @options[:speed].is_a?(Vector)
    @speed = @options[:speed]
  end

  init_variable_property(:scale)
  init_variable_property(:alpha)
end

Instance Method Details

#dead?Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/minigl/particles.rb', line 270

def dead?
  @elapsed_time >= @duration
end

#draw(map, z_index) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/minigl/particles.rb', line 295

def draw(map, z_index)
  x = @x - (map&.cam&.x || 0)
  y = @y - (map&.cam&.y || 0)
  if @options[:round_position]
    x = x.round
    y = y.round
  end
  color = (@alpha << 24) | @options[:color]
  if @img
    if @angle
      @img.draw_rot(x, y, z_index, @angle, 0.5, 0.5, @scale, @scale, color)
    else
      @img.draw(x - @img.width * @scale * 0.5, y - @img.height * @scale * 0.5, z_index, @scale, @scale, color)
    end
  else
    case @shape
    when :square
      G.window.draw_rect(@x - @scale * 0.5, @y - @scale * 0.5, @scale, @scale, color, z_index)
    when :triangle_up
      G.window.draw_triangle(@x - @scale * 0.5, @y + @scale * 0.433, color,
                             @x + @scale * 0.5, @y + @scale * 0.433, color,
                             @x, @y - @scale * 0.433, color, z_index)
    when :triangle_down
      G.window.draw_triangle(@x - @scale * 0.5, @y - @scale * 0.433, color,
                             @x + @scale * 0.5, @y - @scale * 0.433, color,
                             @x, @y + @scale * 0.433, color, z_index)
    end
  end
end

#init_variable_property(name) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
# File 'lib/minigl/particles.rb', line 239

def init_variable_property(name)
  ivar_name = "@#{name}".to_sym
  case @options["#{name}_change".to_sym]
  when :grow, :alternate
    instance_variable_set(ivar_name, @options["#{name}_min".to_sym])
  when :shrink
    instance_variable_set(ivar_name, @options["#{name}_max".to_sym])
  else
    instance_variable_set(ivar_name, @options[name.to_sym])
  end
end

#updateObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/minigl/particles.rb', line 274

def update
  if @options[:rotation] && !@img.nil?
    @angle = 0 if @angle.nil?
    @angle += @options[:rotation]
    @angle -= 360 if @angle >= 360
  end

  if @speed
    @x += @speed.x
    @y += @speed.y
  end

  update_variable_property(:scale) if @options[:scale_change]
  if @options[:alpha_change]
    update_variable_property(:alpha)
    @alpha = @alpha.round
  end

  @elapsed_time += 1
end

#update_variable_property(name) ⇒ Object



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/minigl/particles.rb', line 251

def update_variable_property(name)
  ivar_name = "@#{name}".to_sym
  min = @options["#{name}_min".to_sym]
  max = @options["#{name}_max".to_sym]
  case @options["#{name}_change".to_sym]
  when :grow
    instance_variable_set(ivar_name, min + (@elapsed_time.to_f / @duration) * (max - min))
  when :shrink
    instance_variable_set(ivar_name, max - (@elapsed_time.to_f / @duration) * (max - min))
  when :alternate
    inflection_point = (@options["#{name}_inflection".to_sym] * @duration).round
    if @elapsed_time >= inflection_point
      instance_variable_set(ivar_name, min + (@duration - @elapsed_time).to_f / (@duration - inflection_point) * (max - min))
    else
      instance_variable_set(ivar_name, min + (@elapsed_time.to_f / inflection_point) * (max - min))
    end
  end
end