class AdventureRL::FileGroupPlayer

This is an abstract class, which is inherited by

At its core, it takes a FileGroup in its play method, and “plays” it.

Public Class Methods

new(settings = {}) click to toggle source

Pass settings Hash or Settings as argument. Supersedes the child class' DEFAULT_SETTINGS.

# File lib/AdventureRL/FileGroupPlayer.rb, line 12
def initialize settings = {}
  @settings = get_default_settings.merge settings
  @playing                  = false
  @speed                    = @settings.get(:speed)
  @filegroup_index          = 0
  @filegroup                = nil
  @current_file             = nil
  @current_time             = 0.0
  @last_current_time        = 0.0
  @target_frame_delay       = 1.0 / 24.0  # Default, will be overwritten in #play
  @deltatime                = Deltatime.new
end

Public Instance Methods

get_current_time() click to toggle source

Returns the current playback time in seconds.

# File lib/AdventureRL/FileGroupPlayer.rb, line 39
def get_current_time
  return @current_time
end
Also aliased as: get_time
get_filegroup() click to toggle source

Returns the currently set FileGroup.

# File lib/AdventureRL/FileGroupPlayer.rb, line 34
def get_filegroup
  return @filegroup
end
get_settings(*keys) click to toggle source

Returns the settings as AdventureRL::Settings, unless *keys are given, then it returns the value of @settings.get(*keys).

# File lib/AdventureRL/FileGroupPlayer.rb, line 28
def get_settings *keys
  return @settings  if (keys.empty?)
  return @settings.get(*keys)
end
get_speed() click to toggle source

Returns the current playback speed multiplier.

# File lib/AdventureRL/FileGroupPlayer.rb, line 66
def get_speed
  return @speed
end
get_time()
Alias for: get_current_time
has_filegroup?() click to toggle source

Returns true if there is a currently active FileGroup.

# File lib/AdventureRL/FileGroupPlayer.rb, line 118
def has_filegroup?
  return !!get_filegroup
end
increase_current_time(seconds) click to toggle source

Increase (or decrease) current_time.

# File lib/AdventureRL/FileGroupPlayer.rb, line 55
def increase_current_time seconds
  error(
    "Passed seconds must be an Integer or Float,",
    "but got #{seconds.inspect}:#{seconds.class.name}"
  )  unless ([Integer, Float].include? seconds.class)
  @current_time += seconds
end
Also aliased as: increase_time, seek
increase_speed(amount) click to toggle source

Increment (or decrement) the speed value by amount.

# File lib/AdventureRL/FileGroupPlayer.rb, line 80
def increase_speed amount
  error(
    "Argument passed to #increment_speed must be a Float or Integer, but got",
    "#{seconds.inspect}:#{amount.class.name}"
  )  unless ([Float, Integer].include? amount.class)
  @speed += amount
end
increase_time(seconds)
is_playing?() click to toggle source

Returns true if is currently playing, and false if is paused or stopped.

# File lib/AdventureRL/FileGroupPlayer.rb, line 151
def is_playing?
  return @playing
end
load_filegroup(filegroup) click to toggle source

Load a FileGroup as active FileGroup.

# File lib/AdventureRL/FileGroupPlayer.rb, line 96
def load_filegroup filegroup
  error(
    "Passed argument must be an instance of FileGroup, but got",
    "#{filegroup.inspect}:#{filegroup.class.name}."
  )  unless (filegroup.is_a? FileGroup)
  set_filegroup filegroup
  @target_frame_delay = 1.0 / get_filegroup.get_settings(:fps).to_f
end
pause() click to toggle source

Pause the currently playing FileGroup.

# File lib/AdventureRL/FileGroupPlayer.rb, line 106
def pause
  @playing = false
end
play(filegroup) click to toggle source

Start playing FileGroup filegroup.

# File lib/AdventureRL/FileGroupPlayer.rb, line 89
def play filegroup
  load_filegroup filegroup
  @playing = true
  reset
end
reset() click to toggle source

Reset the current playback. Start playing from the start again.

# File lib/AdventureRL/FileGroupPlayer.rb, line 142
def reset
  @deltatime.reset
  @current_time    = 0.0
  @filegroup_index = 0
  set_file
