Class: Demiurge::Tmx::TmxCache

Inherits:
Object
  • Object
show all
Defined in:
lib/demiurge/tmx.rb

Overview

A TmxCache loads and remembers TMX file maps from the tmx gem. For a variety of reasons, it's not great to reload TMX files every time we need to know about them, but it can also be a problem to store a copy of the parsed version every time and place we use it. Caching is, of course, a time-honored solution to this problem.

Since:

  • 0.3.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ void

Create the TmxCache

Parameters:

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • :root_dir (String)

    The root directory to read TMX and TSX files relative to

Since:

  • 0.3.0



272
273
274
# File 'lib/demiurge/tmx.rb', line 272

def initialize(options = {})
  @root_dir = options[:root_dir] || Dir.pwd
end

Instance Attribute Details

#root_dirString

Returns Root directory the cache was created relative to

Returns:

  • (String)

    Root directory the cache was created relative to

Since:

  • 0.3.0



264
265
266
# File 'lib/demiurge/tmx.rb', line 264

def root_dir
  @root_dir
end

Instance Method Details

#clear_cachevoid

This method returns an undefined value.

Clear the TMX cache. Remove all existing entries. This can potentially break existing TMX-based objects. Objects often refer repeatedly back into the cache to avoid duplicating structures, and to pick up new changes to those structures.

Since:

  • 0.3.0



283
284
285
286
# File 'lib/demiurge/tmx.rb', line 283

def clear_cache
  @tile_cache = {}
  nil
end

#tmx_entry(layout_type, layout_filename) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a TMX entry for the given layout type and filename. A "layout type" is something like normal TMX (called "tmx") or Manasource-style (called "manasource".) But additional types are likely to be added in future versions of Pixiurge.

Parameters:

  • layout_type (String)

    The subtype of TMX file; currently may be one of "tmx" or "manasource"

  • layout_filename (String)

    The path to the TMX file relative to the tile cache's TMX root directory

Returns:

  • (Hash)

    The TMX cache entry, in an internal and potentially changeable format

Since:

  • 0.3.0



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/demiurge/tmx.rb', line 311

def tmx_entry(layout_type, layout_filename)
  @tile_cache ||= {}
  @tile_cache[layout_type] ||= {}
  if @tile_cache[layout_type][layout_filename]
    return @tile_cache[layout_type][layout_filename]
  end

  if layout_type == "manasource"
    @tile_cache[layout_type][layout_filename] = sprites_from_manasource_tmx(layout_filename)
  elsif layout_type == "tmx"
    @tile_cache[layout_type][layout_filename] = sprites_from_tmx(layout_filename)
  else
    raise "A TMX location must have a known type of layout (tmx or manasource), not #{layout_type.inspect}!"
  end

  @tile_cache[layout_type][layout_filename]
end