class AdventureRL::ClipPlayer

Constants

AUDIO_PLAYER_METHODS
DEFAULT_SETTINGS

Default settings for ClipPlayer. Are superseded by settings passed to new.

Public Class Methods

define_audio_player_methods() click to toggle source

Overwrite a bunch of FileGroupPlayer methods, so they also handle Audio, if Clip has one. See AUDIO_PLAYER_METHODS for the list of methods.

Calls superclass method
# File lib/AdventureRL/ClipPlayer.rb, line 55
def define_audio_player_methods
  AUDIO_PLAYER_METHODS.each do |real_method_name|
    [real_method_name, get_aliased_methods(real_method_name)].flatten.each do |method_name|
      define_method(method_name) do |*args|
        super *args
        get_audio_player.method(method_name).call(*args)  if (has_audio_player?)
        # NOTE: Write #sync_audio_player method, maybe?
        #       Should be unnecessarilty doubled work,
        #       but it would garantee that both Players are synced.
        # sync_audio_player
      end
    end
  end
end
get_aliased_methods(method_name) click to toggle source

Returns the method names of all methods aliased to method method_name.

# File lib/AdventureRL/ClipPlayer.rb, line 42
def get_aliased_methods method_name
  real_method = instance_method method_name
  return instance_methods.select do |instance_method_name|
    next (
      real_method == instance_method(instance_method_name) &&
      method_name != instance_method_name
    )
  end
end
new(settings = {}) click to toggle source

Pass settings Hash or Settings as argument. Supersedes DEFAULT_SETTINGS.

Calls superclass method
# File lib/AdventureRL/ClipPlayer.rb, line 75
def initialize settings = {}
  super
  @audio_player = nil
  set_mask_from get_settings(:mask)
end

Public Instance Methods

draw() click to toggle source

Draw the current image in the currently active Clip. This should be called every frame.

# File lib/AdventureRL/ClipPlayer.rb, line 111
def draw
  image = get_current_file
  return  unless (image)
  scale = get_scale_for_image image
  image.draw(
    get_side(:left), get_side(:top), get_settings(:z_index),
    scale[:x], scale[:y],
    get_settings(:color)
  )
end
get_audio_player() click to toggle source

Returns the current AudioPlayer, if there is one.

# File lib/AdventureRL/ClipPlayer.rb, line 82
def get_audio_player
  return @audio_player
end
has_audio_player?() click to toggle source

Returns true if an AudioPlayer was instantiated for this ClipPlayer.

# File lib/AdventureRL/ClipPlayer.rb, line 87
def has_audio_player?
  return !!get_audio_player
end
play(*args) click to toggle source

Overwrite FileGroupPlayer#play separately from above, because it should call handle_play_for_audio_player to create a new AudioPlayer, if necessary.

Calls superclass method
# File lib/AdventureRL/ClipPlayer.rb, line 102
def play *args
  super
  if (get_clip.has_audio?)
    handle_play_for_audio_player
  end
end

Private Instance Methods

get_default_settings() click to toggle source

Returns this class' DEFAULT_SETTINGS.

# File lib/AdventureRL/ClipPlayer.rb, line 183
def get_default_settings
  return DEFAULT_SETTINGS
end
get_scale_for_image(image = get_current_file) click to toggle source
# File lib/AdventureRL/ClipPlayer.rb, line 159
def get_scale_for_image image = get_current_file
  return {
    x: (get_size(:width).to_f  / image.width.to_f),
    y: (get_size(:height).to_f / image.height.to_f)
  }
end
handle_play_for_audio_player() click to toggle source

Create, change, or remove AudioPlayer, depending on if the current Clip has Audio.

# File lib/AdventureRL/ClipPlayer.rb, line 168
def handle_play_for_audio_player
  clip = get_clip
  if    (clip.has_audio?)
    if (has_audio_player?)
      @audio_player.play clip.get_audio
    else
      @audio_player = AudioPlayer.new get_settings
      @audio_player.play clip.get_audio
    end
  elsif (has_audio_player?)
    get_audio_player.stop
  end
end
load_file(file) click to toggle source

Loads the image file file

# File lib/AdventureRL/ClipPlayer.rb, line 140
def load_file file
  if (get_current_image.is_a? Gosu::Image)
    get_current_image.insert file, 0, 0
  else
    set_current_image Gosu::Image.new(
      file,
      get_settings(:image_options)
    )
  end
end
set_mask_from(mask) click to toggle source

Set the Mask for the ClipPlayer.

# File lib/AdventureRL/ClipPlayer.rb, line 125
def set_mask_from mask
  if    (mask.is_a?(Mask))
    mask.assign_to self
  elsif (mask.is_a?(Hash))
    Mask.new(
      mask.merge(
        assign_to: self
      )
    )
  else
    error "Cannot set Mask as #{mask.inspect}:#{mask.class.name} for ClipPlayer."
  end
end