Sha256: f85d9c4145f2ddc94b512081925d98e9f5be4b2d31bfcc5cd83b5b8b5f4624a9

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

module Imageable extend Trait
  
  class ImageMissingError < RuntimeError
    def initialize
      super <<-MESSAGE
        
        In an Imageable, you either need to define method
          image path, *args
        for an unchanging image
        or
          sequenced_image path, width, height, frequency = 10, &block
        for a sprite sequence.
        
      MESSAGE
    end
  end
  
  def self.included klass
    klass.extend ClassMethods
  end
  
  def initialize window
    raise ImageMissingError.new unless self.respond_to? :image
    super window
  end
  
  module ClassMethods
    
    def image path, *args
      InitializerHooks.register self do
        @image = Gosu::Image.new self.window, File.join(Resources.root, path), *args
      end
      define_method :image do
        @image
      end
    end
    
    def sequenced_image path, width, height, frequency = 10, &block
      InitializerHooks.register self do
        @image_sequence_started = Time.now
        @image = Gosu::Image::load_tiles self.window, File.join(Resources.root, path), width, height, false
      end
      # divider = 1000 / frequency
      define_method :image do
        @image[(block ? block : lambda { (Time.now - @image_sequence_started)*frequency % @image.size })[]]
      end
    end
    
  end
  
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
gosu_extensions-0.3.0 lib/traits/imageable.rb
gosu_extensions-0.2.9 lib/traits/imageable.rb
gosu_extensions-0.2.8 lib/traits/imageable.rb
gosu_extensions-0.2.7 lib/traits/imageable.rb
gosu_extensions-0.2.6 lib/traits/imageable.rb
gosu_extensions-0.2.5 lib/traits/imageable.rb
gosu_extensions-0.2.4 lib/traits/imageable.rb
gosu_extensions-0.2.3 lib/traits/imageable.rb
gosu_extensions-0.2.2 lib/traits/imageable.rb
gosu_extensions-0.2.1 lib/traits/imageable.rb
gosu_extensions-0.2.0 lib/traits/imageable.rb
gosu_extensions-0.1.28 lib/traits/imageable.rb
gosu_extensions-0.1.27 lib/traits/imageable.rb
gosu_extensions-0.1.26 lib/traits/imageable.rb