NamedResource complete

I've been working in my spare moments on designing and implementing the NamedResource class. It's now pretty much complete, and I'm quite happy with the result. In addition to using it for Music, Sound, and Surface, it's also a useful mixin for other things. Here's a snippet from the docs, to give you a taste of how nice it is:

Here's an example of how you could use this for a class which loads maps from a file:

class Map
  include Rubygame::NamedResource

  Map.autoload_dirs = [ File.join("maps","world_1"),
                        File.join("maps","custom") ]

  def autoload( filename )
    # Searches autoload_dirs for the file
    path = find_file( filename )

    if( path )
      return load_map( path )
    else
      return nil
    end
  end

  def load_map( path )
    # Your code to do the real loading, then return
    # the created instance of Map class.
    # ...
    return map_instance
  end
end

Here's an example of how you could then use the Map class:

map = Map["level_1.map"]

if( map )
  start_playing( map )
else
  raise "Oops! The map file for Level 1 doesn't exist!"
end

NamedResource will be shipped in Rubygame 2.3. Now I just need to include it in Sound, Music, and Surface classes. Woohoo!


Comments

Have something interesting to say about this post? Email your thoughtful comments to comments@rubygame.org.