class RubyMan::ResourceManager

Handles loading of resources for faster and centralized access.

Attributes

inst[RW]

Public Class Methods

[](resource_name) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 95
def self.[](resource_name)
  @inst.resource(resource_name)
end
media(media_path) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 99
def self.media(media_path)
  @inst.media_file_path(media_path)
end
new(window) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 23
def initialize(window)
  ResourceManager.inst = self
  @window = window
  @resources = {}
  @media_dir = "#{__dir__}/../../media"
  load_config
end

Public Instance Methods

load_animation(cfg) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 48
def load_animation(cfg)
  return unless cfg.size >= 7

  sprite = Sprite.new
  x_offset = cfg[3].to_i
  y_offset = cfg[4].to_i
  sprite.width = cfg[5].to_i
  sprite.height = cfg[6].to_i
  frames  = cfg[7].to_i

  (0...frames).each do |i|
    image = Gosu::Image.new(@window, media_file_path(cfg[2]), true,
                            x_offset + i * sprite.width, y_offset,
                            sprite.width, sprite.height)
    sprite.frames.push(image)
  end

  @resources[cfg[0]] = sprite
end
load_config() click to toggle source
# File lib/ruby_man/resource_manager.rb, line 35
def load_config
  File.open(media_file_path('resource.cfg')).each do |line|
    cfg = line.split(';').map!(&:strip)
    next unless cfg.size >= 3
    case cfg[1]
    when 'image' then load_image(cfg)
    when 'animation' then load_animation(cfg)
    when 'font' then load_font(cfg)
    when 'sound' then load_sound(cfg)
    end
  end
end
load_font(cfg) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 81
def load_font(cfg)
  begin
    font = Gosu::Font.new(@window, media_file_path(cfg[2]), cfg[3].to_i)
    font.text_width('font found test')
  rescue
    font = Gosu::Font.new(@window, Gosu::default_font_name, cfg[3].to_i)
  end
  @resources[cfg[0]] = font
end
load_image(cfg) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 68
def load_image(cfg)
  sprite = Sprite.new
  sprite.frames.push(Gosu::Image.new(@window, media_file_path(cfg[2]), false))
  sprite.width = sprite.frames[0].width
  sprite.height = sprite.frames[0].height
  @resources[cfg[0]] = sprite
end
load_sound(cfg) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 76
def load_sound(cfg)
  sound = Gosu::Sample.new(@window, media_file_path(cfg[2]))
  @resources[cfg[0]] = sound
end
media_file_path(media_path) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 31
def media_file_path(media_path)
  "#{@media_dir}/#{media_path}"
end
resource(name) click to toggle source
# File lib/ruby_man/resource_manager.rb, line 91
def resource(name)
  @resources[name]
end