Sha256: e3ad016d7494ab1c07c15c35bfb35d62bf7b28b0767dc497e3684ca1fac07200

Contents?: true

Size: 757 Bytes

Versions: 4

Compression:

Stored size: 757 Bytes

Contents

module Sound
  class << self
    @@sounds = {}

    def play(sound_name)
      locate_sound(sound_name).play
    end

    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}")

      return @@sounds[sound_name]
    end

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

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

Version data entries

4 entries across 4 versions & 1 rubygems

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