Class: MiniGL::Effect

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

Overview

Represents a visual effect, i.e., a graphic - usually animated - that shows up in the screen, lasts for a given time and “disappears”. You should explicitly dispose of references to effects whose attribute dead is set to true.

Instance Attribute Summary collapse

Attributes inherited from Sprite

#img, #img_index, #x, #y

Instance Method Summary collapse

Methods inherited from Sprite

#animate, #animate_once, #set_animation, #visible?

Constructor Details

#initialize(x, y = nil, img = nil, sprite_cols = nil, sprite_rows = nil, interval = 10, indices = nil, lifetime = nil, sound = nil, sound_ext = '.wav', sound_volume = 1) ⇒ Effect

Creates a new Effect.

Parameters:

x

The x-coordinate in the screen (or map) where the effect will be drawn. This can be modified later via the x attribute.

y

The y-coordinate in the screen (or map) where the effect will be drawn. This can be modified later via the y attribute.

img

The image or spritesheet to use for this effect (see Sprite for details on spritesheets).

sprite_cols

(see Sprite)

sprite_rows

(see Sprite)

interval

The interval between steps of the animation, in updates.

indices

The indices to use in the animation. See Sprite#animate for details. If nil, it will be the sequence from 0 to sprite_cols * sprite_rows - 1.

lifetime

The lifetime of the effect, in updates. After update is called this number of times, the effect will no longer be visible, even when draw is called, and the dead flag will be set to true, so you get to know when to dispose of the Effect object. If nil, it will be set to @indices.length * interval, i.e., the exact time needed for one animation cycle to complete.

sound

The id of a sound to be played when the effect is created (id must be given in the format specified for the Res.sound method).

sound_ext

Extension of the sound file, if a sound is given. Default is ‘.wav’.

sound_volume

The volume (from 0 to 1) to play the sound, if any. Default is 1.

Obs.: This method accepts named parameters, but x, y and img are mandatory.



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/minigl/game_object.rb', line 357

def initialize(x, y = nil, img = nil, sprite_cols = nil, sprite_rows = nil, interval = 10, indices = nil, lifetime = nil,
               sound = nil, sound_ext = '.wav', sound_volume = 1)
  if x.is_a? Hash
    y = x[:y]
    img = x[:img]
    sprite_cols = x.fetch(:sprite_cols, nil)
    sprite_rows = x.fetch(:sprite_rows, nil)
    interval = x.fetch(:interval, 10)
    indices = x.fetch(:indices, nil)
    lifetime = x.fetch(:lifetime, nil)
    sound = x.fetch(:sound, nil)
    sound_ext = x.fetch(:sound_ext, '.wav')
    sound_volume = x.fetch(:sound_volume, 1)
    x = x[:x]
  end

  super x, y, img, sprite_cols, sprite_rows
  @elapsed_time = 0
  if indices
    @indices = indices
  else
    @indices = *(0..(@img.length - 1))
  end
  @interval = interval
  if lifetime
    @lifetime = lifetime
  else
    @lifetime = @indices.length * interval
  end
  Res.sound(sound, false, sound_ext).play(sound_volume) if sound
end

Instance Attribute Details

#deadObject (readonly)

This is true when the effect’s lifetime has already passed.



316
317
318
# File 'lib/minigl/game_object.rb', line 316

def dead
  @dead
end

#elapsed_timeObject (readonly)

The number of times update has been called for this effect, while it was still active (not dead).



324
325
326
# File 'lib/minigl/game_object.rb', line 324

def elapsed_time
  @elapsed_time
end

#lifetimeObject (readonly)

The lifetime of the effect, in updates, i.e., how many calls to update must happen before the effect is marked as dead, since its creation.



320
321
322
# File 'lib/minigl/game_object.rb', line 320

def lifetime
  @lifetime
end

Instance Method Details

#draw(map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, flip = nil, z_index = 0, round = false) ⇒ Object



398
399
400
# File 'lib/minigl/game_object.rb', line 398

def draw(map = nil, scale_x = 1, scale_y = 1, alpha = 0xff, color = 0xffffff, angle = nil, flip = nil, z_index = 0, round = false)
  super unless @dead
end

#time_leftObject

The remaining number of calls to update until the effect is marked dead.



404
405
406
# File 'lib/minigl/game_object.rb', line 404

def time_left
  @lifetime - @elapsed_time
end

#updateObject

Updates the effect, animating and counting its remaining lifetime.



390
391
392
393
394
395
396
# File 'lib/minigl/game_object.rb', line 390

def update
  return if @dead

  animate(@indices, @interval)
  @elapsed_time += 1
  @dead = true if @elapsed_time == @lifetime
end