class RubyFighter::Animation

Public Class Methods

new(window, pattern) click to toggle source
# File lib/ruby_fighter/animation.rb, line 5
def initialize(window, pattern)
  @images = find_images_matching(pattern, window)
end

Public Instance Methods

draw(*args) click to toggle source
# File lib/ruby_fighter/animation.rb, line 13
def draw(*args)
  current_image.draw *args
end
play_once(&callback) click to toggle source
# File lib/ruby_fighter/animation.rb, line 17
def play_once(&callback)
  @index           = nil
  @last_time       = nil
  @finish_callback = callback
end
width() click to toggle source
# File lib/ruby_fighter/animation.rb, line 9
def width
  @images[0].width
end

Private Instance Methods

current_image() click to toggle source
# File lib/ruby_fighter/animation.rb, line 25
def current_image
  @last_time ||= Gosu.milliseconds
  @index     ||= 0

  next_image if Gosu.milliseconds - @last_time > 150

  @images[@index]
end
find_images_matching(pattern, window) click to toggle source
# File lib/ruby_fighter/animation.rb, line 45
def find_images_matching(pattern, window)
  Dir["assets/#{pattern}*"].map do |path|
    Gosu::Image.new(window, path, false)
  end
end
next_image() click to toggle source
# File lib/ruby_fighter/animation.rb, line 34
def next_image
  @last_time   = Gosu.milliseconds
  @index      += 1
  @index       = 0 if @index > @images.size - 1

  if @index == 0 && @finish_callback
    @finish_callback.call
    @finish_callback = nil
  end
end