Class: RubyMan::ResourceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_man/resource_manager.rb

Overview

Handles loading of resources for faster and centralized access.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ ResourceManager

Returns a new instance of ResourceManager.



23
24
25
26
27
28
29
# 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

Class Attribute Details

.instObject

Returns the value of attribute inst.



20
21
22
# File 'lib/ruby_man/resource_manager.rb', line 20

def inst
  @inst
end

Class Method Details

.[](resource_name) ⇒ Object



95
96
97
# File 'lib/ruby_man/resource_manager.rb', line 95

def self.[](resource_name)
  @inst.resource(resource_name)
end

.media(media_path) ⇒ Object



99
100
101
# File 'lib/ruby_man/resource_manager.rb', line 99

def self.media(media_path)
  @inst.media_file_path(media_path)
end

Instance Method Details

#load_animation(cfg) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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_configObject



35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



68
69
70
71
72
73
74
# 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) ⇒ Object



76
77
78
79
# 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) ⇒ Object



31
32
33
# File 'lib/ruby_man/resource_manager.rb', line 31

def media_file_path(media_path)
  "#{@media_dir}/#{media_path}"
end

#resource(name) ⇒ Object



91
92
93
# File 'lib/ruby_man/resource_manager.rb', line 91

def resource(name)
  @resources[name]
end