Sha256: 09f4144af18b088886d980d247075cd765ea80b1f80d79c967525d6cb4e6b3bb

Contents?: true

Size: 1018 Bytes

Versions: 4

Compression:

Stored size: 1018 Bytes

Contents

class Image
  def initialize(image_name)
    @image = Image.load(image_name)
  end

  def draw(x:, y:, scale: 1)
    @image.draw(x, y, 0, scale, scale)
  end

  def width
    @image.width
  end

  def height
    @image.height
  end

  class << self
    @@images = {}

    def load(image_name)
      locate_image(image_name)
    end

    def preload_images
      Dir.each_child(base_path) do |file_name|
        locate_image(file_name) unless file_name.start_with?(".")
      end
    end

    private

    def locate_image(image_name)
      return @@images[image_name] if @@images[image_name]

      puts "Initialize image: '#{image_name}'"

      file_name = Dir.entries(base_path).find { |e| e =~ /^#{image_name}($|\.)/  }

      raise "Image file not found with name '#{image_name}' in #{base_path}" if file_name.nil?

      @@images[image_name] = Gosu::Image.new("#{base_path}/#{file_name}", { retro: true })

      return @@images[image_name]
    end

    def base_path
      "#{Dir.pwd}/images"
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fantasy-0.1.9 lib/fantasy/image.rb
fantasy-0.1.7 lib/fantasy/image.rb
fantasy-0.1.5.1 lib/fantasy/image.rb
fantasy-0.1.5 lib/fantasy/image.rb