Module: Sound

Defined in:
lib/fantasy/sound.rb

Constant Summary collapse

@@sounds =
{}

Class Method Summary collapse

Class Method Details

.base_pathObject



33
34
35
# File 'lib/fantasy/sound.rb', line 33

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

.locate_sound(sound_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/fantasy/sound.rb', line 11

def locate_sound(sound_name)
  return @@sounds[sound_name] if @@sounds[sound_name]

  puts "Initialize Sound: '#{sound_name}'"

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

  raise "Sound file not found with name '#{sound_name}'" if file_name.nil?

  @@sounds[sound_name] = Gosu::Sample.new("#{base_path}/#{file_name}")

  @@sounds[sound_name]
end

.play(sound_name, volume: 1) ⇒ Object



7
8
9
# File 'lib/fantasy/sound.rb', line 7

def play(sound_name, volume: 1)
  locate_sound(sound_name).play(volume)
end

.preload_soundsObject



25
26
27
28
29
30
31
# File 'lib/fantasy/sound.rb', line 25

def preload_sounds
  return unless Dir.exist?(base_path)

  Dir.each_child(base_path) do |file_name|
    locate_sound(file_name) unless file_name.start_with?(".")
  end
end