Sha256: 25663726c2e1bb8503272d31536495c19eeacce7b7c02599685ea8f022a67fc7

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# Add methods to load and access images, sounds & songs
module Lotu
  module Resourceful

    def self.extended(instance)
      instance.init_behavior
    end

    def init_behavior
      @images = {}
      @sounds = {}
      @songs = {}
    end

    def image(name)
      @images[name]
    end

    def sound(name)
      @sounds[name]
    end

    def song(name)
      @songs[name]
    end

    def load_images(path)
      load_resources(@images, /\.png|\.jpg|\.bmp/, path, Gosu::Image)
    end

    def load_sounds(path)
      load_resources(@sounds, /\.ogg|\.mp3|\.wav/, path, Gosu::Sample)
    end

    def load_songs(path)
      load_resources(@songs, /\.ogg|\.mp3|\.wav/, path, Gosu::Song)
    end

    def with_path(path, &blk)
      @path = File.expand_path(File.dirname path)
      yield
    end

    def load_resources(container, regexp, path, klass)
      path = File.expand_path(File.join(@path, path))
      puts "Loading from: #{path}"

      count = 0
      Dir.entries(path).grep(regexp).each do |entry|
        begin
          container[entry] = klass.new($window, File.join(path, entry))
          count += 1
        rescue Exception => e
          puts e, File.join(path, entry)
        end
      end
      puts "#{count} #{klass} files loaded."
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lotu-0.1.5 lib/lotu/behaviors/resourceful.rb
lotu-0.1.4 lib/lotu/behaviors/resourceful.rb