end
resume() click to toggle source

Resumes playing paused FileGroup.

# File lib/AdventureRL/FileGroupPlayer.rb, line 111
def resume
  return  unless (has_filegroup?)
  @playing = true
  @deltatime.reset
end
seek(seconds)
set_current_time(seconds) click to toggle source

Set a new current playback time in seconds.

# File lib/AdventureRL/FileGroupPlayer.rb, line 45
def set_current_time seconds
  error(
    "Passed seconds must be an Integer or Float,",
    "but got #{seconds.inspect}:#{seconds.class.name}"
  )  unless ([Integer, Float].include? seconds.class)
  @current_time = seconds
end
Also aliased as: set_time
set_speed(speed) click to toggle source

Set playback speed multiplier.

# File lib/AdventureRL/FileGroupPlayer.rb, line 71
def set_speed speed
  error(
    "Argument passed to #set_speed must be a Float or Integer, but got",
    "#{speed.inspect}:#{speed.class.name}"
  )  unless ([Float, Integer].include? speed.class)
  @speed = speed
end
set_time(seconds)
Alias for: set_current_time
stop() click to toggle source

Stop playing and clear active FileGroup. Cannot call resume after this, before calling play again.

# File lib/AdventureRL/FileGroupPlayer.rb, line 125
def stop
  @filegroup = nil
  @playing   = false
end
toggle() click to toggle source

Calls resume if is paused, or calls pause if is playing.

# File lib/AdventureRL/FileGroupPlayer.rb, line 132
def toggle
  if    (is_playing?)
    pause
  elsif (has_filegroup?)
    resume
  end
end
update() click to toggle source

Check which file from FileGroup is supposed to be played. This should be called every frame.

# File lib/AdventureRL/FileGroupPlayer.rb, line 157
def update
  return  unless (is_playing?)
  set_filegroup_index
  update_timing
end

Private Instance Methods

get_current_file() click to toggle source
# File lib/AdventureRL/FileGroupPlayer.rb, line 218
def get_current_file
  return @current_file
end
get_default_settings() click to toggle source

This method should be overwritten by the child class, and return their specific DEFAULT_SETTINGS.

# File lib/AdventureRL/FileGroupPlayer.rb, line 167
def get_default_settings
  return {}
end
get_filegroup_index() click to toggle source
# File lib/AdventureRL/FileGroupPlayer.rb, line 191
def get_filegroup_index
  return @filegroup_index
end
Also aliased as: get_index
get_index()
Alias for: get_filegroup_index
load_file(file) click to toggle source

This method should be overwritten by the child class. It is passed the filepath file.

# File lib/AdventureRL/FileGroupPlayer.rb, line 188
def load_file file
end
set_current_file(new_file) click to toggle source
# File lib/AdventureRL/FileGroupPlayer.rb, line 222
def set_current_file new_file
  @current_file = new_file
end
set_file() click to toggle source
# File lib/AdventureRL/FileGroupPlayer.rb, line 177
def set_file
  filegroup = get_filegroup
  return  if (
    !filegroup ||
    !filegroup.has_index?(get_filegroup_index)
  )
  load_file filegroup.get_file(get_filegroup_index).to_s
end
set_filegroup(filegroup) click to toggle source
# File lib/AdventureRL/FileGroupPlayer.rb, line 171
def set_filegroup filegroup
  @filegroup_index = 0
  @filegroup       = filegroup
  set_file
end
set_filegroup_index() click to toggle source
# File lib/AdventureRL/FileGroupPlayer.rb, line 196
def set_filegroup_index
  previous_index = get_filegroup_index
  index = (get_current_time / @target_frame_delay).floor
  return  if (previous_index == index)
  @filegroup_index = index
  filegroup = get_filegroup
  unless (filegroup.has_index? get_filegroup_index)
    if (get_settings(:loop))
      reset
    else
      stop
    end
    return
  end
  set_file
end
update_timing() click to toggle source
# File lib/AdventureRL/FileGroupPlayer.rb, line 213
def update_timing
  @current_time += @deltatime.dt * @speed
  @deltatime.update
end